Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402]

   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 Moodle forum used to add random questions to the quiz.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2008 Olli Savolainen
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/formslib.php');
  29  
  30  
  31  /**
  32   * The add random questions form.
  33   *
  34   * @copyright  1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class quiz_add_random_form extends moodleform {
  38  
  39      protected function definition() {
  40          global $OUTPUT, $PAGE, $CFG;
  41  
  42          $mform = $this->_form;
  43          $mform->setDisableShortforms();
  44  
  45          $contexts = $this->_customdata['contexts'];
  46          $usablecontexts = $contexts->having_cap('moodle/question:useall');
  47  
  48          // Random from existing category section.
  49          $mform->addElement('header', 'existingcategoryheader',
  50                  get_string('randomfromexistingcategory', 'quiz'));
  51  
  52          $mform->addElement('questioncategory', 'category', get_string('category'),
  53                  array('contexts' => $usablecontexts, 'top' => true));
  54          $mform->setDefault('category', $this->_customdata['cat']);
  55  
  56          $mform->addElement('checkbox', 'includesubcategories', '', get_string('recurse', 'quiz'));
  57  
  58          $tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
  59          $mform->hideIf('includesubcategories', 'category', 'in', $tops);
  60  
  61          if ($CFG->usetags) {
  62              $tagstrings = array();
  63              $tags = core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
  64              foreach ($tags as $tag) {
  65                  $tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
  66              }
  67              $options = array(
  68                  'multiple' => true,
  69                  'noselectionstring' => get_string('anytags', 'quiz'),
  70              );
  71              $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
  72              $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
  73          }
  74  
  75          // TODO: in the past, the drop-down used to only show sensible choices for
  76          // number of questions to add. That is, if the currently selected filter
  77          // only matched 9 questions (not already in the quiz), then the drop-down would
  78          // only offer choices 1..9. This nice UI hint got lost when the UI became Ajax-y.
  79          // We should add it back.
  80          $mform->addElement('select', 'numbertoadd', get_string('randomnumber', 'quiz'),
  81                  $this->get_number_of_questions_to_add_choices());
  82  
  83          $previewhtml = $OUTPUT->render_from_template('mod_quiz/random_question_form_preview', []);
  84          $mform->addElement('html', $previewhtml);
  85  
  86          $mform->addElement('submit', 'existingcategory', get_string('addrandomquestion', 'quiz'));
  87  
  88          // If the manage categories plugins is enabled, add the elements to create a new category in the form.
  89          if (\core\plugininfo\qbank::is_plugin_enabled(\qbank_managecategories\helper::PLUGINNAME)) {
  90              // Random from a new category section.
  91              $mform->addElement('header', 'newcategoryheader',
  92                      get_string('randomquestionusinganewcategory', 'quiz'));
  93  
  94              $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
  95              $mform->setType('name', PARAM_TEXT);
  96  
  97              $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
  98                      array('contexts' => $usablecontexts, 'top' => true));
  99              $mform->addHelpButton('parent', 'parentcategory', 'question');
 100  
 101              $mform->addElement('submit', 'newcategory',
 102                      get_string('createcategoryandaddrandomquestion', 'quiz'));
 103          }
 104  
 105          // Cancel button.
 106          $mform->addElement('cancel');
 107          $mform->closeHeaderBefore('cancel');
 108  
 109          $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
 110          $mform->setType('addonpage', PARAM_SEQUENCE);
 111          $mform->addElement('hidden', 'cmid', 0);
 112          $mform->setType('cmid', PARAM_INT);
 113          $mform->addElement('hidden', 'returnurl', 0);
 114          $mform->setType('returnurl', PARAM_LOCALURL);
 115  
 116          // Add the javascript required to enhance this mform.
 117          $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [
 118              $mform->getAttribute('id'),
 119              $contexts->lowest()->id,
 120              $tops,
 121              $CFG->usetags
 122          ]);
 123      }
 124  
 125      public function validation($fromform, $files) {
 126          $errors = parent::validation($fromform, $files);
 127  
 128          if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
 129              $errors['name'] = get_string('categorynamecantbeblank', 'question');
 130          }
 131  
 132          return $errors;
 133      }
 134  
 135      /**
 136       * Return an arbitrary array for the dropdown menu
 137       *
 138       * @param int $maxrand
 139       * @return array of integers [1, 2, ..., 100] (or to the smaller of $maxrand and 100.)
 140       */
 141      private function get_number_of_questions_to_add_choices($maxrand = 100) {
 142          $randomcount = array();
 143          for ($i = 1; $i <= min(100, $maxrand); $i++) {
 144              $randomcount[$i] = $i;
 145          }
 146          return $randomcount;
 147      }
 148  }