Skip to content

Commit

Permalink
Merge branch 'MDL-62262-master' of https://github.com/lucaboesch/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jul 10, 2018
2 parents e7941ae + 4a73ecc commit 76dbb58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 49 deletions.
26 changes: 8 additions & 18 deletions mod/quiz/index.php
Expand Up @@ -55,17 +55,13 @@
die;
}

// Check if we need the closing date header.
$showclosingheader = false;
// Check if we need the feedback header.
$showfeedback = false;
foreach ($quizzes as $quiz) {
if ($quiz->timeclose!=0) {
$showclosingheader=true;
}
if (quiz_has_feedback($quiz)) {
$showfeedback=true;
}
if ($showclosingheader && $showfeedback) {
if ($showfeedback) {
break;
}
}
Expand All @@ -74,10 +70,8 @@
$headings = array(get_string('name'));
$align = array('left');

if ($showclosingheader) {
array_push($headings, get_string('quizcloses', 'quiz'));
array_push($align, 'left');
}
array_push($headings, get_string('quizcloses', 'quiz'));
array_push($align, 'left');

if (course_format_uses_sections($course->format)) {
array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
Expand Down Expand Up @@ -147,14 +141,10 @@
format_string($quiz->name, true) . '</a>';

// Close date.
if ($quiz->timeclose) {
if (($timeclosedates[$quiz->id]->usertimeclose == 0) AND ($timeclosedates[$quiz->id]->usertimelimit == 0)) {
$data[] = get_string('noclose', 'quiz');
} else {
$data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
}
} else if ($showclosingheader) {
$data[] = '';
if (($timeclosedates[$quiz->id]->usertimeclose != 0)) {
$data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
} else {
$data[] = get_string('noclose', 'quiz');
}

if ($showing == 'stats') {
Expand Down
37 changes: 12 additions & 25 deletions mod/quiz/locallib.php
Expand Up @@ -1211,53 +1211,40 @@ function quiz_get_user_image_options() {

/**
* Return an user's timeclose for all quizzes in a course, hereby taking into account group and user overrides.
* The query used herein is very similar to the one in function quiz_get_attempt_usertime_sql, so, in case you
* would change either one of them, make sure to apply your changes to both.
*
* @param int $courseid the course id.
* @return object An object with quizids and unixdates of the most lenient close overrides, if any.
* @return object An object with of all quizids and close unixdates in this course, taking into account the most lenient
* overrides, if existing and 0 if no close date is set.
*/
function quiz_get_user_timeclose($courseid) {
global $DB, $USER;

// For teacher and manager/admins return timeclose.
if (has_capability('moodle/course:update', context_course::instance($courseid))) {
$sql = "SELECT quiz.id, quiz.timeclose AS usertimeclose, COALESCE(quiz.timelimit, 0) AS usertimelimit
$sql = "SELECT quiz.id, quiz.timeclose AS usertimeclose
FROM {quiz} quiz
WHERE quiz.course = :courseid";

$results = $DB->get_records_sql($sql, array('courseid' => $courseid));
return $results;
}

// The multiple qgo JOINS are necessary because we want timeclose/timelimit = 0 (unlimited) to supercede
// any other group override.

$sql = "SELECT q.id,
COALESCE(v.oneclose, v.twoclose, v.threeclose, q.timeclose, 0) AS usertimeclose,
COALESCE(v.onelimit, v.twolimit, v.threelimit, q.timelimit, 0) AS usertimelimit
COALESCE(v.userclose, v.groupclose, q.timeclose, 0) AS usertimeclose
FROM (
SELECT quiz.id AS quizid,
MAX(quo.timeclose) AS oneclose, MAX(qgo1.timeclose) AS twoclose, MAX(qgo2.timeclose) AS threeclose,
MAX(quo.timelimit) AS onelimit, MAX(qgo3.timelimit) AS twolimit, MAX(qgo4.timelimit) AS threelimit
SELECT quiz.id as quizid,
MAX(quo.timeclose) AS userclose, MAX(qgo.timeclose) AS groupclose
FROM {quiz} quiz
LEFT JOIN {quiz_overrides} quo ON quo.quiz = quiz.id
LEFT JOIN {groups_members} gm ON gm.userid = quo.userid
LEFT JOIN {quiz_overrides} qgo1 ON qgo1.timeclose = 0 AND qgo1.quiz = quiz.id
LEFT JOIN {quiz_overrides} qgo2 ON qgo2.timeclose > 0 AND qgo2.quiz = quiz.id
LEFT JOIN {quiz_overrides} qgo3 ON qgo3.timelimit = 0 AND qgo3.quiz = quiz.id
LEFT JOIN {quiz_overrides} qgo4 ON qgo4.timelimit > 0 AND qgo4.quiz = quiz.id
AND qgo1.groupid = gm.groupid
AND qgo2.groupid = gm.groupid
AND qgo3.groupid = gm.groupid
AND qgo4.groupid = gm.groupid
LEFT JOIN {quiz_overrides} quo on quiz.id = quo.quiz AND quo.userid = :userid
LEFT JOIN {groups_members} gm ON gm.userid = :useringroupid
LEFT JOIN {quiz_overrides} qgo on quiz.id = qgo.quiz AND qgo.groupid = gm.groupid
WHERE quiz.course = :courseid
AND ((quo.userid = :userid) OR ((gm.userid IS NULL) AND (quo.userid IS NULL)))
GROUP BY quiz.id) v
JOIN {quiz} q ON q.id = v.quizid";
JOIN {quiz} q ON q.id = v.quizid";

$results = $DB->get_records_sql($sql, array('courseid' => $courseid, 'userid' => $USER->id));
$results = $DB->get_records_sql($sql, array('userid' => $USER->id, 'useringroupid' => $USER->id, 'courseid' => $courseid));
return $results;

}

/**
Expand Down
28 changes: 22 additions & 6 deletions mod/quiz/tests/locallib_test.php
Expand Up @@ -311,6 +311,7 @@ public function test_quiz_get_user_timeclose() {
// Create generator, course and quizzes.
$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();
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
Expand All @@ -323,13 +324,15 @@ public function test_quiz_get_user_timeclose() {

$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.
Expand Down Expand Up @@ -357,14 +360,31 @@ public function test_quiz_get_user_timeclose() {
$object = new stdClass();
$object->id = $quiz1->id;
$object->usertimeclose = $basetimestamp + 10800; // The overriden timeclose for quiz 1.
$object->usertimelimit = 0;

$comparearray[$quiz1->id] = $object;

$object = new stdClass();
$object->id = $quiz2->id;
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
$object->usertimelimit = 0;

$comparearray[$quiz2->id] = $object;

$this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));

// Let's test quiz 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 = $quiz1->id;
$object->usertimeclose = $basetimestamp + 7200; // The original timeclose for quiz 1.

$comparearray[$quiz1->id] = $object;

$object = new stdClass();
$object->id = $quiz2->id;
$object->usertimeclose = $basetimestamp + 7200; // The original timeclose for quiz 2.

$comparearray[$quiz2->id] = $object;

Expand All @@ -386,14 +406,12 @@ public function test_quiz_get_user_timeclose() {
$object = new stdClass();
$object->id = $quiz1->id;
$object->usertimeclose = $basetimestamp + 14400; // The overriden timeclose for quiz 1.
$object->usertimelimit = 0;

$comparearray[$quiz1->id] = $object;

$object = new stdClass();
$object->id = $quiz2->id;
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
$object->usertimelimit = 0;

$comparearray[$quiz2->id] = $object;

Expand All @@ -407,14 +425,12 @@ public function test_quiz_get_user_timeclose() {
$object = new stdClass();
$object->id = $quiz1->id;
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 1.
$object->usertimelimit = 0;

$comparearray[$quiz1->id] = $object;

$object = new stdClass();
$object->id = $quiz2->id;
$object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
$object->usertimelimit = 0;

$comparearray[$quiz2->id] = $object;

Expand Down

0 comments on commit 76dbb58

Please sign in to comment.