. defined('MOODLE_INTERNAL') || die(); /** * Quiz module test data generator class * * @package moodlecore * @subpackage question * @copyright 2013 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class core_question_generator extends component_generator_base { /** * @var number of created instances */ protected $categorycount = 0; public function reset() { $this->categorycount = 0; } /** * Create a new question category. * @param array|stdClass $record * @return stdClass question_categories record. */ public function create_question_category($record = null) { global $DB; $this->categorycount++; $defaults = array( 'name' => 'Test question category ' . $this->categorycount, 'info' => '', 'infoformat' => FORMAT_HTML, 'stamp' => make_unique_id_code(), 'sortorder' => 999, ); $record = $this->datagenerator->combine_defaults_and_record($defaults, $record); if (!isset($record['contextid'])) { $record['contextid'] = context_system::instance()->id; } if (!isset($record['parent'])) { $record['parent'] = question_get_top_category($record['contextid'], true)->id; } $record['id'] = $DB->insert_record('question_categories', $record); return (object) $record; } /** * Create a new question. The question is initialised using one of the * examples from the appropriate {@link question_test_helper} subclass. * Then, any files you want to change from the value in the base example you * can override using $overrides. * @param string $qtype the question type to create an example of. * @param string $which as for the corresponding argument of * {@link question_test_helper::get_question_form_data}. null for the default one. * @param array|stdClass $overrides any fields that should be different from the base example. */ public function create_question($qtype, $which = null, $overrides = null) { global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); $fromform = test_question_maker::get_question_form_data($qtype, $which); $fromform = (object) $this->datagenerator->combine_defaults_and_record( (array) $fromform, $overrides); $question = new stdClass(); $question->category = $fromform->category; $question->qtype = $qtype; $question->createdby = 0; return $this->update_question($question, $which, $overrides); } /** * Update an existing question. * * @param stdClass $question the question data to update. * @param string $which as for the corresponding argument of * {@link question_test_helper::get_question_form_data}. null for the default one. * @param array|stdClass $overrides any fields that should be different from the base example. */ public function update_question($question, $which = null, $overrides = null) { global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); $qtype = $question->qtype; $fromform = test_question_maker::get_question_form_data($qtype, $which); $fromform = (object) $this->datagenerator->combine_defaults_and_record( (array) $question, $fromform); $fromform = (object) $this->datagenerator->combine_defaults_and_record( (array) $fromform, $overrides); return question_bank::get_qtype($qtype)->save_question($question, $fromform); } /** * Setup a course category, course, a question category, and 2 questions * for testing. * * @param string $type The type of question category to create. * @return array The created data objects */ public function setup_course_and_questions($type = 'course') { $datagenerator = $this->datagenerator; $category = $datagenerator->create_category(); $course = $datagenerator->create_course([ 'numsections' => 5, 'category' => $category->id ]); switch ($type) { case 'category': $context = context_coursecat::instance($category->id); break; case 'system': $context = context_system::instance(); break; default: $context = context_course::instance($course->id); break; } $qcat = $this->create_question_category(['contextid' => $context->id]); $questions = array( $this->create_question('shortanswer', null, ['category' => $qcat->id]), $this->create_question('shortanswer', null, ['category' => $qcat->id]), ); return array($category, $course, $qcat, $questions); } }