Skip to content

Commit ad0f688

Browse files
author
David Monllao
committed
Merge branch 'MDL-52832-33' of https://github.com/lucaboesch/moodle into MOODLE_33_STABLE
2 parents e3db31a + efc06d6 commit ad0f688

File tree

3 files changed

+180
-1
lines changed

3 files changed

+180
-1
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: 49 additions & 0 deletions
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.

mod/quiz/tests/locallib_test.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,128 @@ public function test_quiz_view() {
152152
$completiondata = $completion->get_data($cm);
153153
$this->assertEquals(1, $completiondata->completionstate);
154154
}
155+
156+
/**
157+
* Test test_quiz_get_user_timeclose().
158+
*/
159+
public function test_quiz_get_user_timeclose() {
160+
global $DB;
161+
162+
$this->resetAfterTest();
163+
$this->setAdminUser();
164+
165+
$basetimestamp = time(); // The timestamp we will base the enddates on.
166+
167+
// Create generator, course and quizzes.
168+
$student1 = $this->getDataGenerator()->create_user();
169+
$student2 = $this->getDataGenerator()->create_user();
170+
$teacher = $this->getDataGenerator()->create_user();
171+
$course = $this->getDataGenerator()->create_course();
172+
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
173+
174+
// Both quizzes close in two hours.
175+
$quiz1 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
176+
$quiz2 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
177+
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
178+
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
179+
180+
$student1id = $student1->id;
181+
$student2id = $student2->id;
182+
$teacherid = $teacher->id;
183+
184+
// Users enrolments.
185+
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
186+
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
187+
$this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
188+
$this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
189+
$this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');
190+
191+
// Create groups.
192+
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
193+
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
194+
$group1id = $group1->id;
195+
$group2id = $group2->id;
196+
$this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
197+
$this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));
198+
199+
// Group 1 gets an group override for quiz 1 to close in three hours.
200+
$record1 = (object) [
201+
'quiz' => $quiz1->id,
202+
'groupid' => $group1id,
203+
'timeclose' => $basetimestamp + 10800 // In three hours.
204+
];
205+
$DB->insert_record('quiz_overrides', $record1);
206+
207+
// Let's test quiz 1 closes in three hours for user student 1 since member of group 1.
208+
// Quiz 2 closes in two hours.
209+
$this->setUser($student1id);
210+
$params = new stdClass();
211+
212+
$comparearray = array();
213+
$object = new stdClass();
214+
$object->id = $quiz1->id;
215+
$object->usertimeclose = $basetimestamp + 10800; // The overriden timeclose for quiz 1.
216+
$object->usertimelimit = 0;
217+
218+
$comparearray[$quiz1->id] = $object;
219+
220+
$object = new stdClass();
221+
$object->id = $quiz2->id;
222+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
223+
$object->usertimelimit = 0;
224+
225+
$comparearray[$quiz2->id] = $object;
226+
227+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
228+
229+
// User 2 gets an user override for quiz 1 to close in four hours.
230+
$record2 = (object) [
231+
'quiz' => $quiz1->id,
232+
'userid' => $student2id,
233+
'timeclose' => $basetimestamp + 14400 // In four hours.
234+
];
235+
$DB->insert_record('quiz_overrides', $record2);
236+
237+
// Let's test quiz 1 closes in four hours for user student 2 since personally overriden.
238+
// Quiz 2 closes in two hours.
239+
$this->setUser($student2id);
240+
241+
$comparearray = array();
242+
$object = new stdClass();
243+
$object->id = $quiz1->id;
244+
$object->usertimeclose = $basetimestamp + 14400; // The overriden timeclose for quiz 1.
245+
$object->usertimelimit = 0;
246+
247+
$comparearray[$quiz1->id] = $object;
248+
249+
$object = new stdClass();
250+
$object->id = $quiz2->id;
251+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
252+
$object->usertimelimit = 0;
253+
254+
$comparearray[$quiz2->id] = $object;
255+
256+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
257+
258+
// Let's test a teacher sees the original times.
259+
// Quiz 1 and quiz 2 close in two hours.
260+
$this->setUser($teacherid);
261+
262+
$comparearray = array();
263+
$object = new stdClass();
264+
$object->id = $quiz1->id;
265+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 1.
266+
$object->usertimelimit = 0;
267+
268+
$comparearray[$quiz1->id] = $object;
269+
270+
$object = new stdClass();
271+
$object->id = $quiz2->id;
272+
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
273+
$object->usertimelimit = 0;
274+
275+
$comparearray[$quiz2->id] = $object;
276+
277+
$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
278+
}
155279
}

0 commit comments

Comments
 (0)