MDL-76614 quiz: merge mod_quiz_overdue_attempt_updater into task
The only place this code was used was in mod_quiz\task\update_overdue_attempts so neater to combine them into one class.
This commit is contained in:
@@ -24,6 +24,10 @@
|
||||
*/
|
||||
namespace mod_quiz\task;
|
||||
|
||||
use moodle_exception;
|
||||
use moodle_recordset;
|
||||
use quiz_attempt;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
@@ -44,22 +48,103 @@ class update_overdue_attempts extends \core\task\scheduled_task {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Close off any overdue attempts.
|
||||
*/
|
||||
public function execute() {
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/mod/quiz/cronlib.php');
|
||||
$timenow = time();
|
||||
$overduehander = new \mod_quiz_overdue_attempt_updater();
|
||||
|
||||
$processto = $timenow - get_config('quiz', 'graceperiodmin');
|
||||
|
||||
mtrace(' Looking for quiz overdue quiz attempts...');
|
||||
|
||||
list($count, $quizcount) = $overduehander->update_overdue_attempts($timenow, $processto);
|
||||
list($count, $quizcount) = $this->update_all_overdue_attempts($timenow, $processto);
|
||||
|
||||
mtrace(' Considered ' . $count . ' attempts in ' . $quizcount . ' quizzes.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the processing required.
|
||||
* @param int $timenow the time to consider as 'now' during the processing.
|
||||
* @param int $processto only process attempt with timecheckstate longer ago than this.
|
||||
* @return array with two elements, the number of attempt considered, and how many different quizzes that was.
|
||||
*/
|
||||
public function update_all_overdue_attempts($timenow, $processto) {
|
||||
global $DB;
|
||||
|
||||
$attemptstoprocess = $this->get_list_of_overdue_attempts($processto);
|
||||
|
||||
$course = null;
|
||||
$quiz = null;
|
||||
$cm = null;
|
||||
|
||||
$count = 0;
|
||||
$quizcount = 0;
|
||||
foreach ($attemptstoprocess as $attempt) {
|
||||
try {
|
||||
|
||||
// If we have moved on to a different quiz, fetch the new data.
|
||||
if (!$quiz || $attempt->quiz != $quiz->id) {
|
||||
$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST);
|
||||
$cm = get_coursemodule_from_instance('quiz', $attempt->quiz);
|
||||
$quizcount += 1;
|
||||
}
|
||||
|
||||
// If we have moved on to a different course, fetch the new data.
|
||||
if (!$course || $course->id != $quiz->course) {
|
||||
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
// Make a specialised version of the quiz settings, with the relevant overrides.
|
||||
$quizforuser = clone($quiz);
|
||||
$quizforuser->timeclose = $attempt->usertimeclose;
|
||||
$quizforuser->timelimit = $attempt->usertimelimit;
|
||||
|
||||
// Trigger any transitions that are required.
|
||||
$attemptobj = new quiz_attempt($attempt, $quizforuser, $cm, $course);
|
||||
$attemptobj->handle_if_time_expired($timenow, false);
|
||||
$count += 1;
|
||||
|
||||
} catch (moodle_exception $e) {
|
||||
// If an error occurs while processing one attempt, don't let that kill cron.
|
||||
mtrace("Error while processing attempt {$attempt->id} at {$attempt->quiz} quiz:");
|
||||
mtrace($e->getMessage());
|
||||
mtrace($e->getTraceAsString());
|
||||
// Close down any currently open transactions, otherwise one error
|
||||
// will stop following DB changes from being committed.
|
||||
$DB->force_transaction_rollback();
|
||||
}
|
||||
}
|
||||
|
||||
$attemptstoprocess->close();
|
||||
return array($count, $quizcount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return moodle_recordset of quiz_attempts that need to be processed because time has
|
||||
* passed. The array is sorted by courseid then quizid.
|
||||
*/
|
||||
public function get_list_of_overdue_attempts($processto) {
|
||||
global $DB;
|
||||
|
||||
// SQL to compute timeclose and timelimit for each attempt.
|
||||
$quizausersql = quiz_get_attempt_usertime_sql(
|
||||
"iquiza.state IN ('inprogress', 'overdue') AND iquiza.timecheckstate <= :iprocessto");
|
||||
|
||||
// This query should have all the quiz_attempts columns.
|
||||
return $DB->get_recordset_sql("
|
||||
SELECT quiza.*,
|
||||
quizauser.usertimeclose,
|
||||
quizauser.usertimelimit
|
||||
|
||||
FROM {quiz_attempts} quiza
|
||||
JOIN {quiz} quiz ON quiz.id = quiza.quiz
|
||||
JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
|
||||
|
||||
WHERE quiza.state IN ('inprogress', 'overdue')
|
||||
AND quiza.timecheckstate <= :processto
|
||||
ORDER BY quiz.course, quiza.quiz",
|
||||
|
||||
array('processto' => $processto, 'iprocessto' => $processto));
|
||||
}
|
||||
}
|
||||
|
||||
+5
-98
@@ -20,106 +20,13 @@
|
||||
* @package mod_quiz
|
||||
* @copyright 2012 the Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @todo MDL-76612 delete this file as part of Moodle 4.6 development.
|
||||
* @deprecated This file is no longer required in Moodle 4.2+.
|
||||
*/
|
||||
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
debugging('This file is no longer required in Moodle 4.2+. Please do not include/require it.', DEBUG_DEVELOPER);
|
||||
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
|
||||
|
||||
/**
|
||||
* This class holds all the code for automatically updating all attempts that have
|
||||
* gone over their time limit.
|
||||
*
|
||||
* @copyright 2012 the Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_quiz_overdue_attempt_updater {
|
||||
|
||||
/**
|
||||
* Do the processing required.
|
||||
* @param int $timenow the time to consider as 'now' during the processing.
|
||||
* @param int $processto only process attempt with timecheckstate longer ago than this.
|
||||
* @return array with two elements, the number of attempt considered, and how many different quizzes that was.
|
||||
*/
|
||||
public function update_overdue_attempts($timenow, $processto) {
|
||||
global $DB;
|
||||
|
||||
$attemptstoprocess = $this->get_list_of_overdue_attempts($processto);
|
||||
|
||||
$course = null;
|
||||
$quiz = null;
|
||||
$cm = null;
|
||||
|
||||
$count = 0;
|
||||
$quizcount = 0;
|
||||
foreach ($attemptstoprocess as $attempt) {
|
||||
try {
|
||||
|
||||
// If we have moved on to a different quiz, fetch the new data.
|
||||
if (!$quiz || $attempt->quiz != $quiz->id) {
|
||||
$quiz = $DB->get_record('quiz', array('id' => $attempt->quiz), '*', MUST_EXIST);
|
||||
$cm = get_coursemodule_from_instance('quiz', $attempt->quiz);
|
||||
$quizcount += 1;
|
||||
}
|
||||
|
||||
// If we have moved on to a different course, fetch the new data.
|
||||
if (!$course || $course->id != $quiz->course) {
|
||||
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
// Make a specialised version of the quiz settings, with the relevant overrides.
|
||||
$quizforuser = clone($quiz);
|
||||
$quizforuser->timeclose = $attempt->usertimeclose;
|
||||
$quizforuser->timelimit = $attempt->usertimelimit;
|
||||
|
||||
// Trigger any transitions that are required.
|
||||
$attemptobj = new quiz_attempt($attempt, $quizforuser, $cm, $course);
|
||||
$attemptobj->handle_if_time_expired($timenow, false);
|
||||
$count += 1;
|
||||
|
||||
} catch (moodle_exception $e) {
|
||||
// If an error occurs while processing one attempt, don't let that kill cron.
|
||||
mtrace("Error while processing attempt {$attempt->id} at {$attempt->quiz} quiz:");
|
||||
mtrace($e->getMessage());
|
||||
mtrace($e->getTraceAsString());
|
||||
// Close down any currently open transactions, otherwise one error
|
||||
// will stop following DB changes from being committed.
|
||||
$DB->force_transaction_rollback();
|
||||
}
|
||||
}
|
||||
|
||||
$attemptstoprocess->close();
|
||||
return array($count, $quizcount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return moodle_recordset of quiz_attempts that need to be processed because time has
|
||||
* passed. The array is sorted by courseid then quizid.
|
||||
*/
|
||||
public function get_list_of_overdue_attempts($processto) {
|
||||
global $DB;
|
||||
|
||||
|
||||
// SQL to compute timeclose and timelimit for each attempt:
|
||||
$quizausersql = quiz_get_attempt_usertime_sql(
|
||||
"iquiza.state IN ('inprogress', 'overdue') AND iquiza.timecheckstate <= :iprocessto");
|
||||
|
||||
// This query should have all the quiz_attempts columns.
|
||||
return $DB->get_recordset_sql("
|
||||
SELECT quiza.*,
|
||||
quizauser.usertimeclose,
|
||||
quizauser.usertimelimit
|
||||
|
||||
FROM {quiz_attempts} quiza
|
||||
JOIN {quiz} quiz ON quiz.id = quiza.quiz
|
||||
JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
|
||||
|
||||
WHERE quiza.state IN ('inprogress', 'overdue')
|
||||
AND quiza.timecheckstate <= :processto
|
||||
ORDER BY quiz.course, quiza.quiz",
|
||||
|
||||
array('processto' => $processto, 'iprocessto' => $processto));
|
||||
}
|
||||
}
|
||||
require_once($CFG->dirroot . '/mod/quiz/deprecatedlib.php');
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
*/
|
||||
|
||||
use mod_quiz\access_manager;
|
||||
use mod_quiz\task\update_overdue_attempts;
|
||||
|
||||
/**
|
||||
* Internal function used in quiz_get_completion_state. Check passing grade (or no attempts left) requirement for completion.
|
||||
@@ -134,3 +135,30 @@ function quiz_get_completion_state($course, $cm, $userid, $type) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copyright 2012 the Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts.
|
||||
* @todo MDL-71196 Final deprecation in Moodle 4.3
|
||||
*/
|
||||
class mod_quiz_overdue_attempt_updater {
|
||||
|
||||
/**
|
||||
* @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts. that was.
|
||||
*/
|
||||
public function update_overdue_attempts($timenow, $processto) {
|
||||
debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' .
|
||||
'mod_quiz\task\update_overdue_attempts.');
|
||||
return (new update_overdue_attempts())->update_all_overdue_attempts((int) $timenow, (int) $processto);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since Moodle 4.2. Code moved to mod_quiz\task\update_overdue_attempts.
|
||||
*/
|
||||
public function get_list_of_overdue_attempts($processto) {
|
||||
debugging('mod_quiz_overdue_attempt_updater has been deprecated. The code wsa moved to ' .
|
||||
'mod_quiz\task\update_overdue_attempts.');
|
||||
return (new update_overdue_attempts())->get_list_of_overdue_attempts((int) $processto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@
|
||||
|
||||
namespace mod_quiz;
|
||||
|
||||
use mod_quiz_overdue_attempt_updater;
|
||||
use core_question_generator;
|
||||
use mod_quiz\task\update_overdue_attempts;
|
||||
use mod_quiz_generator;
|
||||
use question_engine;
|
||||
use quiz;
|
||||
|
||||
@@ -40,12 +42,8 @@ class attempts_test extends \advanced_testcase {
|
||||
* update_overdue_attempts().
|
||||
*/
|
||||
public function test_bulk_update_functions() {
|
||||
global $DB,$CFG;
|
||||
|
||||
require_once($CFG->dirroot.'/mod/quiz/cronlib.php');
|
||||
|
||||
global $DB;
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Setup course, user and groups
|
||||
@@ -390,7 +388,7 @@ class attempts_test extends \advanced_testcase {
|
||||
// Test get_list_of_overdue_attempts().
|
||||
//
|
||||
|
||||
$overduehander = new mod_quiz_overdue_attempt_updater();
|
||||
$overduehander = new update_overdue_attempts();
|
||||
|
||||
$attempts = $overduehander->get_list_of_overdue_attempts(100000); // way in the future
|
||||
$count = 0;
|
||||
@@ -417,7 +415,7 @@ class attempts_test extends \advanced_testcase {
|
||||
// Test update_overdue_attempts().
|
||||
//
|
||||
|
||||
[$count, $quizcount] = $overduehander->update_overdue_attempts(1000, 940);
|
||||
[$count, $quizcount] = $overduehander->update_all_overdue_attempts(1000, 940);
|
||||
|
||||
$attempts = $DB->get_records('quiz_attempts', null, 'quiz, userid, attempt',
|
||||
'id, quiz, userid, attempt, state, timestart, timefinish, timecheckstate');
|
||||
|
||||
@@ -38,6 +38,9 @@ This files describes API changes in the quiz code.
|
||||
- quiz_access_manager => mod_quiz\access_manager
|
||||
- mod_quiz_preflight_check_form => mod_quiz\form\preflight_check_form
|
||||
|
||||
* The following classes have been deprecated:
|
||||
- mod_quiz_overdue_attempt_updater - merged into mod_quiz\task\update_overdue_attempts
|
||||
|
||||
* As part of the clean-up, the following files are no longer required, and if you try to
|
||||
include them, you will get a debugging notices telling you not to:
|
||||
- mod/quiz/report/attemptsreport.php
|
||||
@@ -47,6 +50,7 @@ This files describes API changes in the quiz code.
|
||||
- mod/quiz/report/default.php
|
||||
- mod/quiz/accessmanager.php
|
||||
- mod/quiz/accessmanager_form.php
|
||||
- mod/quiz/cronlib.php
|
||||
|
||||
|
||||
=== 4.1 ===
|
||||
|
||||
Reference in New Issue
Block a user