Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is 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_rubric
  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  /**
  29   * Conversion handler for the rubric grading strategy data
  30   */
  31  class moodle1_workshopform_rubric_handler extends moodle1_workshopform_handler {
  32  
  33      /** @var array legacy elements to process */
  34      protected $elements = array();
  35  
  36      /** @var array legacy rubrics records to process */
  37      protected $rubrics = array();
  38  
  39      /**
  40       * Prepare to gather legacy elements info for a new workshop instance
  41       */
  42      public function on_elements_start() {
  43          $this->elements = array();
  44          $this->rubrics = array();
  45      }
  46  
  47      /**
  48       * Processes one <ELEMENT>
  49       *
  50       * @param array $data legacy element data
  51       * @param array $raw raw element data
  52       */
  53      public function process_legacy_element(array $data, array $raw) {
  54          $this->elements[] = $data;
  55          $this->rubrics[$data['id']] = array();
  56      }
  57  
  58      /**
  59       * Processes one <RUBRIC>
  60       */
  61      public function process_legacy_rubric($data, $raw) {
  62          $this->rubrics[$data['elementid']][] = $data;
  63      }
  64  
  65      /**
  66       * Processes gathered elements and rubrics
  67       */
  68      public function on_elements_end() {
  69  
  70          $numofrubrics = 0;
  71          foreach ($this->rubrics as $itemid => $levels) {
  72              $numofrubrics += count($levels);
  73          }
  74  
  75          if ($numofrubrics == 0) {
  76              $this->convert_legacy_criterion_elements();
  77  
  78          } else {
  79              $this->convert_legacy_rubric_elements();
  80          }
  81      }
  82  
  83      /**
  84       * Processes gathered elements coming from the legacy criterion strategy
  85       *
  86       * Legacy criterion strategy is converted to a rubric with single rubric item
  87       * and the layout set to 'list'.
  88       */
  89      protected function convert_legacy_criterion_elements() {
  90  
  91          $this->write_xml('workshopform_rubric_config', array('layout' => 'list'));
  92  
  93          $firstelement = reset($this->elements);
  94          if ($firstelement === false) {
  95              // no elements defined in moodle.xml
  96              return;
  97          }
  98  
  99          // write the xml describing the artificial single rubric item
 100          $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $firstelement['id']));
 101          $this->xmlwriter->full_tag('sort', 1);
 102          $this->xmlwriter->full_tag('description', trim(get_string('dimensionnumber', 'workshopform_rubric', '')));
 103          $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
 104  
 105          foreach ($this->elements as $element) {
 106              $this->write_xml('workshopform_rubric_level', array(
 107                  'id'               => $element['id'],
 108                  'grade'            => $element['maxscore'],
 109                  'definition'       => $element['description'],
 110                  'definitionformat' => FORMAT_HTML
 111              ), array('/workshopform_rubric_level/id'));
 112          }
 113  
 114          $this->xmlwriter->end_tag('workshopform_rubric_dimension');
 115      }
 116  
 117      /**
 118       * Processes gathered elements coming from the legacy rubric strategy
 119       */
 120      protected function convert_legacy_rubric_elements() {
 121          $this->write_xml('workshopform_rubric_config', array('layout' => 'grid'));
 122  
 123          foreach ($this->elements as $element) {
 124              $this->xmlwriter->begin_tag('workshopform_rubric_dimension', array('id' => $element['id']));
 125              $this->xmlwriter->full_tag('sort', $element['elementno']);
 126              $this->xmlwriter->full_tag('description', $element['description']);
 127              $this->xmlwriter->full_tag('descriptionformat', FORMAT_HTML);
 128  
 129              foreach ($this->rubrics[$element['id']] as $rubric) {
 130                  $fakerecord          = new stdClass();
 131                  $fakerecord->rgrade  = $rubric['rubricno'];
 132                  $fakerecord->eweight = $element['weight'];
 133                  $fakerecord->rdesc   = $rubric['description'];
 134                  $level = (array)workshopform_rubric_upgrade_rubric_level($fakerecord, $element['id']);
 135                  unset($level['dimensionid']);
 136                  $level['id'] = $this->converter->get_nextid();
 137                  $this->write_xml('workshopform_rubric_level', $level, array('/workshopform_rubric_level/id'));
 138              }
 139  
 140              $this->xmlwriter->end_tag('workshopform_rubric_dimension');
 141          }
 142      }
 143  }
 144  
 145  /**
 146   * Transforms given record into an object to be saved into workshopform_rubric_levels
 147   *
 148   * This is used during Rubric 1.9 -> Rubric 2.0 conversion
 149   *
 150   * @param stdClass $old legacy record from joined workshop_elements_old + workshop_rubrics_old
 151   * @param int $newdimensionid id of the new workshopform_rubric dimension record to be linked to
 152   * @return stdclass to be saved in workshopform_rubric_levels
 153   */
 154  function workshopform_rubric_upgrade_rubric_level(stdclass $old, $newdimensionid) {
 155      $new = new stdclass();
 156      $new->dimensionid = $newdimensionid;
 157      $new->grade = $old->rgrade * workshopform_rubric_upgrade_weight($old->eweight);
 158      $new->definition = $old->rdesc;
 159      $new->definitionformat = FORMAT_HTML;
 160      return $new;
 161  }
 162  
 163  /**
 164   * Given old workshop element weight, returns the weight multiplier
 165   *
 166   * Negative weights are not supported any more and are replaced with weight = 0.
 167   * Legacy workshop did not store the raw weight but the index in the array
 168   * of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x). workshop 2.0 uses
 169   * integer weights only (0-16) so all previous weights are multiplied by 4.
 170   *
 171   * @param $oldweight index in legacy $WORKSHOP_EWEIGHTS
 172   * @return int new weight
 173   */
 174  function workshopform_rubric_upgrade_weight($oldweight) {
 175  
 176      switch ($oldweight) {
 177          case 8: $weight = 1; break;
 178          case 9: $weight = 2; break;
 179          case 10: $weight = 3; break;
 180          case 11: $weight = 4; break;
 181          case 12: $weight = 6; break;
 182          case 13: $weight = 8; break;
 183          case 14: $weight = 16; break;
 184          default: $weight = 0;
 185      }
 186      return $weight;
 187  }