Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 402] [Versions 39 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  /**
  18   * Defines the editing form for random questions.
  19   *
  20   * @package    mod_quiz
  21   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_quiz\form;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot.'/lib/formslib.php');
  30  
  31  /**
  32   * Class randomquestion_form
  33   *
  34   * @package    mod_quiz
  35   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class randomquestion_form extends \moodleform {
  39  
  40      /**
  41       * Form definiton.
  42       */
  43      public function definition() {
  44          $mform = $this->_form;
  45  
  46          $contexts = $this->_customdata['contexts'];
  47          $usablecontexts = $contexts->having_cap('moodle/question:useall');
  48  
  49          // Standard fields at the start of the form.
  50          $mform->addElement('header', 'generalheader', get_string("general", 'form'));
  51  
  52          $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
  53                  array('contexts' => $usablecontexts, 'top' => true));
  54  
  55          $mform->addElement('advcheckbox', 'includesubcategories', get_string('recurse', 'quiz'), null, null, array(0, 1));
  56  
  57          $tops = question_get_top_categories_for_contexts(array_column($contexts->all(), 'id'));
  58          $mform->hideIf('includesubcategories', 'category', 'in', $tops);
  59  
  60          $tags = \core_tag_tag::get_tags_by_area_in_contexts('core_question', 'question', $usablecontexts);
  61          $tagstrings = array();
  62          foreach ($tags as $tag) {
  63              $tagstrings["{$tag->id},{$tag->name}"] = $tag->name;
  64          }
  65          $options = array(
  66                  'multiple' => true,
  67                  'noselectionstring' => get_string('anytags', 'quiz'),
  68          );
  69          $mform->addElement('autocomplete', 'fromtags', get_string('randomquestiontags', 'mod_quiz'), $tagstrings, $options);
  70          $mform->addHelpButton('fromtags', 'randomquestiontags', 'mod_quiz');
  71  
  72          $mform->addElement('hidden', 'slotid');
  73          $mform->setType('slotid', PARAM_INT);
  74  
  75          $mform->addElement('hidden', 'returnurl');
  76          $mform->setType('returnurl', PARAM_LOCALURL);
  77  
  78          $buttonarray = array();
  79          $buttonarray[] = $mform->createElement('submit', 'submitbutton', get_string('savechanges'));
  80          $buttonarray[] = $mform->createElement('cancel');
  81          $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  82          $mform->closeHeaderBefore('buttonar');
  83      }
  84  
  85      public function set_data($defaultvalues) {
  86          $mform = $this->_form;
  87  
  88          if ($defaultvalues->fromtags) {
  89              $fromtagselement = $mform->getElement('fromtags');
  90              foreach ($defaultvalues->fromtags as $fromtag) {
  91                  if (!$fromtagselement->optionExists($fromtag)) {
  92                      $optionname = get_string('randomfromunavailabletag', 'mod_quiz', explode(',', $fromtag)[1]);
  93                      $fromtagselement->addOption($optionname, $fromtag);
  94                  }
  95              }
  96          }
  97  
  98          parent::set_data($defaultvalues);
  99      }
 100  }