upgrade.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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.5 2022/01/10
  17. * + Added new version of iVProg 2022_01_13_21_43
  18. * - v 1.5.4 2021/12/23
  19. * + Added new version of iVProg 2021_11_30_22_06
  20. * - v 1.5.3 2021/10/10
  21. * + Fixed several string concatenation from '+' to '.'
  22. * + Since now is allowed null file associated to "test" activity,
  23. * fixed UPDATE do "filesid=file" with try/catch and 'WHERE file IS NOT NULL"'
  24. * - v 1.5.2 2020/08/03
  25. * + Fixed 'ALTER TABLE' of 'iassign_submission.grade' from BIGINT(11) to REAL
  26. * + New version of iHanoi 1.0.20200803
  27. * - v 1.5.1 2020/05/28-30
  28. * + Avoid to update one iLM causing colision with other instance of the same iLM
  29. * - v 1.4 2013/09/19
  30. * + Insert general fields for iassign statement (grade, timeavaliable, timedue, preventlate, test, max_experiment).
  31. * + Change index field 'name' in 'iassign_ilm' table to index field 'name,version'.
  32. * - v 1.2 2013/08/30
  33. * + Change 'filearea' for new concept for files.
  34. * + Change path file for ilm, consider version in pathname.
  35. *
  36. * @author Leônidas O. Brandão
  37. * @author Patricia Alves Rodrigues
  38. * @author Igor Moreira Félix
  39. * @version v 1.5.1 2020/05/28-30
  40. * @version v 1.5 2019/03/13
  41. * @version v 1.4 2013/09/19
  42. * @package mod_iassign_db
  43. * @since 2010/12/21
  44. * @copyright iMath (http://www.matematica.br) and LInE (http://line.ime.usp.br) - Computer Science Dep. of IME-USP (Brazil)
  45. *
  46. * <b>License</b>
  47. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48. *
  49. * @param $oldversion Number of the old version.
  50. */
  51. require_once ($CFG->dirroot . '/mod/iassign/locallib.php');
  52. function xmldb_iassign_upgrade ($oldversion) {
  53. global $CFG, $DB, $USER;
  54. $dbman = $DB->get_manager();
  55. // Sequence of updates:
  56. // - 2020070613: structure
  57. // - 2020080300
  58. // - 2020120500
  59. // - 2020122900
  60. // - 2021020700
  61. // - 2021122300: new version iVProg "1.0.20211130"
  62. // - 2022011300: new version iVProg "1.0.20220113"
  63. if ($oldversion < 2020070613) {
  64. $table = new xmldb_table('iassign_submission');
  65. $field = new xmldb_field('previous_grade', XMLDB_TYPE_FLOAT, null, null, null, null, null);
  66. if (!$dbman->field_exists($table, $field)) {
  67. $dbman->add_field($table, $field);
  68. }
  69. $records = array( // iLM adjusted to iAssign 2020/08/03
  70. array_combine( // iGeom 5.9.22
  71. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  72. 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)),
  73. array_combine( // iGraf 4.4.0.10
  74. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  75. 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)),
  76. array_combine( // iHanoi 1.0.20200803
  77. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  78. 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)),
  79. array_combine( // iVProg 1.0.20220113/ - HTML5 - 2020
  80. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  81. array('iVProg', 'http://www.usp.br/line/ivprog/', '1.0.20220113', 'HTML5', '{"en":"Visual Interactive Programming on the Internet (HTML)","pt_br":"Programação visual interativa na Internet"}', 'ivph', 'ilm/iVProg/1.0.20220113/ivprog/', 'index.html', 900, 700, 1, time(), $USER->id, time(), 1, 1)),
  82. array_combine( // iFractions 0.1.20200221 - HTML5
  83. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  84. 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))
  85. );
  86. $iassign_ilm = $DB->get_records('iassign_ilm');
  87. $strNot_installed = '';
  88. foreach ($records as $record) { // this version 'iassign_ilm' does not has field 'reevaluate'
  89. $newentry = new stdClass();
  90. $newentry->name = $record['name'];
  91. $newentry->version = $record['version'];
  92. $newentry->type = $record['type'];
  93. $newentry->url = $record['url'];
  94. $newentry->description = $record['description'];
  95. $newentry->extension = $record['extension'];
  96. $newentry->file_jar = $record['file_jar'];
  97. $newentry->file_class = $record['file_class'];
  98. $newentry->width = $record['width'];
  99. $newentry->height = $record['height'];
  100. $newentry->enable = $record['enable'];
  101. $newentry->timemodified = time();
  102. $newentry->author = $USER->id;
  103. $newentry->timecreated = time();
  104. $newentry->evaluate = $record['evaluate'];
  105. $exists = 0;
  106. if ($iassign_ilm) { // Search if there is any iLM of this with this version
  107. $record_type = strtolower($record['type']);
  108. foreach ($iassign_ilm as $iassign) {
  109. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  110. if ($iassign->version == $record['version']) { // or with the one with the same version
  111. $exists = 1; // iLM found - exit with the last one, same version
  112. $strNot_installed .= "\n" . ' <li>' . $record['name'] . ';' . $record['type'] . ';' . $record['version'] . ' </li>' . "\n";
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. if ($exists == 0) try { // iLM does not exists or it has old version
  119. $DB->insert_record("iassign_ilm", $newentry, false);
  120. } catch (Exception $e) {
  121. print 'Caught exception: ' . $e->getMessage() . "<br/>\n";
  122. }
  123. } // foreach ($records as $record)
  124. if ($strNot_installed != '') { // ATTENTION: this implies that Moodle administrator updated this iLM, please verify if it is really the current one.
  125. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' . "\n";
  126. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  127. print ' <ul style="margin-top: 1rem;">' . "\n";
  128. print $strNot_installed;
  129. print ' </ul>' . "\n";
  130. print '</div>' . "\n";
  131. }
  132. // Verify if each iLM previously installed is present in fresh new iAssign. This does not means problem, perhaps the admin installed a particular iLM.
  133. $strNot_found = '';
  134. if ($iassign_ilm) { // exists iLM ($DB->get_records('iassign_ilm'))
  135. foreach ($iassign_ilm as $iassign) {
  136. $found = false;
  137. foreach ($records as $record) {
  138. if ($iassign->name == $record['name']) {
  139. $found = true;
  140. break;
  141. }
  142. }
  143. if (!$found) {
  144. $strNot_found .= '<li>' . $iassign->name . ' - <a href="' . $iassign->url . '" target="_blank">' . $iassign->url . '</a></li>' . "\n";
  145. $updateentry = new stdClass();
  146. $updateentry->id = $iassign->id;
  147. $updateentry->enable = 0;
  148. $updateentry->timemodified = time();
  149. $DB->update_record("iassign_ilm", $updateentry); // insert new iLM
  150. }
  151. } // foreach ($iassign_ilm as $iassign)
  152. } // if ($iassign_ilm)
  153. if ($strNot_found != '') {
  154. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' . "\n";
  155. print get_string('upgrade_alert_iMA_msg', 'iassign'); // Updated but some previous iLM installed are not available in fresh iAssign
  156. print '<ul style="margin-top: 1rem;">' . "\n";
  157. print $strNot_found;
  158. print '</ul>' . "\n";
  159. print get_string('upgrade_alert_iMA_solution_pt1', 'iassign');
  160. print '<a href="'.new moodle_url('/admin/settings.php?section=modsettingiassign').'">' . get_string('upgrade_alert_iMA_solution_pt2', 'iassign') . '</a>.' . "\n";
  161. print '</div>' . "\n";
  162. }
  163. } // if ($oldversion < 2020070613)
  164. if ($oldversion < 2020070613) {
  165. $table = new xmldb_table('iassign_ilm');
  166. $field = new xmldb_field('reevaluate');
  167. $field->set_attributes(XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', null, null, null);
  168. if (!$dbman->field_exists($table, $field)) { // if 'iassign_ilm.reevaluate' does not exist, then create this field
  169. $dbman->add_field($table, $field);
  170. }
  171. $iassign_ilm = $DB->get_records('iassign_ilm');
  172. foreach ($iassign_ilm as $iassign) { // If already installed iVProg/JS or iHanoi/JS set it with re-evaluate feature
  173. if ($iassign->type == 'HTML5' && ($iassign->name == 'iVProg' || $iassign->name == 'iHanoi')) {
  174. $updateentry = new stdClass();
  175. $updateentry->id = $iassign->id;
  176. $updateentry->reevaluate = 1;
  177. $updateentry->timemodified = time();
  178. $DB->update_record("iassign_ilm", $updateentry);
  179. break;
  180. }
  181. }
  182. } // if ($oldversion < 2020070612)
  183. $strNot_installed = '';
  184. if ($oldversion < 2020080300) { // new iHanoi
  185. // iassign_submission . grade : from 'BIGINT(11)' to 'real'
  186. if ($dbman->field_exists('iassign', 'grade')) {
  187. $sql = 'ALTER TABLE {iassign} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  188. $DB->execute($sql);
  189. }
  190. if ($dbman->field_exists('iassign_statement', 'grade')) {
  191. $sql = 'ALTER TABLE {iassign_statement} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  192. $DB->execute($sql);
  193. }
  194. if ($dbman->field_exists('iassign_submission', 'grade')) {
  195. $sql = 'ALTER TABLE {iassign_submission} CHANGE grade grade FLOAT NOT NULL DEFAULT 0.0';
  196. $DB->execute($sql);
  197. }
  198. if ($dbman->field_exists('iassign_submission', 'previous_grade')) {
  199. // $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade REAL NOT NULL DEFAULT 0.0'; // Invalid use of NULL value
  200. $sql = 'ALTER TABLE {iassign_submission} CHANGE previous_grade previous_grade FLOAT DEFAULT 0.0';
  201. $DB->execute($sql);
  202. }
  203. // Update iHanoi
  204. $records = array(
  205. array_combine( // iHanoi 1.0.20200803
  206. array('name', 'url', 'version', 'type', 'description',
  207. 'extension', 'file_jar', 'file_class', 'width', 'height',
  208. 'enable', 'timemodified', 'evaluate', 'reevaluate', 'author', 'timecreated'),
  209. 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)"}',
  210. 'ihn', 'ilm/iHanoi/1.0.20200803/ihanoi/', 'index.html', 1100, 500,
  211. 1, time(), 1, 1, $USER->id, time()))
  212. );
  213. $iassign_ilm = $DB->get_records('iassign_ilm');
  214. foreach ($records as $record) { // For each iLM in the current version of iAssign
  215. $newentry = new stdClass();
  216. $newentry->name = $record['name'];
  217. $newentry->version = $record['version'];
  218. $newentry->type = $record['type'];
  219. $newentry->description = $record['description'];
  220. $newentry->url = $record['url'];
  221. $newentry->extension = $record['extension'];
  222. $newentry->file_jar = $record['file_jar'];
  223. $newentry->file_class = $record['file_class'];
  224. $newentry->width = $record['width'];
  225. $newentry->height = $record['height'];
  226. $newentry->enable = $record['enable'];
  227. $newentry->timemodified = time();
  228. $newentry->evaluate = $record['evaluate'];
  229. $newentry->reevaluate = $record['reevaluate'];
  230. $newentry->author = $USER->id;
  231. $newentry->timecreated = time();
  232. $exists = 0;
  233. $last_id = -1;
  234. if ($iassign_ilm) { // Search if there is any previous installed iLM that is also in the current version of iAssign
  235. $record_type = strtolower($record['type']);
  236. foreach ($iassign_ilm as $iassign) {
  237. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  238. if ($iassign->id > $last_id)
  239. $last_id = $iassign->id; // last ID => last version (hopefully)
  240. if ($iassign->version == $record['version']) { // or with the one with the same version
  241. $exists = 1; // iLM found - exit with the last one, same version
  242. $strNot_installed .= "\n" . ' <li>' . $record['name'] . ';' . $record['type'] . ';' . $record['version'] . ' </li>' . "\n";
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. if ($exists == 0) { // iLM does not exists or it has old version
  249. $newentry->parent = $record['parent'];
  250. $DB->insert_record("iassign_ilm", $newentry, false);
  251. }
  252. } // foreach ($records as $record)
  253. if ($strNot_installed != '') {
  254. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' . "\n";
  255. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  256. print ' <ul style="margin-top: 1rem;">' . "\n";
  257. print $strNot_installed;
  258. print ' </ul>' . "\n";
  259. print '</div>' . "\n";
  260. }
  261. // iassign_ilm: atualizou iHanoi existente, mas nao era isso! Deveria ter inserido novo!
  262. // id name version type ... parent file_jar file_class width height ... evaluate reevaluate
  263. // 53 iHanoi 2 HTML5 ... 0 ilm/iHanoi/2/ihanoi/ index.html 1100 700 ... 1 0
  264. $iassign_ilm = $DB->get_records('iassign_ilm');
  265. foreach ($iassign_ilm as $iassign) { // for iLM iHanoi update the new field 'reevaluate' as 1
  266. if ($iassign->name == 'iHanoi' && $iassign->type == 'HTML5' && $iassign->reevaluate!=1) {
  267. $updateentry = new stdClass();
  268. $updateentry->id = $iassign->id;
  269. $updateentry->reevaluate = 1;
  270. $updateentry->timemodified = time();
  271. $DB->update_record("iassign_ilm", $updateentry);
  272. break;
  273. }
  274. }
  275. // iAssign savepoint reached.
  276. upgrade_mod_savepoint(true, 2020080300, 'iassign');
  277. } // if ($oldversion < 2020080300)
  278. //TODO Codigo do Igor para atualizar 'files.itemid' e 'iassign_statement.filesid':
  279. if ($oldversion < 2020120500) {
  280. // Adding field iassing_statement.filesid
  281. $table = new xmldb_table('iassign_statement');
  282. $field_filesid = new xmldb_field('filesid', XMLDB_TYPE_CHAR, '255', null, null, null, null, null);
  283. if (!$dbman->field_exists($table, $field_filesid))
  284. $dbman->add_field($table, $field_filesid);
  285. // Updating all registers from iassing_statement.filesid
  286. try {
  287. //2021/10/10: $DB->execute("UPDATE {iassign_statement} SET filesid = file");
  288. $DB->execute("UPDATE {iassign_statement} SET filesid = file WHERE file IS NOT NULL"); //2021/10/10:
  289. } catch (Exception $ex) { } // avoid error with 'text' activity (activity with no file!)
  290. // 1. encontrar os contextos dos arquivos do itarefa:
  291. $iassign_contexts_list = $DB->get_records_sql("SELECT DISTINCT contextid FROM {files} f WHERE component='mod_iassign'");
  292. // 2. compor um array com todos os contextos encontrados:
  293. $contexts = array();
  294. foreach ($iassign_contexts_list as $iassign_context_item) {
  295. array_push($contexts, $iassign_context_item->contextid);
  296. }
  297. // 3. encontrar todas as atividades do itarefa, em que o arquivo não tenha o mesmo id do statement:
  298. $iassign_statement_list = $DB->get_records_sql("SELECT * FROM {iassign_statement} s WHERE s.id != s.file");
  299. $fs = get_file_storage();
  300. // 4. percorrer o conjunto de atividades:
  301. foreach ($iassign_statement_list as $iassign_statement_activity_item) {
  302. // 5. encontrar o arquivo, considerando os possíveis contextos:
  303. $files = array();
  304. foreach ($contexts as $context) {
  305. $files = $fs->get_area_files($context, 'mod_iassign', 'exercise', $iassign_statement_activity_item->file);
  306. // 6. se o arquivo for encontrado, fazer uma cópia do conteúdo,
  307. // com o itemid novo, atualizar o iassign_statement, e apagar o arquivo antigo:
  308. if ($files) {
  309. foreach ($files as $value) {
  310. if ($value != null && $value->get_filename() != ".") {
  311. // 6.A. Fazer uma cópia:
  312. $newfile = $fs->create_file_from_storedfile(array('contextid' => $context, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement_activity_item->id), $value);
  313. // 6.B. Atualizar o registro da atividade para o arquivo novo:
  314. $update_entry = new stdClass();
  315. $update_entry->id = $iassign_statement_activity_item->id;
  316. $update_entry->file = $newfile->get_itemid();
  317. $update_entry->filesid = $newfile->get_itemid();
  318. $DB->update_record("iassign_statement", $update_entry);
  319. // 6.C. Remover o arquivo antigo:
  320. $value->delete();
  321. }
  322. else if ($value != null && $value->get_filename() == ".") {
  323. // 6.C.I. Remover também os indicadores de diretório:
  324. $value->delete();
  325. }
  326. }
  327. break;
  328. } // if ($files)
  329. } // foreach ($contexts as $context)
  330. } // foreach ($iassign_statement_list as $iassign_statement_activity_item)
  331. } // if ($oldversion < 2020120500)
  332. /// @Igor - add table 'iassign_allsubmissions'
  333. if ($oldversion < 2020122900) {
  334. // Define table iassign_allsubmissions to be created.
  335. $table = new xmldb_table('iassign_allsubmissions');
  336. // Adding fields to table iassign_allsubmissions.
  337. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  338. $table->add_field('iassign_statementid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  339. $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
  340. $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '16', null, XMLDB_NOTNULL, null, null);
  341. $table->add_field('grade', XMLDB_TYPE_FLOAT, null, null, null, null, null, null);
  342. $table->add_field('answer', XMLDB_TYPE_TEXT, 'long', null, null, null, null, null);
  343. // Adding keys to table iassign_allsubmissions.
  344. $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
  345. // Conditionally launch create table for iassign_allsubmissions.
  346. if (!$dbman->table_exists($table)) {
  347. $dbman->create_table($table);
  348. }
  349. // Adding fields to table iassign_ilm.
  350. $table = new xmldb_table('iassign_ilm');
  351. $field_editingbehavior = new xmldb_field('editingbehavior', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  352. if (!$dbman->field_exists($table, $field_editingbehavior))
  353. $dbman->add_field($table, $field_editingbehavior);
  354. $field_submissionbehavior = new xmldb_field('submissionbehavior', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  355. if (!$dbman->field_exists($table, $field_submissionbehavior))
  356. $dbman->add_field($table, $field_submissionbehavior);
  357. $field_action_buttons = new xmldb_field('action_buttons', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1', null);
  358. if (!$dbman->field_exists($table, $field_action_buttons))
  359. $dbman->add_field($table, $field_action_buttons);
  360. // Adding field to table iassign_statement:
  361. $table = new xmldb_table('iassign_statement');
  362. $field_store_all_submissions = new xmldb_field('store_all_submissions', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', null);
  363. if (!$dbman->field_exists($table, $field_store_all_submissions))
  364. $dbman->add_field($table, $field_store_all_submissions);
  365. // Update new fields for previous installed iLM:
  366. $iassign_ilm = $DB->get_records('iassign_ilm');
  367. foreach ($iassign_ilm as $iassign) {
  368. $updateentry = new stdClass();
  369. $updateentry->id = $iassign->id;
  370. if (($iassign->name == 'iHanoi' && $iassign->type == 'HTML5')) {
  371. $updateentry->editingbehavior = 0;
  372. $updateentry->submissionbehavior = 0;
  373. }
  374. if (($iassign->name == 'iGeom' && $iassign->type == 'Java')) {
  375. $updateentry->editingbehavior = 0;
  376. $updateentry->submissionbehavior = 0;
  377. }
  378. if (($iassign->name == 'iVProg' && $iassign->type == 'HTML5')) {
  379. $updateentry->editingbehavior = 1;
  380. $updateentry->submissionbehavior = 0;
  381. }
  382. if (($iassign->name == 'iFractions' && $iassign->type == 'HTML5')) {
  383. $updateentry->editingbehavior = 0;
  384. $updateentry->submissionbehavior = 1;
  385. }
  386. if (($iassign->name == 'Risko' && $iassign->type == 'Java')) {
  387. $updateentry->editingbehavior = 1;
  388. $updateentry->submissionbehavior = 0;
  389. }
  390. if (isset($updateentry->editingbehavior)) {
  391. $updateentry->timemodified = time();
  392. $DB->update_record("iassign_ilm", $updateentry);
  393. }
  394. }
  395. // Add iassign_allsubmissions table
  396. $table = new xmldb_table('iassign_allsubmissions');
  397. if (!$dbman->table_exists($table)) {
  398. $field1 = new xmldb_field('id');
  399. $field1->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
  400. $field2 = new xmldb_field('iassign_statementid');
  401. $field2->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null);
  402. $field3 = new xmldb_field('userid');
  403. $field3->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null);
  404. $field4 = new xmldb_field('timecreated');
  405. $field4->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null);
  406. $field5 = new xmldb_field('grade');
  407. $field5->set_attributes(XMLDB_TYPE_FLOAT, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null);
  408. $field6 = new xmldb_field('answer');
  409. $field6->set_attributes(XMLDB_TYPE_TEXT, 'long', null, null, null, null, 'type');
  410. $table->addIndex($field1);
  411. $table->addField($field2);
  412. $table->addField($field3);
  413. $table->addField($field4);
  414. $table->addField($field5);
  415. $table->addField($field6);
  416. $dbman->create_table($table);
  417. } // if (!$dbman->table_exists($table))
  418. // Update file_jar field:
  419. try {
  420. $DB->execute("ALTER TABLE {iassign_ilm} DROP INDEX {iassilm_fil_uix}");
  421. } catch (Exception $ex) {}
  422. // Create $CFG->dataroot/temp/iassign_files
  423. $tempfilespath = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp';
  424. if (!file_exists($tempfilespath)) {
  425. mkdir($tempfilespath, 0777, true);
  426. }
  427. $iassignfilespath = $tempfilespath . DIRECTORY_SEPARATOR . 'iassign_files';
  428. if (!file_exists($iassignfilespath)) {
  429. mkdir($iassignfilespath, 0777, true);
  430. }
  431. } // if ($oldversion < 2020122900)
  432. if ($oldversion < 2021020700) {
  433. // Update SAW iVProg to inform reevalute is enabled:
  434. $updateentry = new stdClass();
  435. $updateentry->id = 57;
  436. $updateentry->reevaluate = 1;
  437. $updateentry->timemodified = time();
  438. $DB->update_record("iassign_ilm", $updateentry); // insert new iLM
  439. }
  440. $strNot_installed = '';
  441. if ($oldversion < 2021122300) { // 2021/12/23
  442. // Update iVProg
  443. $records = array(
  444. array_combine( // iVProg 1.0.20220113/ - HTML5 - 2021/12/30
  445. array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
  446. array('iVProg', 'http://www.usp.br/line/ivprog/', '1.0.20220113', 'HTML5', '{"en":"Visual Interactive Programming on the Internet (HTML)","pt_br":"Programação visual interativa na Internet"}',
  447. 'ivph', 'ilm/iVProg/1.0.20220113/ivprog/', 'index.html', 900, 700, 1, time(), $USER->id, time(), 1, 1))
  448. );
  449. $iassign_ilm = $DB->get_records('iassign_ilm');
  450. foreach ($records as $record) { // For each iLM in the current version of iAssign
  451. $newentry = new stdClass();
  452. $newentry->name = $record['name'];
  453. $newentry->url = $record['url'];
  454. $newentry->version = $record['version'];
  455. $newentry->type = $record['type'];
  456. $newentry->description = $record['description'];
  457. $newentry->extension = $record['extension'];
  458. $newentry->file_jar = $record['file_jar'];
  459. $newentry->file_class = $record['file_class'];
  460. $newentry->width = $record['width'];
  461. $newentry->height = $record['height'];
  462. $newentry->enable = $record['enable'];
  463. $newentry->timemodified = time();
  464. $newentry->author = $USER->id;
  465. $newentry->timecreated = time();
  466. $newentry->evaluate = $record['evaluate'];
  467. $newentry->reevaluate = $record['reevaluate'];
  468. $exists = 0;
  469. $last_id = -1;
  470. if ($iassign_ilm) { // Search if there is any previous installed iLM that is also in the current version of iAssign
  471. $record_type = strtolower($record['type']);
  472. foreach ($iassign_ilm as $iassign) {
  473. if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
  474. if ($iassign->id > $last_id)
  475. $last_id = $iassign->id; // last ID => last version (hopefully)
  476. if ($iassign->version == $record['version']) { // or with the one with the same version
  477. $exists = 1; // iLM found - exit with the last one, same version
  478. $strNot_installed .= "\n" . ' <li>' . $record['name'] . ';' . $record['type'] . ';' . $record['version'] . ' </li>' . "\n";
  479. break;
  480. }
  481. }
  482. }
  483. $newentry->parent = $last_id; // parent is the last iLM found (corresponde to the smallest ID)
  484. }
  485. else
  486. $newentry->parent = -1; // first iLM of this one
  487. if ($exists == 0) { // iLM does not exists or it has old version
  488. // $newentry->parent = $record['parent'];
  489. $DB->insert_record("iassign_ilm", $newentry, false);
  490. }
  491. } // foreach ($records as $record)
  492. if ($strNot_installed != '') {
  493. print '<div class="alert alert-warning alert-block fade in " role="alert" data-aria-autofocus="true" tabindex="0" >' . "\n";
  494. print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
  495. print ' <ul style="margin-top: 1rem;">' . "\n";
  496. print $strNot_installed;
  497. print ' </ul>' . "\n";
  498. print '</div>' . "\n";
  499. }
  500. // iassign_ilm: atualizou iHanoi existente, mas nao era isso! Deveria ter inserido novo!
  501. // id name version type ... parent file_jar file_class width height ... evaluate reevaluate
  502. // 53 iHanoi 2 HTML5 ... 0 ilm/iHanoi/2/ihanoi/ index.html 1100 700 ... 1 0
  503. $iassign_ilm = $DB->get_records('iassign_ilm');
  504. foreach ($iassign_ilm as $iassign) { // for iLM iHanoi update the new field 'reevaluate' as 1
  505. if ($iassign->name == 'iHanoi' && $iassign->type == 'HTML5' && $iassign->reevaluate!=1) {
  506. $updateentry = new stdClass();
  507. $updateentry->id = $iassign->id;
  508. $updateentry->reevaluate = 1;
  509. $updateentry->timemodified = time();
  510. $DB->update_record("iassign_ilm", $updateentry);
  511. break;
  512. }
  513. }
  514. // iAssign savepoint reached.
  515. upgrade_mod_savepoint(true, 2021122300, 'iassign');
  516. } // if ($oldversion < 2021122300)
  517. // log event -----------------------------------------------------
  518. if (class_exists('plugin_manager'))
  519. $pluginman = plugin_manager::instance();
  520. else
  521. $pluginman = core_plugin_manager::instance();
  522. $plugins = $pluginman->get_plugins();
  523. iassign_log::add_log('upgrade', 'version: ' . $plugins['mod']['iassign']->versiondisk);
  524. // log event -----------------------------------------------------
  525. return true;
  526. } // function xmldb_iassign_upgrade($oldversion)