[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/ddwtos/ -> questiontype.php (source)

   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      protected function feedback_to_choice_options($feedback) {
  53          $feedbackobj = unserialize($feedback);
  54          return array('draggroup' => $feedbackobj->draggroup, 'infinite' => $feedbackobj->infinite);
  55      }
  56  
  57      protected function make_choice($choicedata) {
  58          $options = unserialize($choicedata->feedback);
  59          return new qtype_ddwtos_choice(
  60                  $choicedata->answer, $options->draggroup, $options->infinite);
  61      }
  62  
  63      public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
  64          if (!isset($data['@']['type']) || $data['@']['type'] != 'ddwtos') {
  65              return false;
  66          }
  67  
  68          $question = $format->import_headers($data);
  69          $question->qtype = 'ddwtos';
  70  
  71          $question->shuffleanswers = $format->trans_single(
  72                  $format->getpath($data, array('#', 'shuffleanswers', 0, '#'), 1));
  73  
  74          if (!empty($data['#']['dragbox'])) {
  75              // Modern XML format.
  76              $dragboxes = $data['#']['dragbox'];
  77              $question->answer = array();
  78              $question->draggroup = array();
  79              $question->infinite = array();
  80  
  81              foreach ($data['#']['dragbox'] as $dragboxxml) {
  82                  $question->choices[] = array(
  83                      'answer' => $format->getpath($dragboxxml, array('#', 'text', 0, '#'), '', true),
  84                      'choicegroup' => $format->getpath($dragboxxml, array('#', 'group', 0, '#'), 1),
  85                      'infinite' => array_key_exists('infinite', $dragboxxml['#']),
  86                  );
  87              }
  88  
  89          } else {
  90              // Legacy format containing PHP serialisation.
  91              foreach ($data['#']['answer'] as $answerxml) {
  92                  $ans = $format->import_answer($answerxml);
  93                  $options = unserialize(stripslashes($ans->feedback['text']));
  94                  $question->choices[] = array(
  95                      'answer' => $ans->answer,
  96                      'choicegroup' => $options->draggroup,
  97                      'infinite' => $options->infinite,
  98                  );
  99              }
 100          }
 101  
 102          $format->import_combined_feedback($question, $data, true);
 103          $format->import_hints($question, $data, true, false,
 104                  $format->get_format($question->questiontextformat));
 105  
 106          return $question;
 107      }
 108  
 109      public function export_to_xml($question, qformat_xml $format, $extra = null) {
 110          $output = '';
 111  
 112          $output .= '    <shuffleanswers>' . $question->options->shuffleanswers .
 113                  "</shuffleanswers>\n";
 114  
 115          $output .= $format->write_combined_feedback($question->options,
 116                                                      $question->id,
 117                                                      $question->contextid);
 118  
 119          foreach ($question->options->answers as $answer) {
 120              $options = unserialize($answer->feedback);
 121  
 122              $output .= "    <dragbox>\n";
 123              $output .= $format->writetext($answer->answer, 3);
 124              $output .= "      <group>{$options->draggroup}</group>\n";
 125              if ($options->infinite) {
 126                  $output .= "      <infinite/>\n";
 127              }
 128              $output .= "    </dragbox>\n";
 129          }
 130  
 131          return $output;
 132      }
 133  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1