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 400 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 multiple choice question type.
  19   *
  20   * @package    qtype
  21   * @subpackage multichoice
  22   * @copyright  2007 Jamie Pratt
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Multiple choice editing form definition.
  32   *
  33   * @copyright  2007 Jamie Pratt
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_multichoice_edit_form extends question_edit_form {
  37      /**
  38       * Add question-type specific form fields.
  39       *
  40       * @param object $mform the form being built.
  41       */
  42      protected function definition_inner($mform) {
  43          $menu = array(
  44              get_string('answersingleno', 'qtype_multichoice'),
  45              get_string('answersingleyes', 'qtype_multichoice'),
  46          );
  47          $mform->addElement('select', 'single',
  48                  get_string('answerhowmany', 'qtype_multichoice'), $menu);
  49          $mform->setDefault('single', $this->get_default_value('single',
  50              get_config('qtype_multichoice', 'answerhowmany')));
  51  
  52          $mform->addElement('advcheckbox', 'shuffleanswers',
  53                  get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
  54          $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
  55          $mform->setDefault('shuffleanswers', $this->get_default_value('shuffleanswers',
  56              get_config('qtype_multichoice', 'shuffleanswers')));
  57  
  58          $mform->addElement('select', 'answernumbering',
  59                  get_string('answernumbering', 'qtype_multichoice'),
  60                  qtype_multichoice::get_numbering_styles());
  61          $mform->setDefault('answernumbering', $this->get_default_value('answernumbering',
  62              get_config('qtype_multichoice', 'answernumbering')));
  63  
  64          $mform->addElement('selectyesno', 'showstandardinstruction',
  65              get_string('showstandardinstruction', 'qtype_multichoice'), null, null, [0, 1]);
  66          $mform->addHelpButton('showstandardinstruction', 'showstandardinstruction', 'qtype_multichoice');
  67          $mform->setDefault('showstandardinstruction', $this->get_default_value('showstandardinstruction',
  68                  get_config('qtype_multichoice', 'showstandardinstruction')));
  69  
  70          $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
  71                  question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
  72  
  73          $this->add_combined_feedback_fields(true);
  74          $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
  75  
  76          $this->add_interactive_settings(true, true);
  77      }
  78  
  79      protected function get_per_answer_fields($mform, $label, $gradeoptions,
  80              &$repeatedoptions, &$answersoption) {
  81          $repeated = array();
  82          $repeated[] = $mform->createElement('editor', 'answer',
  83              $label, ['rows' => 2], $this->editoroptions);
  84          $repeated[] = $mform->createElement('select', 'fraction',
  85                  get_string('gradenoun'), $gradeoptions);
  86          $repeated[] = $mform->createElement('editor', 'feedback',
  87              get_string('feedback', 'question'), ['rows' => 2], $this->editoroptions);
  88          $repeatedoptions['answer']['type'] = PARAM_RAW;
  89          $repeatedoptions['fraction']['default'] = 0;
  90          $answersoption = 'answers';
  91          return $repeated;
  92      }
  93  
  94      protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
  95          list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
  96          $repeatedoptions['hintclearwrong']['disabledif'] = array('single', 'eq', 1);
  97          $repeatedoptions['hintshownumcorrect']['disabledif'] = array('single', 'eq', 1);
  98          return array($repeated, $repeatedoptions);
  99      }
 100  
 101      protected function data_preprocessing($question) {
 102          $question = parent::data_preprocessing($question);
 103          $question = $this->data_preprocessing_answers($question, true);
 104          $question = $this->data_preprocessing_combined_feedback($question, true);
 105          $question = $this->data_preprocessing_hints($question, true, true);
 106  
 107          if (!empty($question->options)) {
 108              $question->single = $question->options->single;
 109              $question->shuffleanswers = $question->options->shuffleanswers;
 110              $question->answernumbering = $question->options->answernumbering;
 111              $question->showstandardinstruction = $question->options->showstandardinstruction;
 112          }
 113  
 114          return $question;
 115      }
 116  
 117      public function validation($data, $files) {
 118          $errors = parent::validation($data, $files);
 119          $answers = $data['answer'];
 120          $answercount = 0;
 121  
 122          $totalfraction = 0;
 123          $maxfraction = -1;
 124  
 125          foreach ($answers as $key => $answer) {
 126              // Check no of choices.
 127              $trimmedanswer = trim($answer['text']);
 128              $fraction = (float) $data['fraction'][$key];
 129              if ($trimmedanswer === '' && empty($fraction)) {
 130                  continue;
 131              }
 132              if ($trimmedanswer === '') {
 133                  $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
 134              }
 135  
 136              $answercount++;
 137  
 138              // Check grades.
 139              if ($data['fraction'][$key] > 0) {
 140                  $totalfraction += $data['fraction'][$key];
 141              }
 142              if ($data['fraction'][$key] > $maxfraction) {
 143                  $maxfraction = $data['fraction'][$key];
 144              }
 145          }
 146  
 147          if ($answercount == 0) {
 148              $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 149              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 150          } else if ($answercount == 1) {
 151              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 152  
 153          }
 154  
 155          // Perform sanity checks on fractional grades.
 156          if ($data['single']) {
 157              if ($maxfraction != 1) {
 158                  $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
 159                          $maxfraction * 100);
 160              }
 161          } else {
 162              $totalfraction = round($totalfraction, 2);
 163              if ($totalfraction != 1) {
 164                  $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
 165                          $totalfraction * 100);
 166              }
 167          }
 168          return $errors;
 169      }
 170  
 171      public function qtype() {
 172          return 'multichoice';
 173      }
 174  }