Skip to content

Commit

Permalink
Merge branch 'MDL-75058-master' of https://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jul 7, 2022
2 parents 4afcb0d + 51c9c70 commit f9af45e
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 97 deletions.
3 changes: 3 additions & 0 deletions course/externallib.php
Expand Up @@ -478,6 +478,9 @@ public static function get_course_contents_returns() {
array(
'label' => new external_value(PARAM_TEXT, 'date label'),
'timestamp' => new external_value(PARAM_INT, 'date timestamp'),
'relativeto' => new external_value(PARAM_INT, 'relative date timestamp',
VALUE_OPTIONAL),
'dataid' => new external_value(PARAM_NOTAGS, 'cm data id', VALUE_OPTIONAL),
)
),
VALUE_DEFAULT,
Expand Down
53 changes: 53 additions & 0 deletions course/tests/externallib_test.php
Expand Up @@ -1655,6 +1655,59 @@ public function test_get_course_contents_hiddensections() {
$this->assertEquals(-1, $sections[5]['id']);
}

/**
* Test get course contents dates.
*/
public function test_get_course_contents_dates() {
$this->resetAfterTest(true);

$this->setAdminUser();
set_config('enablecourserelativedates', 1);

// Course with just main section.
$timenow = time();
$course = self::getDataGenerator()->create_course(
['numsections' => 0, 'relativedatesmode' => true, 'startdate' => $timenow - DAYSECS]);

$teacher = self::getDataGenerator()->create_user();
self::getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');

$this->setUser($teacher);

// Create resource (empty dates).
$resource = self::getDataGenerator()->create_module('resource', ['course' => $course->id]);
// Create activities with dates.
$resource = self::getDataGenerator()->create_module('forum', ['course' => $course->id, 'duedate' => $timenow]);
$resource = self::getDataGenerator()->create_module('choice',
['course' => $course->id, 'timeopen' => $timenow, 'timeclose' => $timenow + DAYSECS]);
$resource = self::getDataGenerator()->create_module('assign',
['course' => $course->id, 'allowsubmissionsfromdate' => $timenow]);

$result = core_course_external::get_course_contents($course->id);
$result = external_api::clean_returnvalue(core_course_external::get_course_contents_returns(), $result);

foreach ($result[0]['modules'] as $module) {
if ($module['modname'] == 'resource') {
$this->assertEmpty($module['dates']);
} else if ($module['modname'] == 'forum') {
$this->assertCount(1, $module['dates']);
$this->assertEquals('duedate', $module['dates'][0]['dataid']);
$this->assertEquals($timenow, $module['dates'][0]['timestamp']);
} else if ($module['modname'] == 'choice') {
$this->assertCount(2, $module['dates']);
$this->assertEquals('timeopen', $module['dates'][0]['dataid']);
$this->assertEquals($timenow, $module['dates'][0]['timestamp']);
$this->assertEquals('timeclose', $module['dates'][1]['dataid']);
$this->assertEquals($timenow + DAYSECS, $module['dates'][1]['timestamp']);
} else if ($module['modname'] == 'assign') {
$this->assertCount(1, $module['dates']);
$this->assertEquals('allowsubmissionsfromdate', $module['dates'][0]['dataid']);
$this->assertEquals($timenow, $module['dates'][0]['timestamp']);
$this->assertEquals($course->startdate, $module['dates'][0]['relativeto']);
}
}
}

/**
* Test duplicate_course
*/
Expand Down
2 changes: 2 additions & 0 deletions mod/assign/classes/dates.php
Expand Up @@ -72,6 +72,7 @@ protected function get_dates(): array {
if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:submissionsopen' : 'activitydate:submissionsopened';
$date = [
'dataid' => 'allowsubmissionsfromdate',
'label' => get_string($openlabelid, 'mod_assign'),
'timestamp' => (int) $timeopen,
];
Expand All @@ -83,6 +84,7 @@ protected function get_dates(): array {

if ($timedue) {
$date = [
'dataid' => 'duedate',
'label' => get_string('activitydate:submissionsdue', 'mod_assign'),
'timestamp' => (int) $timedue,
];
Expand Down
42 changes: 28 additions & 14 deletions mod/assign/tests/dates_test.php
Expand Up @@ -56,48 +56,62 @@ public function get_dates_for_module_provider(): array {
],
'only with opening time' => [
$after, null, null, null, null, null, [
['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after],
['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after,
'dataid' => 'allowsubmissionsfromdate'],
]
],
'only with closing time' => [
null, $after, null, null, null, null, [
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after,
'dataid' => 'duedate'],
]
],
'with both times' => [
$after, $later, null, null, null, null, [
['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
'dataid' => 'duedate'],
]
],
'between the dates' => [
$before, $after, null, null, null, null, [
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $before],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after],
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $before,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after,
'dataid' => 'duedate'],
]
],
'dates are past' => [
$earlier, $before, null, null, null, null, [
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $before],
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $before,
'dataid' => 'duedate'],
]
],
'with user override' => [
$before, $after, $earlier, $later, null, null, [
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
'dataid' => 'duedate'],
]
],
'with group override' => [
$before, $after, null, null, $earlier, $later, [
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
'dataid' => 'duedate'],
]
],
'with both user and group overrides' => [
$before, $after, $earlier, $later, $earlier - DAYSECS, $later + DAYSECS, [
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
'dataid' => 'allowsubmissionsfromdate'],
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
'dataid' => 'duedate'],
]
],
];
Expand Down
1 change: 1 addition & 0 deletions mod/chat/classes/dates.php
Expand Up @@ -46,6 +46,7 @@ protected function get_dates(): array {
if (!empty($chat->schedule) && $chattime > $now) {
return [
[
'dataid' => 'chattime',
'label' => get_string('nextchattime', 'mod_chat'),
'timestamp' => (int) $chattime
]
Expand Down
15 changes: 10 additions & 5 deletions mod/chat/tests/dates_test.php
Expand Up @@ -62,39 +62,44 @@ public function get_dates_for_module_provider(): array {
$future, CHAT_SCHEDULE_SINGLE, [
[
'label' => $label,
'timestamp' => $future
'timestamp' => $future,
'dataid' => 'chattime',
],
]
],
'future chattime weekly' => [
$future, CHAT_SCHEDULE_WEEKLY, [
[
'label' => $label,
'timestamp' => $future
'timestamp' => $future,
'dataid' => 'chattime',
]
]
],
'future chattime daily' => [
$future, CHAT_SCHEDULE_DAILY, [
[
'label' => $label,
'timestamp' => $future
'timestamp' => $future,
'dataid' => 'chattime',
]
]
],
'past chattime daily' => [
$past, CHAT_SCHEDULE_DAILY, [
[
'label' => $label,
'timestamp' => $dailynextchattime
'timestamp' => $dailynextchattime,
'dataid' => 'chattime',
],
]
],
'past chattime weekly' => [
$past, CHAT_SCHEDULE_WEEKLY, [
[
'label' => $label,
'timestamp' => $weeklynextchattime
'timestamp' => $weeklynextchattime,
'dataid' => 'chattime',
],
]
],
Expand Down
2 changes: 2 additions & 0 deletions mod/choice/classes/dates.php
Expand Up @@ -50,6 +50,7 @@ protected function get_dates(): array {
if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
$dates[] = [
'dataid' => 'timeopen',
'label' => get_string($openlabelid, 'course'),
'timestamp' => (int) $timeopen,
];
Expand All @@ -58,6 +59,7 @@ protected function get_dates(): array {
if ($timeclose) {
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
$dates[] = [
'dataid' => 'timeclose',
'label' => get_string($closelabelid, 'course'),
'timestamp' => (int) $timeclose,
];
Expand Down
16 changes: 8 additions & 8 deletions mod/choice/tests/dates_test.php
Expand Up @@ -56,30 +56,30 @@ public function get_dates_for_module_provider(): array {
],
'only with opening time' => [
$after, null, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
]
],
'only with closing time' => [
null, $after, [
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
]
],
'with both times' => [
$after, $later, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $later],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeclose'],
]
],
'between the dates' => [
$before, $after, [
['label' => 'Opened:', 'timestamp' => $before],
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeopen'],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
]
],
'dates are past' => [
$earlier, $before, [
['label' => 'Opened:', 'timestamp' => $earlier],
['label' => 'Closed:', 'timestamp' => $before],
['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeopen'],
['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeclose'],
]
],
];
Expand Down
2 changes: 2 additions & 0 deletions mod/data/classes/dates.php
Expand Up @@ -50,6 +50,7 @@ protected function get_dates(): array {
if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
$dates[] = [
'dataid' => 'timeavailablefrom',
'label' => get_string($openlabelid, 'course'),
'timestamp' => (int) $timeopen,
];
Expand All @@ -58,6 +59,7 @@ protected function get_dates(): array {
if ($timeclose) {
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
$dates[] = [
'dataid' => 'timeavailableto',
'label' => get_string($closelabelid, 'course'),
'timestamp' => (int) $timeclose,
];
Expand Down
16 changes: 8 additions & 8 deletions mod/data/tests/dates_test.php
Expand Up @@ -56,30 +56,30 @@ public function get_dates_for_module_provider(): array {
],
'only with opening time' => [
$after, null, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeavailablefrom'],
]
],
'only with closing time' => [
null, $after, [
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeavailableto'],
]
],
'with both times' => [
$after, $later, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $later],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeavailablefrom'],
['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeavailableto'],
]
],
'between the dates' => [
$before, $after, [
['label' => 'Opened:', 'timestamp' => $before],
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeavailablefrom'],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeavailableto'],
]
],
'dates are past' => [
$earlier, $before, [
['label' => 'Opened:', 'timestamp' => $earlier],
['label' => 'Closed:', 'timestamp' => $before],
['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeavailablefrom'],
['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeavailableto'],
]
],
];
Expand Down
2 changes: 2 additions & 0 deletions mod/feedback/classes/dates.php
Expand Up @@ -50,6 +50,7 @@ protected function get_dates(): array {
if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
$dates[] = [
'dataid' => 'timeopen',
'label' => get_string($openlabelid, 'course'),
'timestamp' => (int) $timeopen,
];
Expand All @@ -58,6 +59,7 @@ protected function get_dates(): array {
if ($timeclose) {
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
$dates[] = [
'dataid' => 'timeclose',
'label' => get_string($closelabelid, 'course'),
'timestamp' => (int) $timeclose,
];
Expand Down
16 changes: 8 additions & 8 deletions mod/feedback/tests/dates_test.php
Expand Up @@ -56,30 +56,30 @@ public function get_dates_for_module_provider(): array {
],
'only with opening time' => [
$after, null, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
]
],
'only with closing time' => [
null, $after, [
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
]
],
'with both times' => [
$after, $later, [
['label' => 'Opens:', 'timestamp' => $after],
['label' => 'Closes:', 'timestamp' => $later],
['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeclose'],
]
],
'between the dates' => [
$before, $after, [
['label' => 'Opened:', 'timestamp' => $before],
['label' => 'Closes:', 'timestamp' => $after],
['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeopen'],
['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
]
],
'dates are past' => [
$earlier, $before, [
['label' => 'Opened:', 'timestamp' => $earlier],
['label' => 'Closed:', 'timestamp' => $before],
['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeopen'],
['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeclose'],
]
],
];
Expand Down
1 change: 1 addition & 0 deletions mod/forum/classes/dates.php
Expand Up @@ -47,6 +47,7 @@ protected function get_dates(): array {

if ($duedate) {
$dates[] = [
'dataid' => 'duedate',
'label' => get_string('activitydate:due', 'mod_forum'),
'timestamp' => (int) $duedate,
];
Expand Down

0 comments on commit f9af45e

Please sign in to comment.