upgrade.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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.5.2 2020/08/03
  17. * + Fixed 'ALTER TABLE' of 'iassign_submission.grade' from BIGINT(11) to REAL
  18. * + New version of iHanoi 1.0.20200803
  19. * - v 1.5.1 2020/05/28-30
  20. * + Avoid to update one iLM causing colision with other instance of the same iLM
  21. * - v 1.4 2013/09/19
  22. * + Insert general fields for iassign statement (grade, timeavaliable, timedue, preventlate, test, max_experiment).
  23. * + Change index field 'name' in 'iassign_ilm' table to index field 'name,version'.
  24. * - v 1.2 2013/08/30
  25. * + Change 'filearea' for new concept for files.
  26. * + Change path file for ilm, consider version in pathname.
  27. *
  28. * @author Leônidas O. Brandão
  29. * @author Patricia Alves Rodrigues
  30. * @author Igor Moreira Félix
  31. * @version v 1.5.1 2020/05/28-30
  32. * @version v 1.5 2019/03/13
  33. * @version v 1.4 2013/09/19
  34. * @package mod_iassign_db
  35. * @since 2010/12/21
  36. * @copyright iMath (http://www.matematica.br) and LInE (http://line.ime.usp.br) - Computer Science Dep. of IME-USP (Brazil)
  37. *
  38. * <b>License</b>
  39. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40. *
  41. * @param $oldversion Number of the old version.
  42. */
  43. require_once ($CFG->dirroot . '/mod/iassign/locallib.php');
  44. function xmldb_iassign_upgrade ($oldversion) {
  45. global $CFG, $DB, $USER;
  46. $dbman = $DB->get_manager();
  47. if ($oldversion < 2014012100) {
  48. // Define field and index in iLM table to be added
  49. $table = new xmldb_table('iassign_ilm');
  50. $index_name = new xmldb_index('name');
  51. $index_name->set_attributes(XMLDB_INDEX_UNIQUE, array('name'));
  52. if ($dbman->index_exists($table, $index_name)) {
  53. $dbman->drop_index($table, $index_name, $continue = true, $feedback = true);
  54. }
  55. $index_name_version = new xmldb_index('name_version');
  56. $index_name_version->set_attributes(XMLDB_INDEX_UNIQUE, array('name', 'version'));
  57. if (!$dbman->index_exists($table, $index_name_version)) {
  58. $dbman->add_index($table, $index_name_version, $continue = true, $feedback = true);
  59. }
  60. // Fix field name in ilm table to be added.
  61. $table = new xmldb_table('iassign_ilm');
  62. $field_height = new xmldb_field('heigth', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '600', 'width');
  63. if ($dbman->field_exists($table, $field_height))
  64. $dbman->rename_field($table, $field_height, 'height');
  65. // Define fields in iassign table to be added.
  66. $table = new xmldb_table('iassign');
  67. $field_intro = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'name');
  68. if (!$dbman->field_exists($table, $field_intro))
  69. $dbman->add_field($table, $field_intro);
  70. $field_introformat = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');
  71. if (!$dbman->field_exists($table, $field_introformat))
  72. $dbman->add_field($table, $field_introformat);
  73. $field_grade = new xmldb_field('grade', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, '0', 'activity_group');
  74. if (!$dbman->field_exists($table, $field_grade))
  75. $dbman->add_field($table, $field_grade);
  76. $field_timeavailable = new xmldb_field('timeavailable', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grade');
  77. if (!$dbman->field_exists($table, $field_timeavailable))
  78. $dbman->add_field($table, $field_timeavailable);
  79. $field_timedue = new xmldb_field('timedue', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timeavailable');
  80. if (!$dbman->field_exists($table, $field_timedue))
  81. $dbman->add_field($table, $field_timedue);
  82. $field_preventlate = new xmldb_field('preventlate', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '1', 'timedue');
  83. if (!$dbman->field_exists($table, $field_preventlate))
  84. $dbman->add_field($table, $field_preventlate);
  85. $field_test = new xmldb_field('test', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0', 'preventlate');
  86. if (!$dbman->field_exists($table, $field_test))
  87. $dbman->add_field($table, $field_test);
  88. $field_max_experiment = new xmldb_field('max_experiment', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'test');
  89. if (!$dbman->field_exists($table, $field_max_experiment))
  90. $dbman->add_field($table, $field_max_experiment);
  91. if (!$dbman->table_exists($table)) {
  92. $field_id = new xmldb_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE);
  93. $field_time = new xmldb_field('time', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'id');
  94. $field_userid = new xmldb_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'time');
  95. $field_ip = new xmldb_field('ip', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'userid');
  96. $field_course = new xmldb_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'ip');
  97. $field_cmid = new xmldb_field('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'course');
  98. $field_ilmid = new xmldb_field('ilmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'cmid');
  99. $field_action = new xmldb_field('action', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'ilmid');
  100. $field_info = new xmldb_field('info', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'action');
  101. $field_language = new xmldb_field('language', XMLDB_TYPE_CHAR, '10', null, null, null, null, 'info');
  102. $field_user_agent = new xmldb_field('user_agent', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'language');
  103. $field_javascript = new xmldb_field('javascript', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'user_agent');
  104. $field_java = new xmldb_field('java', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'javascript');
  105. $key = new xmldb_key('primary');
  106. $key->set_attributes(XMLDB_KEY_PRIMARY, array('id'), null, null);
  107. $index = new xmldb_index('course');
  108. $index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course'));
  109. $table->addField($field_id);
  110. $table->addField($field_time);
  111. $table->addField($field_userid);
  112. $table->addField($field_ip);
  113. $table->addField($field_course);
  114. $table->addField($field_cmid);
  115. $table->addField($field_ilmid);
  116. $table->addField($field_action);
  117. $table->addField($field_info);
  118. $table->addField($field_language);
  119. $table->addField($field_user_agent);
  120. $table->addField($field_javascript);
  121. $table->addField($field_java);
  122. $table->addKey($key);
  123. $table->addIndex($index);
  124. $status = $dbman->create_table($table);
  125. } // if (!$dbman->table_exists($table))
  126. // Now update Moodle table '*_files' related to the iAssign: insert new field 'filearea'
  127. $fs = get_file_storage();
  128. $is_delete = true;
  129. $ilm_path = $CFG->dirroot . "/mod/iassign/ilm/";
  130. // All iAssign 'exercises' must get the label 'exercise'
  131. $exercise_files = $DB->get_records('files', array("component" => "mod_iassign", "filearea" => "activity"));
  132. foreach ($exercise_files as $exercise_file) {
  133. $exercise_file->filearea = "exercise";
  134. $DB->update_record('files', $exercise_file);
  135. }
  136. // All iAssign iLM (usually 'JAR package') must get the label 'activity'
  137. $activity_files = $DB->get_records('files', array("component" => "mod_iassign", "filearea" => "ilm"));
  138. foreach ($activity_files as $activity_file) {
  139. $activity_file->filearea = "activity";
  140. $DB->update_record('files', $activity_file);
  141. }
  142. // iAssign savepoint reached
  143. upgrade_mod_savepoint(true, 2014012100, 'iassign');
  144. } // if ($oldversion < 2014012100)
  145. if ($oldversion < 2017021900) { // update to migrate from 2016021300 to 2017021900 (2.2.00)
  146. // iassign_ilm : id; name; version; description; url; extension; parent; file_jar; file_class; width; height; enable; timemodified; author; timecreated; evaluate
  147. $count_iassign_ilm = $DB->count_records_sql('SELECT COUNT(id) FROM {iassign_ilm}'); // count the number of existing iLM in table '*_iassign_ilm'
  148. if ($count_iassign_ilm>0) { // Insert new iLM: iVProgH5 (package in HTML5 technology)
  149. // Insert an entry in the Moodle table '*_files'
  150. $context = context_system::instance();
  151. $file_ilm = array(
  152. 'userid' => $USER->id, // ID of context
  153. 'contextid' => $context->id, // ID of context
  154. 'component' => 'mod_iassign', // usually = table name
  155. 'filearea' => 'ilm', // in table '*_files' all iAssign exercises will have 'filearea="exercise"' and all iLM must be 'ilm'
  156. 'itemid' => 0,
  157. 'filepath' => '/iassign/ilm/', // any path beginning and ending in /
  158. 'filename' => "iVProg/1.0.20200221/ivprog/index.html"); // any filename: use the starting point of iVProgH5
  159. // Now update Moodle table '*_files' related to the iAssign: insert new field 'filearea'
  160. $fs = get_file_storage();
  161. $is_delete = true;
  162. $ilm_path = $CFG->dirroot . "/mod/iassign/ilm/";
  163. //TODO iLM_HTML5 : What model to implement to HTML5?
  164. //TODO 1. The first version of iLM HTML5 in '/var/www/html/moodle/mod/iassign/ilm/ilm-nome' and others in '/var/moodledata/filedir/'?
  165. //TODO 2. Inserting new iLM HTML5 by ZIP file => explode it and register in '/var/moodledata/filedir/'?
  166. // Create file using the content: the file information is inserted in the Moodle table '*_files'
  167. // The real file will be registered in Moodle data (default '/var/moodledata/filedir/xx/yy' - 'xxyy' is the first 4 char in '*_files.contenthash')
  168. //TODO Keep this even change the model to not insert iLM HTML5 (need this object to connect with '*_iassign_ilm' bellow)
  169. $file_ilm = $fs->create_file_from_string($file_ilm, file_get_contents($ilm_path . "ivprog-html/main.html"));
  170. if ($file_ilm)
  171. $is_delete &= @unlink($ilm_path . "ivprog-html");
  172. // Insert an entry in iAssign table '*_iassign_ilm'
  173. $newentry = new stdClass();
  174. $newentry->name = 'iVProg';
  175. $newentry->url = 'http://www.matematica.br/ivprog';
  176. $newentry->version = '1.0.20200221';
  177. $newentry->description = '{"en":"Visual Interactive Programming on the Internet HTML5","pt_br":"Programação visual interativa na Internet"}';
  178. $newentry->extension = 'ivph';
  179. $newentry->file_jar = 'iVProg/1.0.20200221/ivprog/'; // to JAR this is the '*_files.id' correponding to the iLM storaged in '/var/moodledata/filedir/'
  180. $newentry->file_class = 'index.html';
  181. $newentry->width = 800;
  182. $newentry->height = 700;
  183. $newentry->enable = 1;
  184. $newentry->timemodified = time();
  185. $newentry->author = $USER->id;
  186. $newentry->timecreated = time();
  187. $newentry->evaluate = 1;
  188. // $DB->insert_records('iassign_ilm', $newentry);
  189. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  190. // iAssign savepoint reached
  191. upgrade_mod_savepoint(true, 2017021900, 'iassign');
  192. } // if ($count_iassign_ilm>0)
  193. } // if ($oldversion < 2017021900)
  194. if ($oldversion < 2017120100) { // last one: 2017042800 from 2017/02/19
  195. // Create new field 'type', putting 'Java' if JAR and 'HTML5' if HTML5
  196. // Update fields 'file_jar, file_class' of iLM iVProgH5 (from 'iVProgH5, ivprog-html/main.html' to 'ilm/ivprog-html/, main.html'
  197. //---
  198. // Define new field in iassign table to be added: type varchar(20) in { 'Java' 'HTML5' }
  199. $table = new xmldb_table('iassign_ilm');
  200. $new_field_type = new xmldb_field('type', XMLDB_TYPE_CHAR, '20', null, null, null, null, 'version'); // after field 'version'
  201. if (!$dbman->field_exists($table, $new_field_type))
  202. $dbman->add_field($table, $new_field_type);
  203. //---
  204. // All iAssign iLM (usually 'JAR package') must get the type 'Java'
  205. $list_of_ilm_installed = $DB->get_records('iassign_ilm');
  206. foreach ($list_of_ilm_installed as $ilm_installed) {
  207. $ilm_name = strtolower($ilm_installed->name);
  208. if ($ilm_name=="ivprogh5" || $ilm_name=="ifractions" || $ilm_name=="fractions") { // if it is 'iVProgH5' (perhaps 'iFractions' already installed?)
  209. $ilm_installed->type = "HTML5"; // it is HTML5
  210. $file_jar = strtolower($ilm_installed->file_jar);
  211. if ($ilm_name=="ivprogh5" && $file_jar=="ivprogh5") { // fields 'file_jar' and 'file_class' must have: 'ilm/ivprog-html/' and 'main.html'
  212. $ilm_installed->file_jar = "ilm/ivprog-html/"; // iVProgH5 ivprog-html/main.html
  213. $ilm_installed->file_class = "main.html";
  214. }
  215. }
  216. else
  217. $ilm_installed->type = "Java"; // otherwise it is Java
  218. $DB->update_record('iassign_ilm', $ilm_installed);
  219. } // foreach
  220. //---
  221. // Insert in table '{iassign_ilm}' the new iLM iFractions version 0.1.2017.11.22
  222. // Table 'iassign_ilm' : id name version type description url extension parent file_jar file_class width height enable timemodified author timecreated evaluate
  223. } // if ($oldversion < 2017120101)
  224. if ($oldversion < 2018031000) {
  225. // Verify if exist iLM with the same name
  226. // then, update the version and file_jar
  227. $records = array(
  228. array_combine( // iGeom 5.9.22
  229. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate'),
  230. array('iGeom', 'http://www.matematica.br/igeom', '5.9.22', 'Java', '{"en":"Interactive Geometry on the Internet","pt_br":"Geometria Interativa na Internet"}', 'geo', 'ilm/iGeom/5.9.22/iGeom.jar', 'IGeomApplet.class', 800, 600, 1, time(), $USER->id, time(), 1)),
  231. array_combine( // iGraf 4.4.0.10
  232. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate'),
  233. array('iGraf', 'http://www.matematica.br/igraf', '4.4.0.10', 'Java', '{"en":"Interactive Graphic on the Internet","pt_br":"Gráficos Interativos na Internet"}', 'grf', 'ilm/iGraf/4.4.0.10/iGraf.jar', 'igraf.IGraf.class', 840, 600, 1, time(), $USER->id, time(), 1)),
  234. );
  235. foreach ($records as $record) {
  236. if ($record['type'] == 'HTML5') {
  237. // Verify if there is a HTML5 iLM register => update it
  238. $iassign_ilm = $DB->get_records('iassign_ilm', array('name' => $record['name']));
  239. if ($iassign_ilm) {
  240. // Update file_jar and file_class
  241. foreach ($iassign_ilm as $iassign) {
  242. $iassign->name = trim($iassign->name);
  243. $iassign->version = trim($iassign->version);
  244. // If HTML5 iLM is the same version, only update the path
  245. if ($iassign->version == $record['version']) {
  246. $newentry = new stdClass();
  247. $newentry->id = $iassign->id;
  248. $newentry->file_jar = $record['file_jar'];
  249. $newentry->file_class = $record['file_class'];
  250. $DB->update_record("iassign_ilm", $newentry);
  251. }
  252. else { // If HTML5 iLM is NOT the same version, make a copy of the iLM
  253. $dest = $CFG->dirroot . '/mod/iassign/ilm/' . $iassign->name . '/' . $iassign->version . '/';
  254. mkdir($dest, 0755); // permissions "drwxr-xr-x"
  255. $source = $CFG->dirroot . '/mod/iassign/ilm/' . $iassign->name . '/' . $record['version'] . '/';
  256. foreach (
  257. $iterator = new \RecursiveIteratorIterator(
  258. new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
  259. \RecursiveIteratorIterator::SELF_FIRST) as $item) {
  260. if ($item->isDir()) {
  261. mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
  262. }
  263. else { // copy or move?
  264. copy($item, $dest . $iterator->getSubPathName());
  265. // copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
  266. // rename($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
  267. }
  268. }
  269. $newentry = new stdClass();
  270. $newentry->id = $iassign->id;
  271. $newentry->file_jar = 'ilm/' . $iassign->name . '/' . $iassign->version . '/' . basename($record['file_jar']) . '/';
  272. $newentry->file_class = $record['file_class'];
  273. $DB->update_record("iassign_ilm", $newentry);
  274. }
  275. } // foreach ($iassign_ilm as $iassign)
  276. }
  277. else { // if ($iassign_ilm)
  278. // If not found a iLM with the same name
  279. $DB->insert_record('iassign_ilm', $record, false); // insert new iLM in the table '*_iassign_ilm'
  280. }
  281. } // if ($record['type'] == 'HTML5')
  282. else {
  283. // Verify if there is a Java iLM register to update it
  284. $iassign_ilm = $DB->get_record('iassign_ilm', array('name' => $record['name'], 'version' => $record['version']));
  285. if ($iassign_ilm) {
  286. // Update file_jar and file_class
  287. $newentry = new stdClass();
  288. $newentry->id = $iassign_ilm->id;
  289. $newentry->file_jar = $record['file_jar'];
  290. $newentry->file_class = $record['file_class'];
  291. $DB->update_record("iassign_ilm", $newentry);
  292. }
  293. else { // if ($iassign_ilm) - I couldn't find an iLM with this name ($record['name']) and under this version ($record['version'])
  294. // If not found a Java iLM with the same name and version, search a
  295. // different version, to use as parent of new version
  296. $iassign_ilm_parent = $DB->get_record('iassign_ilm', array('name' => $record['name'], 'parent' => 0));
  297. if ($iassign_ilm_parent) {
  298. $record['parent'] = $iassign_ilm_parent->id;
  299. $DB->insert_record('iassign_ilm', $record, false); // insert with parent
  300. // Download the JAR file of parent from MoodleData and put it in iassign/ilm/
  301. $dest = $CFG->dirroot . '/mod/iassign/ilm/' . $iassign_ilm_parent->name . '/' . $iassign_ilm_parent->version . '/';
  302. mkdir($dest, 0777);
  303. $fs = get_file_storage();
  304. $file = $fs->get_file_by_id($iassign_ilm_parent->file_jar);
  305. $file->copy_content_to($dest . $file->get_filename());
  306. // Update file_jar of iLM parent
  307. $update_parent = new stdClass();
  308. $update_parent->id = $iassign_ilm_parent->id;
  309. $update_parent->file_jar = 'ilm/' . $iassign_ilm_parent->name . '/' . $iassign_ilm_parent->version . '/' . $file->get_filename();
  310. $DB->update_record("iassign_ilm", $update_parent);
  311. }
  312. else {
  313. $DB->insert_record('iassign_ilm', $record, false); // insert new iLM in the table '*_iassign_ilm' without a parent
  314. }
  315. } // else if ($iassign_ilm)
  316. } // else if ($record['type'] == 'HTML5')
  317. } // foreach ($records as $record)
  318. } // if ($oldversion < 2018031000)
  319. //L if ($oldversion < 2019112219) {
  320. if ($oldversion < 2020070613) {
  321. $table = new xmldb_table('iassign_submission');
  322. $field = new xmldb_field('previous_grade', XMLDB_TYPE_FLOAT, null, null, null, null, null);
  323. if (!$dbman->field_exists($table, $field)) {
  324. $dbman->add_field($table, $field);
  325. }
  326. $records = array( // iLM adjusted to iAssign 2020/08/03
  327. array_combine( // iGeom 5.9.22
  328. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  329. array('iGeom', 'http://www.matematica.br/igeom', '5.9.22', 'Java', '{"en":"Interactive Geometry on the Internet","pt_br":"Geometria Interativa na Internet"}', 'geo', 'ilm/iGeom/5.9.22/iGeom.jar', 'IGeomApplet.class', 800, 600, 1, time(), $USER->id, time(), 1, 0)),
  330. array_combine( // iGraf 4.4.0.10
  331. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  332. array('iGraf', 'http://www.matematica.br/igraf', '4.4.0.10', 'Java', '{"en":"Interactive Graphic on the Internet","pt_br":"Gráficos Interativos na Internet"}', 'grf', 'ilm/iGraf/4.4.0.10/iGraf.jar', 'igraf.IGraf.class', 840, 600, 1, time(), $USER->id, time(), 1, 0)),
  333. array_combine( // iHanoi 1.0.20200803
  334. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  335. array('iHanoi', 'http://www.matematica.br/ihanoi', '1.0.20200803', 'HTML5', '{"en":"interactive Tower os Hanoi (by LInE)", "pt_br":"Torres de Hanói (do LInE)"}', 'ihn', 'ilm/iHanoi/1.0.20200803/ihanoi/', 'index.html', 1100, 500, 1, time(), $USER->id, time(), 1, 0)),
  336. array_combine( // iVProg 1.0.20200221/ - HTML5 - 2020
  337. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  338. array('iVProg', 'http://www.usp.br/line/ivprog/', '1.0.20200221', 'HTML5', '{"en":"Visual Interactive Programming on the Internet (HTML)","pt_br":"Programação visual interativa na Internet"}', 'ivph', 'ilm/iVProg/1.0.20200221/ivprog/', 'index.html', 800, 600, 1, time(), $USER->id, time(), 1, 1)),
  339. array_combine( // iFractions 0.1.20200221 - HTML5
  340. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  341. array('iFractions', 'http://www.matematica.br/ifractions', '0.1.20200221', 'HTML5', '{"en":"Interactive Fractions game","pt_br":"Jogo interativa de frações"}', 'frc', 'ilm/iFractions/0.1.20200221/ifractions/', 'index.html', 1000, 600, 1, time(), $USER->id, time(), 1, 0))
  342. );
  343. $iassign_ilm = $DB->get_records('iassign_ilm');
  344. $strNot_installed = '';
  345. foreach ($records as $record) { // this version 'iassign_ilm' does not has field 'reevaluate'
  346. $newentry = new stdClass();
  347. $newentry->name = $record['name'];
  348. $newentry->version = $record['version'];
  349. $newentry->type = $record['type'];
  350. $newentry->url = $record['url'];
  351. $newentry->description = $record['description'];
  352. $newentry->extension = $record['extension'];
  353. $newentry->file_jar = $record['file_jar'];
  354. $newentry->file_class = $record['file_class'];
  355. $newentry->width = $record['width'];
  356. $newentry->height = $record['height'];
  357. $newentry->enable = $record['enable'];
  358. $newentry->timemodified = time();
  359. $newentry->author = $USER->id;
  360. $newentry->timecreated = time();
  361. $newentry->evaluate = $record['evaluate'];
  362. $exists = 0;
  363. if ($iassign_ilm) { // Search if there is any iLM of this with this version
  364. $record_type = strtolower($record['type']);
  365. foreach ($iassign_ilm as $iassign) {
  366. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  367. if ($iassign->version == $record['version']) { // or with the one with the same version
  368. $exists = 1; // iLM found - exit with the last one, same version
  369. $strNot_installed += "\n" + ' <li>' + $record['name'] + ';' + $record['type'] + ';' + $record['version'] + ' </li>' + "\n";
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. if ($exists == 0) try { // iLM does not exists or it has old version
  376. $DB->insert_record("iassign_ilm", $newentry, false);
  377. } catch (Exception $e) {
  378. print 'Caught exception: ' . $e->getMessage() . "<br/>\n";
  379. }
  380. } // foreach ($records as $record)
  381. if ($strNot_installed != '') { // ATTENTION: this implies that Moodle administrator updated this iLM, please verify if it is really the current one.
  382. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  383. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  384. print ' <ul style="margin-top: 1rem;">' + "\n";
  385. print $strNot_installed;
  386. print ' </ul>' + "\n";
  387. print '</div>' + "\n";
  388. }
  389. // Verify if each iLM previously installed is present in fresh new iAssign. This does not means problem, perhaps the admin installed a particular iLM.
  390. $strNot_found = '';
  391. if ($iassign_ilm) { // exists iLM ($DB->get_records('iassign_ilm'))
  392. foreach ($iassign_ilm as $iassign) {
  393. $found = false;
  394. foreach ($records as $record) {
  395. if ($iassign->name == $record['name']) {
  396. $found = true;
  397. break;
  398. }
  399. }
  400. if (!$found) {
  401. $strNot_found .= '<li>' . $iassign->name . ' - <a href="' . $iassign->url . '" target="_blank">' . $iassign->url . '</a></li>' . "\n";
  402. $updateentry = new stdClass();
  403. $updateentry->id = $iassign->id;
  404. $updateentry->enable = 0;
  405. $updateentry->timemodified = time();
  406. $DB->update_record("iassign_ilm", $updateentry); // insert new iLM
  407. }
  408. } // foreach ($iassign_ilm as $iassign)
  409. } // if ($iassign_ilm)
  410. if ($strNot_found != '') {
  411. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  412. print get_string('upgrade_alert_iMA_msg', 'iassign'); // Updated but some previous iLM installed are not available in fresh iAssign
  413. print '<ul style="margin-top: 1rem;">' + "\n";
  414. print $strNot_found;
  415. print '</ul>' + "\n";
  416. print get_string('upgrade_alert_iMA_solution_pt1', 'iassign');
  417. print '<a href="'.new moodle_url('/admin/settings.php?section=modsettingiassign').'">' . get_string('upgrade_alert_iMA_solution_pt2', 'iassign') . '</a>.' . "\n";
  418. print '</div>' + "\n";
  419. }
  420. } // if ($oldversion < 2020070613)
  421. if ($oldversion < 2020070613) {
  422. $table = new xmldb_table('iassign_ilm');
  423. $field = new xmldb_field('reevaluate');
  424. $field->set_attributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', null, null, null);
  425. if (!$dbman->field_exists($table, $field)) { // if 'iassign_ilm.reevaluate' does not exist, then create this field
  426. $dbman->add_field($table, $field);
  427. }
  428. $iassign_ilm = $DB->get_records('iassign_ilm');
  429. foreach ($iassign_ilm as $iassign) { // If already installed iVProg/JS or iHanoi/JS set it with re-evaluate feature
  430. if ($iassign->type == 'HTML5' && ($iassign->name == 'iVProg' || $iassign->name == 'iHanoi')) {
  431. $updateentry = new stdClass();
  432. $updateentry->id = $iassign->id;
  433. $updateentry->reevaluate = 1;
  434. $updateentry->timemodified = time();
  435. $DB->update_record("iassign_ilm", $updateentry);
  436. break;
  437. }
  438. }
  439. } // if ($oldversion < 2020070612)
  440. if ($oldversion < 2020080300) { // new iHanoi
  441. // iassign_submission . grade : from 'BIGINT(11)' to 'real'
  442. if ($dbman->field_exists('iassign', 'grade')) {
  443. $sql = 'ALTER TABLE {iassign} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  444. $DB->execute($sql);
  445. }
  446. if ($dbman->field_exists('iassign_statement', 'grade')) {
  447. $sql = 'ALTER TABLE {iassign_statement} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  448. $DB->execute($sql);
  449. }
  450. if ($dbman->field_exists('iassign_submission', 'grade')) {
  451. $sql = 'ALTER TABLE {iassign_submission} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  452. $DB->execute($sql);
  453. }
  454. if ($dbman->field_exists('iassign_submission', 'previous_grade')) {
  455. // $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade REAL NOT NULL DEFAULT 0.0'; // Invalid use of NULL value
  456. $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade FLOAT DEFAULT 0.0';
  457. $DB->execute($sql);
  458. }
  459. // Update iHanoi, iVProg, iFractions and iGeom
  460. $records = array(
  461. array_combine( // iHanoi 1.0.20200803
  462. array('name', 'url', 'version', 'type', 'description',
  463. 'extension', 'file_jar', 'file_class', 'width', 'height',
  464. 'enable', 'timemodified', 'evaluate', 'reevaluate', 'author', 'timecreated'),
  465. array('iHanoi', 'http://www.matematica.br/ihanoi', '1.0.20200803', 'HTML5', '{"en":"interactive Tower os Hanoi (by LInE)", "pt_br":"Torres de Hanói (do LInE)"}',
  466. 'ihn', 'ilm/iHanoi/1.0.20200803/ihanoi/', 'index.html', 1100, 500,
  467. 1, time(), 1, 1, $USER->id, time()))
  468. );
  469. $iassign_ilm = $DB->get_records('iassign_ilm');
  470. foreach ($records as $record) { // For each iLM in the current version of iAssign
  471. $newentry = new stdClass();
  472. $newentry->name = $record['name'];
  473. $newentry->version = $record['version'];
  474. $newentry->type = $record['type'];
  475. $newentry->url = $record['url'];
  476. $newentry->description = $record['description'];
  477. $newentry->extension = $record['extension'];
  478. $newentry->file_jar = $record['file_jar'];
  479. $newentry->file_class = $record['file_class'];
  480. $newentry->width = $record['width'];
  481. $newentry->height = $record['height'];
  482. $newentry->enable = $record['enable'];
  483. $newentry->timemodified = time();
  484. $newentry->evaluate = $record['evaluate'];
  485. $newentry->reevaluate = $record['reevaluate'];
  486. $newentry->author = $USER->id;
  487. $newentry->timecreated = time();
  488. $newentry->evaluate = $record['evaluate'];
  489. $exists = 0;
  490. $last_id = -1;
  491. if ($iassign_ilm) { // Search if there is any previous installed iLM that is also in the current version of iAssign
  492. $record_type = strtolower($record['type']);
  493. foreach ($iassign_ilm as $iassign) {
  494. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  495. if ($iassign->id > $last_id)
  496. $last_id = $iassign->id; // last ID => last version (hopefully)
  497. if ($iassign->version == $record['version']) { // or with the one with the same version
  498. $exists = 1; // iLM found - exit with the last one, same version
  499. $strNot_installed += "\n" + ' <li>' + $record['name'] + ';' + $record['type'] + ';' + $record['version'] + ' </li>' + "\n";
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. if ($exists == 0) { // iLM does not exists or it has old version
  506. $newentry->parent = $record['parent'];
  507. $DB->insert_record("iassign_ilm", $newentry, false);
  508. }
  509. } // foreach ($records as $record)
  510. if ($strNot_installed != '') {
  511. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  512. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  513. print ' <ul style="margin-top: 1rem;">' + "\n";
  514. print $strNot_installed;
  515. print ' </ul>' + "\n";
  516. print '</div>' + "\n";
  517. }
  518. // s_iassign_ilm: atualizou iHanoi existente, mas nao era isso! Deveria ter inserido novo!
  519. // id name version type ... parent file_jar file_class width height ... evaluate reevaluate
  520. // 53 iHanoi 2 HTML5 ... 0 ilm/iHanoi/2/ihanoi/ index.html 1100 700 ... 1 0
  521. $iassign_ilm = $DB->get_records('iassign_ilm');
  522. foreach ($iassign_ilm as $iassign) { // for iLM iHanoi update the new field 'reevaluate' as 1
  523. if ($iassign->name == 'iHanoi' && $iassign->type == 'HTML5' && $iassign->reevaluate!=1) {
  524. $updateentry = new stdClass();
  525. $updateentry->id = $iassign->id;
  526. $updateentry->reevaluate = 1;
  527. $updateentry->timemodified = time();
  528. $DB->update_record("iassign_ilm", $updateentry);
  529. break;
  530. }
  531. }
  532. // iAssign savepoint reached.
  533. upgrade_mod_savepoint(true, 2020080300, 'iassign');
  534. } // if ($oldversion < 2020080300)
  535. //TODO Codigo do Igor para atualizar 'files.itemid' e 'iassign_statement.filesid':
  536. if ($oldversion < 2020102800) {
  537. // 1. encontrar os contextos dos arquivos do itarefa:
  538. $iassign_contexts_list = $DB->get_records_sql("SELECT contextid FROM {files} f " .
  539. " WHERE component='mod_iassign'");
  540. // 2. compor um array com todos os contextos encontrados:
  541. $contexts = array();
  542. foreach ($iassign_contexts_list as $iassign_context_item) {
  543. array_push($contexts, $iassign_context_item->contextid);
  544. }
  545. // 3. encontrar todas as atividades do itarefa, em que o arquivo não tenha o mesmo id do statement:
  546. $iassign_statement_list = $DB->get_records_sql("SELECT * FROM {iassign_statement} s " .
  547. " WHERE s.id != s.file");
  548. $fs = get_file_storage();
  549. // 4. percorrer o conjunto de atividades:
  550. foreach ($iassign_statement_list as $iassign_statement_activity_item) {
  551. // 5. encontrar o arquivo, considerando os possíveis contextos:
  552. $files = array();
  553. foreach ($contexts as $context) {
  554. $files = $fs->get_area_files($context, 'mod_iassign', 'exercise', $iassign_statement_activity_item->file);
  555. // 6. se o arquivo for encontrado, fazer uma cópia do conteúdo,
  556. // com o itemid novo, atualizar o iassign_statement, e apagar o arquivo antigo:
  557. if ($files) {
  558. foreach ($files as $value) {
  559. if ($value != null && $value->get_filename() != ".") {
  560. // 6.A. Fazer uma cópia:
  561. $newfile = $fs->create_file_from_storedfile(array('contextid' => $context, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement_activity_item->id), $value);
  562. // 6.B. Atualizar o registro da atividade para o arquivo novo:
  563. $update_entry = new stdClass();
  564. $update_entry->id = $iassign_statement_activity_item->id;
  565. $update_entry->file = $newfile->get_itemid();
  566. $DB->update_record("iassign_statement", $update_entry);
  567. // 6.C. Remover o arquivo antigo:
  568. $value->delete();
  569. } else if ($value != null && $value->get_filename() == ".") {
  570. // 6.C.I. Remover também os indicadores de diretório:
  571. $value->delete();
  572. }
  573. }
  574. break;
  575. }
  576. }
  577. }
  578. } // if ($oldversion < 2020102800)
  579. // log event -----------------------------------------------------
  580. if (class_exists('plugin_manager'))
  581. $pluginman = plugin_manager::instance();
  582. else
  583. $pluginman = core_plugin_manager::instance();
  584. $plugins = $pluginman->get_plugins();
  585. iassign_log::add_log('upgrade', 'version: ' . $plugins['mod']['iassign']->versiondisk);
  586. // log event -----------------------------------------------------
  587. return true;
  588. } // function xmldb_iassign_upgrade($oldversion)