From 5ab62d9d3d464654f05d95054629f6cf68dce503 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 6 May 2011 19:13:43 +0100 Subject: [PATCH] MDL-20636 listpreupgrade done. --- .../lang/en/local_qeupgradehelper.php | 5 ++- local/qeupgradehelper/listpreupgrade.php | 6 ++- local/qeupgradehelper/locallib.php | 41 ++++++++++++++----- local/qeupgradehelper/renderer.php | 34 ++++++++++++--- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/local/qeupgradehelper/lang/en/local_qeupgradehelper.php b/local/qeupgradehelper/lang/en/local_qeupgradehelper.php index d682b7a434306..432e184dcbe59 100755 --- a/local/qeupgradehelper/lang/en/local_qeupgradehelper.php +++ b/local/qeupgradehelper/lang/en/local_qeupgradehelper.php @@ -43,6 +43,7 @@ $string['invalidquizid'] = 'Invaid quiz id. Either the quiz does not exist, or it has no attempts to convert.'; $string['listpreupgrade'] = 'List quizzes and attempts'; $string['listpreupgrade_desc'] = 'This will show a report of all the quizzes on the system and how many attempts they have. This will give you an idea of the scope of the upgrade you have to do.'; +$string['listpreupgradeintro'] = 'These are the number of quiz attempts that will need to be processed when you upgrade your site. A few tens of thousands is no worry. Much beyond that and you need to think carefully.'; $string['listtodo'] = 'List quizzes still to upgrade'; $string['listtodo_desc'] = 'This will show a report of all the quizzes on the system (if any) that have attempts that still need to be upgraded to the new question engine.'; $string['listtodointro'] = 'These are all the quizzes with attempt data that still needs to be converted. You can convert the attempts by clicking the link.'; @@ -51,12 +52,14 @@ $string['noquizattempts'] = 'Your site does not have any quiz attempts at all!'; $string['nothingupgradedyet'] = 'No upgraded attempts that can be reset'; $string['notupgradedsiterequired'] = 'This script can only work before the site has been upgraded.'; +$string['numberofattempts'] = 'Number of quiz attempts'; $string['oldsitedetected'] = 'This appears to be a site that has not yet been upgraded to include the new question engine.'; $string['pluginname'] = 'Question engine upgrade helper'; $string['quizid'] = 'Quiz id'; $string['quizupgrade'] = 'Quiz upgrade status'; -$string['quizzeswithunconverted'] = 'The following quizzes have attempts that need to be converted'; $string['quizzesthatcanbereset'] = 'The following quizzes have converted attempts that you may be able to reset'; +$string['quizzestobeupgraded'] = 'All quizzes with attempts'; +$string['quizzeswithunconverted'] = 'The following quizzes have attempts that need to be converted'; $string['resetquiz'] = 'Reset attempts...'; $string['resetcomplete'] = 'Reset complete'; $string['resettingquizattempts'] = 'Resetting quiz attempts'; diff --git a/local/qeupgradehelper/listpreupgrade.php b/local/qeupgradehelper/listpreupgrade.php index 8f03ab8c7d9d2..059f4148d058d 100755 --- a/local/qeupgradehelper/listpreupgrade.php +++ b/local/qeupgradehelper/listpreupgrade.php @@ -34,16 +34,18 @@ local_qeupgradehelper_require_not_upgraded(); admin_externalpage_setup('qeupgradehelper', '', array(), - local_qeupgradehelper_url('listpreupgrade')); + local_qeupgradehelper_url('')); $PAGE->navbar->add(get_string('listpreupgrade', 'local_qeupgradehelper')); $renderer = $PAGE->get_renderer('local_qeupgradehelper'); -$quizzes = local_qeupgradehelper_get_upgradable_quizzes(); +$quizzes = local_qeupgradehelper_get_pre_upgrade_quizzes(); if (empty($quizzes)) { echo $renderer->simple_message_page(get_string('noquizattempts', 'local_qeupgradehelper')); } else { + // TODO, once we have a way to limit which quizzes will be included in the upgrade, + // display that information too. echo $renderer->list_quizzes_pre_upgrade($quizzes); } diff --git a/local/qeupgradehelper/locallib.php b/local/qeupgradehelper/locallib.php index 5dca915357e6c..514b897622615 100755 --- a/local/qeupgradehelper/locallib.php +++ b/local/qeupgradehelper/locallib.php @@ -52,7 +52,7 @@ function local_qeupgradehelper_require_upgraded() { * If the site has been upgraded, display an error. */ function local_qeupgradehelper_require_not_upgraded() { - if (!local_qeupgradehelper_is_upgraded()) { + if (local_qeupgradehelper_is_upgraded()) { throw new moodle_exception('notupgradedsiterequired', 'local_qeupgradehelper', local_qeupgradehelper_url('index')); } @@ -199,8 +199,8 @@ function local_qeupgradehelper_get_resettable_quizzes() { * totalattempts and resettableattempts. */ function local_qeupgradehelper_get_resettable_quiz($quizid) { - global $CFG; - return get_record_sql(" + global $DB; + return $DB->get_record_sql(" SELECT quiz.id, quiz.name, @@ -212,25 +212,44 @@ function local_qeupgradehelper_get_resettable_quiz($quizid) { SUM(CASE WHEN quiza.needsupgradetonewqe = 0 AND oldtimemodified.time >= newtimemodified.time THEN 1 ELSE 0 END) AS resettableattempts - FROM {$CFG->prefix}quiz_attempts quiza - JOIN {$CFG->prefix}quiz quiz ON quiz.id = quiza.quiz - JOIN {$CFG->prefix}course c ON c.id = quiz.course + FROM {quiz_attempts} quiza + JOIN {quiz} quiz ON quiz.id = quiza.quiz + JOIN {course} c ON c.id = quiz.course LEFT JOIN ( SELECT attempt, MAX(timestamp) AS time - FROM {$CFG->prefix}question_states + FROM {question_states} GROUP BY attempt ) AS oldtimemodified ON oldtimemodified.attempt = quiza.uniqueid LEFT JOIN ( SELECT qa.questionusageid, MAX(qas.timecreated) AS time - FROM {$CFG->prefix}question_attempts qa - JOIN {$CFG->prefix}question_attempt_steps qas ON qas.questionattemptid = qa.id + FROM {question_attempts} qa + JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id GROUP BY qa.questionusageid ) AS newtimemodified ON newtimemodified.questionusageid = quiza.uniqueid WHERE quiza.preview = 0 - AND quiz.id = {$quizid} + AND quiz.id = ? - GROUP BY quiz.id, quiz.name, c.shortname, c.id"); + GROUP BY quiz.id, quiz.name, c.shortname, c.id", array($quizid)); } +function local_qeupgradehelper_get_pre_upgrade_quizzes() { + global $DB; + return $DB->get_records_sql(" + SELECT + quiz.id, + quiz.name, + c.shortname, + c.id AS courseid, + COUNT(1) AS attemptcount + + FROM {quiz_attempts} quiza + JOIN {quiz} quiz ON quiz.id = quiza.quiz + JOIN {course} c ON c.id = quiz.course + + WHERE quiza.preview = 0 + + GROUP BY quiz.id, quiz.name, c.shortname, c.id + ORDER BY c.shortname, quiz.name, quiz.id"); +} diff --git a/local/qeupgradehelper/renderer.php b/local/qeupgradehelper/renderer.php index 6caf640b281b5..3be108d19ea54 100755 --- a/local/qeupgradehelper/renderer.php +++ b/local/qeupgradehelper/renderer.php @@ -78,27 +78,32 @@ public function simple_message_page($message) { * @param unknown_type $actionscript * @return string */ - public function quizzes_table($quizzes, $countheader, $actionscript) { + public function quizzes_table($quizzes, $countheader, $actionscript = null) { $table = new html_table(); $table->head = array( get_string('quizid', 'local_qeupgradehelper'), get_string('course'), get_string('pluginname', 'quiz'), $countheader, - get_string('actions', 'local_qeupgradehelper'), ); + if ($actionscript) { + $table->head[] = get_string('actions', 'local_qeupgradehelper'); + } foreach ($quizzes as $quiz) { - $table->data[] = array( + $row = array( $quiz->id, html_writer::link(new moodle_url('/course/view.php', array('id' => $quiz->courseid)), format_string($quiz->shortname)), html_writer::link(new moodle_url('/mod/quiz/view.php', array('id' => $quiz->name)), format_string($quiz->name)), $quiz->attemptcount, - html_writer::link(local_qeupgradehelper_url($actionscript, array('quizid' => $quiz->id)), - get_string($actionscript, 'local_qeupgradehelper')), ); + if ($actionscript) { + $row[] = html_writer::link(local_qeupgradehelper_url($actionscript, array('quizid' => $quiz->id)), + get_string($actionscript, 'local_qeupgradehelper')); + } + $table->data[] = $row; } return html_writer::table($table); @@ -142,6 +147,25 @@ public function list_quizzes_upgraded($quizzes) { return $output; } + /** + * Render the list of quizzes that will need to be processed during the upgrade. + * @param array $quizzes of data about quizzes. + * @return string html to output. + */ + public function list_quizzes_pre_upgrade($quizzes) { + $output = ''; + $output .= $this->header(); + $output .= $this->heading(get_string('quizzestobeupgraded', 'local_qeupgradehelper')); + $output .= $this->box(get_string('listpreupgradeintro', 'local_qeupgradehelper')); + + $output .= $this->quizzes_table($quizzes, + get_string('numberofattempts', 'local_qeupgradehelper')); + + $output .= $this->back_to_index(); + $output .= $this->footer(); + return $output; + } + /** * Render the are-you-sure page to confirm a manual upgrade. * @param object $quizsummary data about the quiz to upgrade.