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.
   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   * Question type class for the select missing words question type.
  19   *
  20   * @package    qtype_gapselect
  21   * @copyright  2011 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir . '/questionlib.php');
  29  require_once($CFG->dirroot . '/question/engine/lib.php');
  30  require_once($CFG->dirroot . '/question/format/xml/format.php');
  31  require_once($CFG->dirroot . '/question/type/gapselect/questiontypebase.php');
  32  
  33  
  34  /**
  35   * The select missing words question type class.
  36   *
  37   * @copyright  2011 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_gapselect extends qtype_gapselect_base {
  41      protected function choice_options_to_feedback($choice) {
  42          return $choice['choicegroup'];
  43      }
  44  
  45      protected function make_choice($choicedata) {
  46          return new qtype_gapselect_choice($choicedata->answer, $choicedata->feedback);
  47      }
  48  
  49      protected function feedback_to_choice_options($feedback) {
  50          return array('selectgroup' => $feedback);
  51      }
  52  
  53  
  54      protected function choice_group_key() {
  55          return 'selectgroup';
  56      }
  57  
  58      public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
  59          if (!isset($data['@']['type']) || $data['@']['type'] != 'gapselect') {
  60              return false;
  61          }
  62  
  63          $question = $format->import_headers($data);
  64          $question->qtype = 'gapselect';
  65  
  66          $question->shuffleanswers = $format->trans_single(
  67                  $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
  68  
  69          if (!empty($data['#']['selectoption'])) {
  70              // Modern XML format.
  71              $selectoptions = $data['#']['selectoption'];
  72              $question->answer = array();
  73              $question->selectgroup = array();
  74  
  75              foreach ($data['#']['selectoption'] as $selectoptionxml) {
  76                  $question->choices[] = array(
  77                      'answer'      => $format->getpath($selectoptionxml,
  78                                                        array('#', 'text', 0, '#'), '', true),
  79                      'choicegroup' => $format->getpath($selectoptionxml,
  80                                                        array('#', 'group', 0, '#'), 1),
  81                  );
  82              }
  83  
  84          } else {
  85              // Legacy format containing PHP serialisation.
  86              foreach ($data['#']['answer'] as $answerxml) {
  87                  $ans = $format->import_answer($answerxml);
  88                  $question->choices[] = array(
  89                      'answer' => $ans->answer,
  90                      'choicegroup' => $ans->feedback,
  91                  );
  92              }
  93          }
  94  
  95          $format->import_combined_feedback($question, $data, true);
  96          $format->import_hints($question, $data, true, false,
  97                  $format->get_format($question->questiontextformat));
  98  
  99          return $question;
 100      }
 101  
 102      public function export_to_xml($question, qformat_xml $format, $extra = null) {
 103          $output = '';
 104  
 105          $output .= '    <shuffleanswers>' . $question->options->shuffleanswers .
 106                  "</shuffleanswers>\n";
 107  
 108          $output .= $format->write_combined_feedback($question->options,
 109                                                      $question->id,
 110                                                      $question->contextid);
 111  
 112          foreach ($question->options->answers as $answer) {
 113              $output .= "    <selectoption>\n";
 114              $output .= $format->writetext($answer->answer, 3);
 115              $output .= "      <group>{$answer->feedback}</group>\n";
 116              $output .= "    </selectoption>\n";
 117          }
 118  
 119          return $output;
 120      }
 121  }