PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/quiz/db/upgrade.php

https://github.com/manoj240375/moodle_edify
PHP | 377 lines | 228 code | 74 blank | 75 comment | 43 complexity | e0d39e10028a89025aae2b35994937f8 MD5 | raw file
  1. <?php
  2. // This file keeps track of upgrades to
  3. // the quiz module
  4. //
  5. // Sometimes, changes between versions involve
  6. // alterations to database structures and other
  7. // major things that may break installations.
  8. //
  9. // The upgrade function in this file will attempt
  10. // to perform all the necessary actions to upgrade
  11. // your older installation to the current version.
  12. //
  13. // If there's something it cannot do itself, it
  14. // will tell you what you need to do.
  15. //
  16. // The commands in here will all be database-neutral,
  17. // using the methods of database_manager class
  18. //
  19. // Please do not forget to use upgrade_set_timeout()
  20. // before any action that may take longer time to finish.
  21. function xmldb_quiz_upgrade($oldversion) {
  22. global $CFG, $DB;
  23. $dbman = $DB->get_manager();
  24. //===== 1.9.0 upgrade line ======//
  25. if ($oldversion < 2008062000) {
  26. /// Define table quiz_report to be created
  27. $table = new xmldb_table('quiz_report');
  28. /// Adding fields to table quiz_report
  29. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  30. $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
  31. $table->add_field('displayorder', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
  32. /// Adding keys to table quiz_report
  33. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  34. /// Conditionally launch create table for quiz_report
  35. if (!$dbman->table_exists($table)) {
  36. $dbman->create_table($table);
  37. }
  38. upgrade_mod_savepoint(true, 2008062000, 'quiz');
  39. }
  40. if ($oldversion < 2008062001) {
  41. $reporttoinsert = new stdClass();
  42. $reporttoinsert->name = 'overview';
  43. $reporttoinsert->displayorder = 10000;
  44. $DB->insert_record('quiz_report', $reporttoinsert);
  45. $reporttoinsert = new stdClass();
  46. $reporttoinsert->name = 'responses';
  47. $reporttoinsert->displayorder = 9000;
  48. $DB->insert_record('quiz_report', $reporttoinsert);
  49. $reporttoinsert = new stdClass();
  50. $reporttoinsert->name = 'regrade';
  51. $reporttoinsert->displayorder = 7000;
  52. $DB->insert_record('quiz_report', $reporttoinsert);
  53. $reporttoinsert = new stdClass();
  54. $reporttoinsert->name = 'grading';
  55. $reporttoinsert->displayorder = 6000;
  56. $DB->insert_record('quiz_report', $reporttoinsert);
  57. upgrade_mod_savepoint(true, 2008062001, 'quiz');
  58. }
  59. if ($oldversion < 2008072402) {
  60. /// Define field lastcron to be added to quiz_report
  61. $table = new xmldb_table('quiz_report');
  62. $field = new xmldb_field('lastcron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'displayorder');
  63. /// Conditionally launch add field lastcron
  64. if (!$dbman->field_exists($table, $field)) {
  65. $dbman->add_field($table, $field);
  66. }
  67. /// Define field cron to be added to quiz_report
  68. $field = new xmldb_field('cron', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'lastcron');
  69. /// Conditionally launch add field cron
  70. if (!$dbman->field_exists($table, $field)) {
  71. $dbman->add_field($table, $field);
  72. }
  73. /// quiz savepoint reached
  74. upgrade_mod_savepoint(true, 2008072402, 'quiz');
  75. }
  76. if ($oldversion < 2008072900) {
  77. /// Delete the regrade report - it is now part of the overview report.
  78. $DB->delete_records('quiz_report', array('name' => 'regrade'));
  79. /// quiz savepoint reached
  80. upgrade_mod_savepoint(true, 2008072900, 'quiz');
  81. }
  82. if ($oldversion < 2008081500) {
  83. /// Define table quiz_question_versions to be dropped
  84. $table = new xmldb_table('quiz_question_versions');
  85. /// Launch drop table for quiz_question_versions
  86. $dbman->drop_table($table);
  87. /// quiz savepoint reached
  88. upgrade_mod_savepoint(true, 2008081500, 'quiz');
  89. }
  90. /// Changing the type of all the columns that store grades to be NUMBER(10, 5) or similar.
  91. if ($oldversion < 2008081501) {
  92. $table = new xmldb_table('quiz');
  93. $field = new xmldb_field('sumgrades', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'questions');
  94. $dbman->change_field_type($table, $field);
  95. upgrade_mod_savepoint(true, 2008081501, 'quiz');
  96. }
  97. if ($oldversion < 2008081502) {
  98. $table = new xmldb_table('quiz');
  99. $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'sumgrades');
  100. $dbman->change_field_type($table, $field);
  101. upgrade_mod_savepoint(true, 2008081502, 'quiz');
  102. }
  103. if ($oldversion < 2008081503) {
  104. $table = new xmldb_table('quiz_attempts');
  105. $field = new xmldb_field('sumgrades', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'attempt');
  106. $dbman->change_field_type($table, $field);
  107. upgrade_mod_savepoint(true, 2008081503, 'quiz');
  108. }
  109. if ($oldversion < 2008081504) {
  110. $table = new xmldb_table('quiz_feedback');
  111. $field = new xmldb_field('mingrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'feedbacktext');
  112. $dbman->change_field_type($table, $field);
  113. upgrade_mod_savepoint(true, 2008081504, 'quiz');
  114. }
  115. if ($oldversion < 2008081505) {
  116. $table = new xmldb_table('quiz_feedback');
  117. $field = new xmldb_field('maxgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'mingrade');
  118. $dbman->change_field_type($table, $field);
  119. upgrade_mod_savepoint(true, 2008081505, 'quiz');
  120. }
  121. if ($oldversion < 2008081506) {
  122. $table = new xmldb_table('quiz_grades');
  123. $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'userid');
  124. $dbman->change_field_type($table, $field);
  125. upgrade_mod_savepoint(true, 2008081506, 'quiz');
  126. }
  127. if ($oldversion < 2008081507) {
  128. $table = new xmldb_table('quiz_question_instances');
  129. $field = new xmldb_field('grade', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null, 'question');
  130. $dbman->change_field_type($table, $field);
  131. upgrade_mod_savepoint(true, 2008081507, 'quiz');
  132. }
  133. /// Move all of the quiz config settings from $CFG to the config_plugins table.
  134. if ($oldversion < 2008082200) {
  135. foreach (get_object_vars($CFG) as $name => $value) {
  136. if (strpos($name, 'quiz_') === 0) {
  137. $shortname = substr($name, 5);
  138. if ($shortname == 'fix_adaptive') {
  139. // Special case - remove old inconsistency.
  140. $shortname == 'fix_optionflags';
  141. }
  142. set_config($shortname, $value, 'quiz');
  143. unset_config($name);
  144. }
  145. }
  146. upgrade_mod_savepoint(true, 2008082200, 'quiz');
  147. }
  148. /// Now that the quiz is no longer responsible for creating all the question
  149. /// bank tables, and some of the tables are now the responsibility of the
  150. /// datasetdependent question type, which did not have a version.php file before,
  151. /// we need to say that these tables are already installed, otherwise XMLDB
  152. /// will try to create them again and give an error.
  153. if ($oldversion < 2008082600) {
  154. // Since MDL-16505 was fixed, and we eliminated the datasetdependent
  155. // question type, this is now a no-op.
  156. upgrade_mod_savepoint(true, 2008082600, 'quiz');
  157. }
  158. if ($oldversion < 2008112101) {
  159. /// Define field lastcron to be added to quiz_report
  160. $table = new xmldb_table('quiz_report');
  161. $field = new xmldb_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'cron');
  162. /// Conditionally launch add field lastcron
  163. if (!$dbman->field_exists($table, $field)) {
  164. $dbman->add_field($table, $field);
  165. }
  166. /// quiz savepoint reached
  167. upgrade_mod_savepoint(true, 2008112101, 'quiz');
  168. }
  169. if ($oldversion < 2009010700) {
  170. /// Define field showuserpicture to be added to quiz
  171. $table = new xmldb_table('quiz');
  172. $field = new xmldb_field('showuserpicture', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'delay2');
  173. /// Conditionally launch add field showuserpicture
  174. if (!$dbman->field_exists($table, $field)) {
  175. $dbman->add_field($table, $field);
  176. }
  177. /// quiz savepoint reached
  178. upgrade_mod_savepoint(true, 2009010700, 'quiz');
  179. }
  180. if ($oldversion < 2009030900) {
  181. /// If there are no quiz settings set to advanced yet, the set up the default
  182. /// advanced fields from Moodle 2.0.
  183. $quizconfig = get_config('quiz');
  184. $arealreadyadvanced = false;
  185. foreach (array($quizconfig) as $name => $value) {
  186. if (strpos($name, 'fix_') === 0 && !empty($value)) {
  187. $arealreadyadvanced = true;
  188. break;
  189. }
  190. }
  191. if (!$arealreadyadvanced) {
  192. set_config('fix_penaltyscheme', 1, 'quiz');
  193. set_config('fix_attemptonlast', 1, 'quiz');
  194. set_config('fix_questiondecimalpoints', 1, 'quiz');
  195. set_config('fix_password', 1, 'quiz');
  196. set_config('fix_subnet', 1, 'quiz');
  197. set_config('fix_delay1', 1, 'quiz');
  198. set_config('fix_delay2', 1, 'quiz');
  199. set_config('fix_popup', 1, 'quiz');
  200. }
  201. /// quiz savepoint reached
  202. upgrade_mod_savepoint(true, 2009030900, 'quiz');
  203. }
  204. if ($oldversion < 2009031000) {
  205. /// Add new questiondecimaldigits setting, separate form the overall decimaldigits one.
  206. $table = new xmldb_table('quiz');
  207. $field = new xmldb_field('questiondecimalpoints', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '2', 'decimalpoints');
  208. if (!$dbman->field_exists($table, $field)) {
  209. $dbman->add_field($table, $field);
  210. }
  211. /// quiz savepoint reached
  212. upgrade_mod_savepoint(true, 2009031000, 'quiz');
  213. }
  214. if ($oldversion < 2009031001) {
  215. /// Convert quiz.timelimit from minutes to seconds.
  216. $DB->execute('UPDATE {quiz} SET timelimit = timelimit * 60');
  217. $default = get_config('quiz', 'timelimit');
  218. set_config('timelimit', 60 * $default, 'quiz');
  219. /// quiz savepoint reached
  220. upgrade_mod_savepoint(true, 2009031001, 'quiz');
  221. }
  222. if ($oldversion < 2009042000) {
  223. /// Define field introformat to be added to quiz
  224. $table = new xmldb_table('quiz');
  225. $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
  226. if (!$dbman->field_exists($table, $field)) {
  227. $dbman->add_field($table, $field);
  228. }
  229. // conditionally migrate to html format in intro
  230. if ($CFG->texteditors !== 'textarea') {
  231. $rs = $DB->get_recordset('quiz', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
  232. foreach ($rs as $q) {
  233. $q->intro = text_to_html($q->intro, false, false, true);
  234. $q->introformat = FORMAT_HTML;
  235. $DB->update_record('quiz', $q);
  236. upgrade_set_timeout();
  237. }
  238. $rs->close();
  239. }
  240. /// quiz savepoint reached
  241. upgrade_mod_savepoint(true, 2009042000, 'quiz');
  242. }
  243. if ($oldversion < 2010030501) {
  244. /// Define table quiz_overrides to be created
  245. $table = new xmldb_table('quiz_overrides');
  246. /// Adding fields to table quiz_overrides
  247. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  248. $table->add_field('quiz', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
  249. $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
  250. $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
  251. $table->add_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
  252. $table->add_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
  253. $table->add_field('timelimit', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
  254. $table->add_field('attempts', XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, null, null, null);
  255. $table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
  256. /// Adding keys to table quiz_overrides
  257. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  258. $table->add_key('quiz', XMLDB_KEY_FOREIGN, array('quiz'), 'quiz', array('id'));
  259. $table->add_key('groupid', XMLDB_KEY_FOREIGN, array('groupid'), 'groups', array('id'));
  260. $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
  261. /// Conditionally launch create table for quiz_overrides
  262. if (!$dbman->table_exists($table)) {
  263. $dbman->create_table($table);
  264. }
  265. /// quiz savepoint reached
  266. upgrade_mod_savepoint(true, 2010030501, 'quiz');
  267. }
  268. if ($oldversion < 2010051800) {
  269. // Define field showblocks to be added to quiz
  270. $table = new xmldb_table('quiz');
  271. $field = new xmldb_field('showblocks', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showuserpicture');
  272. // Conditionally launch add field showblocks
  273. if (!$dbman->field_exists($table, $field)) {
  274. $dbman->add_field($table, $field);
  275. }
  276. // quiz savepoint reached
  277. upgrade_mod_savepoint(true, 2010051800, 'quiz');
  278. }
  279. if ($oldversion < 2010080600) {
  280. // Define field feedbacktextformat to be added to quiz_feedback
  281. $table = new xmldb_table('quiz_feedback');
  282. $field = new xmldb_field('feedbacktextformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'feedbacktext');
  283. // Conditionally launch add field feedbacktextformat
  284. if (!$dbman->field_exists($table, $field)) {
  285. $dbman->add_field($table, $field);
  286. }
  287. // quiz savepoint reached
  288. upgrade_mod_savepoint(true, 2010080600, 'quiz');
  289. }
  290. if ($oldversion < 2010102000) {
  291. // Define field showblocks to be added to quiz
  292. // Repeat this step, because the column was missing from install.xml for a time.
  293. $table = new xmldb_table('quiz');
  294. $field = new xmldb_field('showblocks', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'showuserpicture');
  295. // Conditionally launch add field showblocks
  296. if (!$dbman->field_exists($table, $field)) {
  297. $dbman->add_field($table, $field);
  298. }
  299. // quiz savepoint reached
  300. upgrade_mod_savepoint(true, 2010102000, 'quiz');
  301. }
  302. return true;
  303. }