Skip to content

Commit 100e9f3

Browse files
author
David Monllao
committed
Merge branch 'MDL-52832-34' of https://github.com/lucaboesch/moodle into MOODLE_34_STABLE
2 parents 5bcc32e + 3687722 commit 100e9f3

File tree

3 files changed

+181
-2
lines changed

3 files changed

+181
-2
lines changed

mod/quiz/index.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@
129129

130130
// Populate the table with the list of instances.
131131
$currentsection = '';
132+
// Get all closing dates.
133+
$timeclosedates = quiz_get_user_timeclose($course->id);
132134
foreach ($quizzes as $quiz) {
133135
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
134136
$context = context_module::instance($cm->id);
@@ -158,7 +160,11 @@
158160

159161
// Close date.
160162
if ($quiz->timeclose) {
161-
$data[] = userdate($quiz->timeclose);
163+
if (($timeclosedates[$quiz->id]->usertimeclose == 0) AND ($timeclosedates[$quiz->id]->usertimelimit == 0)) {
164+
$data[] = get_string('noclose', 'quiz');
165+
} else {
166+
$data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
167+
}
162168
} else if ($showclosingheader) {
163169
$data[] = '';
164170
}

mod/quiz/locallib.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,8 @@ function quiz_update_open_attempts(array $conditions) {
10901090

10911091
/**
10921092
* Returns SQL to compute timeclose and timelimit for every attempt, taking into account user and group overrides.
1093+
* The query used herein is very similar to the one in function quiz_get_user_timeclose, so, in case you
1094+
* would change either one of them, make sure to apply your changes to both.
10931095
*
10941096
* @param string $redundantwhereclauses extra where clauses to add to the subquery
10951097
* for performance. These can use the table alias iquiza for the quiz attempts table.
@@ -1205,6 +1207,53 @@ function quiz_get_user_image_options() {
12051207
);
12061208
}
12071209

1210+
/**
1211+
* Return an user's timeclose for all quizzes in a course, hereby taking into account group and user overrides.
1212+
* The query used herein is very similar to the one in function quiz_get_attempt_usertime_sql, so, in case you
1213+
* would change either one of them, make sure to apply your changes to both.
1214+
*
1215+
* @param int $courseid the course id.
1216+
* @return object An object with quizids and unixdates of the most lenient close overrides, if any.
1217+
*/
1218+
function quiz_get_user_timeclose($courseid) {
1219+
global $DB, $USER;
1220+
1221+
// For teacher and manager/admins return timeclose.
1222+
if (has_capability('moodle/course:update', context_course::instance($courseid))) {
1223+
$sql = "SELECT quiz.id, quiz.timeclose AS usertimeclose, COALESCE(quiz.timelimit, 0) AS usertimelimit
1224+
FROM {quiz} quiz
1225+
WHERE quiz.course = :courseid
1226+
GROUP BY quiz.id";
1227+
1228+
$results = $DB->get_records_sql($sql, array('courseid' => $courseid));
1229+
return $results;
1230+
}
1231+
1232+
// The multiple qgo JOINS are necessary because we want timeclose/timelimit = 0 (unlimited) to supercede
1233+
// any other group override.
1234+
1235+
$sql = "SELECT quiz.id,
1236+
COALESCE(MAX(quo.timeclose), MAX(qgo1.timeclose), MAX(qgo2.timeclose), quiz.timeclose, 0) AS usertimeclose,
1237+
COALESCE(MAX(quo.timelimit), MAX(qgo3.timelimit), MAX(qgo4.timelimit), quiz.timelimit, 0) AS usertimelimit
1238+
FROM {quiz} quiz
1239+
LEFT JOIN {quiz_overrides} quo ON quo.quiz = quiz.id
1240+
LEFT JOIN {groups_members} gm ON gm.userid = quo.userid
1241+
LEFT JOIN {quiz_overrides} qgo1 ON qgo1.timeclose = 0 AND qgo1.quiz = quiz.id
1242+
LEFT JOIN {quiz_overrides} qgo2 ON qgo2.timeclose > 0 AND qgo2.quiz = quiz.id
1243+
LEFT JOIN {quiz_overrides} qgo3 ON qgo3.timelimit = 0 AND qgo3.quiz = quiz.id
1244+
LEFT JOIN {quiz_overrides} qgo4 ON qgo4.timelimit > 0 AND qgo4.quiz = quiz.id
1245+
AND qgo1.groupid = gm.groupid
1246+
AND qgo2.groupid = gm.groupid
1247+
AND qgo3.groupid = gm.groupid
1248+
AND qgo4.groupid = gm.groupid
1249+
WHERE quiz.course = :courseid
1250+
AND ((quo.userid = :userid) OR ((gm.userid IS NULL) AND (quo.userid IS NULL)))
1251+
GROUP BY quiz.id";
1252+
1253+
$results = $DB->get_records_sql($sql, array('courseid' => $courseid, 'userid' => $USER->id));
1254+
return $results;
1255+
}
1256+
12081257
/**
12091258
* Get the choices to offer for the 'Questions per page' option.
12101259
* @return array int => string.
@@ -2136,7 +2185,7 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
21362185
FROM {quiz_slots}
21372186
WHERE questionid = q.id)
21382187
ORDER BY id", array($category->id, ($includesubcategories ? '1' : '0')))) {
2139-
// Take as many of these as needed.
2188+
// Take as many of these as needed.
21402189
while (($existingquestion = array_shift($existingquestions)) && $number > 0) {
21412190
quiz_add_quiz_question($existingquestion->id, $quiz, $addonpage);
21422191
$number -= 1;

mod/quiz/tests/locallib_test.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,128 @@ public function test_quiz_is_overriden_calendar_event_group_override() {
296296

297297
$this->assertTrue(quiz_is_overriden_calendar_event($event));
298298
}
299+
300+
/**
301+
* Test test_quiz_get_user_timeclose().
302+
*/
303+
public function test_quiz_get_user_timeclose() {
304+
global $DB;
305+
306+
$this->resetAfterTest();
307+
$this->setAdminUser();
308+
309+
$basetimestamp = time(); // The timestamp we will base the enddates on.
310+
311+
// Create generator, course and quizzes.
312+
$student1 = $this->getDataGenerator()->create_user();
313+
$student2 = $this->getDataGenerator()->create_user();
314+
$teacher = $this->getDataGenerator()->create_user();
315+
$course = $this->getDataGenerator()->create_course();
316+
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
317+
318+
// Both quizzes close in two hours.
319+
$quiz1 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
320+
$quiz2 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
321+
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
322+
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
323+
324+
$student1id = $student1->id;
325+
$student2id = $student2->id;
326+
$teacherid = $teacher->id;
327+
328+
// Users enrolments.
329+
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
330+
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
331+
$this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
332+
$this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
333+
$this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');
334+
335+
// Create groups.
336+
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
337+
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
338+
$group1id = $group1->id;
339+
$group2id = $group2->id;
340+
$this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
341+
$this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));
342+
343+
// Group 1 gets an group override for quiz 1 to close in three hours.
344+
$record1 = (object) [
345+
'quiz' => $quiz1->id,
346+
'groupid' => $group1id,
347+
'timeclose' => $basetimestamp + 10800 // In three hours.
348+
];
349+
$DB->insert_record('quiz_overrides', $record1);
350+
351+
// Let's test quiz 1 closes in three hours for user student 1 since member of group 1.
352+
// Quiz 2 closes in two hours.
353+
$this->setUser($student1id);
354+
$params = new stdClass();
355+
356+
$comparearray = array();
357+
$object = new stdClass();
358+
$object->id = $quiz1->id;
359+
$object->usertimeclose = $basetimestamp + 10800; // The overriden timeclose for quiz 1.
360+
$object->usertimelimit = 0;
361+
362+
$comparearray[$quiz1->id] = $object;
363+
364+
$object = new stdClass();
365+
$object->id = $quiz2->id;
366+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
367+
$object->usertimelimit = 0;
368+
369+
$comparearray[$quiz2->id] = $object;
370+
371+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
372+
373+
// User 2 gets an user override for quiz 1 to close in four hours.
374+
$record2 = (object) [
375+
'quiz' => $quiz1->id,
376+
'userid' => $student2id,
377+
'timeclose' => $basetimestamp + 14400 // In four hours.
378+
];
379+
$DB->insert_record('quiz_overrides', $record2);
380+
381+
// Let's test quiz 1 closes in four hours for user student 2 since personally overriden.
382+
// Quiz 2 closes in two hours.
383+
$this->setUser($student2id);
384+
385+
$comparearray = array();
386+
$object = new stdClass();
387+
$object->id = $quiz1->id;
388+
$object->usertimeclose = $basetimestamp + 14400; // The overriden timeclose for quiz 1.
389+
$object->usertimelimit = 0;
390+
391+
$comparearray[$quiz1->id] = $object;
392+
393+
$object = new stdClass();
394+
$object->id = $quiz2->id;
395+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
396+
$object->usertimelimit = 0;
397+
398+
$comparearray[$quiz2->id] = $object;
399+
400+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
401+
402+
// Let's test a teacher sees the original times.
403+
// Quiz 1 and quiz 2 close in two hours.
404+
$this->setUser($teacherid);
405+
406+
$comparearray = array();
407+
$object = new stdClass();
408+
$object->id = $quiz1->id;
409+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 1.
410+
$object->usertimelimit = 0;
411+
412+
$comparearray[$quiz1->id] = $object;
413+
414+
$object = new stdClass();
415+
$object->id = $quiz2->id;
416+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
417+
$object->usertimelimit = 0;
418+
419+
$comparearray[$quiz2->id] = $object;
420+
421+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
422+
}
299423
}

0 commit comments

Comments
 (0)