Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 402 and 403]

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