Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Provides support for the conversion of moodle1 backup to the moodle2 format
  20   *
  21   * @package    workshopform_numerrors
  22   * @copyright  2011 David Mudrak <david@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/gradelib.php'); // grade_floatval() called here
  29  
  30  /**
  31   * Conversion handler for the numerrors grading strategy data
  32   */
  33  class moodle1_workshopform_numerrors_handler extends moodle1_workshopform_handler {
  34  
  35      /** @var array */
  36      protected $mappings = array();
  37  
  38      /** @var array */
  39      protected $dimensions = array();
  40  
  41      /**
  42       * New workshop instance is being processed
  43       */
  44      public function on_elements_start() {
  45          $this->mappings = array();
  46          $this->dimensions = array();
  47      }
  48  
  49      /**
  50       * Converts <ELEMENT> into <workshopform_numerrors_dimension> and stores it for later writing
  51       *
  52       * @param array $data legacy element data
  53       * @param array $raw raw element data
  54       *
  55       * @return array to be written to workshop.xml
  56       */
  57      public function process_legacy_element(array $data, array $raw) {
  58  
  59          $workshop = $this->parenthandler->get_current_workshop();
  60  
  61          $mapping = array();
  62          $mapping['id'] = $data['id'];
  63          $mapping['nonegative'] = $data['elementno'];
  64          if ($workshop['grade'] == 0 or $data['maxscore'] == 0) {
  65              $mapping['grade'] = 0;
  66          } else {
  67              $mapping['grade'] = grade_floatval($data['maxscore'] / $workshop['grade'] * 100);
  68          }
  69          $this->mappings[] = $mapping;
  70  
  71          $converted = null;
  72  
  73          if (trim($data['description']) and $data['description'] <> '@@ GRADE_MAPPING_ELEMENT @@') {
  74              // prepare a fake record and re-use the upgrade logic
  75              $fakerecord = (object)$data;
  76              $converted = (array)workshopform_numerrors_upgrade_element($fakerecord, 12345678);
  77              unset($converted['workshopid']);
  78  
  79              $converted['id'] = $data['id'];
  80              $this->dimensions[] = $converted;
  81          }
  82  
  83          return $converted;
  84      }
  85  
  86      /**
  87       * Writes gathered mappings and dimensions
  88       */
  89      public function on_elements_end() {
  90  
  91          foreach ($this->mappings as $mapping) {
  92              $this->write_xml('workshopform_numerrors_map', $mapping, array('/workshopform_numerrors_map/id'));
  93          }
  94  
  95          foreach ($this->dimensions as $dimension) {
  96              $this->write_xml('workshopform_numerrors_dimension', $dimension, array('/workshopform_numerrors_dimension/id'));
  97          }
  98      }
  99  }
 100  
 101  /**
 102   * Transforms a given record from workshop_elements_old into an object to be saved into workshopform_numerrors
 103   *
 104   * @param stdClass $old legacy record from workshop_elements_old
 105   * @param int $newworkshopid id of the new workshop instance that replaced the previous one
 106   * @return stdclass to be saved in workshopform_numerrors
 107   */
 108  function workshopform_numerrors_upgrade_element(stdclass $old, $newworkshopid) {
 109      $new = new stdclass();
 110      $new->workshopid = $newworkshopid;
 111      $new->sort = $old->elementno;
 112      $new->description = $old->description;
 113      $new->descriptionformat = FORMAT_HTML;
 114      $new->grade0 = get_string('grade0default', 'workshopform_numerrors');
 115      $new->grade1 = get_string('grade1default', 'workshopform_numerrors');
 116      // calculate new weight of the element. Negative weights are not supported any more and
 117      // are replaced with weight = 0. Legacy workshop did not store the raw weight but the index
 118      // in the array of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x)
 119      // workshop 2.0 uses integer weights only (0-16) so all previous weights are multiplied by 4.
 120      switch ($old->weight) {
 121          case 8: $new->weight = 1; break;
 122          case 9: $new->weight = 2; break;
 123          case 10: $new->weight = 3; break;
 124          case 11: $new->weight = 4; break;
 125          case 12: $new->weight = 6; break;
 126          case 13: $new->weight = 8; break;
 127          case 14: $new->weight = 16; break;
 128          default: $new->weight = 0;
 129      }
 130      return $new;
 131  }