Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is 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 drag-and-drop words into sentences question type.
  19   *
  20   * @package   qtype_ddwtos
  21   * @copyright 2009 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 drag-and-drop words into sentences question type class.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_ddwtos extends qtype_gapselect_base {
  41      protected function choice_group_key() {
  42          return 'draggroup';
  43      }
  44  
  45      protected function choice_options_to_feedback($choice) {
  46          $output = new stdClass();
  47          $output->draggroup = $choice['choicegroup'];
  48          $output->infinite = !empty($choice['infinite']);
  49          return serialize($output);
  50      }
  51  
  52      /**
  53       * Safely convert given serialized feedback string into valid feedback object
  54       *
  55       * @param string $feedback
  56       * @return stdClass
  57       */
  58      protected function unserialize_feedback(string $feedback): stdClass {
  59          $feedbackobject = unserialize_object($feedback);
  60  
  61          return (object) [
  62              'draggroup' => $feedbackobject->draggroup ?? 1,
  63              'infinite' => !empty($feedbackobject->infinite),
  64          ];
  65      }
  66  
  67      protected function feedback_to_choice_options($feedback) {
  68          return (array) $this->unserialize_feedback($feedback);
  69      }
  70  
  71      protected function make_choice($choicedata) {
  72          $options = $this->unserialize_feedback($choicedata->feedback);
  73          return new qtype_ddwtos_choice(
  74                  $choicedata->answer, $options->draggroup, $options->infinite);
  75      }
  76  
  77      public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
  78          if (!isset($data['@']['type']) || $data['@']['type'] != 'ddwtos') {
  79              return false;
  80          }
  81  
  82          $question = $format->import_headers($data);
  83          $question->qtype = 'ddwtos';
  84  
  85          $question->shuffleanswers = $format->trans_single(
  86                  $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
  87  
  88          // Import the choices.
  89          $question->answer = array();
  90          $question->draggroup = array();
  91          $question->infinite = array();
  92  
  93          foreach ($data['#']['dragbox'] as $dragboxxml) {
  94              $question->choices[] = array(
  95                  'answer' => $format->getpath($dragboxxml, array('#', 'text', 0, '#'), '', true),
  96                  'choicegroup' => $format->getpath($dragboxxml, array('#', 'group', 0, '#'), 1),
  97                  'infinite' => array_key_exists('infinite', $dragboxxml['#']),
  98              );
  99          }
 100  
 101          $format->import_combined_feedback($question, $data, true);
 102          $format->import_hints($question, $data, true, false,
 103                  $format->get_format($question->questiontextformat));
 104  
 105          return $question;
 106      }
 107  
 108      public function export_to_xml($question, qformat_xml $format, $extra = null) {
 109          $output = '';
 110  
 111          $output .= '    <shuffleanswers>' . $question->options->shuffleanswers .
 112                  "</shuffleanswers>\n";
 113  
 114          $output .= $format->write_combined_feedback($question->options,
 115                                                      $question->id,
 116                                                      $question->contextid);
 117  
 118          foreach ($question->options->answers as $answer) {
 119              $options = $this->unserialize_feedback($answer->feedback);
 120  
 121              $output .= "    <dragbox>\n";
 122              $output .= $format->writetext($answer->answer, 3);
 123              $output .= "      <group>{$options->draggroup}</group>\n";
 124              if ($options->infinite) {
 125                  $output .= "      <infinite/>\n";
 126              }
 127              $output .= "    </dragbox>\n";
 128          }
 129  
 130          return $output;
 131      }
 132  }