upgrade.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. //L if ($oldversion < 2019112219) {
  48. if ($oldversion < 2020070613) {
  49. $table = new xmldb_table('iassign_submission');
  50. $field = new xmldb_field('previous_grade', XMLDB_TYPE_FLOAT, null, null, null, null, null);
  51. if (!$dbman->field_exists($table, $field)) {
  52. $dbman->add_field($table, $field);
  53. }
  54. $records = array( // iLM adjusted to iAssign 2020/08/03
  55. array_combine( // iGeom 5.9.22
  56. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  57. 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)),
  58. array_combine( // iGraf 4.4.0.10
  59. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  60. 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)),
  61. array_combine( // iHanoi 1.0.20200803
  62. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  63. 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)),
  64. array_combine( // iVProg 1.0.20200221/ - HTML5 - 2020
  65. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  66. 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)),
  67. array_combine( // iFractions 0.1.20200221 - HTML5
  68. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  69. 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))
  70. );
  71. $iassign_ilm = $DB->get_records('iassign_ilm');
  72. $strNot_installed = '';
  73. foreach ($records as $record) { // this version 'iassign_ilm' does not has field 'reevaluate'
  74. $newentry = new stdClass();
  75. $newentry->name = $record['name'];
  76. $newentry->version = $record['version'];
  77. $newentry->type = $record['type'];
  78. $newentry->url = $record['url'];
  79. $newentry->description = $record['description'];
  80. $newentry->extension = $record['extension'];
  81. $newentry->file_jar = $record['file_jar'];
  82. $newentry->file_class = $record['file_class'];
  83. $newentry->width = $record['width'];
  84. $newentry->height = $record['height'];
  85. $newentry->enable = $record['enable'];
  86. $newentry->timemodified = time();
  87. $newentry->author = $USER->id;
  88. $newentry->timecreated = time();
  89. $newentry->evaluate = $record['evaluate'];
  90. $exists = 0;
  91. if ($iassign_ilm) { // Search if there is any iLM of this with this version
  92. $record_type = strtolower($record['type']);
  93. foreach ($iassign_ilm as $iassign) {
  94. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  95. if ($iassign->version == $record['version']) { // or with the one with the same version
  96. $exists = 1; // iLM found - exit with the last one, same version
  97. $strNot_installed += "\n" + ' <li>' + $record['name'] + ';' + $record['type'] + ';' + $record['version'] + ' </li>' + "\n";
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. if ($exists == 0) try { // iLM does not exists or it has old version
  104. $DB->insert_record("iassign_ilm", $newentry, false);
  105. } catch (Exception $e) {
  106. print 'Caught exception: ' . $e->getMessage() . "<br/>\n";
  107. }
  108. } // foreach ($records as $record)
  109. if ($strNot_installed != '') { // ATTENTION: this implies that Moodle administrator updated this iLM, please verify if it is really the current one.
  110. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  111. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  112. print ' <ul style="margin-top: 1rem;">' + "\n";
  113. print $strNot_installed;
  114. print ' </ul>' + "\n";
  115. print '</div>' + "\n";
  116. }
  117. // Verify if each iLM previously installed is present in fresh new iAssign. This does not means problem, perhaps the admin installed a particular iLM.
  118. $strNot_found = '';
  119. if ($iassign_ilm) { // exists iLM ($DB->get_records('iassign_ilm'))
  120. foreach ($iassign_ilm as $iassign) {
  121. $found = false;
  122. foreach ($records as $record) {
  123. if ($iassign->name == $record['name']) {
  124. $found = true;
  125. break;
  126. }
  127. }
  128. if (!$found) {
  129. $strNot_found .= '<li>' . $iassign->name . ' - <a href="' . $iassign->url . '" target="_blank">' . $iassign->url . '</a></li>' . "\n";
  130. $updateentry = new stdClass();
  131. $updateentry->id = $iassign->id;
  132. $updateentry->enable = 0;
  133. $updateentry->timemodified = time();
  134. $DB->update_record("iassign_ilm", $updateentry); // insert new iLM
  135. }
  136. } // foreach ($iassign_ilm as $iassign)
  137. } // if ($iassign_ilm)
  138. if ($strNot_found != '') {
  139. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  140. print get_string('upgrade_alert_iMA_msg', 'iassign'); // Updated but some previous iLM installed are not available in fresh iAssign
  141. print '<ul style="margin-top: 1rem;">' + "\n";
  142. print $strNot_found;
  143. print '</ul>' + "\n";
  144. print get_string('upgrade_alert_iMA_solution_pt1', 'iassign');
  145. print '<a href="'.new moodle_url('/admin/settings.php?section=modsettingiassign').'">' . get_string('upgrade_alert_iMA_solution_pt2', 'iassign') . '</a>.' . "\n";
  146. print '</div>' + "\n";
  147. }
  148. } // if ($oldversion < 2020070613)
  149. if ($oldversion < 2020070613) {
  150. $table = new xmldb_table('iassign_ilm');
  151. $field = new xmldb_field('reevaluate');
  152. $field->set_attributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', null, null, null);
  153. if (!$dbman->field_exists($table, $field)) { // if 'iassign_ilm.reevaluate' does not exist, then create this field
  154. $dbman->add_field($table, $field);
  155. }
  156. $iassign_ilm = $DB->get_records('iassign_ilm');
  157. foreach ($iassign_ilm as $iassign) { // If already installed iVProg/JS or iHanoi/JS set it with re-evaluate feature
  158. if ($iassign->type == 'HTML5' && ($iassign->name == 'iVProg' || $iassign->name == 'iHanoi')) {
  159. $updateentry = new stdClass();
  160. $updateentry->id = $iassign->id;
  161. $updateentry->reevaluate = 1;
  162. $updateentry->timemodified = time();
  163. $DB->update_record("iassign_ilm", $updateentry);
  164. break;
  165. }
  166. }
  167. } // if ($oldversion < 2020070612)
  168. if ($oldversion < 2020080300) { // new iHanoi
  169. // iassign_submission . grade : from 'BIGINT(11)' to 'real'
  170. if ($dbman->field_exists('iassign', 'grade')) {
  171. $sql = 'ALTER TABLE {iassign} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  172. $DB->execute($sql);
  173. }
  174. if ($dbman->field_exists('iassign_statement', 'grade')) {
  175. $sql = 'ALTER TABLE {iassign_statement} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  176. $DB->execute($sql);
  177. }
  178. if ($dbman->field_exists('iassign_submission', 'grade')) {
  179. $sql = 'ALTER TABLE {iassign_submission} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  180. $DB->execute($sql);
  181. }
  182. if ($dbman->field_exists('iassign_submission', 'previous_grade')) {
  183. // $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade REAL NOT NULL DEFAULT 0.0'; // Invalid use of NULL value
  184. $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade FLOAT DEFAULT 0.0';
  185. $DB->execute($sql);
  186. }
  187. // Update iHanoi, iVProg, iFractions and iGeom
  188. $records = array(
  189. array_combine( // iHanoi 1.0.20200803
  190. array('name', 'url', 'version', 'type', 'description',
  191. 'extension', 'file_jar', 'file_class', 'width', 'height',
  192. 'enable', 'timemodified', 'evaluate', 'reevaluate', 'author', 'timecreated'),
  193. 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)"}',
  194. 'ihn', 'ilm/iHanoi/1.0.20200803/ihanoi/', 'index.html', 1100, 500,
  195. 1, time(), 1, 1, $USER->id, time()))
  196. );
  197. $iassign_ilm = $DB->get_records('iassign_ilm');
  198. foreach ($records as $record) { // For each iLM in the current version of iAssign
  199. $newentry = new stdClass();
  200. $newentry->name = $record['name'];
  201. $newentry->version = $record['version'];
  202. $newentry->type = $record['type'];
  203. $newentry->url = $record['url'];
  204. $newentry->description = $record['description'];
  205. $newentry->extension = $record['extension'];
  206. $newentry->file_jar = $record['file_jar'];
  207. $newentry->file_class = $record['file_class'];
  208. $newentry->width = $record['width'];
  209. $newentry->height = $record['height'];
  210. $newentry->enable = $record['enable'];
  211. $newentry->timemodified = time();
  212. $newentry->evaluate = $record['evaluate'];
  213. $newentry->reevaluate = $record['reevaluate'];
  214. $newentry->author = $USER->id;
  215. $newentry->timecreated = time();
  216. $newentry->evaluate = $record['evaluate'];
  217. $exists = 0;
  218. $last_id = -1;
  219. if ($iassign_ilm) { // Search if there is any previous installed iLM that is also in the current version of iAssign
  220. $record_type = strtolower($record['type']);
  221. foreach ($iassign_ilm as $iassign) {
  222. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  223. if ($iassign->id > $last_id)
  224. $last_id = $iassign->id; // last ID => last version (hopefully)
  225. if ($iassign->version == $record['version']) { // or with the one with the same version
  226. $exists = 1; // iLM found - exit with the last one, same version
  227. $strNot_installed += "\n" + ' <li>' + $record['name'] + ';' + $record['type'] + ';' + $record['version'] + ' </li>' + "\n";
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. if ($exists == 0) { // iLM does not exists or it has old version
  234. $newentry->parent = $record['parent'];
  235. $DB->insert_record("iassign_ilm", $newentry, false);
  236. }
  237. } // foreach ($records as $record)
  238. if ($strNot_installed != '') {
  239. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' + "\n";
  240. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  241. print ' <ul style="margin-top: 1rem;">' + "\n";
  242. print $strNot_installed;
  243. print ' </ul>' + "\n";
  244. print '</div>' + "\n";
  245. }
  246. // s_iassign_ilm: atualizou iHanoi existente, mas nao era isso! Deveria ter inserido novo!
  247. // id name version type ... parent file_jar file_class width height ... evaluate reevaluate
  248. // 53 iHanoi 2 HTML5 ... 0 ilm/iHanoi/2/ihanoi/ index.html 1100 700 ... 1 0
  249. $iassign_ilm = $DB->get_records('iassign_ilm');
  250. foreach ($iassign_ilm as $iassign) { // for iLM iHanoi update the new field 'reevaluate' as 1
  251. if ($iassign->name == 'iHanoi' && $iassign->type == 'HTML5' && $iassign->reevaluate!=1) {
  252. $updateentry = new stdClass();
  253. $updateentry->id = $iassign->id;
  254. $updateentry->reevaluate = 1;
  255. $updateentry->timemodified = time();
  256. $DB->update_record("iassign_ilm", $updateentry);
  257. break;
  258. }
  259. }
  260. // iAssign savepoint reached.
  261. upgrade_mod_savepoint(true, 2020080300, 'iassign');
  262. } // if ($oldversion < 2020080300)
  263. //TODO Codigo do Igor para atualizar 'files.itemid' e 'iassign_statement.filesid':
  264. if ($oldversion < 2020102800) {
  265. // 1. encontrar os contextos dos arquivos do itarefa:
  266. $iassign_contexts_list = $DB->get_records_sql("SELECT contextid FROM {files} f " .
  267. " WHERE component='mod_iassign'");
  268. // 2. compor um array com todos os contextos encontrados:
  269. $contexts = array();
  270. foreach ($iassign_contexts_list as $iassign_context_item) {
  271. array_push($contexts, $iassign_context_item->contextid);
  272. }
  273. // 3. encontrar todas as atividades do itarefa, em que o arquivo não tenha o mesmo id do statement:
  274. $iassign_statement_list = $DB->get_records_sql("SELECT * FROM {iassign_statement} s " .
  275. " WHERE s.id != s.file");
  276. $fs = get_file_storage();
  277. // 4. percorrer o conjunto de atividades:
  278. foreach ($iassign_statement_list as $iassign_statement_activity_item) {
  279. // 5. encontrar o arquivo, considerando os possíveis contextos:
  280. $files = array();
  281. foreach ($contexts as $context) {
  282. $files = $fs->get_area_files($context, 'mod_iassign', 'exercise', $iassign_statement_activity_item->file);
  283. // 6. se o arquivo for encontrado, fazer uma cópia do conteúdo,
  284. // com o itemid novo, atualizar o iassign_statement, e apagar o arquivo antigo:
  285. if ($files) {
  286. foreach ($files as $value) {
  287. if ($value != null && $value->get_filename() != ".") {
  288. // 6.A. Fazer uma cópia:
  289. $newfile = $fs->create_file_from_storedfile(array('contextid' => $context, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement_activity_item->id), $value);
  290. // 6.B. Atualizar o registro da atividade para o arquivo novo:
  291. $update_entry = new stdClass();
  292. $update_entry->id = $iassign_statement_activity_item->id;
  293. $update_entry->file = $newfile->get_itemid();
  294. $DB->update_record("iassign_statement", $update_entry);
  295. // 6.C. Remover o arquivo antigo:
  296. $value->delete();
  297. } else if ($value != null && $value->get_filename() == ".") {
  298. // 6.C.I. Remover também os indicadores de diretório:
  299. $value->delete();
  300. }
  301. }
  302. break;
  303. }
  304. }
  305. }
  306. } // if ($oldversion < 2020102800)
  307. /// @Igor - adicionar a tabela iassign_allsubmissions
  308. if ($oldversion < 2020112000) {
  309. // Define table iassign_allsubmissions to be created.
  310. $table = new xmldb_table('iassign_allsubmissions');
  311. // Adding fields to table iassign_allsubmissions.
  312. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  313. $table->add_field('iassign_statementid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  314. $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  315. $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '16', null, XMLDB_NOTNULL, null, null);
  316. $table->add_field('grade', XMLDB_TYPE_FLOAT, null, null, null, null, null, null);
  317. $table->add_field('answer', XMLDB_TYPE_TEXT, 'long', null, null, null, null, null);
  318. // Adding keys to table iassign_allsubmissions.
  319. $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
  320. // Conditionally launch create table for iassign_allsubmissions.
  321. if (!$dbman->table_exists($table)) {
  322. $dbman->create_table($table);
  323. }
  324. // Adding fields to table iassign_ilm.
  325. $table = new xmldb_table('iassign_ilm');
  326. $field_editingbehavior = new xmldb_field('editingbehavior', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  327. if (!$dbman->field_exists($table, $field_editingbehavior))
  328. $dbman->add_field($table, $field_editingbehavior);
  329. $field_submissionbehavior = new xmldb_field('submissionbehavior', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  330. if (!$dbman->field_exists($table, $field_submissionbehavior))
  331. $dbman->add_field($table, $field_submissionbehavior);
  332. // Adding field to table iassign_statement:
  333. $table = new xmldb_table('iassign_statement');
  334. $field_store_all_submissions = new xmldb_field('store_all_submissions', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  335. if (!$dbman->field_exists($table, $field_store_all_submissions))
  336. $dbman->add_field($table, $field_store_all_submissions);
  337. }
  338. // log event -----------------------------------------------------
  339. if (class_exists('plugin_manager'))
  340. $pluginman = plugin_manager::instance();
  341. else
  342. $pluginman = core_plugin_manager::instance();
  343. $plugins = $pluginman->get_plugins();
  344. iassign_log::add_log('upgrade', 'version: ' . $plugins['mod']['iassign']->versiondisk);
  345. // log event -----------------------------------------------------
  346. return true;
  347. } // function xmldb_iassign_upgrade($oldversion)