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]

   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 calculated question data set definitions.
  19   *
  20   * @package    qtype
  21   * @subpackage calculated
  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  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  30  
  31  
  32  /**
  33   * Calculated question data set definitions editing form definition.
  34   *
  35   * @copyright  2007 Jamie Pratt me@jamiep.org
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class question_dataset_dependent_definitions_form extends question_wizard_form {
  39      /**
  40       * Question object with options and answers already loaded by get_question_options
  41       * Be careful how you use this it is needed sometimes to set up the structure of the
  42       * form in definition_inner but data is always loaded into the form with set_defaults.
  43       *
  44       * @var object
  45       */
  46      protected $question;
  47      /**
  48       * Reference to question type object
  49       *
  50       * @var question_dataset_dependent_questiontype
  51       */
  52      protected $qtypeobj;
  53      /**
  54       * Add question-type specific form fields.
  55       *
  56       * @param MoodleQuickForm $mform the form being built.
  57       */
  58      public function __construct($submiturl, $question) {
  59          // Validate the question category.
  60          if (!isset($question->categoryobject)) {
  61              throw new moodle_exception('categorydoesnotexist', 'question');
  62          }
  63          $question->category = $question->categoryobject->id;
  64          $this->question = $question;
  65          $this->qtypeobj = question_bank::get_qtype($this->question->qtype);
  66          parent::__construct($submiturl);
  67      }
  68  
  69      protected function definition() {
  70          global $SESSION;
  71  
  72          $mform = $this->_form;
  73          $mform->setDisableShortforms();
  74  
  75          $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
  76          $mandatorydatasets = array();
  77          if (isset($this->question->options->answers)) {
  78              foreach ($this->question->options->answers as $answer) {
  79                  $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
  80              }
  81          } else {
  82              foreach ($SESSION->calculated->questionform->answers as $answer) {
  83                  $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
  84              }
  85          }
  86  
  87          $key = 0;
  88          $datadefscat= array();
  89          $datadefscat  = $this->qtypeobj->get_dataset_definitions_category($this->question);
  90          $datasetmenus = array();
  91          $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
  92          // Explaining the role of datasets so other strings can be shortened.
  93          $mform->addElement('html', $label);
  94          $mform->addElement('header', 'mandatoryhdr',
  95                  get_string('mandatoryhdr', 'qtype_calculated'));
  96          $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
  97  
  98          foreach ($mandatorydatasets as $datasetname) {
  99              if (!isset($datasetmenus[$datasetname])) {
 100                  list($options, $selected) =
 101                          $this->qtypeobj->dataset_options($this->question, $datasetname);
 102                  unset($options['0']); // Mandatory...
 103                  $label = get_string('wildcard', 'qtype_calculated', $datasetname);
 104                  $mform->addElement('select', "dataset[{$key}]", $label, $options);
 105                  if (isset($datadefscat[$datasetname])) {
 106                      $mform->addElement('static', "there is a category",
 107                              get_string('sharedwildcard', 'qtype_calculated', $datasetname),
 108                              get_string('dataitemdefined', 'qtype_calculated',
 109                              $datadefscat[$datasetname]));
 110                  }
 111                  $mform->setDefault("dataset[{$key}]", $selected);
 112                  $datasetmenus[$datasetname] = '';
 113                  $key++;
 114              }
 115          }
 116          $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
 117  
 118          foreach ($possibledatasets as $datasetname) {
 119              if (!isset($datasetmenus[$datasetname])) {
 120                  list($options, $selected) = $this->qtypeobj->dataset_options(
 121                          $this->question, $datasetname, false);
 122                  $label = get_string('wildcard', 'qtype_calculated', $datasetname);
 123                  $mform->addElement('select', "dataset[{$key}]", $label, $options);
 124                  if (isset($datadefscat[$datasetname])) {
 125                      $mform->addElement('static', "there is a category",
 126                              get_string('sharedwildcard', 'qtype_calculated', $datasetname),
 127                              get_string('dataitemdefined', 'qtype_calculated',
 128                                      $datadefscat[$datasetname]));
 129                  }
 130  
 131                  $mform->setDefault("dataset[{$key}]", $selected);
 132                  $datasetmenus[$datasetname] = '';
 133                  $key++;
 134              }
 135          }
 136          // Temporary strings.
 137          $mform->addElement('header', 'synchronizehdr',
 138                  get_string('synchronize', 'qtype_calculated'));
 139          $mform->addElement('radio', 'synchronize', '',
 140                  get_string('synchronizeno', 'qtype_calculated'), 0);
 141          $mform->addElement('radio', 'synchronize', '',
 142                  get_string('synchronizeyes', 'qtype_calculated'), 1);
 143          $mform->addElement('radio', 'synchronize', '',
 144                  get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
 145          if (isset($this->question->options) &&
 146                  isset($this->question->options->synchronize)) {
 147              $mform->setDefault('synchronize', $this->question->options->synchronize);
 148          } else {
 149              $mform->setDefault('synchronize', 0);
 150          }
 151  
 152          $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
 153  
 154          $this->add_hidden_fields();
 155  
 156          $mform->addElement('hidden', 'category');
 157          $mform->setType('category', PARAM_SEQUENCE);
 158  
 159          $mform->addElement('hidden', 'wizard', 'datasetitems');
 160          $mform->setType('wizard', PARAM_ALPHA);
 161      }
 162  
 163      public function validation($data, $files) {
 164          $errors = parent::validation($data, $files);
 165          $datasets = $data['dataset'];
 166          $countvalid = 0;
 167          foreach ($datasets as $key => $dataset) {
 168              if ($dataset != '0') {
 169                  $countvalid++;
 170              }
 171          }
 172          if (!$countvalid) {
 173              foreach ($datasets as $key => $dataset) {
 174                  $errors['dataset['.$key.']'] =
 175                          get_string('atleastonerealdataset', 'qtype_calculated');
 176              }
 177          }
 178          return $errors;
 179      }
 180  }