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 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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', 0));
  68  
  69          $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
  70                  question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
  71  
  72          $this->add_combined_feedback_fields(true);
  73          $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
  74  
  75          $this->add_interactive_settings(true, true);
  76      }
  77  
  78      protected function get_per_answer_fields($mform, $label, $gradeoptions,
  79              &$repeatedoptions, &$answersoption) {
  80          $repeated = array();
  81          $repeated[] = $mform->createElement('editor', 'answer',
  82                  $label, array('rows' => 1), $this->editoroptions);
  83          $repeated[] = $mform->createElement('select', 'fraction',
  84                  get_string('gradenoun'), $gradeoptions);
  85          $repeated[] = $mform->createElement('editor', 'feedback',
  86                  get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions);
  87          $repeatedoptions['answer']['type'] = PARAM_RAW;
  88          $repeatedoptions['fraction']['default'] = 0;
  89          $answersoption = 'answers';
  90          return $repeated;
  91      }
  92  
  93      protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
  94          list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
  95          $repeatedoptions['hintclearwrong']['disabledif'] = array('single', 'eq', 1);
  96          $repeatedoptions['hintshownumcorrect']['disabledif'] = array('single', 'eq', 1);
  97          return array($repeated, $repeatedoptions);
  98      }
  99  
 100      protected function data_preprocessing($question) {
 101          $question = parent::data_preprocessing($question);
 102          $question = $this->data_preprocessing_answers($question, true);
 103          $question = $this->data_preprocessing_combined_feedback($question, true);
 104          $question = $this->data_preprocessing_hints($question, true, true);
 105  
 106          if (!empty($question->options)) {
 107              $question->single = $question->options->single;
 108              $question->shuffleanswers = $question->options->shuffleanswers;
 109              $question->answernumbering = $question->options->answernumbering;
 110              $question->showstandardinstruction = $question->options->showstandardinstruction;
 111          }
 112  
 113          return $question;
 114      }
 115  
 116      public function validation($data, $files) {
 117          $errors = parent::validation($data, $files);
 118          $answers = $data['answer'];
 119          $answercount = 0;
 120  
 121          $totalfraction = 0;
 122          $maxfraction = -1;
 123  
 124          foreach ($answers as $key => $answer) {
 125              // Check no of choices.
 126              $trimmedanswer = trim($answer['text']);
 127              $fraction = (float) $data['fraction'][$key];
 128              if ($trimmedanswer === '' && empty($fraction)) {
 129                  continue;
 130              }
 131              if ($trimmedanswer === '') {
 132                  $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
 133              }
 134  
 135              $answercount++;
 136  
 137              // Check grades.
 138              if ($data['fraction'][$key] > 0) {
 139                  $totalfraction += $data['fraction'][$key];
 140              }
 141              if ($data['fraction'][$key] > $maxfraction) {
 142                  $maxfraction = $data['fraction'][$key];
 143              }
 144          }
 145  
 146          if ($answercount == 0) {
 147              $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 148              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 149          } else if ($answercount == 1) {
 150              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 151  
 152          }
 153  
 154          // Perform sanity checks on fractional grades.
 155          if ($data['single']) {
 156              if ($maxfraction != 1) {
 157                  $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
 158                          $maxfraction * 100);
 159              }
 160          } else {
 161              $totalfraction = round($totalfraction, 2);
 162              if ($totalfraction != 1) {
 163                  $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
 164                          $totalfraction * 100);
 165              }
 166          }
 167          return $errors;
 168      }
 169  
 170      public function qtype() {
 171          return 'multichoice';
 172      }
 173  }