Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 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          // Random from a new category section.
  89          $mform->addElement('header', 'newcategoryheader',
  90                  get_string('randomquestionusinganewcategory', 'quiz'));
  91  
  92          $mform->addElement('text', 'name', get_string('name'), 'maxlength="254" size="50"');
  93          $mform->setType('name', PARAM_TEXT);
  94  
  95          $mform->addElement('questioncategory', 'parent', get_string('parentcategory', 'question'),
  96                  array('contexts' => $usablecontexts, 'top' => true));
  97          $mform->addHelpButton('parent', 'parentcategory', 'question');
  98  
  99          $mform->addElement('submit', 'newcategory',
 100                  get_string('createcategoryandaddrandomquestion', 'quiz'));
 101  
 102          // Cancel button.
 103          $mform->addElement('cancel');
 104          $mform->closeHeaderBefore('cancel');
 105  
 106          $mform->addElement('hidden', 'addonpage', 0, 'id="rform_qpage"');
 107          $mform->setType('addonpage', PARAM_SEQUENCE);
 108          $mform->addElement('hidden', 'cmid', 0);
 109          $mform->setType('cmid', PARAM_INT);
 110          $mform->addElement('hidden', 'returnurl', 0);
 111          $mform->setType('returnurl', PARAM_LOCALURL);
 112  
 113          // Add the javascript required to enhance this mform.
 114          $PAGE->requires->js_call_amd('mod_quiz/add_random_form', 'init', [
 115              $mform->getAttribute('id'),
 116              $contexts->lowest()->id,
 117              $tops,
 118              $CFG->usetags
 119          ]);
 120      }
 121  
 122      public function validation($fromform, $files) {
 123          $errors = parent::validation($fromform, $files);
 124  
 125          if (!empty($fromform['newcategory']) && trim($fromform['name']) == '') {
 126              $errors['name'] = get_string('categorynamecantbeblank', 'question');
 127          }
 128  
 129          return $errors;
 130      }
 131  
 132      /**
 133       * Return an arbitrary array for the dropdown menu
 134       *
 135       * @param int $maxrand
 136       * @return array of integers [1, 2, ..., 100] (or to the smaller of $maxrand and 100.)
 137       */
 138      private function get_number_of_questions_to_add_choices($maxrand = 100) {
 139          $randomcount = array();
 140          for ($i = 1; $i <= min(100, $maxrand); $i++) {
 141              $randomcount[$i] = $i;
 142          }
 143          return $randomcount;
 144      }
 145  }