[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/multianswer/db/ -> upgrade.php (source)

   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   * Multi-answer question type upgrade code.
  19   *
  20   * @package    qtype
  21   * @subpackage multianswer
  22   * @copyright  1999 onwards Martin Dougiamas {@link http://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   * Upgrade code for the multi-answer question type.
  30   * @param int $oldversion the version we are upgrading from.
  31   */
  32  function xmldb_qtype_multianswer_upgrade($oldversion) {
  33      global $CFG, $DB;
  34  
  35      $dbman = $DB->get_manager();
  36  
  37      // Moodle v2.8.0 release upgrade line.
  38      // Put any upgrade step following this.
  39  
  40      // Moodle v2.9.0 release upgrade line.
  41      // Put any upgrade step following this.
  42  
  43      if ($oldversion < 2015100201) {
  44  
  45          // Upgrade steps need to be isolated from our APIs, so we should prevent using constants that
  46          // can change over time. This is a simple copy & paste from question/type/multianswer/questiontype.php
  47          // with the following changes, the idea is to prevent regressions as much as possible.
  48          // * All constants prefixed with UPGRADE_2015100201_
  49          // * Removed all constants not required by UPGRADE_2015100201_ANSWER_REGEX
  50          if (!defined('UPGRADE_2015100201_ANSWER_REGEX')) {
  51              define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_FRACTION_REGEX',
  52                     '=|%(-?[0-9]+)%');
  53              define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_ANSWER_REGEX',
  54                      '.+?(?<!\\\\|&|&amp;)(?=[~#}]|$)');
  55              define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_FEEDBACK_REGEX',
  56                      '.*?(?<!\\\\)(?=[~}]|$)');
  57              define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX',
  58                     '(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_FRACTION_REGEX .')?' .
  59                     '(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_ANSWER_REGEX . ')' .
  60                     '(#(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_FEEDBACK_REGEX .'))?');
  61  
  62              // Remaining ANSWER regexes.
  63              define('UPGRADE_2015100201_ANSWER_TYPE_DEF_REGEX',
  64                      '(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|' .
  65                      '(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)|' .
  66                      '(MULTICHOICE_S|MCS)|(MULTICHOICE_VS|MCVS)|(MULTICHOICE_HS|MCHS)');
  67              define('UPGRADE_2015100201_ANSWER_START_REGEX',
  68                     '\{([0-9]*):(' . UPGRADE_2015100201_ANSWER_TYPE_DEF_REGEX . '):');
  69  
  70              define('UPGRADE_2015100201_ANSWER_REGEX',
  71                      UPGRADE_2015100201_ANSWER_START_REGEX
  72                      . '(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX
  73                      . '(~'
  74                      . UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX
  75                      . ')*)\}');
  76  
  77              define('UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE', 4);
  78          }
  79  
  80          // Detect the exact table/field we want to use, coz can be different
  81          // depending of the version we are upgrading from. See MDL-52291 and MDL-52298.
  82          $multichoicetable = 'qtype_multichoice_options';
  83          $multichoicefield = 'questionid';
  84          if (!$dbman->table_exists($multichoicetable)) {
  85              // Multichoice not upgraded yet, let's use old names.
  86              $multichoicetable = 'question_multichoice';
  87              $multichoicefield = 'question';
  88          }
  89          $rs = $DB->get_recordset_sql("SELECT q.id, q.category, qma.sequence
  90                   FROM {question} q
  91                   JOIN {question_multianswer} qma ON q.id = qma.question");
  92          foreach ($rs as $q) {
  93              $sequence = preg_split('/,/', $q->sequence, -1, PREG_SPLIT_NO_EMPTY);
  94              if ($sequence) {
  95                  // Get relevant data indexed by positionkey from the multianswers table.
  96                  $wrappedquestions = $DB->get_records_list('question', 'id', $sequence, 'id ASC');
  97                  foreach ($wrappedquestions as $wrapped) {
  98                      if ($wrapped->qtype == 'multichoice') {
  99                          $options = $DB->get_record($multichoicetable, array($multichoicefield => $wrapped->id), '*');
 100                          if (isset($options->shuffleanswers)) {
 101                              preg_match('/'.UPGRADE_2015100201_ANSWER_REGEX.'/s', $wrapped->questiontext, $answerregs);
 102                              if (isset($answerregs[UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
 103                                      $answerregs[UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
 104                                  $DB->set_field($multichoicetable, 'shuffleanswers', '0',
 105                                          array('id' => $options->id) );
 106                              }
 107                          } else {
 108                              $newrecord = new stdClass();
 109                              $newrecord->$multichoicefield = $wrapped->id;
 110                              $newrecord->correctfeedback = '';
 111                              $newrecord->partiallycorrectfeedback = '';
 112                              $newrecord->incorrectfeedback = '';
 113                              $DB->insert_record($multichoicetable, $newrecord);
 114                          }
 115                      }
 116                  }
 117              }
 118          }
 119          $rs->close();
 120          // Multianswer savepoint reached.
 121          upgrade_plugin_savepoint(true, 2015100201, 'qtype', 'multianswer');
 122      }
 123  
 124      // Moodle v3.0.0 release upgrade line.
 125      // Put any upgrade step following this.
 126  
 127      // Moodle v3.1.0 release upgrade line.
 128      // Put any upgrade step following this.
 129  
 130      return true;
 131  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1