Skip to content

Commit

Permalink
MDL-20636 Implement upgrade of calculated* question type attempts.
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed May 20, 2011
1 parent c35bf0e commit 667cdde
Show file tree
Hide file tree
Showing 9 changed files with 2,821 additions and 8 deletions.
15 changes: 10 additions & 5 deletions question/engine/upgrade/behaviourconverters.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ abstract class question_behaviour_attempt_updater {
protected $qtypeupdater;
/** @var question_engine_assumption_logger */
protected $logger;
/** @var question_engine_attempt_upgrader */
protected $qeupdater;

/**
* @var object this is the data for the upgraded questions attempt that
* we are building.
*/
protected $qa;
public $qa;

/** @var object the quiz settings. */
protected $quiz;
Expand All @@ -73,13 +75,14 @@ abstract class question_behaviour_attempt_updater {
/** @var object pointer to the state that has already finished this attempt. */
protected $finishstate;

public function __construct($quiz, $attempt, $question, $qsession, $qstates, $logger) {
public function __construct($quiz, $attempt, $question, $qsession, $qstates, $logger, $qeupdater) {
$this->quiz = $quiz;
$this->attempt = $attempt;
$this->question = $question;
$this->qsession = $qsession;
$this->qstates = $qstates;
$this->logger = $logger;
$this->qeupdater = $qeupdater;
}

public function discard() {
Expand All @@ -93,6 +96,7 @@ public function discard() {
$this->qtypeupdater->discard();
$this->qtypeupdater = null;
$this->logger = null;
$this->qeupdater = null;
}

protected abstract function behaviour_name();
Expand Down Expand Up @@ -128,11 +132,11 @@ protected function initialise_qa() {
$qa = new stdClass();
$qa->questionid = $this->question->id;
$qa->behaviour = $this->behaviour_name();
$qa->questionsummary = $this->qtypeupdater->question_summary($this->question);
$qa->rightanswer = $this->qtypeupdater->right_answer($this->question);
$qa->maxmark = $this->question->maxmark;
$qa->minfraction = 0;
$qa->flagged = 0;
$qa->questionsummary = $this->qtypeupdater->question_summary($this->question);
$qa->rightanswer = $this->qtypeupdater->right_answer($this->question);
$qa->responsesummary = '';
$qa->timemodified = 0;
$qa->steps = array();
Expand Down Expand Up @@ -271,7 +275,7 @@ protected function make_qtype_updater() {
is missing important code (the class {$class})
required to run the upgrade to the new question engine.");
}
return new $class($this, $this->question, $this->logger);
return new $class($this, $this->question, $this->logger, $this->qeupdater);
}

public function to_text($html) {
Expand Down Expand Up @@ -438,6 +442,7 @@ protected function behaviour_name() {
}

protected function finish_up() {
parent::finish_up();
if ($this->finishstate || !$this->attempt->timefinish) {
return;
}
Expand Down
14 changes: 14 additions & 0 deletions question/engine/upgrade/simpletest/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ public function load_question($questionid, $quizid) {

return null;
}

public function put_dataset_in_cache($questionid, $selecteditem, $dataset) {
$this->datasetcache[$questionid][$selecteditem] = $dataset;
}

public function load_dataset($questionid, $selecteditem) {
global $DB;

if (isset($this->datasetcache[$questionid][$selecteditem])) {
return $this->datasetcache[$questionid][$selecteditem];
}

throw new coding_exception('Test dataset not loaded.');
}
}


Expand Down
33 changes: 30 additions & 3 deletions question/engine/upgrade/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ public function load_question($questionid, $quizid = null) {
return $this->questionloader->get_question($questionid, $quizid);
}

public function load_dataset($questionid, $selecteditem) {
return $this->questionloader->load_dataset($questionid, $selecteditem);
}

public function get_next_question_session($attempt, moodle_recordset $questionsessionsrs) {
if (!$questionsessionsrs->valid()) {
return false;
Expand Down Expand Up @@ -335,7 +339,7 @@ public function supply_missing_question_attempt($quiz, $attempt, $question) {
$converterclass = $this->get_converter_class_name($question, $quiz, 'missing');

$qbehaviourupdater = new $converterclass($quiz, $attempt, $question,
null, null, $this->logger);
null, null, $this->logger, $this);
$qa = $qbehaviourupdater->supply_missing_qa();
$qbehaviourupdater->discard();
return $qa;
Expand All @@ -352,7 +356,7 @@ public function convert_question_attempt($quiz, $attempt, $question, $qsession,
$converterclass = $this->get_converter_class_name($question, $quiz, $qsession->id);

$qbehaviourupdater = new $converterclass($quiz, $attempt, $question, $qsession,
$qstates, $this->logger);
$qstates, $this->logger, $this);
$qa = $qbehaviourupdater->get_converted_qa();
$qbehaviourupdater->discard();
return $qa;
Expand Down Expand Up @@ -400,6 +404,7 @@ protected function decode_random_attempt($qstates, $maxmark) {
*/
class question_engine_upgrade_question_loader {
private $cache = array();
private $datasetcache = array();

public function __construct($logger) {
$this->logger = $logger;
Expand Down Expand Up @@ -464,6 +469,24 @@ public function get_question($questionid, $quizid) {
$this->cache[$questionid] = $question;
return $this->cache[$questionid];
}

public function load_dataset($questionid, $selecteditem) {
global $DB;

if (isset($this->datasetcache[$questionid][$selecteditem])) {
return $this->datasetcache[$questionid][$selecteditem];
}

$this->datasetcache[$questionid][$selecteditem] = $DB->get_records_sql_menu('
SELECT qdd.name, qdi.value
FROM {question_dataset_items} qdi
JOIN {question_dataset_definitions} qdd ON qdd.id = qdi.definition
JOIN {question_datasets} qd ON qdd.id = qd.datasetdefinition
WHERE qd.question = ?
AND qdi.itemnumber = ?
', array($questionid, $selecteditem));
return $this->datasetcache[$questionid][$selecteditem];
}
}


Expand All @@ -481,18 +504,22 @@ abstract class question_qtype_attempt_updater {
protected $updater;
/** @var question_engine_assumption_logger */
protected $logger;
/** @var question_engine_attempt_upgrader */
protected $qeupdater;

public function __construct($updater, $question, $logger) {
public function __construct($updater, $question, $logger, $qeupdater) {
$this->updater = $updater;
$this->question = $question;
$this->logger = $logger;
$this->qeupdater = $qeupdater;
}

public function discard() {
// Help the garbage collector, which seems to be struggling.
$this->updater = null;
$this->question = null;
$this->logger = null;
$this->qeupdater = null;
}

protected function to_text($html) {
Expand Down
Loading

0 comments on commit 667cdde

Please sign in to comment.