MDL-27948 The question engine should use recordsets to load attempt data

This should be good for performance (memory usage). It also avoids having to construct a meaningless, unique, first column, which is a pain on MyQSL.
This commit is contained in:
Tim Hunt 2011-06-22 18:53:15 +01:00
parent 3552484b91
commit 35d5f1c28d
8 changed files with 155 additions and 72 deletions

View file

@ -554,6 +554,71 @@ class ContainsEmptyTag extends XMLStructureExpectation {
}
/**
* Simple class that implements the {@link moodle_recordset} API based on an
* array of test data.
*
* See the {@link question_attempt_step_db_test} class in
* question/engine/simpletest/testquestionattemptstep.php for an example of how
* this is used.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_recordset extends moodle_recordset {
protected $records;
/**
* Constructor
* @param $table as for {@link testing_db_record_builder::build_db_records()}
* but does not need a unique first column.
*/
public function __construct(array $table) {
$columns = array_shift($table);
$this->records = array();
foreach ($table as $row) {
if (count($row) != count($columns)) {
throw new coding_exception("Row contains the wrong number of fields.");
}
$rec = array();
foreach ($columns as $i => $name) {
$rec[$name] = $row[$i];
}
$this->records[] = $rec;
}
reset($this->records);
}
public function __destruct() {
$this->close();
}
public function current() {
return (object) current($this->records);
}
public function key() {
if (is_null(key($this->records))) {
return false;
}
$current = current($this->records);
return reset($current);
}
public function next() {
next($this->records);
}
public function valid() {
return !is_null(key($this->records));
}
public function close() {
$this->records = null;
}
}
/**
* This class lets you write unit tests that access a separate set of test
* tables with a different prefix. Only those tables you explicitly ask to