Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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->minwordlimit)) {
  65              $data->minwordlimit = null;
  66          }
  67          if (!isset($data->maxwordlimit)) {
  68              $data->maxwordlimit = null;
  69          }
  70          if (!isset($data->attachmentsrequired)) {
  71              $data->attachmentsrequired = 0;
  72          }
  73  
  74          // Detect if the question is created or mapped.
  75          $questioncreated = $this->get_mappingid('question_created',
  76                  $this->get_old_parentid('question')) ? true : false;
  77  
  78          // If the question has been created by restore, we need to create its
  79          // qtype_essay too.
  80          if ($questioncreated) {
  81              $data->questionid = $this->get_new_parentid('question');
  82              $newitemid = $DB->insert_record('qtype_essay_options', $data);
  83              $this->set_mapping('qtype_essay', $oldid, $newitemid);
  84          }
  85      }
  86  
  87      /**
  88       * Return the contents of this qtype to be processed by the links decoder
  89       */
  90      public static function define_decode_contents() {
  91          return array(
  92              new restore_decode_content('qtype_essay_options', 'graderinfo', 'qtype_essay'),
  93          );
  94      }
  95  
  96      /**
  97       * When restoring old data, that does not have the essay options information
  98       * in the XML, supply defaults.
  99       */
 100      protected function after_execute_question() {
 101          global $DB;
 102  
 103          $essayswithoutoptions = $DB->get_records_sql("
 104                      SELECT q.*
 105                        FROM {question} q
 106                        JOIN {backup_ids_temp} bi ON bi.newitemid = q.id
 107                   LEFT JOIN {qtype_essay_options} qeo ON qeo.questionid = q.id
 108                       WHERE q.qtype = ?
 109                         AND qeo.id IS NULL
 110                         AND bi.backupid = ?
 111                         AND bi.itemname = ?
 112                  ", array('essay', $this->get_restoreid(), 'question_created'));
 113  
 114          foreach ($essayswithoutoptions as $q) {
 115              $defaultoptions = new stdClass();
 116              $defaultoptions->questionid = $q->id;
 117              $defaultoptions->responseformat = 'editor';
 118              $defaultoptions->responserequired = 1;
 119              $defaultoptions->responsefieldlines = 15;
 120              $defaultoptions->minwordlimit = null;
 121              $defaultoptions->maxwordlimit = null;
 122              $defaultoptions->attachments = 0;
 123              $defaultoptions->attachmentsrequired = 0;
 124              $defaultoptions->graderinfo = '';
 125              $defaultoptions->graderinfoformat = FORMAT_HTML;
 126              $defaultoptions->responsetemplate = '';
 127              $defaultoptions->responsetemplateformat = FORMAT_HTML;
 128              $DB->insert_record('qtype_essay_options', $defaultoptions);
 129          }
 130      }
 131  }