PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/scorm/db/upgrade.php

https://github.com/totara/moodle
PHP | 324 lines | 195 code | 52 blank | 77 comment | 35 complexity | 8423538e3dd827896b2fc7279a4c447f 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. * Upgrade script for the scorm module.
  18. *
  19. * @package mod_scorm
  20. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * @global moodle_database $DB
  25. * @param int $oldversion
  26. * @return bool
  27. */
  28. function xmldb_scorm_upgrade($oldversion) {
  29. global $CFG, $DB;
  30. $dbman = $DB->get_manager();
  31. // Moodle v2.2.0 release upgrade line
  32. // Put any upgrade step following this
  33. if ($oldversion < 2012032100) {
  34. unset_config('updatetime', 'scorm');
  35. upgrade_mod_savepoint(true, 2012032100, 'scorm');
  36. }
  37. // Adding completion fields to scorm table
  38. if ($oldversion < 2012032101) {
  39. $table = new xmldb_table('scorm');
  40. $field = new xmldb_field('completionstatusrequired', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'timemodified');
  41. if (!$dbman->field_exists($table, $field)) {
  42. $dbman->add_field($table, $field);
  43. }
  44. $field = new xmldb_field('completionscorerequired', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null, 'completionstatusrequired');
  45. if (!$dbman->field_exists($table, $field)) {
  46. $dbman->add_field($table, $field);
  47. }
  48. upgrade_mod_savepoint(true, 2012032101, 'scorm');
  49. }
  50. // Moodle v2.3.0 release upgrade line
  51. // Put any upgrade step following this
  52. //rename config var from maxattempts to maxattempt
  53. if ($oldversion < 2012061701) {
  54. $maxattempts = get_config('scorm', 'maxattempts');
  55. $maxattempts_adv = get_config('scorm', 'maxattempts_adv');
  56. set_config('maxattempt', $maxattempts, 'scorm');
  57. set_config('maxattempt_adv', $maxattempts_adv, 'scorm');
  58. unset_config('maxattempts', 'scorm'); //remove old setting.
  59. unset_config('maxattempts_adv', 'scorm'); //remove old setting.
  60. upgrade_mod_savepoint(true, 2012061701, 'scorm');
  61. }
  62. // Moodle v2.4.0 release upgrade line
  63. // Put any upgrade step following this.
  64. // Moodle v2.5.0 release upgrade line.
  65. // Put any upgrade step following this.
  66. // Remove old imsrepository type - convert any existing records to external type to help prevent major errors.
  67. if ($oldversion < 2013081301) {
  68. require_once($CFG->dirroot . '/mod/scorm/lib.php');
  69. $scorms = $DB->get_recordset('scorm', array('scormtype' => 'imsrepository'));
  70. foreach ($scorms as $scorm) {
  71. $scorm->scormtype = SCORM_TYPE_EXTERNAL;
  72. if (!empty($CFG->repository)) { // Fix path to imsmanifest if $CFG->repository is set.
  73. $scorm->reference = $CFG->repository.substr($scorm->reference, 1).'/imsmanifest.xml';
  74. $scorm->sha1hash = sha1($scorm->reference);
  75. }
  76. $scorm->revision++;
  77. $DB->update_record('scorm', $scorm);
  78. }
  79. upgrade_mod_savepoint(true, 2013081301, 'scorm');
  80. }
  81. // Fix AICC parent/child relationships (MDL-37394).
  82. if ($oldversion < 2013081302) {
  83. // Get all AICC packages.
  84. $aiccpackages = $DB->get_recordset('scorm', array('version' => 'AICC'), '', 'id');
  85. foreach ($aiccpackages as $aicc) {
  86. $sql = "UPDATE {scorm_scoes}
  87. SET parent = organization
  88. WHERE scorm = ?
  89. AND " . $DB->sql_isempty('scorm_scoes', 'manifest', false, false) . "
  90. AND " . $DB->sql_isnotempty('scorm_scoes', 'organization', false, false) . "
  91. AND parent = '/'";
  92. $DB->execute($sql, array($aicc->id));
  93. }
  94. $aiccpackages->close();
  95. upgrade_mod_savepoint(true, 2013081302, 'scorm');
  96. }
  97. if ($oldversion < 2013081303) {
  98. // Define field sortorder to be added to scorm_scoes.
  99. $table = new xmldb_table('scorm_scoes');
  100. $field = new xmldb_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'title');
  101. // Conditionally launch add field sortorder.
  102. if (!$dbman->field_exists($table, $field)) {
  103. $dbman->add_field($table, $field);
  104. }
  105. // Scorm savepoint reached.
  106. upgrade_mod_savepoint(true, 2013081303, 'scorm');
  107. }
  108. if ($oldversion < 2013090100) {
  109. global $CFG;
  110. $table = new xmldb_table('scorm');
  111. $field = new xmldb_field('nav', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, true, null, 1, 'hidetoc');
  112. if (!$dbman->field_exists($table, $field)) {
  113. $dbman->add_field($table, $field);
  114. }
  115. $field = new xmldb_field('navpositionleft', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'nav');
  116. if (!$dbman->field_exists($table, $field)) {
  117. $dbman->add_field($table, $field);
  118. }
  119. $field = new xmldb_field('navpositiontop', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, -100, 'navpositionleft');
  120. if (!$dbman->field_exists($table, $field)) {
  121. $dbman->add_field($table, $field);
  122. }
  123. $field = new xmldb_field('hidenav');
  124. if ($dbman->field_exists($table, $field)) {
  125. // Update nav setting to show floating navigation buttons under TOC.
  126. $DB->set_field('scorm', 'nav', 2, array('hidenav' => 0));
  127. $DB->set_field('scorm', 'navpositionleft', 215, array('hidenav' => 2));
  128. $DB->set_field('scorm', 'navpositiontop', 300, array('hidenav' => 2));
  129. // Update nav setting to disable navigation buttons.
  130. $DB->set_field('scorm', 'nav', 0, array('hidenav' => 1));
  131. // Drop hidenav field.
  132. $dbman->drop_field($table, $field);
  133. }
  134. $hide = get_config('scorm', 'hidenav');
  135. unset_config('hidenav', 'scorm');
  136. if (!empty($hide)) {
  137. require_once($CFG->dirroot . '/mod/scorm/lib.php');
  138. set_config('nav', SCORM_NAV_DISABLED, 'scorm');
  139. }
  140. $hideadv = get_config('scorm', 'hidenav_adv');
  141. unset_config('hidenav_adv', 'scorm');
  142. set_config('nav_adv', $hideadv, 'scorm');
  143. upgrade_mod_savepoint(true, 2013090100, 'scorm');
  144. }
  145. // Moodle v2.6.0 release upgrade line.
  146. // Put any upgrade step following this.
  147. if ($oldversion < 2013110501) {
  148. // Fix invalid $scorm->launch records.
  149. // Get all scorms that have a launch value that references a sco from a different scorm.
  150. $sql = "SELECT s.*
  151. FROM {scorm} s
  152. LEFT JOIN {scorm_scoes} c ON s.launch = c.id
  153. WHERE c.id IS null OR s.id <> c.scorm";
  154. $scorms = $DB->get_recordset_sql($sql);
  155. foreach ($scorms as $scorm) {
  156. // Find the first launchable sco for this SCORM.
  157. // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
  158. $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
  159. // We use get_records here as we need to pass a limit in the query that works cross db.
  160. $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
  161. if (!empty($scoes)) {
  162. $sco = reset($scoes); // We only care about the first record - the above query only returns one.
  163. $scorm->launch = $sco->id;
  164. $DB->update_record('scorm', $scorm);
  165. }
  166. }
  167. $scorms->close();
  168. upgrade_mod_savepoint(true, 2013110501, 'scorm');
  169. }
  170. if ($oldversion < 2014031700) {
  171. // Define field displayactivityname to be added to scorm.
  172. $table = new xmldb_table('scorm');
  173. $field = new xmldb_field(
  174. 'displayactivityname',
  175. XMLDB_TYPE_INTEGER,
  176. '4',
  177. null,
  178. XMLDB_NOTNULL,
  179. null,
  180. '1',
  181. 'completionscorerequired'
  182. );
  183. // Conditionally launch add field displayactivityname.
  184. if (!$dbman->field_exists($table, $field)) {
  185. $dbman->add_field($table, $field);
  186. }
  187. // Scorm savepoint reached.
  188. upgrade_mod_savepoint(true, 2014031700, 'scorm');
  189. }
  190. if ($oldversion < 2014040200) {
  191. // Fix invalid $scorm->launch records that launch an org sco instead of a real sco.
  192. $sql = "SELECT s.*, c.identifier
  193. FROM {scorm} s
  194. LEFT JOIN {scorm_scoes} c ON s.launch = c.id
  195. WHERE ".$DB->sql_isempty('scorm_scoes', 'c.launch', false, true);
  196. $scorms = $DB->get_recordset_sql($sql);
  197. foreach ($scorms as $scorm) {
  198. upgrade_set_timeout(60); // Increase execution time just in case. (60 sec is minimum but prob excessive here).
  199. $originallaunch = $scorm->launch;
  200. // Find the first sco using the current identifier as it's parent
  201. // we use get records here as we need to pass a limit in the query that works cross db.
  202. $firstsco = $DB->get_records('scorm_scoes',
  203. array('scorm' => $scorm->id, 'parent' => $scorm->identifier), 'sortorder', '*', 0, 1);
  204. if (!empty($firstsco)) {
  205. $firstsco = reset($firstsco);
  206. }
  207. if (!empty($firstsco->launch)) {
  208. // Usual behavior - this is a valid sco with a launch param so use it.
  209. $scorm->launch = $firstsco->id;
  210. } else {
  211. // The firstsco found is not launchable - find the first launchable sco after this sco.
  212. $sqlselect = 'scorm = ? AND sortorder > ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
  213. // We use get_records here as we need to pass a limit in the query that works cross db.
  214. $scoes = $DB->get_records_select('scorm_scoes', $sqlselect,
  215. array($scorm->id, $firstsco->sortorder), 'sortorder', 'id', 0, 1);
  216. if (!empty($scoes)) {
  217. $sco = reset($scoes); // We only care about the first record - the above query only returns one.
  218. $scorm->launch = $sco->id;
  219. } else {
  220. // This is an invalid package - it has a default org that doesn't contain a launchable sco.
  221. // Check for any valid sco with a launch param.
  222. $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
  223. // We use get_records here as we need to pass a limit in the query that works cross db.
  224. $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id), 'sortorder', 'id', 0, 1);
  225. if (!empty($scoes)) {
  226. $sco = reset($scoes); // We only care about the first record - the above query only returns one.
  227. $scorm->launch = $sco->id;
  228. }
  229. }
  230. }
  231. if ($originallaunch != $scorm->launch) {
  232. $DB->update_record('scorm', $scorm);
  233. }
  234. }
  235. $scorms->close();
  236. upgrade_mod_savepoint(true, 2014040200, 'scorm');
  237. }
  238. // Moodle v2.7.0 release upgrade line.
  239. // Put any upgrade step following this.
  240. if ($oldversion < 2014072500) {
  241. // Define field autocommit to be added to scorm.
  242. $table = new xmldb_table('scorm');
  243. $field = new xmldb_field('autocommit', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'displayactivityname');
  244. // Conditionally launch add field autocommit.
  245. if (!$dbman->field_exists($table, $field)) {
  246. $dbman->add_field($table, $field);
  247. }
  248. // Scorm savepoint reached.
  249. upgrade_mod_savepoint(true, 2014072500, 'scorm');
  250. }
  251. // Moodle v2.8.0 release upgrade line.
  252. // Put any upgrade step following this.
  253. if ($oldversion < 2015031800) {
  254. // Check to see if this site has any AICC packages - if so set the aiccuserid to pass the username
  255. // so that the data remains consistent with existing packages.
  256. $alreadyset = $DB->record_exists('config_plugins', array('plugin' => 'scorm', 'name' => 'aiccuserid'));
  257. if (!$alreadyset) {
  258. $hasaicc = $DB->record_exists('scorm', array('version' => 'AICC'));
  259. if ($hasaicc) {
  260. set_config('aiccuserid', 0, 'scorm');
  261. } else {
  262. // We set the config value to hide this from upgrades as most users will not know what AICC is anyway.
  263. set_config('aiccuserid', 1, 'scorm');
  264. }
  265. }
  266. // Scorm savepoint reached.
  267. upgrade_mod_savepoint(true, 2015031800, 'scorm');
  268. }
  269. // Moodle v2.9.0 release upgrade line.
  270. // Put any upgrade step following this.
  271. return true;
  272. }