[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/tests/generator/ -> lib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  defined('MOODLE_INTERNAL') || die();
  18  
  19  /**
  20   * Quiz module test data generator class
  21   *
  22   * @package    moodlecore
  23   * @subpackage question
  24   * @copyright  2013 The Open University
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class core_question_generator extends component_generator_base {
  28  
  29      /**
  30       * @var number of created instances
  31       */
  32      protected $categorycount = 0;
  33  
  34      public function reset() {
  35          $this->categorycount = 0;
  36      }
  37  
  38      /**
  39       * Create a new question category.
  40       * @param array|stdClass $record
  41       * @return stdClass question_categories record.
  42       */
  43      public function create_question_category($record = null) {
  44          global $DB;
  45  
  46          $this->categorycount++;
  47  
  48          $defaults = array(
  49              'name'       => 'Test question category ' . $this->categorycount,
  50              'contextid'  => context_system::instance()->id,
  51              'info'       => '',
  52              'infoformat' => FORMAT_HTML,
  53              'stamp'      => '',
  54              'parent'     => 0,
  55              'sortorder'  => 999,
  56          );
  57  
  58          $record = $this->datagenerator->combine_defaults_and_record($defaults, $record);
  59          $record['id'] = $DB->insert_record('question_categories', $record);
  60          return (object) $record;
  61      }
  62  
  63      /**
  64       * Create a new question. The question is initialised using one of the
  65       * examples from the appropriate {@link question_test_helper} subclass.
  66       * Then, any files you want to change from the value in the base example you
  67       * can override using $overrides.
  68       * @param string $qtype the question type to create an example of.
  69       * @param string $which as for the corresponding argument of
  70       *      {@link question_test_helper::get_question_form_data}. null for the default one.
  71       * @param array|stdClass $overrides any fields that should be different from the base example.
  72       */
  73      public function create_question($qtype, $which = null, $overrides = null) {
  74          global $CFG;
  75          require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  76  
  77          $fromform = test_question_maker::get_question_form_data($qtype, $which);
  78          $fromform = (object) $this->datagenerator->combine_defaults_and_record(
  79                  (array) $fromform, $overrides);
  80  
  81          $question = new stdClass();
  82          $question->category  = $fromform->category;
  83          $question->qtype     = $qtype;
  84          $question->createdby = 0;
  85          return question_bank::get_qtype($qtype)->save_question($question, $fromform);
  86      }
  87  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1