MDL-20636 QE upgrade helper: Make resetting quiz attempts work.

This commit is contained in:
Tim Hunt 2011-05-13 16:46:47 +01:00
parent d02ac70862
commit 0c404c4211
5 changed files with 53 additions and 66 deletions

View file

@ -48,50 +48,66 @@ class local_qeupgradehelper_attempt_upgrader extends question_engine_attempt_upg
protected function convert_quiz_attempt($quiz, $attempt, $questionsessionsrs, $questionsstatesrs) { protected function convert_quiz_attempt($quiz, $attempt, $questionsessionsrs, $questionsstatesrs) {
$this->attemptsdone += 1; $this->attemptsdone += 1;
print_progress($this->attemptsdone, $this->attemptstodo);
return parent::convert_quiz_attempt($quiz, $attempt, $questionsessionsrs, $questionsstatesrs); return parent::convert_quiz_attempt($quiz, $attempt, $questionsessionsrs, $questionsstatesrs);
} }
protected function reset_progress($done, $outof) {
if (is_null($this->progressbar)) {
$this->progressbar = new progress_bar('qe2reset');
}
gc_collect_cycles(); // This was really helpful in PHP 5.2. Perhaps remove.
$a = new stdClass();
$a->done = $done;
$a->todo = $outof;
$this->progressbar->update($done, $outof,
get_string('resettingquizattemptsprogress', 'local_qeupgradehelper', $a));
}
protected function get_resettable_attempts($quiz) { protected function get_resettable_attempts($quiz) {
global $CFG; global $DB;
return get_records_sql(" return $DB->get_records_sql("
SELECT SELECT
quiza.* quiza.*
FROM {$CFG->prefix}quiz_attempts quiza FROM {quiz_attempts} quiza
LEFT JOIN ( LEFT JOIN (
SELECT attempt, MAX(timestamp) AS time SELECT attempt, MAX(timestamp) AS time
FROM {$CFG->prefix}question_states FROM {question_states}
GROUP BY attempt GROUP BY attempt
) AS oldtimemodified ON oldtimemodified.attempt = quiza.uniqueid ) AS oldtimemodified ON oldtimemodified.attempt = quiza.uniqueid
LEFT JOIN ( LEFT JOIN (
SELECT qa.questionusageid, MAX(qas.timecreated) AS time SELECT qa.questionusageid, MAX(qas.timecreated) AS time
FROM {$CFG->prefix}question_attempts qa FROM {question_attempts} qa
JOIN {$CFG->prefix}question_attempt_steps qas ON qas.questionattemptid = qa.id JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
GROUP BY qa.questionusageid GROUP BY qa.questionusageid
) AS newtimemodified ON newtimemodified.questionusageid = quiza.uniqueid ) AS newtimemodified ON newtimemodified.questionusageid = quiza.uniqueid
WHERE quiza.preview = 0 WHERE quiza.preview = 0
AND quiza.needsupgradetonewqe = 0 AND quiza.needsupgradetonewqe = 0
AND oldtimemodified.time >= newtimemodified.time AND oldtimemodified.time >= newtimemodified.time
AND quiza.quiz = {$quiz->id}"); AND quiza.quiz = :quizid", array('quizid' => $quiz->id));
} }
public function reset_all_resettable_attempts() { public function reset_all_resettable_attempts() {
begin_sql(); global $DB;
$quiz = get_record('quiz', 'id', $this->quizid);
$transaction = $DB->start_delegated_transaction();
$quiz = $DB->get_record('quiz', array('id' => $this->quizid));
$attempts = $this->get_resettable_attempts($quiz); $attempts = $this->get_resettable_attempts($quiz);
foreach ($attempts as $attempt) { foreach ($attempts as $attempt) {
$this->reset_attempt($quiz, $attempt); $this->reset_attempt($quiz, $attempt);
} }
commit_sql();
$transaction->allow_commit();
} }
protected function reset_attempt($quiz, $attempt) { protected function reset_attempt($quiz, $attempt) {
global $CFG; global $DB;
$this->attemptsdone += 1; $this->attemptsdone += 1;
print_progress($this->attemptsdone, $this->attemptstodo); $this->reset_progress($this->attemptsdone, $this->attemptstodo);
$questionids = explode(',', $quiz->questions); $questionids = explode(',', $quiz->questions);
$slottoquestionid = array(0 => 0); $slottoquestionid = array(0 => 0);
@ -119,56 +135,25 @@ class local_qeupgradehelper_attempt_upgrader extends question_engine_attempt_upg
$layout = $attempt->layout; $layout = $attempt->layout;
} }
delete_records_select('question_attempt_step_data', "attemptstepid IN ( $DB->delete_records_select('question_attempt_step_data', "attemptstepid IN (
SELECT qas.id SELECT qas.id
FROM {$CFG->prefix}question_attempts qa FROM {question_attempts} qa
JOIN {$CFG->prefix}question_attempt_steps qas ON qas.questionattemptid = qa.id JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
WHERE questionusageid = {$attempt->uniqueid})"); WHERE questionusageid = :uniqueid)",
delete_records_select('question_attempt_steps', "questionattemptid IN ( array('uniqueid' => $attempt->uniqueid));
$DB->delete_records_select('question_attempt_steps', "questionattemptid IN (
SELECT qa.id SELECT qa.id
FROM {$CFG->prefix}question_attempts qa FROM {question_attempts} qa
WHERE questionusageid = {$attempt->uniqueid})"); WHERE questionusageid = :uniqueid)",
delete_records('question_attempts', 'questionusageid', $attempt->uniqueid); array('uniqueid' => $attempt->uniqueid));
$DB->delete_records('question_attempts',
array('questionusageid' => $attempt->uniqueid));
set_field('question_usages', 'preferredbehaviour', 'to_be_set_later', $DB->set_field('question_usages', 'preferredbehaviour', 'to_be_set_later',
'id', $attempt->uniqueid); array('id' => $attempt->uniqueid));
set_field('quiz_attempts', 'layout', $layout, $DB->set_field('quiz_attempts', 'layout', $layout,
'uniqueid', $attempt->uniqueid); array('uniqueid' => $attempt->uniqueid));
set_field('quiz_attempts', 'needsupgradetonewqe', 1, $DB->set_field('quiz_attempts', 'needsupgradetonewqe', 1,
'uniqueid', $attempt->uniqueid); array('uniqueid' => $attempt->uniqueid));
}
}
class grabber_question_engine_attempt_upgrader extends question_engine_attempt_upgrader {
public function __construct() {
$this->questionloader = new question_engine_upgrade_question_loader(null);
}
public function format_var($name, $var) {
$out = var_export($var, true);
$out = str_replace('<', '&lt;', $out);
$out = str_replace('ADOFetchObj::__set_state(array(', '(object) array(', $out);
$out = str_replace('stdClass::__set_state(array(', '(object) array(', $out);
$out = str_replace('array (', 'array(', $out);
$out = preg_replace('/=> \n\s*/', '=> ', $out);
$out = str_replace(')),', '),', $out);
$out = str_replace('))', ')', $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n(?! )/', "\n ", $out);
return " $name = $out;\n";
}
public function display_convert_attempt_input($quiz, $attempt, $question, $qsession, $qstates) {
echo $this->format_var('$quiz', $quiz);
echo $this->format_var('$attempt', $attempt);
echo $this->format_var('$question', $question);
echo $this->format_var('$qsession', $qsession);
echo $this->format_var('$qstates', $qstates);
} }
} }

View file

@ -69,6 +69,7 @@ $string['quizzeswithunconverted'] = 'The following quizzes have attempts that ne
$string['resetquiz'] = 'Reset attempts...'; $string['resetquiz'] = 'Reset attempts...';
$string['resetcomplete'] = 'Reset complete'; $string['resetcomplete'] = 'Reset complete';
$string['resettingquizattempts'] = 'Resetting quiz attempts'; $string['resettingquizattempts'] = 'Resetting quiz attempts';
$string['resettingquizattemptsprogress'] = 'Resetting attempt {$a->done} / {$a->outof}';
$string['upgradingquizattempts'] = 'Upgrading the attempts for quiz \'{$a->name}\' in course {$a->shortname}'; $string['upgradingquizattempts'] = 'Upgrading the attempts for quiz \'{$a->name}\' in course {$a->shortname}';
$string['upgradedsitedetected'] = 'This appears to be a site that has been upgraded to include the new question engine.'; $string['upgradedsitedetected'] = 'This appears to be a site that has been upgraded to include the new question engine.';
$string['upgradedsiterequired'] = 'This script can only work after the site has been upgraded.'; $string['upgradedsiterequired'] = 'This script can only work after the site has been upgraded.';

View file

@ -255,8 +255,8 @@ class local_qeupgradehelper_resettable_quiz_list extends local_qeupgradehelper_q
public function get_row($quizinfo) { public function get_row($quizinfo) {
$row = parent::get_row($quizinfo); $row = parent::get_row($quizinfo);
$row[] = html_writer::link(local_qeupgradehelper_url('resetattempts', array('quizid' => $quizinfo->id)), $row[] = html_writer::link(local_qeupgradehelper_url('resetquiz', array('quizid' => $quizinfo->id)),
get_string('resetattempts', 'local_qeupgradehelper')); get_string('resetquiz', 'local_qeupgradehelper'));
return $row; return $row;
} }
} }

View file

@ -137,6 +137,7 @@ class local_qeupgradehelper_renderer extends plugin_renderer_base {
local_qeupgradehelper_url('listupgraded')); local_qeupgradehelper_url('listupgraded'));
$output .= $this->footer(); $output .= $this->footer();
return $output;
} }
/** /**

View file

@ -23,9 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
require_once(dirname(__FILE__) . '/../../config.php');
require_once(dirname(__FILE__) . '/../../../config.php');
require_once(dirname(__FILE__) . '/locallib.php'); require_once(dirname(__FILE__) . '/locallib.php');
require_once(dirname(__FILE__) . '/afterupgradelib.php');
require_once($CFG->libdir . '/adminlib.php'); require_once($CFG->libdir . '/adminlib.php');
$quizid = required_param('quizid', PARAM_INT); $quizid = required_param('quizid', PARAM_INT);
@ -39,7 +39,7 @@ admin_externalpage_setup('qeupgradehelper', '', array(),
local_qeupgradehelper_url('resetquiz', array('quizid' => $quizid))); local_qeupgradehelper_url('resetquiz', array('quizid' => $quizid)));
$PAGE->navbar->add(get_string('listupgraded', 'local_qeupgradehelper'), $PAGE->navbar->add(get_string('listupgraded', 'local_qeupgradehelper'),
local_qeupgradehelper_url('listtodo')); local_qeupgradehelper_url('listtodo'));
$PAGE->navbar->add(get_string('resetattempts', 'local_qeupgradehelper')); $PAGE->navbar->add(get_string('resetquiz', 'local_qeupgradehelper'));
$renderer = $PAGE->get_renderer('local_qeupgradehelper'); $renderer = $PAGE->get_renderer('local_qeupgradehelper');