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  
   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_accumulative
  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 accumulative grading strategy data
  30   */
  31  class moodle1_workshopform_accumulative_handler extends moodle1_workshopform_handler {
  32  
  33      /** @var used by {@link self::migrate_legacy_scales()} */
  34      private $newscaleids = array();
  35  
  36      /**
  37       * Converts <ELEMENT> into <workshopform_accumulative_dimension>
  38       */
  39      public function process_legacy_element(array $data, array $raw) {
  40          // prepare a fake record and re-use the upgrade logic
  41          $fakerecord = (object)$data;
  42          $newscaleid = $this->get_new_scaleid($data['scale']);
  43          $converted = (array)workshopform_accumulative_upgrade_element($fakerecord, $newscaleid, 12345678);
  44          unset($converted['workshopid']);
  45  
  46          $converted['id'] = $data['id'];
  47          $this->write_xml('workshopform_accumulative_dimension', $converted, array('/workshopform_accumulative_dimension/id'));
  48  
  49          return $converted;
  50      }
  51  
  52      /**
  53       * If needed, creates new standard (global) scale to replace the legacy workshop one and returns the mapping
  54       *
  55       * If the given $oldscaleid represents a scale, returns array $oldscaleid => $newscaleid that
  56       * can be used as a parameter for {@link workshopform_accumulative_upgrade_element()}. Otherwise
  57       * this method returns empty array.
  58       *
  59       * In workshop 1.x, scale field in workshop_elements had the following meaning:
  60       *   0 | 2 point Yes/No scale
  61       *   1 | 2 point Present/Absent scale
  62       *   2 | 2 point Correct/Incorrect scale
  63       *   3 | 3 point Good/Poor scale
  64       *   4 | 4 point Excellent/Very Poor scale
  65       *   5 | 5 point Excellent/Very Poor scale
  66       *   6 | 7 point Excellent/Very Poor scale
  67       *   7 | Score out of 10
  68       *   8 | Score out of 20
  69       *   9 | Score out of 100
  70       *
  71       * @see workshopform_accumulative_upgrade_scales()
  72       * @param int $oldscaleid the value of the 'scale' field in the moodle.xml backup file
  73       * @return array (int)oldscaleid => (int)newscaleid
  74       */
  75      protected function get_new_scaleid($oldscaleid) {
  76  
  77          if ($oldscaleid >= 0 and $oldscaleid <= 6) {
  78              // we need a new scale id
  79              if (!isset($this->newscaleids[$oldscaleid])) {
  80                  // this is the first time the legacy scale is used in moodle.xml
  81                  // let us migrate it
  82                  $scale = $this->get_new_scale_definition($oldscaleid);
  83                  // other scales are already stashed - let us append a new artificial record
  84                  $currentscaleids = $this->converter->get_stash_itemids('scales');
  85                  if (empty($currentscaleids)) {
  86                      $scale['id'] = 1;
  87                  } else {
  88                      $scale['id'] = max($currentscaleids) + 1;
  89                  }
  90                  $this->converter->set_stash('scales', $scale, $scale['id']);
  91                  $this->newscaleids[$oldscaleid] = $scale['id'];
  92                  // inform the workshop instance that it should annotate the new scale
  93                  $inforefman = $this->parenthandler->get_inforef_manager();
  94                  $inforefman->add_ref('scale', $scale['id']);
  95              }
  96              return array($oldscaleid => $this->newscaleids[$oldscaleid]);
  97  
  98          } else {
  99              // not a scale
 100              return array();
 101          }
 102      }
 103  
 104      /**
 105       * Returns a definition of a legacy workshop scale
 106       *
 107       * @see workshopform_accumulative_upgrade_scales
 108       * @param object $oldscaleid
 109       * @return array
 110       */
 111      private function get_new_scale_definition($oldscaleid) {
 112  
 113          $data = array(
 114              'userid'            => 0,   // restore will remap to the current user
 115              'courseid'          => 0,   // global scale
 116              'description'       => '',
 117              'descriptionformat' => FORMAT_HTML,
 118          );
 119  
 120          switch ($oldscaleid) {
 121          case 0:
 122              $data['name']  = get_string('scalename0', 'workshopform_accumulative');
 123              $data['scale'] = implode(',', array(get_string('no'), get_string('yes')));
 124              break;
 125          case 1:
 126              $data['name']  = get_string('scalename1', 'workshopform_accumulative');
 127              $data['scale'] = implode(',', array(get_string('absent', 'workshopform_accumulative'),
 128                                                  get_string('present', 'workshopform_accumulative')));
 129              break;
 130          case 2:
 131              $data['name']  = get_string('scalename2', 'workshopform_accumulative');
 132              $data['scale'] = implode(',', array(get_string('incorrect', 'workshopform_accumulative'),
 133                                                  get_string('correct', 'workshopform_accumulative')));
 134              break;
 135          case 3:
 136              $data['name']  = get_string('scalename3', 'workshopform_accumulative');
 137              $data['scale'] = implode(',', array('* ' . get_string('poor', 'workshopform_accumulative'),
 138                                                  '**',
 139                                                  '*** ' . get_string('good', 'workshopform_accumulative')));
 140              break;
 141          case 4:
 142              $data['name']  = get_string('scalename4', 'workshopform_accumulative');
 143              $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
 144                                                  '**',
 145                                                  '***',
 146                                                  '**** ' . get_string('excellent', 'workshopform_accumulative')));
 147              break;
 148          case 5:
 149              $data['name']  = get_string('scalename5', 'workshopform_accumulative');
 150              $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
 151                                                  '**',
 152                                                  '***',
 153                                                  '****',
 154                                                  '***** ' . get_string('excellent', 'workshopform_accumulative')));
 155              break;
 156          case 6:
 157              $data['name']  = get_string('scalename6', 'workshopform_accumulative');
 158              $data['scale'] = implode(',', array('* ' . get_string('verypoor', 'workshopform_accumulative'),
 159                                                  '**',
 160                                                  '***',
 161                                                  '****',
 162                                                  '*****',
 163                                                  '******',
 164                                                  '******* ' . get_string('excellent', 'workshopform_accumulative')));
 165              break;
 166          }
 167  
 168          return $data;
 169      }
 170  }
 171  
 172  /**
 173   * Transforms a given record from workshop_elements_old into an object to be saved into workshopform_accumulative
 174   *
 175   * @param stdClass $old legacy record from workshop_elements_old
 176   * @param array $newscaleids mapping from old scale types into new standard ones
 177   * @param int $newworkshopid id of the new workshop instance that replaced the previous one
 178   * @return stdclass to be saved in workshopform_accumulative
 179   */
 180  function workshopform_accumulative_upgrade_element(stdclass $old, array $newscaleids, $newworkshopid) {
 181      $new = new stdclass();
 182      $new->workshopid = $newworkshopid;
 183      $new->sort = $old->elementno;
 184      $new->description = $old->description;
 185      $new->descriptionformat = FORMAT_HTML;
 186      // calculate new grade/scale of the element
 187      if ($old->scale >= 0 and $old->scale <= 6 and isset($newscaleids[$old->scale])) {
 188          $new->grade = -$newscaleids[$old->scale];
 189      } elseif ($old->scale == 7) {
 190          $new->grade = 10;
 191      } elseif ($old->scale == 8) {
 192          $new->grade = 20;
 193      } elseif ($old->scale == 9) {
 194          $new->grade = 100;
 195      } else {
 196          $new->grade = 0;    // something is wrong
 197      }
 198      // calculate new weight of the element. Negative weights are not supported any more and
 199      // are replaced with weight = 0. Legacy workshop did not store the raw weight but the index
 200      // in the array of weights (see $WORKSHOP_EWEIGHTS in workshop 1.x)
 201      // workshop 2.0 uses integer weights only (0-16) so all previous weights are multiplied by 4.
 202      switch ($old->weight) {
 203          case 8: $new->weight = 1; break;
 204          case 9: $new->weight = 2; break;
 205          case 10: $new->weight = 3; break;
 206          case 11: $new->weight = 4; break;
 207          case 12: $new->weight = 6; break;
 208          case 13: $new->weight = 8; break;
 209          case 14: $new->weight = 16; break;
 210          default: $new->weight = 0;
 211      }
 212      return $new;
 213  }