upgrade.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * This file keeps track of upgrades to the nasatlx module
  4. *
  5. * Sometimes, changes between versions involve alterations to database
  6. * structures and other major things that may break installations. The upgrade
  7. * function in this file will attempt to perform all the necessary actions to
  8. * upgrade your older installation to the current version. If there's something
  9. * it cannot do itself, it will tell you what you need to do. The commands in
  10. * here will all be database-neutral, using the functions defined in DLL libraries.
  11. *
  12. * @package mod_nasatlx
  13. * @copyright 2014 LInE - http://line.ime.usp.br
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  15. */
  16. defined('MOODLE_INTERNAL') || die();
  17. /**
  18. * Execute nasatlx upgrade from the given old version
  19. *
  20. * @param int $oldversion
  21. * @return bool
  22. */
  23. function xmldb_nasatlx_upgrade($oldversion) {
  24. global $DB;
  25. $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
  26. // And upgrade begins here. For each one, you'll need one
  27. // block of code similar to the next one. Please, delete
  28. // this comment lines once this file start handling proper
  29. // upgrade code.
  30. // if ($oldversion < YYYYMMDD00) { //New version in version.php
  31. //
  32. // }
  33. // Lines below (this included) MUST BE DELETED once you get the first version
  34. // of your module ready to be installed. They are here only
  35. // for demonstrative purposes and to show how the nasatlx
  36. // iself has been upgraded.
  37. // For each upgrade block, the file nasatlx/version.php
  38. // needs to be updated . Such change allows Moodle to know
  39. // that this file has to be processed.
  40. // To know more about how to write correct DB upgrade scripts it's
  41. // highly recommended to read information available at:
  42. // http://docs.moodle.org/en/Development:XMLDB_Documentation
  43. // and to play with the XMLDB Editor (in the admin menu) and its
  44. // PHP generation posibilities.
  45. // First example, some fields were added to install.xml on 2007/04/01
  46. if ($oldversion < 2007040100) {
  47. // Define field course to be added to nasatlx
  48. $table = new xmldb_table('nasatlx');
  49. $field = new xmldb_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'id');
  50. // Add field course
  51. if (!$dbman->field_exists($table, $field)) {
  52. $dbman->add_field($table, $field);
  53. }
  54. // Define field intro to be added to nasatlx
  55. $table = new xmldb_table('nasatlx');
  56. $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null,'name');
  57. // Add field intro
  58. if (!$dbman->field_exists($table, $field)) {
  59. $dbman->add_field($table, $field);
  60. }
  61. // Define field introformat to be added to nasatlx
  62. $table = new xmldb_table('nasatlx');
  63. $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0',
  64. 'intro');
  65. // Add field introformat
  66. if (!$dbman->field_exists($table, $field)) {
  67. $dbman->add_field($table, $field);
  68. }
  69. // Once we reach this point, we can store the new version and consider the module
  70. // upgraded to the version 2007040100 so the next time this block is skipped
  71. upgrade_mod_savepoint(true, 2007040100, 'nasatlx');
  72. }
  73. // Second example, some hours later, the same day 2007/04/01
  74. // two more fields and one index were added to install.xml (note the micro increment
  75. // "01" in the last two digits of the version
  76. if ($oldversion < 2007040101) {
  77. // Define field timecreated to be added to nasatlx
  78. $table = new xmldb_table('nasatlx');
  79. $field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0',
  80. 'introformat');
  81. // Add field timecreated
  82. if (!$dbman->field_exists($table, $field)) {
  83. $dbman->add_field($table, $field);
  84. }
  85. // Define field timemodified to be added to nasatlx
  86. $table = new xmldb_table('nasatlx');
  87. $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0',
  88. 'timecreated');
  89. // Add field timemodified
  90. if (!$dbman->field_exists($table, $field)) {
  91. $dbman->add_field($table, $field);
  92. }
  93. // Define index course (not unique) to be added to nasatlx
  94. $table = new xmldb_table('nasatlx');
  95. $index = new xmldb_index('courseindex', XMLDB_INDEX_NOTUNIQUE, array('course'));
  96. // Add index to course field
  97. if (!$dbman->index_exists($table, $index)) {
  98. $dbman->add_index($table, $index);
  99. }
  100. // Another save point reached
  101. upgrade_mod_savepoint(true, 2007040101, 'nasatlx');
  102. }
  103. // Third example, the next day, 2007/04/02 (with the trailing 00), some actions were performed to install.php,
  104. // related with the module
  105. if ($oldversion < 2007040200) {
  106. // insert here code to perform some actions (same as in install.php)
  107. upgrade_mod_savepoint(true, 2007040200, 'nasatlx');
  108. }
  109. // And that's all. Please, examine and understand the 3 example blocks above. Also
  110. // it's interesting to look how other modules are using this script. Remember that
  111. // the basic idea is to have "blocks" of code (each one being executed only once,
  112. // when the module version (version.php) is updated.
  113. // Lines above (this included) MUST BE DELETED once you get the first version of
  114. // yout module working. Each time you need to modify something in the module (DB
  115. // related, you'll raise the version and add one upgrade block here.
  116. // Final return of upgrade result (true, all went good) to Moodle.
  117. return true;
  118. }