upgrade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * This file keeps track of upgrades to the lams module.
  4. *
  5. * Sometimes, changes between versions involve
  6. * alterations to database structures and other
  7. * major things that may break installations.
  8. * The upgrade function in this file will attempt
  9. * to perform all the necessary actions to upgrade
  10. * your older installtion to the current version.
  11. * If there's something it cannot do itself, it
  12. * will tell you what you need to do.
  13. * The commands in here will all be database-neutral,
  14. * using the functions defined in lib/ddllib.php
  15. *
  16. * - v 1.4 2013/09/19
  17. * + Insert general fields for iassign statement (grade, timeavaliable, timedue, preventlate, test, max_experiment).
  18. * + Change index field 'name' in 'iassign_ilm' table to index field 'name,version'.
  19. * - v 1.2 2013/08/30
  20. * + Change 'filearea' for new concept for files.
  21. * + Change path file for ilm, consider version in pathname.
  22. *
  23. * @author Patricia Alves Rodrigues
  24. * @author Leônidas O. Brandão
  25. * @author Luciano Oliveira Borges
  26. * @version v 1.4 2013/09/19
  27. * @package mod_iassign_db
  28. * @since 2010/12/21
  29. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  30. *
  31. * <b>License</b>
  32. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. *
  34. * @param $oldversion Number of the old version.
  35. */
  36. require_once ($CFG->dirroot . '/mod/iassign/locallib.php');
  37. function xmldb_iassign_upgrade ($oldversion) {
  38. global $CFG, $DB, $USER;
  39. $dbman = $DB->get_manager();
  40. if ($oldversion < 2014012100) {
  41. // Define field and index in iLM table to be added
  42. $table = new xmldb_table('iassign_ilm');
  43. $index_name = new xmldb_index('name');
  44. $index_name->set_attributes(XMLDB_INDEX_UNIQUE, array('name'));
  45. if ($dbman->index_exists($table, $index_name)) {
  46. $dbman->drop_index($table, $index_name, $continue = true, $feedback = true);
  47. }
  48. $index_name_version = new xmldb_index('name_version');
  49. $index_name_version->set_attributes(XMLDB_INDEX_UNIQUE, array('name', 'version'));
  50. if (!$dbman->index_exists($table, $index_name_version)) {
  51. $dbman->add_index($table, $index_name_version, $continue = true, $feedback = true);
  52. }
  53. // Fix field name in ilm table to be added.
  54. $table = new xmldb_table('iassign_ilm');
  55. $field_height = new xmldb_field('heigth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '600', 'width');
  56. if ($dbman->field_exists($table, $field_height))
  57. $dbman->rename_field($table, $field_height, 'height');
  58. // Define fields in iassign table to be added.
  59. $table = new xmldb_table('iassign');
  60. $field_intro = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'name');
  61. if (!$dbman->field_exists($table, $field_intro))
  62. $dbman->add_field($table, $field_intro);
  63. $field_introformat = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');
  64. if (!$dbman->field_exists($table, $field_introformat))
  65. $dbman->add_field($table, $field_introformat);
  66. $field_grade = new xmldb_field('grade', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, '0', 'activity_group');
  67. if (!$dbman->field_exists($table, $field_grade))
  68. $dbman->add_field($table, $field_grade);
  69. $field_timeavailable = new xmldb_field('timeavailable', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grade');
  70. if (!$dbman->field_exists($table, $field_timeavailable))
  71. $dbman->add_field($table, $field_timeavailable);
  72. $field_timedue = new xmldb_field('timedue', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeavailable');
  73. if (!$dbman->field_exists($table, $field_timedue))
  74. $dbman->add_field($table, $field_timedue);
  75. $field_preventlate = new xmldb_field('preventlate', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '1', 'timedue');
  76. if (!$dbman->field_exists($table, $field_preventlate))
  77. $dbman->add_field($table, $field_preventlate);
  78. $field_test = new xmldb_field('test', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0', 'preventlate');
  79. if (!$dbman->field_exists($table, $field_test))
  80. $dbman->add_field($table, $field_test);
  81. $field_max_experiment = new xmldb_field('max_experiment', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'test');
  82. if (!$dbman->field_exists($table, $field_max_experiment))
  83. $dbman->add_field($table, $field_max_experiment);
  84. if (!$dbman->table_exists($table)) {
  85. $field_id = new xmldb_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
  86. $field_time = new xmldb_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'id');
  87. $field_userid = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'time');
  88. $field_ip = new xmldb_field('ip', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'userid');
  89. $field_course = new xmldb_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'ip');
  90. $field_cmid = new xmldb_field('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'course');
  91. $field_ilmid = new xmldb_field('ilmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'cmid');
  92. $field_action = new xmldb_field('action', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'ilmid');
  93. $field_info = new xmldb_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'action');
  94. $field_language = new xmldb_field('language', XMLDB_TYPE_CHAR, '10', null, null, null, null, 'info');
  95. $field_user_agent = new xmldb_field('user_agent', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'language');
  96. $field_javascript = new xmldb_field('javascript', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'user_agent');
  97. $field_java = new xmldb_field('java', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'javascript');
  98. $key = new xmldb_key('primary');
  99. $key->set_attributes(XMLDB_KEY_PRIMARY, array('id'), null, null);
  100. $index = new xmldb_index('course');
  101. $index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course'));
  102. $table->addField($field_id);
  103. $table->addField($field_time);
  104. $table->addField($field_userid);
  105. $table->addField($field_ip);
  106. $table->addField($field_course);
  107. $table->addField($field_cmid);
  108. $table->addField($field_ilmid);
  109. $table->addField($field_action);
  110. $table->addField($field_info);
  111. $table->addField($field_language);
  112. $table->addField($field_user_agent);
  113. $table->addField($field_javascript);
  114. $table->addField($field_java);
  115. $table->addKey($key);
  116. $table->addIndex($index);
  117. $status = $dbman->create_table($table);
  118. } // if (!$dbman->table_exists($table))
  119. // Now update Moodle table '*_files' related to the iAssign: insert new field 'filearea'
  120. $fs = get_file_storage();
  121. $is_delete = true;
  122. $ilm_path = $CFG->dirroot . "/mod/iassign/ilm/";
  123. // All iAssign 'exercises' must get the label 'exercise'
  124. $exercise_files = $DB->get_records('files', array("component" => "mod_iassign", "filearea" => "activity"));
  125. foreach ($exercise_files as $exercise_file) {
  126. $exercise_file->filearea = "exercise";
  127. $DB->update_record('files', $exercise_file);
  128. }
  129. // All iAssign iLM (usually 'JAR package') must get the label 'activity'
  130. $activity_files = $DB->get_records('files', array("component" => "mod_iassign", "filearea" => "ilm"));
  131. foreach ($activity_files as $activity_file) {
  132. $activity_file->filearea = "activity";
  133. $DB->update_record('files', $activity_file);
  134. }
  135. // iAssign savepoint reached
  136. upgrade_mod_savepoint(true, 2014012100, 'iassign');
  137. } // if ($oldversion < 2014012100)
  138. if ($oldversion < 2017021900) { // update to migrate from 2016021300 to 2017021900 (2.2.00)
  139. // iassign_ilm : id; name; version; description; url; extension; parent; file_jar; file_class; width; height; enable; timemodified; author; timecreated; evaluate
  140. $count_iassign_ilm = $DB->count_records_sql('SELECT COUNT(id) FROM {iassign_ilm}'); // count the number of existing iLM in table '*_iassign_ilm'
  141. if ($count_iassign_ilm>0) { // Insert new iLM: iVProgH5 (package in HTML5 technology)
  142. // Insert an entry in the Moodle table '*_files'
  143. $context = context_system::instance();
  144. $file_ilm = array(
  145. 'userid' => $USER->id, // ID of context
  146. 'contextid' => $context->id, // ID of context
  147. 'component' => 'mod_iassign', // usually = table name
  148. 'filearea' => 'ilm', // in table '*_files' all iAssign exercises will have 'filearea="exercise"' and all iLM must be 'ilm'
  149. 'itemid' => 0,
  150. 'filepath' => '/iassign/ilm/', // any path beginning and ending in /
  151. 'filename' => "ivprog-html/main.html"); // any filename: use the starting point of iVProgH5
  152. // Now update Moodle table '*_files' related to the iAssign: insert new field 'filearea'
  153. $fs = get_file_storage();
  154. $is_delete = true;
  155. $ilm_path = $CFG->dirroot . "/mod/iassign/ilm/";
  156. //TODO iLM_HTML5 : What model to implement to HTML5?
  157. //TODO 1. The first version of iLM HTML5 in '/var/www/html/moodle/mod/iassign/ilm/ilm-nome' and others in '/var/moodledata/filedir/'?
  158. //TODO 2. Inserting new iLM HTML5 by ZIP file => explode it and register in '/var/moodledata/filedir/'?
  159. // Create file using the content: the file information is inserted in the Moodle table '*_files'
  160. // The real file will be registered in Moodle data (default '/var/moodledata/filedir/xx/yy' - 'xxyy' is the first 4 char in '*_files.contenthash')
  161. //TODO Keep this even change the model to not insert iLM HTML5 (need this object to connect with '*_iassign_ilm' bellow)
  162. $file_ilm = $fs->create_file_from_string($file_ilm, file_get_contents($ilm_path . "ivprog-html/main.html"));
  163. if ($file_ilm)
  164. $is_delete &= @unlink($ilm_path . "ivprog-html");
  165. // Insert an entry in iAssign table '*_iassign_ilm'
  166. $newentry = new stdClass();
  167. $newentry->name = 'iVProgH5';
  168. $newentry->url = 'http://www.matematica.br/ivprogh5';
  169. $newentry->version = '0.1.0';
  170. $newentry->description = '{"en":"Visual Interactive Programming on the Internet HTML5","pt_br":"Programação visual interativa na Internet"}';
  171. $newentry->extension = 'ivph';
  172. $newentry->file_jar = 'iVProgH5'; // to JAR this is the '*_files.id' correponding to the iLM storaged in '/var/moodledata/filedir/'
  173. $newentry->file_class = 'ivprog-html/main.html';
  174. $newentry->width = 800;
  175. $newentry->height = 700;
  176. $newentry->enable = 1;
  177. $newentry->timemodified = time();
  178. $newentry->author = $USER->id;
  179. $newentry->timecreated = time();
  180. $newentry->evaluate = 1;
  181. // $DB->insert_records('iassign_ilm', $newentry);
  182. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  183. // iAssign savepoint reached
  184. upgrade_mod_savepoint(true, 2017021900, 'iassign');
  185. } // if ($count_iassign_ilm>0)
  186. } // if ($oldversion < 2017021900)
  187. // log event -----------------------------------------------------
  188. if (class_exists('plugin_manager'))
  189. $pluginman = plugin_manager::instance();
  190. else
  191. $pluginman = core_plugin_manager::instance();
  192. $plugins = $pluginman->get_plugins();
  193. iassign_log::add_log('upgrade', 'version: ' . $plugins['mod']['iassign']->versiondisk);
  194. // log event -----------------------------------------------------
  195. return true;
  196. }