Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 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', get_config('qtype_multichoice', 'answerhowmany'));
  50  
  51          $mform->addElement('advcheckbox', 'shuffleanswers',
  52                  get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
  53          $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
  54          $mform->setDefault('shuffleanswers', get_config('qtype_multichoice', 'shuffleanswers'));
  55  
  56          $mform->addElement('select', 'answernumbering',
  57                  get_string('answernumbering', 'qtype_multichoice'),
  58                  qtype_multichoice::get_numbering_styles());
  59          $mform->setDefault('answernumbering', get_config('qtype_multichoice', 'answernumbering'));
  60  
  61          $mform->addElement('selectyesno', 'showstandardinstruction',
  62              get_string('showstandardinstruction', 'qtype_multichoice'), null, null, [0, 1]);
  63          $mform->addHelpButton('showstandardinstruction', 'showstandardinstruction', 'qtype_multichoice');
  64          $mform->setDefault('showstandardinstruction', 0);
  65  
  66          $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
  67                  question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
  68  
  69          $this->add_combined_feedback_fields(true);
  70          $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
  71  
  72          $this->add_interactive_settings(true, true);
  73      }
  74  
  75      protected function get_per_answer_fields($mform, $label, $gradeoptions,
  76              &$repeatedoptions, &$answersoption) {
  77          $repeated = array();
  78          $repeated[] = $mform->createElement('editor', 'answer',
  79                  $label, array('rows' => 1), $this->editoroptions);
  80          $repeated[] = $mform->createElement('select', 'fraction',
  81                  get_string('grade'), $gradeoptions);
  82          $repeated[] = $mform->createElement('editor', 'feedback',
  83                  get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions);
  84          $repeatedoptions['answer']['type'] = PARAM_RAW;
  85          $repeatedoptions['fraction']['default'] = 0;
  86          $answersoption = 'answers';
  87          return $repeated;
  88      }
  89  
  90      protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
  91          list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
  92          $repeatedoptions['hintclearwrong']['disabledif'] = array('single', 'eq', 1);
  93          $repeatedoptions['hintshownumcorrect']['disabledif'] = array('single', 'eq', 1);
  94          return array($repeated, $repeatedoptions);
  95      }
  96  
  97      protected function data_preprocessing($question) {
  98          $question = parent::data_preprocessing($question);
  99          $question = $this->data_preprocessing_answers($question, true);
 100          $question = $this->data_preprocessing_combined_feedback($question, true);
 101          $question = $this->data_preprocessing_hints($question, true, true);
 102  
 103          if (!empty($question->options)) {
 104              $question->single = $question->options->single;
 105              $question->shuffleanswers = $question->options->shuffleanswers;
 106              $question->answernumbering = $question->options->answernumbering;
 107              $question->showstandardinstruction = $question->options->showstandardinstruction;
 108          }
 109  
 110          return $question;
 111      }
 112  
 113      public function validation($data, $files) {
 114          $errors = parent::validation($data, $files);
 115          $answers = $data['answer'];
 116          $answercount = 0;
 117  
 118          $totalfraction = 0;
 119          $maxfraction = -1;
 120  
 121          foreach ($answers as $key => $answer) {
 122              // Check no of choices.
 123              $trimmedanswer = trim($answer['text']);
 124              $fraction = (float) $data['fraction'][$key];
 125              if ($trimmedanswer === '' && empty($fraction)) {
 126                  continue;
 127              }
 128              if ($trimmedanswer === '') {
 129                  $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
 130              }
 131  
 132              $answercount++;
 133  
 134              // Check grades.
 135              if ($data['fraction'][$key] > 0) {
 136                  $totalfraction += $data['fraction'][$key];
 137              }
 138              if ($data['fraction'][$key] > $maxfraction) {
 139                  $maxfraction = $data['fraction'][$key];
 140              }
 141          }
 142  
 143          if ($answercount == 0) {
 144              $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 145              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 146          } else if ($answercount == 1) {
 147              $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
 148  
 149          }
 150  
 151          // Perform sanity checks on fractional grades.
 152          if ($data['single']) {
 153              if ($maxfraction != 1) {
 154                  $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
 155                          $maxfraction * 100);
 156              }
 157          } else {
 158              $totalfraction = round($totalfraction, 2);
 159              if ($totalfraction != 1) {
 160                  $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
 161                          $totalfraction * 100);
 162              }
 163          }
 164          return $errors;
 165      }
 166  
 167      public function qtype() {
 168          return 'multichoice';
 169      }
 170  }