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

/mod/lesson/db/upgrade.php

https://github.com/kpike/moodle
PHP | 298 lines | 160 code | 46 blank | 92 comment | 32 complexity | 4c4966ca179863d92f13ccd7b051fd62 MD5 | raw file
  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. * This file keeps track of upgrades to
  18. * the lesson module
  19. *
  20. * Sometimes, changes between versions involve
  21. * alterations to database structures and other
  22. * major things that may break installations.
  23. *
  24. * The upgrade function in this file will attempt
  25. * to perform all the necessary actions to upgrade
  26. * your older installation to the current version.
  27. *
  28. * If there's something it cannot do itself, it
  29. * will tell you what you need to do.
  30. *
  31. * The commands in here will all be database-neutral,
  32. * using the methods of database_manager class
  33. *
  34. * Please do not forget to use upgrade_set_timeout()
  35. * before any action that may take longer time to finish.
  36. *
  37. * @package mod
  38. * @subpackage lesson
  39. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  40. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 o
  41. */
  42. defined('MOODLE_INTERNAL') || die();
  43. /**
  44. *
  45. * @global stdClass $CFG
  46. * @global moodle_database $DB
  47. * @global core_renderer $OUTPUT
  48. * @param int $oldversion
  49. * @return bool
  50. */
  51. function xmldb_lesson_upgrade($oldversion) {
  52. global $CFG, $DB, $OUTPUT;
  53. $dbman = $DB->get_manager();
  54. //===== 1.9.0 upgrade line ======//
  55. if ($oldversion < 2007072201) {
  56. $table = new xmldb_table('lesson');
  57. $field = new xmldb_field('usegrademax');
  58. $field2 = new xmldb_field('usemaxgrade');
  59. /// Rename lesson->usegrademax to lesson->usemaxgrade. Some old sites can have it incorrect. MDL-13177
  60. if ($dbman->field_exists($table, $field) && !$dbman->field_exists($table, $field2)) {
  61. /// Set field specs
  62. $field->set_attributes(XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'ongoing');
  63. /// Launch rename field usegrademax to usemaxgrade
  64. $dbman->rename_field($table, $field, 'usemaxgrade');
  65. }
  66. upgrade_mod_savepoint(true, 2007072201, 'lesson');
  67. }
  68. if ($oldversion < 2008112601) {
  69. //NOTE: this is a hack, we can not call module lib.php in the middle of upgrade, the necessary db structures/data may not exist yet!
  70. require_once($CFG->dirroot.'/mod/lesson/lib.php');
  71. lesson_upgrade_grades();
  72. upgrade_mod_savepoint(true, 2008112601, 'lesson');
  73. }
  74. if ($oldversion < 2009111600) {
  75. /**
  76. * Change the grade field within lesson_answers to an unsigned int and increment
  77. * the length by one to ensure that no values are changed (reduced)
  78. */
  79. $table = new xmldb_table('lesson_answers');
  80. $field = new xmldb_field('grade');
  81. $field->set_attributes(XMLDB_TYPE_INTEGER, '4', false, XMLDB_NOTNULL, null, '0', 'jumpto');
  82. $dbman->change_field_type($table, $field);
  83. upgrade_mod_savepoint(true, 2009111600, 'lesson');
  84. }
  85. if ($oldversion < 2009120800) {
  86. /**
  87. * Drop the lesson_default table, as of Moodle 2.0 it is no longer used
  88. * the module now has a settings.php instead
  89. */
  90. $table = new xmldb_table('lesson_default');
  91. $dbman->drop_table($table);
  92. upgrade_mod_savepoint(true, 2009120800, 'lesson');
  93. }
  94. if ($oldversion < 2009120801) {
  95. /// Define field contentsformat to be added to lesson_pages
  96. $table = new xmldb_table('lesson_pages');
  97. $field = new xmldb_field('contentsformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, FORMAT_MOODLE, 'contents');
  98. /// Conditionally launch add field contentsformat
  99. if (!$dbman->field_exists($table, $field)) {
  100. $dbman->add_field($table, $field);
  101. }
  102. // conditionally migrate to html format in contents
  103. if ($CFG->texteditors !== 'textarea') {
  104. $rs = $DB->get_recordset('lesson_pages', array('contentsformat'=>FORMAT_MOODLE), '', 'id,contents,contentsformat');
  105. foreach ($rs as $lp) {
  106. $lp->contents = text_to_html($lp->contents, false, false, true);
  107. $lp->contentsformat = FORMAT_HTML;
  108. $DB->update_record('lesson_pages', $lp);
  109. upgrade_set_timeout();
  110. }
  111. $rs->close();
  112. }
  113. /// lesson savepoint reached
  114. upgrade_mod_savepoint(true, 2009120801, 'lesson');
  115. }
  116. if ($oldversion < 2010072000) {
  117. // Define field answerformat to be added to lesson_answers
  118. $table = new xmldb_table('lesson_answers');
  119. $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'answer');
  120. // Launch add field answerformat
  121. $dbman->add_field($table, $field);
  122. // lesson savepoint reached
  123. upgrade_mod_savepoint(true, 2010072000, 'lesson');
  124. }
  125. if ($oldversion < 2010072001) {
  126. // Define field responseformat to be added to lesson_answers
  127. $table = new xmldb_table('lesson_answers');
  128. $field = new xmldb_field('responseformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'response');
  129. // Launch add field responseformat
  130. $dbman->add_field($table, $field);
  131. // lesson savepoint reached
  132. upgrade_mod_savepoint(true, 2010072001, 'lesson');
  133. }
  134. if ($oldversion < 2010072003) {
  135. $rs = $DB->get_recordset('lesson_answers', array());
  136. foreach ($rs as $answer) {
  137. $flags = intval($answer->flags);
  138. $update = false;
  139. if ($flags & 1) {
  140. $answer->answer = text_to_html($answer->answer, false, false, true);
  141. $answer->answerformat = FORMAT_HTML;
  142. $update = true;
  143. }
  144. if ($flags & 2) {
  145. $answer->response = text_to_html($answer->response, false, false, true);
  146. $answer->responseformat = FORMAT_HTML;
  147. $update = true;
  148. }
  149. if ($update) {
  150. $DB->update_record('lesson_answers', $answer);
  151. }
  152. }
  153. $rs->close();
  154. upgrade_mod_savepoint(true, 2010072003, 'lesson');
  155. }
  156. if ($oldversion < 2010081200) {
  157. require_once("$CFG->dirroot/mod/lesson/db/upgradelib.php");
  158. $sqlfrom = "FROM {lesson} l
  159. JOIN {modules} m ON m.name = 'lesson'
  160. JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = l.id)";
  161. $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
  162. if ($count > 0) {
  163. $rs = $DB->get_recordset_sql("SELECT l.id, l.mediafile, l.course, cm.id AS cmid $sqlfrom ORDER BY l.course, l.id");
  164. $pbar = new progress_bar('migratelessonfiles', 500, true);
  165. $fs = get_file_storage();
  166. $i = 0;
  167. foreach ($rs as $lesson) {
  168. $i++;
  169. upgrade_set_timeout(120); // set up timeout, may also abort execution
  170. $pbar->update($i, $count, "Migrating lesson files - $i/$count.");
  171. // fix images incorrectly placed in moddata subfolders,
  172. // this was a really bad decision done by the ppt import developer
  173. if (file_exists("$CFG->dataroot/$lesson->course/moddata/lesson/")) {
  174. lesson_20_migrate_moddata_mixture($lesson->course, '/moddata/lesson/');
  175. @rmdir("$CFG->dataroot/$lesson->course/moddata/lesson/"); // remove dir if empty
  176. @rmdir("$CFG->dataroot/$lesson->course/moddata/"); // remove dir if empty
  177. }
  178. // migrate media file only if local course file selected - this is not nice at all,
  179. // it should better be a real block, not a lesson specific hack
  180. if (strpos($lesson->mediafile, '://') !== false) {
  181. // some external URL
  182. } else if ($lesson->mediafile) {
  183. $context = get_context_instance(CONTEXT_MODULE, $lesson->cmid);
  184. $coursecontext = get_context_instance(CONTEXT_COURSE, $lesson->course);
  185. $filepathname = clean_param('/'.$lesson->mediafile, PARAM_PATH);
  186. $fullpath = "/$context->id/mod_lesson/mediafile/0$filepathname";
  187. if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
  188. // already converted, just update filename
  189. $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
  190. } else {
  191. // let's copy file from current course legacy files if possible
  192. $fullpath = "/$coursecontext->id/course/legacy/0$filepathname";
  193. if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
  194. $file_record = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'mediafile', 'itemid'=>0, 'filepath'=>$file->get_filepath(), 'filename'=>$file->get_filename(), 'sortorder'=>1);
  195. $fs->create_file_from_storedfile($file_record, $file);
  196. $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
  197. } else {
  198. // bad luck, no such file exists
  199. $DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
  200. }
  201. }
  202. }
  203. }
  204. $rs->close();
  205. }
  206. upgrade_mod_savepoint(true, 2010081200, 'lesson');
  207. }
  208. if ($oldversion < 2010121400) {
  209. // Fix matching question pages.
  210. // In Moodle 1.9 matching question pages stored the correct and incorrect
  211. // jumps on the third and forth answers, in Moodle 2.0 they are stored
  212. // in the first and second answers.
  213. // This upgrade block detects matching questions where this is the case
  214. // and fixed it by making firstjump = thirdjump && secondjump = forthjump.
  215. $pages = $DB->get_recordset('lesson_pages', array('qtype'=>'5'));
  216. foreach ($pages as $page) {
  217. $answers = $DB->get_records('lesson_answers', array('pageid'=>$page->id), 'id', 'id, jumpto', 0, 4);
  218. if (count($answers) < 4) {
  219. // If there are less then four answers the problem wont exist.
  220. // All Moodle 1.9 matching questions had a least 4 answers.
  221. continue;
  222. }
  223. $first = array_shift($answers);
  224. $second = array_shift($answers);
  225. $third = array_shift($answers);
  226. $forth = array_shift($answers);
  227. if ($first->jumpto !== '0' || $second->jumpto !== '0') {
  228. // If either are set to something other than the next page then
  229. // there is no problem.
  230. continue;
  231. }
  232. if ($third->jumpto !== '0') {
  233. $first->jumpto = $third->jumpto;
  234. $DB->update_record('lesson_answers', $first);
  235. $third->jumpto = '0';
  236. $DB->update_record('lesson_answers', $third);
  237. }
  238. if ($forth->jumpto !== '0') {
  239. $second->jumpto = $forth->jumpto;
  240. $DB->update_record('lesson_answers', $second);
  241. $forth->jumpto = '0';
  242. $DB->update_record('lesson_answers', $forth);
  243. }
  244. }
  245. // Close the record set
  246. $pages->close();
  247. upgrade_mod_savepoint(true, 2010121400, 'lesson');
  248. }
  249. // Moodle v2.1.0 release upgrade line
  250. // Put any upgrade step following this
  251. return true;
  252. }