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.
   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    block_quiz_results
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  /**
  26   * Specialised restore task for the quiz_results block
  27   * (using execute_after_tasks for recoding of target quiz)
  28   *
  29   * TODO: Finish phpdocs
  30   *
  31   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class restore_quiz_results_block_task extends restore_block_task {
  35  
  36      protected function define_my_settings() {
  37      }
  38  
  39      protected function define_my_steps() {
  40      }
  41  
  42      public function get_fileareas() {
  43          return array(); // No associated fileareas
  44      }
  45  
  46      public function get_configdata_encoded_attributes() {
  47          return array(); // No special handling of configdata
  48      }
  49  
  50      /**
  51       * This function, executed after all the tasks in the plan
  52       * have been executed, will perform the recode of the
  53       * target quiz for the block. This must be done here
  54       * and not in normal execution steps because the quiz
  55       * can be restored after the block.
  56       */
  57      public function after_restore() {
  58          global $DB;
  59  
  60          // Get the blockid.
  61          $blockid = $this->get_blockid();
  62  
  63          // Extract block configdata and update it to point to the new quiz.
  64          $configdata = $DB->get_field('block_instances', 'configdata', array('id' => $blockid));
  65          $newconfigdata = '';
  66  
  67          // The block was configured.
  68          if (!empty($configdata)) {
  69              $config = $this->decode_configdata($configdata);
  70              $config->activityparent = 'quiz';
  71              $config->activityparentid = 0;
  72              $config->gradeformat = isset($config->gradeformat) ? $config->gradeformat : 1;
  73  
  74              if (!empty($config->quizid)
  75                      && $quizmap = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'quiz', $config->quizid)) {
  76                  $config->activityparentid = $quizmap->newitemid;
  77              }
  78  
  79              // Set the decimal valuue as appropriate.
  80              if ($config->gradeformat == 1) {
  81                  // This block is using percentages, do not display any decimal places.
  82                  $config->decimalpoints = 0;
  83              } else {
  84                  // Get the decimal value from the corresponding quiz.
  85                  $config->decimalpoints = $DB->get_field('quiz', 'decimalpoints', array('id' => $config->activityparentid));
  86              }
  87  
  88              // Get the grade_items record to set the activitygradeitemid.
  89              $info = $DB->get_record('grade_items',
  90                      array('iteminstance' => $config->activityparentid, 'itemmodule' => $config->activityparent));
  91              $config->activitygradeitemid = 0;
  92              if ($info) {
  93                  $config->activitygradeitemid = $info->id;
  94              }
  95  
  96              unset($config->quizid);
  97              $newconfigdata = base64_encode(serialize($config));
  98          }
  99  
 100          // Update the configuration and convert the block.
 101          $DB->set_field('block_instances', 'configdata', $newconfigdata, array('id' => $blockid));
 102          $DB->set_field('block_instances', 'blockname', 'activity_results', array('id' => $blockid));
 103      }
 104  
 105      static public function define_decode_contents() {
 106          return array();
 107      }
 108  
 109      static public function define_decode_rules() {
 110          return array();
 111      }
 112  }