MDL-34109 quiz cron: change whitespace before applying the fix.
[moodle.git] / mod / quiz / cronlib.php
CommitLineData
e1bf1619
TH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Library code used by quiz cron.
19 *
20 * @package mod_quiz
21 * @copyright 2012 the Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->dirroot . '/mod/quiz/locallib.php');
29
30
31/**
32 * This class holds all the code for automatically updating all attempts that have
33 * gone over their time limit.
34 *
35 * @copyright 2012 the Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class mod_quiz_overdue_attempt_updater {
39
40 /**
41 * Do the processing required.
42 * @param int $timenow the time to consider as 'now' during the processing.
43 * @param int $processfrom the value of $processupto the last time update_overdue_attempts was
44 * called called and completed successfully.
45 * @param int $processto only process attempt modifed longer ago than this.
815fcecc 46 * @return array with two elements, the number of attempt considered, and how many different quizzes that was.
e1bf1619
TH
47 */
48 public function update_overdue_attempts($timenow, $processfrom, $processto) {
49 global $DB;
50
51 $attemptstoprocess = $this->get_list_of_overdue_attempts($processfrom, $processto);
52
53 $course = null;
54 $quiz = null;
55 $cm = null;
56
815fcecc
TH
57 $count = 0;
58 $quizcount = 0;
e1bf1619
TH
59 foreach ($attemptstoprocess as $attempt) {
60 // If we have moved on to a different quiz, fetch the new data.
61 if (!$quiz || $attempt->quiz != $quiz->id) {
62 $quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST);
63 $cm = get_coursemodule_from_instance('quiz', $attempt->quiz);
815fcecc 64 $quizcount += 1;
e1bf1619
TH
65 }
66
67 // If we have moved on to a different course, fetch the new data.
68 if (!$course || $course->id != $quiz->course) {
69 $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
70 }
71
72 // Make a specialised version of the quiz settings, with the relevant overrides.
73 $quizforuser = clone($quiz);
74 $quizforuser->timeclose = $attempt->usertimeclose;
75 $quizforuser->timelimit = $attempt->usertimelimit;
76
77 // Trigger any transitions that are required.
78 $attemptobj = new quiz_attempt($attempt, $quizforuser, $cm, $course);
79 $attemptobj->handle_if_time_expired($timenow, false);
815fcecc 80 $count += 1;
e1bf1619
TH
81 }
82
83 $attemptstoprocess->close();
815fcecc 84 return array($count, $quizcount);
e1bf1619
TH
85 }
86
87 /**
88 * @return moodle_recordset of quiz_attempts that need to be processed because time has
89 * passed. The array is sorted by courseid then quizid.
90 */
91 protected function get_list_of_overdue_attempts($processfrom, $processto) {
92 global $DB;
93
94 // This query should have all the quiz_attempts columns.
95 return $DB->get_recordset_sql("
f59f8571
TH
96 SELECT quiza.*,
97 group_by_results.usertimeclose,
98 group_by_results.usertimelimit
99
100 FROM (
101
102 SELECT iquiza.id AS attemptid,
103 quiz.course,
104 quiz.graceperiod,
e1bf1619
TH
105 COALESCE(quo.timeclose, MAX(qgo.timeclose), quiz.timeclose) AS usertimeclose,
106 COALESCE(quo.timelimit, MAX(qgo.timelimit), quiz.timelimit) AS usertimelimit
107
f59f8571
TH
108 FROM {quiz_attempts} iquiza
109 JOIN {quiz} quiz ON quiz.id = iquiza.quiz
110 LEFT JOIN {quiz_overrides} quo ON quo.quiz = quiz.id AND quo.userid = iquiza.userid
e1bf1619 111 LEFT JOIN {quiz_overrides} qgo ON qgo.quiz = quiz.id
f59f8571
TH
112 LEFT JOIN {groups_members} gm ON gm.userid = iquiza.userid AND gm.groupid = qgo.groupid
113
114 WHERE iquiza.state IN ('inprogress', 'overdue')
115 AND iquiza.timemodified >= :processfrom
116 AND iquiza.timemodified < :processto
117
118 GROUP BY iquiza.id,
e1bf1619
TH
119 quiz.course,
120 quiz.timeclose,
121 quiz.timelimit,
122 quiz.graceperiod,
123 quo.timeclose,
124 quo.timelimit
f59f8571
TH
125 ) group_by_results
126 JOIN {quiz_attempts} quiza ON quiza.id = group_by_results.attemptid
e1bf1619 127
592443f4
TH
128 WHERE (
129 state = 'inprogress' AND (
130 :timenow1 > usertimeclose OR
131 :timenow2 > quiza.timestart + usertimelimit
132 )
133 )
134 OR
135 (
136 state = 'overdue' AND (
137 :timenow3 > graceperiod + usertimeclose OR
138 :timenow4 > graceperiod + quiza.timestart + usertimelimit
139 )
140 )
e1bf1619 141
f59f8571 142 ORDER BY course, quiz",
e1bf1619
TH
143
144 array('processfrom' => $processfrom, 'processto' => $processto,
145 'timenow1' => $processto, 'timenow2' => $processto,
146 'timenow3' => $processto, 'timenow4' => $processto));
147 }
148}