Permalink
Browse files

Merge branch 'MDL-62761-34' of https://github.com/jmvedrine/moodle in…

…to MOODLE_34_STABLE
  • Loading branch information...
dmonllao committed Aug 13, 2018
2 parents 3d5d4ad + 5eb7a89 commit 7fda5667ba8323ba9bfc61fb02ef73c6c4dc30ae
Showing with 185 additions and 5 deletions.
  1. +7 −5 mod/lesson/index.php
  2. +38 −0 mod/lesson/locallib.php
  3. +140 −0 mod/lesson/tests/locallib_test.php
@@ -85,7 +85,8 @@
$table->head = array ($strname, $strgrade, $strdeadline);
$table->align = array ("left", "center", "center");
}
// Get all deadlines.
$deadlines = lesson_get_user_deadline($course->id);
foreach ($lessons as $lesson) {
if (!$lesson->visible) {
//Show dimmed if the mod is hidden
@@ -97,12 +98,13 @@
$cm = get_coursemodule_from_instance('lesson', $lesson->id);
$context = context_module::instance($cm->id);
if ($lesson->deadline == 0) {
$deadline = $deadlines[$lesson->id]->userdeadline;
if ($deadline == 0) {
$due = $strnodeadline;
} else if ($lesson->deadline > $timenow) {
$due = userdate($lesson->deadline);
} else if ($deadline > $timenow) {
$due = userdate($deadline);
} else {
$due = "<font color=\"red\">".userdate($lesson->deadline)."</font>";
$due = "<font color=\"red\">" . userdate($deadline) . "</font>";
}
if ($usesections) {
@@ -1187,6 +1187,44 @@ function lesson_get_user_detailed_report_data(lesson $lesson, $userid, $attempt)
return array($answerpages, $userstats);
}
/**
* Return user's deadline for all lessons in a course, hereby taking into account group and user overrides.
*
* @param int $courseid the course id.
* @return object An object with of all lessonsids and close unixdates in this course,
* taking into account the most lenient overrides, if existing and 0 if no close date is set.
*/
function lesson_get_user_deadline($courseid) {
global $DB, $USER;
// For teacher and manager/admins return lesson's deadline.
if (has_capability('moodle/course:update', context_course::instance($courseid))) {
$sql = "SELECT lesson.id, lesson.deadline AS userdeadline
FROM {lesson} lesson
WHERE lesson.course = :courseid";
$results = $DB->get_records_sql($sql, array('courseid' => $courseid));
return $results;
}
$sql = "SELECT a.id,
COALESCE(v.userclose, v.groupclose, a.deadline, 0) AS userdeadline
FROM (
SELECT lesson.id as lessonid,
MAX(leo.deadline) AS userclose, MAX(qgo.deadline) AS groupclose
FROM {lesson} lesson
LEFT JOIN {lesson_overrides} leo on lesson.id = leo.lessonid AND leo.userid = :userid
LEFT JOIN {groups_members} gm ON gm.userid = :useringroupid
LEFT JOIN {lesson_overrides} qgo on lesson.id = qgo.lessonid AND qgo.groupid = gm.groupid
WHERE lesson.course = :courseid
GROUP BY lesson.id
) v
JOIN {lesson} a ON a.id = v.lessonid";
$results = $DB->get_records_sql($sql, array('userid' => $USER->id, 'useringroupid' => $USER->id, 'courseid' => $courseid));
return $results;
}
/**
* Abstract class that page type's MUST inherit from.
@@ -76,4 +76,144 @@ public function test_duplicate_page() {
}
}
}
/**
* Test test_lesson_get_user_deadline().
*/
public function test_lesson_get_user_deadline() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$basetimestamp = time(); // The timestamp we will base the enddates on.
// Create generator, course and lessons.
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
$student3 = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
// Both lessons close in two hours.
$lesson1 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
$lesson2 = $lessongenerator->create_instance(array('course' => $course->id, 'deadline' => $basetimestamp + 7200));
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$student1id = $student1->id;
$student2id = $student2->id;
$student3id = $student3->id;
$teacherid = $teacher->id;
// Users enrolments.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($student3id, $course->id, $studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');
// Create groups.
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
$group1id = $group1->id;
$group2id = $group2->id;
$this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
$this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));
// Group 1 gets an group override for lesson 1 to close in three hours.
$record1 = (object) [
'lessonid' => $lesson1->id,
'groupid' => $group1id,
'deadline' => $basetimestamp + 10800 // In three hours.
];
$DB->insert_record('lesson_overrides', $record1);
// Let's test lesson 1 closes in three hours for user student 1 since member of group 1.
// lesson 2 closes in two hours.
$this->setUser($student1id);
$params = new stdClass();
$comparearray = array();
$object = new stdClass();
$object->id = $lesson1->id;
$object->userdeadline = $basetimestamp + 10800; // The overriden deadline for lesson 1.
$comparearray[$lesson1->id] = $object;
$object = new stdClass();
$object->id = $lesson2->id;
$object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
$comparearray[$lesson2->id] = $object;
$this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
// Let's test lesson 1 closes in two hours (the original value) for user student 3 since member of no group.
$this->setUser($student3id);
$params = new stdClass();
$comparearray = array();
$object = new stdClass();
$object->id = $lesson1->id;
$object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 1.
$comparearray[$lesson1->id] = $object;
$object = new stdClass();
$object->id = $lesson2->id;
$object->userdeadline = $basetimestamp + 7200; // The original deadline for lesson 2.
$comparearray[$lesson2->id] = $object;
$this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
// User 2 gets an user override for lesson 1 to close in four hours.
$record2 = (object) [
'lessonid' => $lesson1->id,
'userid' => $student2id,
'deadline' => $basetimestamp + 14400 // In four hours.
];
$DB->insert_record('lesson_overrides', $record2);
// Let's test lesson 1 closes in four hours for user student 2 since personally overriden.
// lesson 2 closes in two hours.
$this->setUser($student2id);
$comparearray = array();
$object = new stdClass();
$object->id = $lesson1->id;
$object->userdeadline = $basetimestamp + 14400; // The overriden deadline for lesson 1.
$comparearray[$lesson1->id] = $object;
$object = new stdClass();
$object->id = $lesson2->id;
$object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
$comparearray[$lesson2->id] = $object;
$this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
// Let's test a teacher sees the original times.
// lesson 1 and lesson 2 close in two hours.
$this->setUser($teacherid);
$comparearray = array();
$object = new stdClass();
$object->id = $lesson1->id;
$object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 1.
$comparearray[$lesson1->id] = $object;
$object = new stdClass();
$object->id = $lesson2->id;
$object->userdeadline = $basetimestamp + 7200; // The unchanged deadline for lesson 2.
$comparearray[$lesson2->id] = $object;
$this->assertEquals($comparearray, lesson_get_user_deadline($course->id));
}
}

0 comments on commit 7fda566

Please sign in to comment.