mirror of
https://github.com/moodle/moodle.git
synced 2025-08-03 16:13:28 +02:00
MDL-20636 QE upgrade helper: Make resetting quiz attempts work.
This commit is contained in:
parent
d02ac70862
commit
0c404c4211
5 changed files with 53 additions and 66 deletions
|
@ -48,50 +48,66 @@ class local_qeupgradehelper_attempt_upgrader extends question_engine_attempt_upg
|
|||
|
||||
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);
|
||||
|
@ -119,56 +135,25 @@ class local_qeupgradehelper_attempt_upgrader extends question_engine_attempt_upg
|
|||
$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);
|
||||
FROM {question_attempts} qa
|
||||
WHERE questionusageid = :uniqueid)",
|
||||
array('uniqueid' => $attempt->uniqueid));
|
||||
$DB->delete_records('question_attempts',
|
||||
array('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('<', '<', $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);
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,6 +69,7 @@ $string['quizzeswithunconverted'] = 'The following quizzes have attempts that ne
|
|||
$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.';
|
||||
|
|
|
@ -255,8 +255,8 @@ class local_qeupgradehelper_resettable_quiz_list extends local_qeupgradehelper_q
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -137,6 +137,7 @@ class local_qeupgradehelper_renderer extends plugin_renderer_base {
|
|||
local_qeupgradehelper_url('listupgraded'));
|
||||
|
||||
$output .= $this->footer();
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
|
@ -39,7 +39,7 @@ admin_externalpage_setup('qeupgradehelper', '', array(),
|
|||
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');
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue