Skip to content

Commit

Permalink
MDL-71163 core: support relative dates in activity dates
Browse files Browse the repository at this point in the history
AMOS BEGIN
 CPY [relativedatessubmissionduedateafter,mod_assign],[relativedatessubmissionduedateafter,core_course]
 CPY [relativedatessubmissionduedatebefore,mod_assign],[relativedatessubmissionduedatebefore,core_course]
AMOS END
  • Loading branch information
rezaies committed Apr 30, 2021
1 parent 5b315fa commit d8a1cf6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 9 deletions.
25 changes: 24 additions & 1 deletion course/classes/output/activity_information.php
Expand Up @@ -80,12 +80,35 @@ public function export_for_template(renderer_base $output): stdClass {

$data->cmid = $this->cminfo->id;
$data->activityname = $this->cminfo->name;
$data->activitydates = $this->activitydates;
$this->build_dates_data($data);
$data->hasdates = !empty($this->activitydates);

return $data;
}

/**
* Builds the dates data for export.
*
* @param stdClass $data
*/
protected function build_dates_data(stdClass $data): void {
foreach ($this->activitydates as $date) {
if (empty($date['relativeto'])) {
$date['datestring'] = userdate($date['timestamp'], get_string('strftimedatetime', 'core_langconfig'));
} else {
$diffstr = get_time_interval_string($date['timestamp'], $date['relativeto']);
if ($date['timestamp'] >= $date['relativeto']) {
$date['datestring'] = get_string('relativedatessubmissionduedateafter', 'core_course',
['datediffstr' => $diffstr]);
} else {
$date['datestring'] = get_string('relativedatessubmissionduedatebefore', 'core_course',
['datediffstr' => $diffstr]);
}
}
$data->activitydates[] = $date;
}
}

/**
* Builds the completion data for export.
*
Expand Down
4 changes: 2 additions & 2 deletions course/templates/activity_date.mustache
Expand Up @@ -22,9 +22,9 @@
Example context (json):
{
"label": "Opens:",
"timestamp": 1293876000
"datestring": "6 April 2021, 6:46 PM"
}
}}
<div>
<strong>{{label}}</strong> {{#userdate}} {{timestamp}}, {{#str}} strftimedatetime, core_langconfig {{/str}} {{/userdate}}
<strong>{{label}}</strong> {{datestring}}
</div>
2 changes: 2 additions & 0 deletions lang/en/course.php
Expand Up @@ -97,6 +97,8 @@
$string['privacy:metadata:favouritessummary'] = 'The course contains information relating to the course being starred by the user.';
$string['recommend'] = 'Recommend';
$string['recommendcheckbox'] = 'Recommend activity: {$a}';
$string['relativedatessubmissionduedateafter'] = '{$a->datediffstr} after course start';
$string['relativedatessubmissionduedatebefore'] = '{$a->datediffstr} before course start';
$string['searchactivitiesbyname'] = 'Search for activities by name';
$string['searchresults'] = 'Search results: {$a}';
$string['submitsearch'] = 'Submit search';
Expand Down
20 changes: 18 additions & 2 deletions mod/assign/classes/dates.php
Expand Up @@ -42,24 +42,40 @@ class dates extends activity_dates {
* @return array
*/
protected function get_dates(): array {
global $CFG;

require_once($CFG->dirroot . '/mod/assign/locallib.php');

$course = get_course($this->cm->course);
$context = \context_module::instance($this->cm->id);
$assign = new \assign($context, $this->cm, $course);

$timeopen = $this->cm->customdata['allowsubmissionsfromdate'] ?? null;
$timedue = $this->cm->customdata['duedate'] ?? null;
$now = time();
$dates = [];

if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:submissionsopen' : 'activitydate:submissionsopened';
$dates[] = [
$date = [
'label' => get_string($openlabelid, 'mod_assign'),
'timestamp' => (int) $timeopen,
];
if ($course->relativedatesmode && $assign->can_view_grades()) {
$date['relativeto'] = $course->startdate;
}
$dates[] = $date;
}

if ($timedue) {
$dates[] = [
$date = [
'label' => get_string('activitydate:submissionsdue', 'mod_assign'),
'timestamp' => (int) $timedue,
];
if ($course->relativedatesmode && $assign->can_view_grades()) {
$date['relativeto'] = $course->startdate;
}
$dates[] = $date;
}

return $dates;
Expand Down
6 changes: 4 additions & 2 deletions mod/assign/lang/en/assign.php
Expand Up @@ -450,8 +450,6 @@
$string['quickgradingchangessaved'] = 'The grade changes were saved';
$string['quickgrading_help'] = 'Quick grading allows you to assign grades (and outcomes) directly in the submissions table. Quick grading is not compatible with advanced grading and is not recommended when there are multiple markers.';
$string['relativedatessubmissiontimeleft'] = 'Calculated for each student';
$string['relativedatessubmissionduedateafter'] = '{$a->datediffstr} after course start';
$string['relativedatessubmissionduedatebefore'] = '{$a->datediffstr} before course start';
$string['removeallgroupoverrides'] = 'Delete all group overrides';
$string['removealluseroverrides'] = 'Delete all user overrides';
$string['reopenuntilpassincompatiblewithblindmarking'] = 'Reopen until pass option is incompatible with anonymous submissions, because the grades are not released to the gradebook until the student identities are revealed.';
Expand Down Expand Up @@ -640,3 +638,7 @@
$string['nosubmissionsacceptedafter'] = 'No submissions accepted after ';
$string['notsubmittedyet'] = 'Not submitted yet';
$string['submissionsnotgraded'] = 'Submissions not graded: {$a}';

// Deprecated since Moodle 3.11.
$string['relativedatessubmissionduedateafter'] = '{$a->datediffstr} after course start';
$string['relativedatessubmissionduedatebefore'] = '{$a->datediffstr} before course start';
2 changes: 2 additions & 0 deletions mod/assign/lang/en/deprecated.txt
Expand Up @@ -3,4 +3,6 @@ mysubmission,mod_assign
nolatesubmissions,mod_assign
nosubmissionsacceptedafter,mod_assign
notsubmittedyet,mod_assign
relativedatessubmissionduedateafter,mod_assign
relativedatessubmissionduedatebefore,mod_assign
submissionsnotgraded,mod_assign
10 changes: 10 additions & 0 deletions mod/assign/lib.php
Expand Up @@ -572,6 +572,16 @@ function mod_assign_cm_info_dynamic(cm_info $cm) {
}
}

// Calculate relative dates. The assignment module calculates relative date only for duedate.
// A user or group override always has higher priority over any relative date calculation.
if (empty($override->duedate) && !empty($cm->customdata['duedate'])) {
$course = get_course($cm->course);
$usercoursedates = course_get_course_dates_for_user_id($course, $USER->id);
if ($usercoursedates['start']) {
$override->duedate = $cm->customdata['duedate'] + $usercoursedates['startoffset'];
}
}

// Populate some other values that can be used in calendar or on dashboard.
if (!is_null($override->allowsubmissionsfromdate)) {
$cm->override_customdata('allowsubmissionsfromdate', $override->allowsubmissionsfromdate);
Expand Down
4 changes: 2 additions & 2 deletions mod/assign/renderer.php
Expand Up @@ -342,10 +342,10 @@ public function render_assign_grading_summary(assign_grading_summary $summary) {
// Returns a formatted string, in the format '10d 10h 45m'.
$diffstr = get_time_interval_string($duedate, $summary->coursestartdate);
if ($duedate >= $summary->coursestartdate) {
$cell2content = get_string('relativedatessubmissionduedateafter', 'mod_assign',
$cell2content = get_string('relativedatessubmissionduedateafter', 'core_course',
['datediffstr' => $diffstr]);
} else {
$cell2content = get_string('relativedatessubmissionduedatebefore', 'mod_assign',
$cell2content = get_string('relativedatessubmissionduedatebefore', 'core_course',
['datediffstr' => $diffstr]);
}
} else {
Expand Down
23 changes: 23 additions & 0 deletions mod/assign/tests/behat/relative_dates.feature
Expand Up @@ -35,6 +35,29 @@ I should be able to create an assignment with a due date relative to the course
And I follow "Test assignment name"
And I should not see "Assignment is overdue by:" in the "Time remaining" "table_row"

Scenario: As a student the due date I see for submitting my assignment is relative to my course start date
Given the following config values are set as admin:
| enablecourserelativedates | 1 |
And the following "courses" exist:
# A course with start date set to 1 Jan 2021.
| fullname | shortname | category | groupmode | relativedatesmode | startdate |
| Course 1 | C1 | 0 | 1 | 1 | 1609459200 |
And the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And the following "course enrolments" exist:
# User's enrolment starts from 5 Jan 2021.
| user | course | role | timestart |
| student1 | C1 | student | 1609804800 |
And the following "activities" exist:
# The assignment's due date is 3 Jan 2021.
| activity | name | intro | course | idnumber | assignsubmission_onlinetext_enabled | duedate |
| assign | Test assignment name | Test assignment description | C1 | assign0 | 1 | 1609632000 |
When I log in as "student1"
And I am on "Course 1" course homepage
And I follow "Test assignment name"
Then the activity date in "Test assignment name" should contain "Due: 7 January 2021, 8:00 AM"

Scenario: As a teacher, I should see the relative dates when reviewing assignment submissions
Given the following config values are set as admin:
| enablecourserelativedates | 1 |
Expand Down

0 comments on commit d8a1cf6

Please sign in to comment.