MDL-25031 fixed question ugprade - credit goes to Mark Nelson
[moodle.git] / question / type / numerical / db / upgrade.php
CommitLineData
aeb15530 1<?php
dfdd5332 2
aeb15530 3// This file keeps track of upgrades to
dfdd5332 4// the numerical qtype plugin
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
2e0406a5 12// your older installation to the current version.
dfdd5332 13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
dfdd5332 22
775f811a 23function xmldb_qtype_numerical_upgrade($oldversion) {
24 global $CFG, $DB;
dfdd5332 25
775f811a 26 $dbman = $DB->get_manager();
dfdd5332 27
67516980 28//===== 1.9.0 upgrade line ======//
a4cdd6d2 29 if ($oldversion < 2009100100 ) { //New version in version.php
b003b818 30
31 /// Define table question_numerical_options to be created
32 $table = new xmldb_table('question_numerical_options');
33
34 /// Adding fields to table question_numerical_options
35 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
36 $table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
37 $table->add_field('instructions', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
38 $table->add_field('showunits', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
39 $table->add_field('unitsleft', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
40 $table->add_field('unitgradingtype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
41 $table->add_field('unitpenalty', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0.1');
42
43 /// Adding keys to table question_numerical_options
44 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
45 $table->add_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id'));
46 /// Conditionally launch create table for question_calculated_options
47 if (!$dbman->table_exists($table)) {
48 // $dbman->create_table doesnt return a result, we just have to trust it
49 $dbman->create_table($table);
aeb15530 50 }//else
a4cdd6d2 51 upgrade_plugin_savepoint(true, 2009100100, 'qtype', 'numerical');
b003b818 52 }
dfdd5332 53
fe6ce234
DC
54 if ($oldversion < 2009100101) {
55
56 // Define field instructionsformat to be added to question_numerical_options
57 $table = new xmldb_table('question_numerical_options');
58 $field = new xmldb_field('instructionsformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'instructions');
59
60 // Conditionally launch add field instructionsformat
61 if (!$dbman->field_exists($table, $field)) {
62 $dbman->add_field($table, $field);
63 }
64
a9efae50
TH
65 // In the past, question_match_sub.questiontext assumed to contain
66 // content of the same form as question.questiontextformat. If we are
67 // using the HTML editor, then convert FORMAT_MOODLE content to FORMAT_HTML.
68 $rs = $DB->get_recordset_sql('
69 SELECT qno.*, q.oldquestiontextformat
70 FROM {question_numerical_options} qno
785d2cf2 71 JOIN {question} q ON qno.question = q.id');
fe6ce234 72 foreach ($rs as $record) {
a9efae50
TH
73 if ($CFG->texteditors !== 'textarea' && $record->oldquestiontextformat == FORMAT_MOODLE) {
74 $record->instructions = text_to_html($record->questiontext, false, false, true);
fe6ce234
DC
75 $record->instructionsformat = FORMAT_HTML;
76 } else {
a9efae50 77 $record->instructionsformat = $record->oldquestiontextformat;
fe6ce234
DC
78 }
79 $DB->update_record('question_numerical_options', $record);
80 }
81 $rs->close();
82
83 // numerical savepoint reached
84 upgrade_plugin_savepoint(true, 2009100101, 'qtype', 'numerical');
85 }
86
a4cdd6d2 87 return true;
dfdd5332 88}
89
aeb15530 90