Skip to content

Commit

Permalink
MDL-20636 QE upgrade helper: Make resetting quiz attempts work.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed May 13, 2011
1 parent d02ac70 commit 0c404c4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 67 deletions.
109 changes: 47 additions & 62 deletions local/qeupgradehelper/afterupgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,66 @@ protected function print_progress($done, $outof, $quizid) {

protected function convert_quiz_attempt($quiz, $attempt, $questionsessionsrs, $questionsstatesrs) {
$this->attemptsdone += 1;
print_progress($this->attemptsdone, $this->attemptstodo);
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) {
global $CFG;
return get_records_sql("
global $DB;
return $DB->get_records_sql("
SELECT
quiza.*
FROM {$CFG->prefix}quiz_attempts quiza
FROM {quiz_attempts} quiza
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 quiza.needsupgradetonewqe = 0
AND oldtimemodified.time >= newtimemodified.time
AND quiza.quiz = {$quiz->id}");
AND quiza.quiz = :quizid", array('quizid' => $quiz->id));
}

public function reset_all_resettable_attempts() {
begin_sql();
$quiz = get_record('quiz', 'id', $this->quizid);
global $DB;

$transaction = $DB->start_delegated_transaction();

$quiz = $DB->get_record('quiz', array('id' => $this->quizid));
$attempts = $this->get_resettable_attempts($quiz);
foreach ($attempts as $attempt) {
$this->reset_attempt($quiz, $attempt);
}
commit_sql();

$transaction->allow_commit();
}

protected function reset_attempt($quiz, $attempt) {
global $CFG;
global $DB;

$this->attemptsdone += 1;
print_progress($this->attemptsdone, $this->attemptstodo);
$this->reset_progress($this->attemptsdone, $this->attemptstodo);

$questionids = explode(',', $quiz->questions);
$slottoquestionid = array(0 => 0);
Expand Down Expand Up @@ -119,56 +135,25 @@ protected function reset_attempt($quiz, $attempt) {
$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
FROM {$CFG->prefix}question_attempts qa
JOIN {$CFG->prefix}question_attempt_steps qas ON qas.questionattemptid = qa.id
WHERE questionusageid = {$attempt->uniqueid})");
delete_records_select('question_attempt_steps', "questionattemptid IN (
FROM {question_attempts} qa
JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id
WHERE questionusageid = :uniqueid)",
array('uniqueid' => $attempt->uniqueid));
$DB->delete_records_select('question_attempt_steps', "questionattemptid IN (
SELECT qa.id
FROM {$CFG->prefix}question_attempts qa
WHERE questionusageid = {$attempt->uniqueid})");
delete_records('question_attempts', 'questionusageid', $attempt->uniqueid);

set_field('question_usages', 'preferredbehaviour', 'to_be_set_later',
'id', $attempt->uniqueid);
set_field('quiz_attempts', 'layout', $layout,
'uniqueid', $attempt->uniqueid);
set_field('quiz_attempts', 'needsupgradetonewqe', 1,
'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);
FROM {question_attempts} qa
WHERE questionusageid = :uniqueid)",
array('uniqueid' => $attempt->uniqueid));
$DB->delete_records('question_attempts',
array('questionusageid' => $attempt->uniqueid));

$DB->set_field('question_usages', 'preferredbehaviour', 'to_be_set_later',
array('id' => $attempt->uniqueid));
$DB->set_field('quiz_attempts', 'layout', $layout,
array('uniqueid' => $attempt->uniqueid));
$DB->set_field('quiz_attempts', 'needsupgradetonewqe', 1,
array('uniqueid' => $attempt->uniqueid));
}
}
1 change: 1 addition & 0 deletions local/qeupgradehelper/lang/en/local_qeupgradehelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
$string['resetquiz'] = 'Reset attempts...';
$string['resetcomplete'] = 'Reset complete';
$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['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.';
Expand Down
4 changes: 2 additions & 2 deletions local/qeupgradehelper/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ public function get_col_headings() {

public function get_row($quizinfo) {
$row = parent::get_row($quizinfo);
$row[] = html_writer::link(local_qeupgradehelper_url('resetattempts', array('quizid' => $quizinfo->id)),
get_string('resetattempts', 'local_qeupgradehelper'));
$row[] = html_writer::link(local_qeupgradehelper_url('resetquiz', array('quizid' => $quizinfo->id)),
get_string('resetquiz', 'local_qeupgradehelper'));
return $row;
}
}
Expand Down
1 change: 1 addition & 0 deletions local/qeupgradehelper/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function reset_quiz_are_you_sure($quizsummary) {
local_qeupgradehelper_url('listupgraded'));

$output .= $this->footer();
return $output;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions local/qeupgradehelper/resetquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* @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__) . '/afterupgradelib.php');
require_once($CFG->libdir . '/adminlib.php');

$quizid = required_param('quizid', PARAM_INT);
Expand All @@ -39,7 +39,7 @@
local_qeupgradehelper_url('resetquiz', array('quizid' => $quizid)));
$PAGE->navbar->add(get_string('listupgraded', 'local_qeupgradehelper'),
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');

Expand Down

0 comments on commit 0c404c4

Please sign in to comment.