Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * @package    moodlecore
  19   * @subpackage backup-moodle2
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * restore plugin class that provides the necessary information
  30   * needed to restore one essay qtype plugin
  31   *
  32   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class restore_qtype_essay_plugin extends restore_qtype_plugin {
  36  
  37      /**
  38       * Returns the paths to be handled by the plugin at question level
  39       */
  40      protected function define_question_plugin_structure() {
  41          return array(
  42              new restore_path_element('essay', $this->get_pathfor('/essay'))
  43          );
  44      }
  45  
  46      /**
  47       * Process the qtype/essay element
  48       */
  49      public function process_essay($data) {
  50          global $DB;
  51  
  52          $data = (object)$data;
  53          $oldid = $data->id;
  54  
  55          if (!isset($data->responsetemplate)) {
  56              $data->responsetemplate = '';
  57          }
  58          if (!isset($data->responsetemplateformat)) {
  59              $data->responsetemplateformat = FORMAT_HTML;
  60          }
  61          if (!isset($data->responserequired)) {
  62              $data->responserequired = 1;
  63          }
  64          if (!isset($data->attachmentsrequired)) {
  65              $data->attachmentsrequired = 0;
  66          }
  67  
  68          // Detect if the question is created or mapped.
  69          $questioncreated = $this->get_mappingid('question_created',
  70                  $this->get_old_parentid('question')) ? true : false;
  71  
  72          // If the question has been created by restore, we need to create its
  73          // qtype_essay too.
  74          if ($questioncreated) {
  75              $data->questionid = $this->get_new_parentid('question');
  76              $newitemid = $DB->insert_record('qtype_essay_options', $data);
  77              $this->set_mapping('qtype_essay', $oldid, $newitemid);
  78          }
  79      }
  80  
  81      /**
  82       * Return the contents of this qtype to be processed by the links decoder
  83       */
  84      public static function define_decode_contents() {
  85          return array(
  86              new restore_decode_content('qtype_essay_options', 'graderinfo', 'qtype_essay'),
  87          );
  88      }
  89  
  90      /**
  91       * When restoring old data, that does not have the essay options information
  92       * in the XML, supply defaults.
  93       */
  94      protected function after_execute_question() {
  95          global $DB;
  96  
  97          $essayswithoutoptions = $DB->get_records_sql("
  98                      SELECT q.*
  99                        FROM {question} q
 100                        JOIN {backup_ids_temp} bi ON bi.newitemid = q.id
 101                   LEFT JOIN {qtype_essay_options} qeo ON qeo.questionid = q.id
 102                       WHERE q.qtype = ?
 103                         AND qeo.id IS NULL
 104                         AND bi.backupid = ?
 105                         AND bi.itemname = ?
 106                  ", array('essay', $this->get_restoreid(), 'question_created'));
 107  
 108          foreach ($essayswithoutoptions as $q) {
 109              $defaultoptions = new stdClass();
 110              $defaultoptions->questionid = $q->id;
 111              $defaultoptions->responseformat = 'editor';
 112              $defaultoptions->responserequired = 1;
 113              $defaultoptions->responsefieldlines = 15;
 114              $defaultoptions->attachments = 0;
 115              $defaultoptions->attachmentsrequired = 0;
 116              $defaultoptions->graderinfo = '';
 117              $defaultoptions->graderinfoformat = FORMAT_HTML;
 118              $defaultoptions->responsetemplate = '';
 119              $defaultoptions->responsetemplateformat = FORMAT_HTML;
 120              $DB->insert_record('qtype_essay_options', $defaultoptions);
 121          }
 122      }
 123  }