Skip to content

Commit

Permalink
MDL-20636 Implement the ability to only upgrade some of the quiz atte…
Browse files Browse the repository at this point in the history
…mpts during the main upgrade.
  • Loading branch information
timhunt committed May 27, 2011
1 parent 61eca13 commit 1027301
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 11 deletions.
2 changes: 2 additions & 0 deletions local/qeupgradehelper/lang/en/local_qeupgradehelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$string['gotoindex'] = 'Back to the list of quizzes that can be upgraded';
$string['gotoquizreport'] = 'Go to the reports for this quiz, to check the upgrade';
$string['gotoresetlink'] = 'Go to the list of quizzes that can be reset';
$string['includedintheupgrade'] = 'Included in the upgrade?';
$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.';
Expand All @@ -57,6 +58,7 @@
$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['outof'] = '{$a->some} out of {$a->total}';
$string['pluginname'] = 'Question engine upgrade helper';
$string['pretendupgrade'] = 'Do a dry-run of the attempts upgrade';
$string['pretendupgrade_desc'] = 'The upgrade does three things: Load the existing data from the database; transform it; then write the transformed data to the DB. This script will test the first two parts of the process.';
Expand Down
17 changes: 13 additions & 4 deletions local/qeupgradehelper/listpreupgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,29 @@
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
local_qeupgradehelper_require_not_upgraded();

admin_externalpage_setup('qeupgradehelper', '', array(),
local_qeupgradehelper_url(''));
admin_externalpage_setup('qeupgradehelper', '', array(), local_qeupgradehelper_url(''));
$PAGE->navbar->add(get_string('listpreupgrade', 'local_qeupgradehelper'));

$renderer = $PAGE->get_renderer('local_qeupgradehelper');

$quizzes = new local_qeupgradehelper_pre_upgrade_quiz_list();

// Look to see if the admin has set things up to only upgrade certain attempts.
$partialupgradefile = $CFG->dirroot . '/local/qeupgradehelper/partialupgrade.php';
$partialupgradefunction = 'local_qeupgradehelper_get_quizzes_to_upgrade';
if (is_readable($partialupgradefile)) {
include_once($partialupgradefile);
if (function_exists($partialupgradefunction)) {
$quizzes = new local_qeupgradehelper_pre_upgrade_quiz_list_restricted(
$partialupgradefunction());
}
}

$numveryoldattemtps = local_qeupgradehelper_get_num_very_old_attempts();

if ($quizzes->is_empty()) {
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->quiz_list_page($quizzes, $numveryoldattemtps);
}
70 changes: 69 additions & 1 deletion local/qeupgradehelper/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public function get_row($quizinfo) {
);
}

public function get_row_class($quizinfo) {
return null;
}

public function get_total_row() {
return array(
'',
Expand Down Expand Up @@ -263,7 +267,7 @@ public function get_row($quizinfo) {


/**
* A list of quizzes that still need to be upgraded after the main upgrade.
* A list of quizzes that will be upgraded during the main upgrade.
*
* @copyright 2010 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand All @@ -279,6 +283,70 @@ protected function extra_where_clause() {
}


/**
* A list of quizzes that will be upgraded during the main upgrade, when the
* partialupgrade.php script is being used.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_qeupgradehelper_pre_upgrade_quiz_list_restricted extends local_qeupgradehelper_pre_upgrade_quiz_list {
protected $quizids;
protected $restrictedtotalquizas = 0;
protected $restrictedtotalqas = 0;

public function __construct($quizids) {
parent::__construct();
$this->quizids = $quizids;
}

public function get_row_class($quizinfo) {
if (!in_array($quizinfo->id, $this->quizids)) {
return 'dimmed';
} else {
return parent::get_row_class($quizinfo);
}
}

public function get_col_headings() {
$headings = parent::get_col_headings();
$headings[] = get_string('includedintheupgrade', 'local_qeupgradehelper');
return $headings;
}

public function get_row($quizinfo) {
$row = parent::get_row($quizinfo);
if (in_array($quizinfo->id, $this->quizids)) {
$this->restrictedtotalquizas += $quizinfo->attemptcount;
$this->restrictedtotalqas += $quizinfo->questionattempts;
$row[] = get_string('yes');
} else {
$row[] = get_string('no');
}
return $row;
}

protected function out_of($restrictedtotal, $fulltotal) {
$a = new stdClass();
$a->some = $restrictedtotal;
$a->total = $fulltotal;
return get_string('outof', 'local_qeupgradehelper', $a);
}

public function get_total_row() {
return array(
'',
html_writer::tag('b', get_string('total')),
'',
html_writer::tag('b', $this->out_of(
$this->restrictedtotalquizas, $this->totalquizas),
html_writer::tag('b', $this->out_of(
$this->restrictedtotalqas, $this->totalqas))),
);
}
}


/**
* List the number of quiz attempts that were never upgraded from 1.4 -> 1.5.
* @return int the number of such attempts.
Expand Down
115 changes: 115 additions & 0 deletions local/qeupgradehelper/partialupgrade-example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Example script, showing how it is possible to only do a part-upgrade of the
* attempt data during the main upgrade, and then finish the job off later.
*
* If you want to use this facility, then you need to:
*
* 1. Rename this script to partialupgrade.php.
* 2. Look at the various example functions below for controlling the upgrade,
* chooose one you like, and un-comment it. Alternatively, write your own
* custom function.
* 3. Use the List quizzes and attempts options in this plugin, which should now
* display updated information.
* 4. Once you are sure that works, you can proceed with the upgrade as usual.
*
* @package local
* @subpackage qeupgradehelper
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


/**
* This is a very simple example that just uses a hard-coded array to control
* which attempts are upgraded.
*
* @return array of quiz ids that are the ones to upgrade during the main
* upgrade from 2.0 to 2.1. Attempts at other quizzes are left alone, you will
* have to take steps to upgrade them yourself using the facilities provided by
* this plugin.
*/
//function local_qeupgradehelper_get_quizzes_to_upgrade() {
// return array(1, 2, 3);
//}


/**
* This example function uses a list of quiz ids from a file.
*
* It is currently set to use the file quiz-ids-to-upgrade.txt in the same
* folder as this script, but you can change that if you like.
*
* That file should contain one quiz id per line, with no punctuation. Any line
* that does not look like an integer is ignored.
*
* @return array of quiz ids that are the ones to upgrade during the main
* upgrade from 2.0 to 2.1. Attempts at other quizzes are left alone, you will
* have to take steps to upgrade them yourself using the facilities provided by
* this plugin.
*/
//function local_qeupgradehelper_get_quizzes_to_upgrade() {
// global $CFG;
// $rawids = file($CFG->dirroot . '/local/qeupgradehelper/quiz-ids-to-upgrade.txt');
// $cleanids = array();
// foreach ($rawids as $id) {
// $id = clean_param($id, PARAM_INT);
// if ($id) {
// $cleanids[] = $id;
// }
// }
// return $cleanids;
//}


/**
* This example uses a complex SQL query to decide which attempts to upgrade.
*
* The particular example I have done here is to return the ids of all the quizzes
* in courses that started more recently than one year ago. Of coures, you can
* write any query you like to meet your needs.
*
* Remember that you can use the List quizzes and attempts options option provided
* by this plugin to verify that your query is selecting the quizzes you intend.
*
* @return array of quiz ids that are the ones to upgrade during the main
* upgrade from 2.0 to 2.1. Attempts at other quizzes are left alone, you will
* have to take steps to upgrade them yourself using the facilities provided by
* this plugin.
*/
//function local_qeupgradehelper_get_quizzes_to_upgrade() {
// global $DB;
//
// $quizmoduleid = $DB->get_field('modules', 'id', array('name' => 'quiz'));
//
// $oneyearago = strtotime('-1 year');
//
// return $DB->get_fieldset_sql('
// SELECT DISTINCT quiz.id
//
// FROM {quiz} quiz
// JOIN {course_modules} cm ON cm.module = :quizmoduleid
// AND cm.instance = quiz.id
// JOIN {course} c ON quiz.course = c.id
//
// WHERE c.startdate > :cutoffdate
//
// ORDER BY quiz.id
// ', array('quizmoduleid' => $quizmoduleid, 'cutoffdate' => $oneyearago));
// ");
//}
8 changes: 7 additions & 1 deletion local/qeupgradehelper/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ public function quiz_list_page(local_qeupgradehelper_quiz_list $quizzes,

$table = new html_table();
$table->head = $quizzes->get_col_headings();

$rowcount = 0;
foreach ($quizzes->quizlist as $quizinfo) {
$table->data[] = $quizzes->get_row($quizinfo);
$table->data[$rowcount] = $quizzes->get_row($quizinfo);
if ($class = $quizzes->get_row_class($quizinfo)) {
$table->rowclasses[$rowcount] = $class;
}
$rowcount += 1;
}
$table->data[] = $quizzes->get_total_row();
$output .= html_writer::table($table);
Expand Down
31 changes: 26 additions & 5 deletions question/engine/upgrade/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,31 @@ protected function prevent_timeout() {
}

protected function get_quiz_ids() {
global $DB;
// TODO, if local/qeupgradehelper/ is installed, and defines the right
// function, use that to get the lest of quizzes instead.
return $DB->get_records_menu('quiz', array(), '', 'id', 'id, 1');
global $CFG, $DB;

// Look to see if the admin has set things up to only upgrade certain attempts.
$partialupgradefile = $CFG->dirroot . '/local/qeupgradehelper/partialupgrade.php';
$partialupgradefunction = 'local_qeupgradehelper_get_quizzes_to_upgrade';
if (is_readable($partialupgradefile)) {
include_once($partialupgradefile);
if (function_exists($partialupgradefunction)) {
$quizids = $partialupgradefunction();

// Ignore any quiz ids that do not acually exist.
if (empty($quizids)) {
return array();
}
list($test, $params) = $DB->get_in_or_equal($quizids);
return $DB->get_fieldset_sql("
SELECT id
FROM {quiz}
WHERE id $test
ORDER BY id", $params);
}
}

// Otherwise, upgrade all attempts.
return $DB->get_fieldset_sql('SELECT id FROM {quiz} ORDER BY id');
}

public function convert_all_quiz_attempts() {
Expand All @@ -97,7 +118,7 @@ public function convert_all_quiz_attempts() {
$outof = count($quizids);
$this->logger = new question_engine_assumption_logger();

foreach ($quizids as $quizid => $notused) {
foreach ($quizids as $quizid) {
$this->print_progress($done, $outof, $quizid);

$quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST);
Expand Down

0 comments on commit 1027301

Please sign in to comment.