[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/ddimageortext/ -> 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 onto image question type.
  19   *
  20   * @package    qtype_ddimageortext
  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->dirroot . '/question/type/ddimageortext/questiontypebase.php');
  29  
  30  define('QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXWIDTH', 600);
  31  define('QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXHEIGHT', 400);
  32  define('QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXWIDTH', 150);
  33  define('QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT', 100);
  34  
  35  /**
  36   * The drag-and-drop onto image question type class.
  37   *
  38   * @copyright  2009 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qtype_ddimageortext extends qtype_ddtoimage_base {
  42  
  43      protected function make_choice($dragdata) {
  44          return new qtype_ddimageortext_drag_item($dragdata->label, $dragdata->no,
  45                                          $dragdata->draggroup, $dragdata->infinite, $dragdata->id);
  46      }
  47  
  48      protected function make_place($dropzonedata) {
  49          return new qtype_ddimageortext_drop_zone($dropzonedata->label, $dropzonedata->no,
  50                                                      $dropzonedata->group,
  51                                                      $dropzonedata->xleft, $dropzonedata->ytop);
  52      }
  53  
  54      protected function make_hint($hint) {
  55          return question_hint_with_parts::load_from_record($hint);
  56      }
  57  
  58      public function save_question_options($formdata) {
  59          global $DB, $USER;
  60          $context = $formdata->context;
  61  
  62          $options = $DB->get_record('qtype_ddimageortext', array('questionid' => $formdata->id));
  63          if (!$options) {
  64              $options = new stdClass();
  65              $options->questionid = $formdata->id;
  66              $options->correctfeedback = '';
  67              $options->partiallycorrectfeedback = '';
  68              $options->incorrectfeedback = '';
  69              $options->id = $DB->insert_record('qtype_ddimageortext', $options);
  70          }
  71  
  72          $options->shuffleanswers = !empty($formdata->shuffleanswers);
  73          $options = $this->save_combined_feedback_helper($options, $formdata, $context, true);
  74          $this->save_hints($formdata, true);
  75          $DB->update_record('qtype_ddimageortext', $options);
  76          $DB->delete_records('qtype_ddimageortext_drops', array('questionid' => $formdata->id));
  77          foreach (array_keys($formdata->drops) as $dropno) {
  78              if ($formdata->drops[$dropno]['choice'] == 0) {
  79                  continue;
  80              }
  81              $drop = new stdClass();
  82              $drop->questionid = $formdata->id;
  83              $drop->no = $dropno + 1;
  84              $drop->xleft = $formdata->drops[$dropno]['xleft'];
  85              $drop->ytop = $formdata->drops[$dropno]['ytop'];
  86              $drop->choice = $formdata->drops[$dropno]['choice'];
  87              $drop->label = $formdata->drops[$dropno]['droplabel'];
  88  
  89              $DB->insert_record('qtype_ddimageortext_drops', $drop);
  90          }
  91  
  92          // An array of drag no -> drag id.
  93          $olddragids = $DB->get_records_menu('qtype_ddimageortext_drags',
  94                                      array('questionid' => $formdata->id),
  95                                      '', 'no, id');
  96          foreach (array_keys($formdata->drags) as $dragno) {
  97              $info = file_get_draft_area_info($formdata->dragitem[$dragno]);
  98              if ($info['filecount'] > 0 || (trim($formdata->draglabel[$dragno]) != '')) {
  99                  $draftitemid = $formdata->dragitem[$dragno];
 100  
 101                  $drag = new stdClass();
 102                  $drag->questionid = $formdata->id;
 103                  $drag->no = $dragno + 1;
 104                  $drag->draggroup = $formdata->drags[$dragno]['draggroup'];
 105                  $drag->infinite = empty($formdata->drags[$dragno]['infinite']) ? 0 : 1;
 106                  $drag->label = $formdata->draglabel[$dragno];
 107  
 108                  if (isset($olddragids[$dragno + 1])) {
 109                      $drag->id = $olddragids[$dragno + 1];
 110                      unset($olddragids[$dragno + 1]);
 111                      $DB->update_record('qtype_ddimageortext_drags', $drag);
 112                  } else {
 113                      $drag->id = $DB->insert_record('qtype_ddimageortext_drags', $drag);
 114                  }
 115  
 116                  if ($formdata->drags[$dragno]['dragitemtype'] == 'image') {
 117                      self::constrain_image_size_in_draft_area($draftitemid,
 118                                          QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXWIDTH,
 119                                          QTYPE_DDIMAGEORTEXT_DRAGIMAGE_MAXHEIGHT);
 120                      file_save_draft_area_files($draftitemid, $formdata->context->id,
 121                                          'qtype_ddimageortext', 'dragimage', $drag->id,
 122                                          array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
 123                  } else {
 124                      // Delete any existing files for draggable text item type.
 125                      $fs = get_file_storage();
 126                      $fs->delete_area_files($formdata->context->id, 'qtype_ddimageortext',
 127                                                                  'dragimage', $drag->id);
 128                  }
 129  
 130              }
 131  
 132          }
 133  
 134          if (!empty($olddragids)) {
 135              list($sql, $params) = $DB->get_in_or_equal(array_values($olddragids));
 136              $DB->delete_records_select('qtype_ddimageortext_drags', "id $sql", $params);
 137          }
 138  
 139          self::constrain_image_size_in_draft_area($formdata->bgimage,
 140                                                      QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXWIDTH,
 141                                                      QTYPE_DDIMAGEORTEXT_BGIMAGE_MAXHEIGHT);
 142          file_save_draft_area_files($formdata->bgimage, $formdata->context->id,
 143                                      'qtype_ddimageortext', 'bgimage', $formdata->id,
 144                                      array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1));
 145      }
 146      public function move_files($questionid, $oldcontextid, $newcontextid) {
 147          global $DB;
 148          $fs = get_file_storage();
 149  
 150          parent::move_files($questionid, $oldcontextid, $newcontextid);
 151          $fs->move_area_files_to_new_context($oldcontextid,
 152                                      $newcontextid, 'qtype_ddimageortext', 'bgimage', $questionid);
 153          $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
 154                                                  array('questionid' => $questionid), 'id', 'id,1');
 155          foreach ($dragids as $dragid => $notused) {
 156              $fs->move_area_files_to_new_context($oldcontextid,
 157                                      $newcontextid, 'qtype_ddimageortext', 'dragimage', $dragid);
 158          }
 159  
 160          $this->move_files_in_combined_feedback($questionid, $oldcontextid, $newcontextid);
 161          $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
 162      }
 163  
 164      /**
 165       * Delete all the files belonging to this question.
 166       * @param int $questionid the question being deleted.
 167       * @param int $contextid the context the question is in.
 168       */
 169  
 170      protected function delete_files($questionid, $contextid) {
 171          global $DB;
 172          $fs = get_file_storage();
 173  
 174          parent::delete_files($questionid, $contextid);
 175  
 176          $dragids = $DB->get_records_menu('qtype_ddimageortext_drags',
 177                                                  array('questionid' => $questionid), 'id', 'id,1');
 178          foreach ($dragids as $dragid => $notused) {
 179              $fs->delete_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $dragid);
 180          }
 181  
 182          $this->delete_files_in_combined_feedback($questionid, $contextid);
 183          $this->delete_files_in_hints($questionid, $contextid);
 184      }
 185  
 186  
 187      public function export_to_xml($question, qformat_xml $format, $extra = null) {
 188          $fs = get_file_storage();
 189          $contextid = $question->contextid;
 190          $output = '';
 191  
 192          if ($question->options->shuffleanswers) {
 193              $output .= "    <shuffleanswers/>\n";
 194          }
 195          $output .= $format->write_combined_feedback($question->options,
 196                                                      $question->id,
 197                                                      $question->contextid);
 198          $files = $fs->get_area_files($contextid, 'qtype_ddimageortext', 'bgimage', $question->id);
 199          $output .= "    ".$this->write_files($files, 2)."\n";;
 200  
 201          foreach ($question->options->drags as $drag) {
 202              $files =
 203                      $fs->get_area_files($contextid, 'qtype_ddimageortext', 'dragimage', $drag->id);
 204              $output .= "    <drag>\n";
 205              $output .= "      <no>{$drag->no}</no>\n";
 206              $output .= $format->writetext($drag->label, 3)."\n";
 207              $output .= "      <draggroup>{$drag->draggroup}</draggroup>\n";
 208              if ($drag->infinite) {
 209                  $output .= "      <infinite/>\n";
 210              }
 211              $output .= $this->write_files($files, 3);
 212              $output .= "    </drag>\n";
 213          }
 214          foreach ($question->options->drops as $drop) {
 215              $output .= "    <drop>\n";
 216              $output .= $format->writetext($drop->label, 3);
 217              $output .= "      <no>{$drop->no}</no>\n";
 218              $output .= "      <choice>{$drop->choice}</choice>\n";
 219              $output .= "      <xleft>{$drop->xleft}</xleft>\n";
 220              $output .= "      <ytop>{$drop->ytop}</ytop>\n";
 221              $output .= "    </drop>\n";
 222          }
 223  
 224          return $output;
 225      }
 226  
 227      public function import_from_xml($data, $question, qformat_xml $format, $extra=null) {
 228          if (!isset($data['@']['type']) || $data['@']['type'] != 'ddimageortext') {
 229              return false;
 230          }
 231  
 232          $question = $format->import_headers($data);
 233          $question->qtype = 'ddimageortext';
 234  
 235          $question->shuffleanswers = array_key_exists('shuffleanswers',
 236                                                      $format->getpath($data, array('#'), array()));
 237  
 238          $filexml = $format->getpath($data, array('#', 'file'), array());
 239          $question->bgimage = $format->import_files_as_draft($filexml);
 240          $drags = $data['#']['drag'];
 241          $question->drags = array();
 242  
 243          foreach ($drags as $dragxml) {
 244              $dragno = $format->getpath($dragxml, array('#', 'no', 0, '#'), 0);
 245              $dragindex = $dragno - 1;
 246              $question->drags[$dragindex] = array();
 247              $question->draglabel[$dragindex] =
 248                          $format->getpath($dragxml, array('#', 'text', 0, '#'), '', true);
 249              $question->drags[$dragindex]['infinite'] = array_key_exists('infinite', $dragxml['#']);
 250              $question->drags[$dragindex]['draggroup'] =
 251                          $format->getpath($dragxml, array('#', 'draggroup', 0, '#'), 1);
 252              $filexml = $format->getpath($dragxml, array('#', 'file'), array());
 253              $question->dragitem[$dragindex] = $format->import_files_as_draft($filexml);
 254              if (count($filexml)) {
 255                  $question->drags[$dragindex]['dragitemtype'] = 'image';
 256              } else {
 257                  $question->drags[$dragindex]['dragitemtype'] = 'word';
 258              }
 259          }
 260  
 261          $drops = $data['#']['drop'];
 262          $question->drops = array();
 263          foreach ($drops as $dropxml) {
 264              $dropno = $format->getpath($dropxml, array('#', 'no', 0, '#'), 0);
 265              $dropindex = $dropno - 1;
 266              $question->drops[$dropindex] = array();
 267              $question->drops[$dropindex]['choice'] =
 268                          $format->getpath($dropxml, array('#', 'choice', 0, '#'), 0);
 269              $question->drops[$dropindex]['droplabel'] =
 270                          $format->getpath($dropxml, array('#', 'text', 0, '#'), '', true);
 271              $question->drops[$dropindex]['xleft'] =
 272                          $format->getpath($dropxml, array('#', 'xleft', 0, '#'), '');
 273              $question->drops[$dropindex]['ytop'] =
 274                          $format->getpath($dropxml, array('#', 'ytop', 0, '#'), '');
 275          }
 276  
 277          $format->import_combined_feedback($question, $data, true);
 278          $format->import_hints($question, $data, true, false,
 279                  $format->get_format($question->questiontextformat));
 280  
 281          return $question;
 282      }
 283  
 284  }


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