Skip to content

Commit

Permalink
Merge branch 'MDL-68200-master-take2' of git://github.com/rezaies/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed May 14, 2020
2 parents 85e7049 + a30bb16 commit ccc7a30
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/user_date.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amd/build/user_date.min.js.map

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions lib/amd/src/user_date.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@ define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'],
*/
var loadDatesFromServer = function(dates) {
var args = dates.map(function(data) {
var fixDay = data.hasOwnProperty('fixday') ? data.fixday : 1;
var fixHour = data.hasOwnProperty('fixhour') ? data.fixhour : 1;
return {
timestamp: data.timestamp,
format: data.format
format: data.format,
type: data.type || '',
fixday: fixDay,
fixhour: fixHour
};
});

Expand Down Expand Up @@ -155,7 +160,8 @@ define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'],
* Only dates not found in either cache will be sent to the server
* for transforming.
*
* A request object must have a timestamp key and a format key.
* A request object must have a timestamp key and a format key and
* optionally may have a type key.
*
* E.g.
* var request = [
Expand All @@ -165,7 +171,10 @@ define(['jquery', 'core/ajax', 'core/sessionstorage', 'core/config'],
* },
* {
* timestamp: 1293876000,
* format: '%A, %d %B %Y, %I:%M %p'
* format: '%A, %d %B %Y, %I:%M %p',
* type: 'gregorian',
* fixday: false,
* fixhour: false
* }
* ];
*
Expand Down
20 changes: 14 additions & 6 deletions lib/external/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function get_string($stringid, $component = 'moodle', $lang = null
/**
* Returns description of get_string() result value
*
* @return string
* @return external_description
* @since Moodle 2.4
*/
public static function get_string_returns() {
Expand Down Expand Up @@ -189,7 +189,7 @@ public static function get_strings($strings) {
/**
* Returns description of get_string() result value
*
* @return array
* @return external_description
* @since Moodle 2.4
*/
public static function get_strings_returns() {
Expand Down Expand Up @@ -233,6 +233,9 @@ public static function get_user_dates_parameters() {
[
'timestamp' => new external_value(PARAM_INT, 'unix timestamp'),
'format' => new external_value(PARAM_TEXT, 'format string'),
'type' => new external_value(PARAM_PLUGIN, 'The calendar type', VALUE_DEFAULT),
'fixday' => new external_value(PARAM_INT, 'Remove leading zero for day', VALUE_DEFAULT, 1),
'fixhour' => new external_value(PARAM_INT, 'Remove leading zero for hour', VALUE_DEFAULT, 1),
]
)
)
Expand Down Expand Up @@ -264,7 +267,12 @@ public static function get_user_dates($contextid, $contextlevel, $instanceid, $t
self::validate_context($context);

$formatteddates = array_map(function($timestamp) {
return userdate($timestamp['timestamp'], $timestamp['format']);

$calendartype = $timestamp['type'];
$fixday = !empty($timestamp['fixday']);
$fixhour = !empty($timestamp['fixhour']);
$calendar = \core_calendar\type_factory::get_calendar_instance($calendartype);
return $calendar->timestamp_to_date_string($timestamp['timestamp'], $timestamp['format'], 99, $fixday, $fixhour);
}, $params['timestamps']);

return ['dates' => $formatteddates];
Expand All @@ -273,7 +281,7 @@ public static function get_user_dates($contextid, $contextlevel, $instanceid, $t
/**
* Returns description of get_user_dates() result value
*
* @return array
* @return external_description
*/
public static function get_user_dates_returns() {
return new external_single_structure(
Expand Down Expand Up @@ -333,7 +341,7 @@ public static function get_component_strings($component, $lang = null) {
/**
* Returns description of get_component_strings() result value
*
* @return array
* @return external_description
* @since Moodle 2.4
*/
public static function get_component_strings_returns() {
Expand Down Expand Up @@ -421,7 +429,7 @@ public static function get_fragment($component, $callback, $contextid, $args = n
/**
* Returns description of get_fragment() result value
*
* @return array
* @return external_description
* @since Moodle 3.1
*/
public static function get_fragment_returns() {
Expand Down
9 changes: 7 additions & 2 deletions lib/external/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public function test_update_inplace_editable() {
}

public function test_get_user_dates() {
global $USER, $CFG, $DB;
$this->resetAfterTest();

$this->setAdminUser();
Expand All @@ -222,6 +221,11 @@ public function test_get_user_dates() {
'timestamp' => 1293876000,
'format' => '%d %m %Y'
],
[
'timestamp' => 1293876000,
'format' => '%d %m %Y',
'type' => 'gregorian'
],
[
'timestamp' => 1293876000,
'format' => 'some invalid format'
Expand All @@ -233,6 +237,7 @@ public function test_get_user_dates() {

$this->assertEquals('Saturday, 1 January 2011, 6:00', $result['dates'][0]);
$this->assertEquals('1 01 2011', $result['dates'][1]);
$this->assertEquals('some invalid format', $result['dates'][2]);
$this->assertEquals('1 01 2011', $result['dates'][2]);
$this->assertEquals('some invalid format', $result['dates'][3]);
}
}
92 changes: 92 additions & 0 deletions lib/templates/time_element.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core/time_element
Template to display an HTML time element.
Classes required for JS:
* none
Data attributes required for JS:
* data-timestamp Number - The timestamp for the element.
* data-datetimeformat String - A valid format for the datetime attribute.
Context variables required for this template:
* timestamp Number - The timestamp for the element.
* userdateformat String - The user-facing date format
* datetimeformat String - A valid format for the datetime attribute. Defaults to the ISO-8601 format of '%Y-%m-%dT%H:%M%z'.
Example context (json):
{
"timestamp": 0,
"userdateformat": "%d %b %Y",
"datetimeformat": "%Y-%m-%dT%H:%M%z"
}
}}
<time id="time-{{$elementid}}{{uniqid}}{{/elementid}}" class="{{$elementclass}}{{timeclass}}{{/elementclass}}" datetime="{{$datetimeval}}{{datetime}}{{/datetimeval}}"
data-timestamp="{{$timestampval}}{{timestamp}}{{/timestampval}}"
data-datetimeformat="{{$datetimeformatval}}{{#datetimeformat}}{{.}}{{/datetimeformat}}{{^datetimeformat}}%Y-%m-%dT%H:%M%z{{/datetimeformat}}{{/datetimeformatval}}">
{{$datedisplay}}
{{#userdate}} {{$timestampval}}{{timestamp}}{{/timestampval}}, {{$userdateformatval}}{{userdateformat}}{{/userdateformatval}} {{/userdate}}
{{/datedisplay}}
</time>
{{#js}}
/** Fetches the formatted date/time for the time element's datetime attribute. */
require(['core/user_date'], function(UserDate) {
var root = document.getElementById('time-{{$elementid}}{{uniqid}}{{/elementid}}');
// Fetch value for the datetime attribute using core/user_date, if it's not available.
if (!root.getAttribute('datetime')) {
var dateTimeFormat = root.getAttribute('data-datetimeformat');
var timestamp = root.getAttribute('data-timestamp');
if (!dateTimeFormat.match(/%(?![YmdHMSzZ])./g)) {
var zeroPad = function(nNum, nPad) {
return ((Math.pow(10, nPad) + nNum) + '').slice(1);
};
var date = new Date(timestamp * 1000);
var datetime = dateTimeFormat.replace(/%./g, function(sMatch) {
return (({
'%Y': date.getFullYear(),
'%m': zeroPad(date.getMonth() + 1, 2),
'%d': zeroPad(date.getDate(), 2),
'%H': zeroPad(date.getHours(), 2),
'%M': zeroPad(date.getMinutes(), 2),
'%S': zeroPad(date.getSeconds(), 2),
'%z': date.toTimeString().replace(/.+GMT([+-]\d+).+/, '$1'),
'%Z': date.toTimeString().replace(/.+\((.+?)\)$/, '$1')
}[sMatch] || '') + '') || sMatch;
});
root.setAttribute('datetime', datetime);
} else {
// Otherwise, use core/user_date.
var timestamps = [{
timestamp: timestamp,
format: dateTimeFormat,
type: 'gregorian',
fixday: 0,
fixhour: 0
}];
UserDate.get(timestamps).done(function(dates) {
var datetime = dates.pop();
root.setAttribute('datetime', datetime);
});
}
}
});
{{/js}}
4 changes: 4 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ information provided here is intended especially for developers.
* The following functions have been updated to support passing in an array of group IDs (but still support passing in a single ID):
* groups_get_members_join()
* groups_get_members_ids_sql()
* Additional parameters were added to core_get_user_dates:
- type: specifies the calendar type. Optional, defaults to Gregorian.
- fixday: Whether to remove leading zero for day. Optional, defaults to 1.
- fixhour: Whether to remove leading zero for hour. Optional, defaults to 1.

=== 3.8 ===
* Add CLI option to notify all cron tasks to stop: admin/cli/cron.php --stop
Expand Down
3 changes: 1 addition & 2 deletions mod/forum/classes/local/exporters/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,8 @@ private function export_tags(array $tags) : array {
private function get_author_subheading_html(stdClass $exportedauthor, int $timecreated) : string {
$fullname = $exportedauthor->fullname;
$profileurl = $exportedauthor->urls['profile'] ?? null;
$formatteddate = userdate($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
$name = $profileurl ? "<a href=\"{$profileurl}\">{$fullname}</a>" : $fullname;
$date = "<time>{$formatteddate}</time>";
$date = userdate_htmltime($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
}
}
12 changes: 10 additions & 2 deletions mod/forum/templates/discussion_list.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@
<div class="author-info align-middle">
<div class="mb-1 line-height-3 text-truncate">{{fullname}}</div>
<div class="line-height-3">
{{#userdate}}{{discussion.times.created}}, {{#str}}strftimedatemonthabbr, langconfig{{/str}}{{/userdate}}
{{< core/time_element }}
{{$elementid}}created-{{discussion.id}}{{/elementid}}
{{$timestampval}}{{discussion.times.created}}{{/timestampval}}
{{$userdateformatval}}{{#str}}strftimedatemonthabbr, langconfig{{/str}}{{/userdateformatval}}
{{/core/time_element}}
</div>
</div>
</div>
Expand All @@ -277,7 +281,11 @@
<div class="line-height-3">
{{#latestpostid}}
<a href="{{{discussion.urls.viewlatest}}}" title="{{#userdate}}{{discussion.times.modified}},{{#str}}strftimerecentfull{{/str}}{{/userdate}}">
{{#userdate}}{{discussion.times.modified}}, {{#str}}strftimedatemonthabbr, langconfig{{/str}}{{/userdate}}
{{< core/time_element }}
{{$elementid}}modified-{{discussion.id}}{{/elementid}}
{{$timestampval}}{{discussion.times.modified}}{{/timestampval}}
{{$userdateformatval}}{{#str}}strftimedatemonthabbr, langconfig{{/str}}{{/userdateformatval}}
{{/ core/time_element }}
</a>
{{/latestpostid}}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<header id="post-header-{{id}}-{{uniqid}}">
{{^isdeleted}}
<div class="d-flex flex-wrap align-items-center mb-1">
<address class="mb-0 mr-2" tabindex="-1">
<div class="mr-2" tabindex="-1">
{{#author}}
<h4 class="h6 d-lg-inline-block mb-0 author-header mr-1">
{{#parentauthorname}}
Expand Down Expand Up @@ -105,10 +105,13 @@
{{/parentauthorname}}
</h4>
{{/author}}
<time class="text-muted">
{{#userdate}} {{timecreated}}, {{#str}} strftimerecentfull, core_langconfig {{/str}} {{/userdate}}
</time>
</address>
{{< core/time_element }}
{{$elementid}}created-{{id}}-{{uniqid}}{{/elementid}}
{{$elementclass}}text-muted{{/elementclass}}
{{$timestampval}}{{timecreated}}{{/timestampval}}
{{$userdateformatval}}{{#str}} strftimerecentfull, core_langconfig {{/str}}{{/userdateformatval}}
{{/core/time_element}}
</div>

<div class="d-flex align-items-center ml-auto">
{{#author.groups}}
Expand Down
12 changes: 7 additions & 5 deletions mod/forum/templates/forum_discussion_post.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@
}}>{{$subject}}{{{subject}}}{{/subject}}</h3>
{{/subjectheading}}
{{^isdeleted}}
<address tabindex="-1">
<div class="mb-3" tabindex="-1">
{{#html.authorsubheading}}{{{.}}}{{/html.authorsubheading}}
{{^html.authorsubheading}}
<time>
{{#userdate}} {{timecreated}}, {{#str}} strftimedaydatetime, core_langconfig {{/str}} {{/userdate}}
</time>
{{< core/time_element }}
{{$elementid}}created-{{id}}-{{uniqid}}{{/elementid}}
{{$timestampval}}{{timecreated}}{{/timestampval}}
{{$userdateformatval}}{{#str}} strftimedaydatetime, core_langconfig {{/str}}{{/userdateformatval}}
{{/core/time_element}}
{{/html.authorsubheading}}
</address>
</div>
{{/isdeleted}}
{{#isprivatereply}}
<div class="privatereplyinfo">
Expand Down
4 changes: 1 addition & 3 deletions mod/forum/templates/forum_discussion_threaded_post.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
>
<a href="{{{urls.viewisolated}}}">{{subject}}</a>
{{^isdeleted}}
<address class="d-inline-block mb-0">
{{{html.authorsubheading}}}
</address>
{{{html.authorsubheading}}}
{{/isdeleted}}

<div data-region="replies-container">
Expand Down
3 changes: 1 addition & 2 deletions mod/forum/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,8 @@ public function create_content($instance, $record = array()) {
public function get_author_subheading_html(stdClass $exportedauthor, int $timecreated) : string {
$fullname = $exportedauthor->fullname;
$profileurl = $exportedauthor->urls['profile'] ?? null;
$formatteddate = userdate($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
$name = $profileurl ? "<a href=\"{$profileurl}\">{$fullname}</a>" : $fullname;
$date = "<time>{$formatteddate}</time>";
$date = userdate_htmltime($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
}
}
Loading

0 comments on commit ccc7a30

Please sign in to comment.