Skip to content

Commit

Permalink
MDL-71410 mod_assign: implement activity_dates for the assignment module
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Apr 27, 2021
1 parent 4532d3f commit 18efd91
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
67 changes: 67 additions & 0 deletions mod/assign/classes/dates.php
@@ -0,0 +1,67 @@
<?php
// 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/>.

/**
* Contains the class for fetching the important dates in mod_assign for a given module instance and a user.
*
* @package mod_assign
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

declare(strict_types=1);

namespace mod_assign;

use core\activity_dates;

/**
* Class for fetching the important dates in mod_assign for a given module instance and a user.
*
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dates extends activity_dates {

/**
* Returns a list of important dates in mod_assign
*
* @return array
*/
protected function get_dates(): array {
$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[] = [
'label' => get_string($openlabelid, 'mod_assign'),
'timestamp' => (int) $timeopen,
];
}

if ($timedue) {
$dates[] = [
'label' => get_string('activitydate:submissionsdue', 'mod_assign'),
'timestamp' => (int) $timedue,
];
}

return $dates;
}
}
3 changes: 3 additions & 0 deletions mod/assign/lang/en/assign.php
Expand Up @@ -22,6 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['activitydate:submissionsdue'] = 'Submissions due:';
$string['activitydate:submissionsopen'] = 'Submissions open:';
$string['activitydate:submissionsopened'] = 'Submissions opened:';
$string['activityoverview'] = 'You have assignments that need attention';
$string['addsubmission'] = 'Add submission';
$string['addsubmission_help'] = 'You have not made a submission yet.';
Expand Down
175 changes: 175 additions & 0 deletions mod/assign/tests/dates_test.php
@@ -0,0 +1,175 @@
<?php
// 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/>.

/**
* Contains unit tests for mod_assign\dates.
*
* @package mod_assign
* @category test
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

declare(strict_types=1);

namespace mod_assign;

use advanced_testcase;
use cm_info;
use core\activity_dates;

/**
* Class for unit testing mod_assign\dates.
*
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dates_test extends advanced_testcase {

/**
* Data provider for get_dates_for_module().
* @return array[]
*/
public function get_dates_for_module_provider(): array {
$now = time();
$before = $now - DAYSECS;
$earlier = $before - DAYSECS;
$after = $now + DAYSECS;
$later = $after + DAYSECS;

return [
'without any dates' => [
null, null, null, null, null, null, []
],
'only with opening time' => [
$after, null, null, null, null, null, [
['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after],
]
],
'only with closing time' => [
null, $after, null, null, null, null, [
['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after],
]
],
'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],
]
],
'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],
]
],
'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],
]
],
'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],
]
],
'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],
]
],
'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],
]
],
];
}

/**
* Test for get_dates_for_module().
*
* @dataProvider get_dates_for_module_provider
* @param int|null $from Time of opening submissions in the assignment.
* @param int|null $due Assignment's due date.
* @param int|null $userfrom The user override for opening submissions.
* @param int|null $userdue The user override for due date.
* @param int|null $groupfrom The group override for opening submissions.
* @param int|null $groupdue The group override for due date.
* @param array $expected The expected value of calling get_dates_for_module()
*/
public function test_get_dates_for_module(?int $from, ?int $due,
?int $userfrom, ?int $userdue,
?int $groupfrom, ?int $groupdue,
array $expected) {

$this->resetAfterTest();
$generator = $this->getDataGenerator();
/** @var \mod_assign_generator $assigngenerator */
$assigngenerator = $generator->get_plugin_generator('mod_assign');

$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id);

$data = ['course' => $course->id];
if ($from) {
$data['allowsubmissionsfromdate'] = $from;
}
if ($due) {
$data['duedate'] = $due;
}
$assign = $assigngenerator->create_instance($data);

if ($userfrom || $userdue || $groupfrom || $groupdue) {
$generator->enrol_user($user->id, $course->id);
$group = $generator->create_group(['courseid' => $course->id]);
$generator->create_group_member(['groupid' => $group->id, 'userid' => $user->id]);

if ($userfrom || $userdue) {
$assigngenerator->create_override([
'assignid' => $assign->id,
'userid' => $user->id,
'allowsubmissionsfromdate' => $userfrom,
'duedate' => $userdue,
]);
}

if ($groupfrom || $groupdue) {
$assigngenerator->create_override([
'assignid' => $assign->id,
'groupid' => $group->id,
'allowsubmissionsfromdate' => $groupfrom,
'duedate' => $groupdue,
]);
}
}

$this->setUser($user);

$cm = get_coursemodule_from_instance('assign', $assign->id);
// Make sure we're using a cm_info object.
$cm = cm_info::create($cm);

$dates = activity_dates::get_dates_for_module($cm, (int) $user->id);

$this->assertEquals($expected, $dates);
}
}
24 changes: 24 additions & 0 deletions mod/assign/tests/generator/lib.php
Expand Up @@ -59,4 +59,28 @@ public function create_instance($record = null, array $options = null) {

return parent::create_instance($record, (array)$options);
}

/**
* Create an assign override (either user or group).
*
* @param array $data must specify assignid, and one of userid or groupid.
* @throws coding_exception
*/
public function create_override(array $data): void {
global $DB;

if (!isset($data['assignid'])) {
throw new coding_exception('Must specify assignid when creating an assign override.');
}

if (!isset($data['userid']) && !isset($data['groupid'])) {
throw new coding_exception('Must specify one of userid or groupid when creating an assign override.');
}

if (isset($data['userid']) && isset($data['groupid'])) {
throw new coding_exception('Cannot specify both userid and groupid when creating an assign override.');
}

$DB->insert_record('assign_overrides', (object) $data);
}
}

0 comments on commit 18efd91

Please sign in to comment.