diff --git a/local/qeupgradehelper/lib.php b/local/qeupgradehelper/lib.php index 35b9e87ac81ed..74413754da7e3 100644 --- a/local/qeupgradehelper/lib.php +++ b/local/qeupgradehelper/lib.php @@ -25,9 +25,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require_once (dirname(__FILE__) . '/locallib.php'); - - /** * Standard cron function */ @@ -51,11 +48,16 @@ function local_qeupgradehelper_cron() { * This function does the cron process within the time range according to settings. */ function local_qeupgradehelper_process($settings) { + global $CFG; + require_once(dirname(__FILE__) . '/locallib.php'); + if (!local_qeupgradehelper_is_upgraded()) { mtrace('qeupgradehelper: site not yet upgraded. Doing nothing.'); return; } + require_once(dirname(__FILE__) . '/afterupgradelib.php'); + $hour = (int) date('H'); if ($hour < $settings->starthour || $hour >= $settings->stophour) { mtrace('qeupgradehelper: not between starthour and stophour, so doing nothing (hour = ' . @@ -64,11 +66,27 @@ function local_qeupgradehelper_process($settings) { } $stoptime = time() + $settings->procesingtime; + + mtrace('qeupgradehelper: processing ...'); while (time() < $stoptime) { - mtrace('qeupgradehelper: processing ...'); - // TODO - mtrace('qeupgradehelper: sorry, not implemented yet.'); - return; + $quiz = local_qeupgradehelper_get_quiz_for_upgrade(); + if (!$quiz) { + mtrace('qeupgradehelper: No more quizzes to process. You should probably disable the qeupgradehelper cron settings now.'); + break; // No more to do; + } + + $quizid = $quiz->id; + $quizsummary = local_qeupgradehelper_get_quiz($quizid); + if ($quizsummary) { + mtrace(' starting upgrade of attempts at quiz ' . $quizid); + $upgrader = new local_qeupgradehelper_attempt_upgrader( + $quizsummary->id, $quizsummary->numtoconvert); + $upgrader->convert_all_quiz_attempts(); + mtrace(' upgrade of quiz ' . $quizid . ' complete.'); + } } + + mtrace('qeupgradehelper: Done.'); + return; } diff --git a/local/qeupgradehelper/locallib.php b/local/qeupgradehelper/locallib.php index e27388d7b19fe..dabae5426eeb8 100644 --- a/local/qeupgradehelper/locallib.php +++ b/local/qeupgradehelper/locallib.php @@ -659,3 +659,15 @@ function local_qeupgradehelper_load_question($questionid, $quizid) { return $question; } + +function local_qeupgradehelper_get_quiz_for_upgrade() { + global $DB; + + return $DB->get_record_sql("SELECT quiz.id + FROM {quiz_attempts} quiza + JOIN {quiz} quiz ON quiz.id = quiza.quiz + JOIN {course} c ON c.id = quiz.course + WHERE quiza.preview = 0 AND quiza.needsupgradetonewqe = 1 + GROUP BY quiz.id, quiz.name, c.shortname, c.id + ORDER BY quiza.timemodified DESC", array(), IGNORE_MULTIPLE); +}