[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/ddmarker/ -> 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 images onto images question type.
  19   *
  20   * @package   qtype_ddmarker
  21   * @copyright 2012 The Open University
  22   * @author    Jamie Pratt <me@jamiep.org>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/question/type/ddimageortext/questiontypebase.php');
  30  
  31  define('QTYPE_DDMARKER_BGIMAGE_MAXWIDTH', 600);
  32  define('QTYPE_DDMARKER_BGIMAGE_MAXHEIGHT', 400);
  33  
  34  /**
  35   * Question hint for ddmarker.
  36   *
  37   * An extension of {@link question_hint} for questions like match and multiple
  38   * choice with multile answers, where there are options for whether to show the
  39   * number of parts right at each stage, and to reset the wrong parts.
  40   *
  41   * @copyright  2010 The Open University
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class question_hint_ddmarker extends question_hint_with_parts {
  45  
  46      public $statewhichincorrect;
  47  
  48      /**
  49       * Constructor.
  50       * @param int the hint id from the database.
  51       * @param string $hint The hint text
  52       * @param int the corresponding text FORMAT_... type.
  53       * @param bool $shownumcorrect whether the number of right parts should be shown
  54       * @param bool $clearwrong whether the wrong parts should be reset.
  55       */
  56      public function __construct($id, $hint, $hintformat, $shownumcorrect,
  57                                                              $clearwrong, $statewhichincorrect) {
  58          parent::__construct($id, $hint, $hintformat, $shownumcorrect, $clearwrong);
  59          $this->statewhichincorrect = $statewhichincorrect;
  60      }
  61  
  62      /**
  63       * Create a basic hint from a row loaded from the question_hints table in the database.
  64       * @param object $row with property options as well as hint, shownumcorrect and clearwrong set.
  65       * @return question_hint_ddmarker
  66       */
  67      public static function load_from_record($row) {
  68          return new question_hint_ddmarker($row->id, $row->hint, $row->hintformat,
  69                  $row->shownumcorrect, $row->clearwrong, $row->options);
  70      }
  71  
  72      public function adjust_display_options(question_display_options $options) {
  73          parent::adjust_display_options($options);
  74          $options->statewhichincorrect = $this->statewhichincorrect;
  75      }
  76  }
  77  
  78  
  79  
  80  /**
  81   * The drag-and-drop markers question type class.
  82   *
  83   * @copyright  2009 The Open University
  84   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  85   */
  86  class qtype_ddmarker extends qtype_ddtoimage_base {
  87  
  88      public function save_question_options($formdata) {
  89          global $DB, $USER;
  90          $context = $formdata->context;
  91  
  92          $options = $DB->get_record('qtype_ddmarker', array('questionid' => $formdata->id));
  93          if (!$options) {
  94              $options = new stdClass();
  95              $options->questionid = $formdata->id;
  96              $options->correctfeedback = '';
  97              $options->partiallycorrectfeedback = '';
  98              $options->incorrectfeedback = '';
  99              $options->id = $DB->insert_record('qtype_ddmarker', $options);
 100          }
 101  
 102          $options->shuffleanswers = !empty($formdata->shuffleanswers);
 103          $options->showmisplaced = !empty($formdata->showmisplaced);
 104          $options = $this->save_combined_feedback_helper($options, $formdata, $context, true);
 105          $this->save_hints($formdata, true);
 106          $DB->update_record('qtype_ddmarker', $options);
 107          $DB->delete_records('qtype_ddmarker_drops', array('questionid' => $formdata->id));
 108          foreach (array_keys($formdata->drops) as $dropno) {
 109              if ($formdata->drops[$dropno]['choice'] == 0) {
 110                  continue;
 111              }
 112              $drop = new stdClass();
 113              $drop->questionid = $formdata->id;
 114              $drop->no = $dropno + 1;
 115              $drop->shape = $formdata->drops[$dropno]['shape'];
 116              $drop->coords = $formdata->drops[$dropno]['coords'];
 117              $drop->choice = $formdata->drops[$dropno]['choice'];
 118  
 119              $DB->insert_record('qtype_ddmarker_drops', $drop);
 120          }
 121  
 122          // An array of drag no -> drag id.
 123          $olddragids = $DB->get_records_menu('qtype_ddmarker_drags',
 124                                      array('questionid' => $formdata->id),
 125                                      '', 'no, id');
 126          foreach (array_keys($formdata->drags) as $dragno) {
 127              if (!empty($formdata->drags[$dragno]['label'])) {
 128                  $drag = new stdClass();
 129                  $drag->questionid = $formdata->id;
 130                  $drag->no = $dragno + 1;
 131                  if ($formdata->drags[$dragno]['noofdrags'] == 0) {
 132                      $drag->infinite = 1;
 133                      $drag->noofdrags = 1;
 134                  } else {
 135                      $drag->infinite = 0;
 136                      $drag->noofdrags = $formdata->drags[$dragno]['noofdrags'];
 137                  }
 138                  $drag->label = $formdata->drags[$dragno]['label'];
 139  
 140                  if (isset($olddragids[$dragno + 1])) {
 141                      $drag->id = $olddragids[$dragno + 1];
 142                      unset($olddragids[$dragno + 1]);
 143                      $DB->update_record('qtype_ddmarker_drags', $drag);
 144                  } else {
 145                      $drag->id = $DB->insert_record('qtype_ddmarker_drags', $drag);
 146                  }
 147  
 148              }
 149  
 150          }
 151          if (!empty($olddragids)) {
 152              list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
 153              $DB->delete_records_select('qtype_ddmarker_drags', "id $sql", $params);
 154          }
 155  
 156          self::constrain_image_size_in_draft_area($formdata->bgimage,
 157                                                      QTYPE_DDMARKER_BGIMAGE_MAXWIDTH,
 158                                                      QTYPE_DDMARKER_BGIMAGE_MAXHEIGHT);
 159          file_save_draft_area_files($formdata->bgimage, $formdata->context->id,
 160                                      'qtype_ddmarker', 'bgimage', $formdata->id,
 161                                      array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
 162      }
 163  
 164      public function save_hints($formdata, $withparts = false) {
 165          global $DB;
 166          $context = $formdata->context;
 167  
 168          $oldhints = $DB->get_records('question_hints',
 169                  array('questionid' => $formdata->id), 'id ASC');
 170  
 171          if (!empty($formdata->hint)) {
 172              $numhints = max(array_keys($formdata->hint)) + 1;
 173          } else {
 174              $numhints = 0;
 175          }
 176  
 177          if ($withparts) {
 178              if (!empty($formdata->hintclearwrong)) {
 179                  $numclears = max(array_keys($formdata->hintclearwrong)) + 1;
 180              } else {
 181                  $numclears = 0;
 182              }
 183              if (!empty($formdata->hintshownumcorrect)) {
 184                  $numshows = max(array_keys($formdata->hintshownumcorrect)) + 1;
 185              } else {
 186                  $numshows = 0;
 187              }
 188              $numhints = max($numhints, $numclears, $numshows);
 189          }
 190  
 191          for ($i = 0; $i < $numhints; $i += 1) {
 192              if (html_is_blank($formdata->hint[$i]['text'])) {
 193                  $formdata->hint[$i]['text'] = '';
 194              }
 195  
 196              if ($withparts) {
 197                  $clearwrong = !empty($formdata->hintclearwrong[$i]);
 198                  $shownumcorrect = !empty($formdata->hintshownumcorrect[$i]);
 199                  $statewhichincorrect = !empty($formdata->hintoptions[$i]);
 200              }
 201  
 202              if (empty($formdata->hint[$i]['text']) && empty($clearwrong) &&
 203                      empty($shownumcorrect) && empty($statewhichincorrect)) {
 204                  continue;
 205              }
 206  
 207              // Update an existing hint if possible.
 208              $hint = array_shift($oldhints);
 209              if (!$hint) {
 210                  $hint = new stdClass();
 211                  $hint->questionid = $formdata->id;
 212                  $hint->hint = '';
 213                  $hint->id = $DB->insert_record('question_hints', $hint);
 214              }
 215  
 216              $hint->hint = $this->import_or_save_files($formdata->hint[$i],
 217                      $context, 'question', 'hint', $hint->id);
 218              $hint->hintformat = $formdata->hint[$i]['format'];
 219              if ($withparts) {
 220                  $hint->clearwrong = $clearwrong;
 221                  $hint->shownumcorrect = $shownumcorrect;
 222                  $hint->options = $statewhichincorrect;
 223              }
 224              $DB->update_record('question_hints', $hint);
 225          }
 226  
 227          // Delete any remaining old hints.
 228          $fs = get_file_storage();
 229          foreach ($oldhints as $oldhint) {
 230              $fs->delete_area_files($context->id, 'question', 'hint', $oldhint->id);
 231              $DB->delete_records('question_hints', array('id' => $oldhint->id));
 232          }
 233      }
 234  
 235      protected function make_hint($hint) {
 236          return question_hint_ddmarker::load_from_record($hint);
 237      }
 238      protected function make_choice($dragdata) {
 239          return new qtype_ddmarker_drag_item($dragdata->label, $dragdata->no, $dragdata->infinite, $dragdata->noofdrags);
 240      }
 241  
 242      protected function make_place($dropdata) {
 243          return new qtype_ddmarker_drop_zone($dropdata->no, $dropdata->shape, $dropdata->coords);
 244      }
 245  
 246      protected function initialise_combined_feedback(question_definition $question,
 247                                                                  $questiondata, $withparts = false) {
 248          parent::initialise_combined_feedback($question, $questiondata, $withparts);
 249          $question->showmisplaced = $questiondata->options->showmisplaced;
 250      }
 251  
 252      public function move_files($questionid, $oldcontextid, $newcontextid) {
 253          global $DB;
 254          $fs = get_file_storage();
 255  
 256          parent::move_files($questionid, $oldcontextid, $newcontextid);
 257          $fs->move_area_files_to_new_context($oldcontextid,
 258                                      $newcontextid, 'qtype_ddmarker', 'bgimage', $questionid);
 259  
 260          $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
 261          $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
 262      }
 263  
 264      /**
 265       * Delete all the files belonging to this question.
 266       * @param int $questionid the question being deleted.
 267       * @param int $contextid the context the question is in.
 268       */
 269  
 270      protected function delete_files($questionid, $contextid) {
 271          global $DB;
 272          $fs = get_file_storage();
 273  
 274          parent::delete_files($questionid, $contextid);
 275  
 276          $this->delete_files_in_combined_feedback($questionid, $contextid);
 277          $this->delete_files_in_hints($questionid, $contextid);
 278      }
 279  
 280      public function export_to_xml($question, qformat_xml $format, $extra = null) {
 281          $fs = get_file_storage();
 282          $contextid = $question->contextid;
 283          $output = '';
 284  
 285          if ($question->options->shuffleanswers) {
 286              $output .= "    <shuffleanswers/>\n";
 287          }
 288          if ($question->options->showmisplaced) {
 289              $output .= "    <showmisplaced/>\n";
 290          }
 291          $output .= $format->write_combined_feedback($question->options,
 292                                                      $question->id,
 293                                                      $question->contextid);
 294          $files = $fs->get_area_files($contextid, 'qtype_ddmarker', 'bgimage', $question->id);
 295          $output .= "    ".$this->write_files($files, 2)."\n";;
 296  
 297          foreach ($question->options->drags as $drag) {
 298              $files =
 299                      $fs->get_area_files($contextid, 'qtype_ddmarker', 'dragimage', $drag->id);
 300              $output .= "    <drag>\n";
 301              $output .= "      <no>{$drag->no}</no>\n";
 302              $output .= $format->writetext($drag->label, 3);
 303              if ($drag->infinite) {
 304                  $output .= "      <infinite/>\n";
 305              }
 306              $output .= "      <noofdrags>{$drag->noofdrags}</noofdrags>\n";
 307              $output .= "    </drag>\n";
 308          }
 309          foreach ($question->options->drops as $drop) {
 310              $output .= "    <drop>\n";
 311              $output .= "      <no>{$drop->no}</no>\n";
 312              $output .= "      <shape>{$drop->shape}</shape>\n";
 313              $output .= "      <coords>{$drop->coords}</coords>\n";
 314              $output .= "      <choice>{$drop->choice}</choice>\n";
 315              $output .= "    </drop>\n";
 316          }
 317  
 318          return $output;
 319      }
 320  
 321      public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
 322          if (!isset($data['@']['type']) || $data['@']['type'] != 'ddmarker') {
 323              return false;
 324          }
 325  
 326          $question = $format->import_headers($data);
 327          $question->qtype = 'ddmarker';
 328  
 329          $question->shuffleanswers = array_key_exists('shuffleanswers',
 330                                                      $format->getpath($data, array('#'), array()));
 331          $question->showmisplaced = array_key_exists('showmisplaced',
 332                                                      $format->getpath($data, array('#'), array()));
 333  
 334          $filexml = $format->getpath($data, array('#', 'file'), array());
 335          $question->bgimage = $format->import_files_as_draft($filexml);
 336          $drags = $data['#']['drag'];
 337          $question->drags = array();
 338  
 339          foreach ($drags as $dragxml) {
 340              $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
 341              $dragindex = $dragno - 1;
 342              $question->drags[$dragindex] = array();
 343              $question->drags[$dragindex]['label'] =
 344                          $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
 345              if (array_key_exists('infinite', $dragxml['#'])) {
 346                  $question->drags[$dragindex]['noofdrags'] = 0; // Means infinite in the form.
 347              } else {
 348                  // Defaults to 1 if 'noofdrags' not set.
 349                  $question->drags[$dragindex]['noofdrags'] = $format->getpath($dragxml, array('#', 'noofdrags', 0, '#'), 1);
 350              }
 351          }
 352  
 353          $drops = $data['#']['drop'];
 354          $question->drops = array();
 355          foreach ($drops as $dropxml) {
 356              $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
 357              $dropindex = $dropno - 1;
 358              $question->drops[$dropindex] = array();
 359              $question->drops[$dropindex]['choice'] =
 360                          $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
 361              $question->drops[$dropindex]['shape'] =
 362                          $format->getpath($dropxml, array('#', 'shape', 0, '#'), '');
 363              $question->drops[$dropindex]['coords'] =
 364                          $format->getpath($dropxml, array('#', 'coords', 0, '#'), '');
 365          }
 366  
 367          $format->import_combined_feedback($question, $data, true);
 368          $format->import_hints($question, $data, true, true,
 369                  $format->get_format($question->questiontextformat));
 370  
 371          return $question;
 372      }
 373  
 374      public function get_random_guess_score($questiondata) {
 375          return null;
 376      }
 377  
 378  }


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