Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  /**
  18   * Defines the editing form for the random question type.
  19   *
  20   * @package    qtype
  21   * @subpackage random
  22   * @copyright  2007 Jamie Pratt me@jamiep.org
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * random editing form definition.
  32   *
  33   * @copyright  2007 Jamie Pratt me@jamiep.org
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_random_edit_form extends question_edit_form {
  37      /**
  38       * Build the form definition.
  39       *
  40       * This adds all the form files that the default question type supports.
  41       * If your question type does not support all these fields, then you can
  42       * override this method and remove the ones you don't want with $mform->removeElement().
  43       */
  44      protected function definition() {
  45          $mform = $this->_form;
  46  
  47          // Standard fields at the start of the form.
  48          $mform->addElement('header', 'generalheader', get_string("general", 'form'));
  49  
  50          $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
  51                  array('contexts' => $this->contexts->having_cap('moodle/question:useall'), 'top' => true));
  52  
  53          $mform->addElement('advcheckbox', 'questiontext[text]',
  54                  get_string('includingsubcategories', 'qtype_random'), null, null, array(0, 1));
  55  
  56          $tops = question_get_top_categories_for_contexts(array_column($this->contexts->all(), 'id'));
  57          $mform->hideIf('questiontext[text]', 'category', 'in', $tops);
  58  
  59          $mform->addElement('hidden', 'qtype');
  60          $mform->setType('qtype', PARAM_ALPHA);
  61  
  62          $this->add_hidden_fields();
  63  
  64          $buttonarray = array();
  65          $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
  66          $buttonarray[] = $mform->createElement('cancel');
  67          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  68          $mform->closeHeaderBefore('buttonar');
  69      }
  70  
  71      public function set_data($question) {
  72          $question->questiontext = array('text' => $question->questiontext);
  73          // We don't want the complex stuff in the base class to run.
  74          moodleform::set_data($question);
  75      }
  76  
  77      public function validation($fromform, $files) {
  78          // Validation of category is not relevant for this question type.
  79  
  80          return array();
  81      }
  82  
  83      public function qtype() {
  84          return 'random';
  85      }
  86  }