restore_iassign_stepslib.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * Define all the restore steps that will be used by the restore_iassign_activity_task
  4. *
  5. * It uses the 'iassign.xml' with XML code to rebuild the block activities {iassign} and {iassign_statement} activities.
  6. *
  7. * After 2021 we stated to use the file associated to each activity with: {iassign_statement}.filesid={files}.id and {iassign_statement}.id={files}.itemid
  8. *
  9. * From 2.0 version (2023/04/25) we use the trick to (artificially) register on {iassign_statement}
  10. * tags (<statement...>...</statement>) additional fields from {iassign_ilm}, with the name of
  11. * the used iLM (fields: name, version, type, extension, url).
  12. * These additional fields was artificially introduced by 'backup_iassign_stepslib.php' to the bellow function
  13. * "process_iassign_statement($data)" recover the better iLM to be associated with each activity
  14. * (it uses auxiliary function "get_destination_ilmid($data)").
  15. *
  16. * @author Patricia Alves Rodrigues
  17. * @author Leo^nidas de Oliveira Branda~o
  18. * @version v 2.0 2023
  19. * @version v 1.0 2012
  20. * @package mod_iassign_backup
  21. * @since 2012
  22. * @cite iMath (http://www.matematica.br) - LInE (www.usp.br/line) - Computer Science Dep. of IME-USP (Brazil)
  23. *
  24. * <b>License</b>
  25. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. *
  27. * @see restore_activity_structure_step
  28. * @calledby /backup/util/plan/restore_structure_step.class.php: restore_iassign_activity_structure_step->process_iassign_statement()
  29. *
  30. */
  31. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  32. defined('MOODLE_INTERNAL') || die();
  33. /// Define the complete assignment structure for restore, with file and id annotations.
  34. // @see restore_activity_structure_step
  35. class restore_iassign_activity_structure_step extends restore_activity_structure_step {
  36. // /var/www/html/saw_limpo/backup/moodle2/restore_plugin.class.php
  37. // + abstract class restore_plugin :
  38. // * protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null)
  39. // /var/www/html/saw_limpo/backup/moodle2/restore_subplugin.class.php <- 'add_related_files(.)' alterei
  40. // * public function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null)
  41. // /var/www/html/saw_limpo/backup/util/plan/restore_structure_step.class.php <- 'add_related_files(.)' alterei!
  42. // + abstract class restore_structure_step extends restore_step
  43. // * public function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) : is no called with mappingitemname="files"...
  44. // /var/www/html/saw_limpo/backup/moodle2/restore_stepslib.php
  45. // + abstract class restore_activity_structure_step extends restore_structure_step
  46. var $source_course_id = null; // to register the {course}.id from the original course
  47. var $old_id = null; // to register the old {iassign_statement}.id
  48. // Define the structure of the restore workflow
  49. // @return void Adds support for the 'exercise' path that is common to all the activities.
  50. protected function define_structure () {
  51. $paths = array();
  52. // To know if we are including userinfo
  53. $userinfo = $this->get_setting_value('userinfo');
  54. // Define each element separated
  55. $paths[] = new restore_path_element('iassign', '/activity/iassign');
  56. $paths[] = new restore_path_element('iassign_statement', '/activity/iassign/statements/statement');
  57. //x $paths[] = new restore_path_element('iassign', '/exercise/iassign'); //2025
  58. //x $paths[] = new restore_path_element('iassign_statement', '/exercise/iassign/statements/statement'); //2025
  59. if ($userinfo) {
  60. $iassign_submissions = new restore_path_element('iassign_submission', '/activity/iassign/statements/statement/iassign_submissions/iassign_submission');
  61. $paths[] = $iassign_submissions;
  62. $iassign_submission_comments = new restore_path_element('iassign_submission_comment', '/activity/iassign/statements/statement/iassign_submissions/iassign_submission/iassign_submission_comments/iassign_submission_comment');
  63. $paths[] = $iassign_submission_comments;
  64. }
  65. return $this->prepare_activity_structure($paths);
  66. }
  67. // Process an iAssign activity (restore it)
  68. // @param object $data The data in object form
  69. protected function process_iassign ($data) {
  70. global $DB, $CFG;
  71. $data = (object) $data;
  72. $oldid = $data->id; // source {iassign}.id
  73. $this->old_id = $oldid; // to register the old {iassign_statement}.id, to be used by 'after_execute()' bellow //xxxxxxxxxxxxxxx
  74. // In the first time reaching this point (first iAssign activity processed), register the source {course}.id
  75. // {iassign}.course = {course}.id - {iassign_statement}.iassignid = {iassign}.id
  76. if ($this->source_course_id==null)
  77. $this->source_course_id = $data->course; // source {course}.id
  78. $data->course = $this->get_courseid(); // target {course}.id: new {iassign} uses the new {course}.id
  79. if ($CFG->debugdisplay) { // debug messages is turned on
  80. print "restore_iassign_stepslib.php!process_iassign(.): this->source_course_id=" . $this->source_course_id . " is the {course}.id of source course! destination course.id=" . $data->course . "<br/>"; //DEBUG
  81. }
  82. // Create a copy
  83. $newitemid = $DB->insert_record('iassign', $data); // insert in 'iassign'
  84. $this->apply_activity_instance($newitemid);
  85. }
  86. // Considering the destination Moodle, try to find the nearest iLM to a given {iassign_statement} activity
  87. // Given artificial {iassign_statement} fields provided by 'backup_iassign_stepslib.php!define_structure()' (ilm_name,version,type,extension)
  88. // find the destination Moodle the closest iLM (considering those matching lower(ilm_name) and lower(extension)).
  89. // @param $data is from table {iassign_statement}
  90. protected function get_destination_ilmid ($data) {
  91. global $DB, $CFG;
  92. // Since iAssign X the backup of each {iassign_statement} receive the additional fields: "ilm_name", "version", "type", "extension"
  93. if (!isset($data->ilm_name) || !isset($data->version) || !isset($data->type) || !isset($data->extension)) {
  94. if ($CFG->debugdisplay) {
  95. print "Error in mod/iassign/backup/moodle2/restore_iassign_stepslib.php!get_destination_ilmid(data): ilm_name, version, type or extension empty<br/>\n";
  96. print " - ilm_name=" . (!isset($data->ilm_name)?'null':$data->ilm_name) . ", version=" . (!isset($data->version)?'null':$data->version) . ", type=" .
  97. (!isset($data->type)?'null':$data->type) . ", extension=" . (!isset($data->extension)?'null':$data->extension) . "<br/>\ndata=";
  98. var_dump($data); // ja imprime
  99. }
  100. return NULL; // launch warning the Admin that iAssign is missing some iLM...
  101. }
  102. // Select 1 from table_name will return false if the table does not exist.
  103. $sql_query = "SELECT 1 FROM {iassign} LIMIT 1";
  104. $iassign = $DB->get_records_sql($sql_query);
  105. if ($iassign == FALSE) { // iAssign is not installed!
  106. if ($CFG->debugdisplay) {
  107. print "Error in mod/iassign/backup/moodle2/restore_iassign_stepslib.php!get_destination_ilmid(data): iAssign is NOT installed yet! Ask admin to install it<br/>\n";
  108. }
  109. return NULL; // launch warning the Admin that iAssign is missing some iLM...
  110. }
  111. // Artificials field inserted into the {iassign_statement} entry:
  112. $origin_ilm_name = strtolower($data->ilm_name); // field {iassign_ilm}.name
  113. $origin_version = strtolower($data->version); // field {iassign_ilm}.version
  114. $origin_type = strtolower($data->type); // field {iassign_ilm}.type
  115. $origin_extension = strtolower($data->extension); // field {iassign_ilm}.extension
  116. // $iassign_ilm = $DB->get_records('iassign_ilm', array('name' => $origin_ilm_name)); // search for all with this 'name'
  117. // Search in the current Moodle (target) a iLM similar to the origin Moodle
  118. $sql_query = "SELECT id, LOWER(name) AS ilm_name, LOWER(version) AS version, LOWER(type) AS type, LOWER(extension) AS extension " .
  119. " FROM {iassign_ilm} WHERE name='" . $origin_ilm_name . "' AND LOWER(extension)='" . $origin_extension . "' ORDER BY id DESC";
  120. $iassign_ilm = $DB->get_records_sql($sql_query);
  121. foreach ($iassign_ilm as $one_iassign_ilm) { // preference to the same 'type' and 'version'
  122. if ($one_iassign_ilm->type == $origin_type && $one_iassign_ilm->version == $origin_version)
  123. return $one_iassign_ilm->id; // return the {iassign_ilm} with bigger ID
  124. }
  125. foreach ($iassign_ilm as $one_iassign_ilm) { // accept only the same 'type'
  126. if ($one_iassign_ilm->type == $origin_type)
  127. return $one_iassign_ilm->id; // return the {iassign_ilm} with bigger ID
  128. }
  129. foreach ($iassign_ilm as $one_iassign_ilm) { // accept any iLM...
  130. return $one_iassign_ilm->id; // return the {iassign_ilm} with bigger ID
  131. }
  132. return NULL; // launch warning the Admin that iAssign is missing some iLM...
  133. }
  134. // Process a {iassign_statement} restoration
  135. // Table {iassign_statement} : id, name, iassignid, type_iassign, proposition, author_name, author_modified_name, iassign_ilmid, file ..., fileid
  136. // Attention, in order to perform the correct association {iassign_ilm}.id from origin to destination
  137. // the 'backup_iassign_stepslib.php' used all {iassign_statement} field besides some from {iassign_ilm}
  138. // on each '<statement id="X">... </statement>' in 'iassign.xml'.
  139. // From the original Moodle was used {iassign_statement}.* and from {iassign_ilm}: name as ilm_name, version, type, extension
  140. // @param object $data The data in object form
  141. // @calledby /backup/util/plan/restore_structure_step.class.php : final public function process($data) : $rdata=$object->$method($data['tags']);
  142. // @calledby /backup/util/plan/restore_structure_step.class.php : line 418, call to restore_iassign_activity_structure_step->after_execute()
  143. protected function process_iassign_statement ($data) {
  144. global $DB, $CFG;
  145. require_once($CFG->dirroot . '/mod/iassign/locallib.php');
  146. $data = (object) $data;
  147. // Since iAssign X the backup of each {iassign_statement} receive the additional fields: "ilm_name", "version", "type", "extension"
  148. if (isset($data->ilm_name)) $ilm_name = $data->ilm_name;
  149. else $ilm_name = "empty";
  150. // If we find the iLM on destination, change the {iassign_statement}.iassign_ilmid = {iassign_ilm}.id
  151. $destination_ilmid = $this->get_destination_ilmid($data); //TODO: give an error message in case of iLM not found NULL
  152. if ($destination_ilmid != NULL)
  153. $data->iassign_ilmid = $destination_ilmid; // fix {iassign_statement}.iassign_ilmid from origin to destination
  154. else { // launch warning the Admin that iAssign is missing some iLM...
  155. if ($CFG->debugdisplay)
  156. print "process_iassign_statement(data): returned empty from get_destination_ilmid(.)<br/>\n";
  157. $data->iassign_ilmid = 0; // point to the first one...
  158. $destination_ilmid = 0; // point to the first one...
  159. $error_msg = "Potential problem: the backup file has one iAssign activity to which the iLM " . $ilm_name . " was not found.<br/>\n";
  160. $error_msg .= "Please, try to install " . $ilm_name . " from the URL " . $data->url;
  161. // With any of the bellow warning, the restoring process is interrupted!
  162. // throw new restore_step_exception($error_msg);
  163. // debugging('Error in restore_iassign_stepslib.php!process_iassign_statement(.): the iLM ' . $ilm_name . ' is missing!', DEBUG_DEVELOPER);
  164. }
  165. $oldid = $data->id; // from source {course}.id: old {iassign_statement}.id
  166. $msgD = "";
  167. $msgD1 = "restore_iassign_stepslib.php!process_iassign_statement(.): <br/>" .
  168. " - original {iassign_statement}.id=" . $oldid . "; this->source_course_id=" . $this->source_course_id . "<br/>\n" . //DEBUG
  169. " - original {iassign_ilm}.name=" . $ilm_name . " (nome usado para buscar iMA semelhante) destination_ilmid=" . $destination_ilmid . "<br/>\n"; //DEBUG
  170. if ($CFG->debugdisplay) { // debug messages is turned on
  171. print $msgD1; //DEBUG
  172. }
  173. $msgD .= $msgD1; // append
  174. // In /var/www/html/saw_limpo/backup/moodle2/restore_plugin.class.php
  175. // + abstract class restore_plugin: protected 'apply_date_offset(.)' is a Moodle function to ajust source date to current date
  176. // In /var/www/html/saw_limpo/backup/moodle2/restore_stepslib.php
  177. // + abstract class restore_activity_structure_step extends restore_structure_step : 4873/6612
  178. // In /var/www/html/saw_limpo/backup/util/plan/restore_structure_step.class.php : 30/549
  179. // + abstract class restore_structure_step extends restore_step:
  180. // public function get_new_parentid($itemname): 199/549 - Returns the latest (parent) old id mapped by one pathelement
  181. // public function get_mappingid($itemname, $oldid, $ifnotfound = false): 210/549
  182. // // Return the new id of a mapping for the given itemname - itemname is the Moodle table name as {user}, {files}
  183. // // @param string $itemname the type of item - "the type of item you wish to backup - for example, 'activities and resources'" - "type of item" e' o tipo do item 'activities and resources' - como "iassign", "files", "assignment"...???
  184. // // @param int $oldid the item ID from the backup
  185. // +
  186. // /var/www/html/saw_limpo/backup/moodle2/restore_plugin.class.php
  187. // * To send ids pairs to backup_ids_table and to store them into paths
  188. // * This method will send the given itemname and old/new ids to the backup_ids_temp table, and, at the same time, will save the new id
  189. // * into the corresponding restore_path_element for easier access by children. Also will inject the known old context id for the task
  190. // * in case it's going to be used for restoring files later
  191. // + protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null)
  192. $str_aux = "";
  193. //$aux_file = $this->get_mappingid('files', $data->file); // get the new ID corresponding to the old {files}.id=$data->file
  194. //$files_id = $this->get_mappingid('files', $data->filesid); //TODO: depois precisa processar {files}.itemid para este {iassign_statement}.id!
  195. //if (empty($aux_file) || !$aux_file) { $str_aux.="(1)"; $aux_file = $this->get_mappingid('iassign_statement', $data->file); } // get the new ID corresponding to the old {files}.id=$data->file
  196. //if (empty($files_id) || !$files_id) { $str_aux.="(2)"; $files_id = $this->get_mappingid('iassign_statement', $data->file); } // get the new ID corresponding to the old {files}.id=$data->file
  197. $aux_file = $data->file; // get the new ID corresponding to the old {files}.id=$data->file
  198. $files_id = $data->filesid; //TODO: depois precisa processar {files}.itemid para este {iassign_statement}.id!
  199. $msgD1 = " - original {iassign_statement}.id=" . $data->id . " (old id) :: " . //DEBUG
  200. " - get_new_parentid('files')=" . $this->get_new_parentid('files') . "'<br/>\n" . //DEBUG
  201. " - original {iassign_statement}.file=" . $data->file . " -> " . $data->id . " :: str_aux=" . $str_aux . ", after get_mappingid to 'file' id='" . $aux_file . "'<br/>\n" . //DEBUG
  202. " - original {iassign_statement}.filesid=" . $data->filesid . " :: str_aux=" . $str_aux . ", after get_mappingid to 'filesid' id='" . $files_id . "'<br/>\n"; //DEBUG
  203. $msgD1 .= " * timeavailable=" . $data->timeavailable . " -> apply_date_offset(timeavailable)=" . $this->apply_date_offset($data->timeavailable) . "<br/>\n";
  204. $msgD1 .= " * timedue=" . $data->timedue . " -> apply_date_offset(timedue)=" . $this->apply_date_offset($data->timedue) . "<br/>\n";
  205. // Open discussion if is allowed to roll date. See capability "moodle/restore:rolldates"
  206. // https://tracker.moodle.org/browse/MDL-78420
  207. if ($CFG->debugdisplay) { // debug messages is turned on
  208. print $msgD1; //DEBUG
  209. }
  210. $msgD .= $msgD1; // append
  211. // All field to table {iassign_statement}.
  212. // The {iassign_statement}.id will be generated by "$DB->insert_record('iassign_statement', $new_iassign_statement)"
  213. $new_iassign_statement = new stdClass();
  214. $new_iassign_statement->name = $data->name ;
  215. $new_iassign_statement->iassignid = $this->get_new_parentid('iassign'); // replace {iassign_statement}.iassignid from the origin to the destination
  216. $new_iassign_statement->type_iassign = $data->type_iassign ;
  217. $new_iassign_statement->proposition = $data->proposition ;
  218. $new_iassign_statement->author_name = $data->author_name ;
  219. $new_iassign_statement->author_modified_name = $data->author_modified_name ;
  220. $new_iassign_statement->iassign_ilmid = $destination_ilmid; // $this->get_mappingid('iassign_ilm', $data->iassign_ilmid); // ideally it was changed above (with iLM on destination)
  221. $new_iassign_statement->file = $data->id; // old {iassign_statement}.id eventually will be used by 'after_execute()' to recover {files} associated to this {iassign_statement} //2025 $files_id; // $this->get_mappingid('files', $data->file);
  222. $new_iassign_statement->grade = $data->grade ;
  223. $new_iassign_statement->timecreated = $data->timecreated ;
  224. $new_iassign_statement->timemodified = $data->timemodified ;
  225. $new_iassign_statement->timeavailable = $this->apply_date_offset($data->timeavailable);
  226. $new_iassign_statement->timedue = $this->apply_date_offset($data->timedue);
  227. $new_iassign_statement->preventlate = $data->preventlate ;
  228. $new_iassign_statement->test = $data->test ;
  229. $new_iassign_statement->special_param1 = $data->special_param1 ;
  230. $new_iassign_statement->position = $data->position ;
  231. $new_iassign_statement->visible = $data->visible ;
  232. $new_iassign_statement->max_experiment = $data->max_experiment ;
  233. $new_iassign_statement->dependency = $data->dependency ;
  234. $new_iassign_statement->automatic_evaluate = $data->automatic_evaluate ;
  235. $new_iassign_statement->show_answer = $data->show_answer ;
  236. $new_iassign_statement->store_all_submissions = $data->store_all_submissions ;
  237. $new_iassign_statement->filesid = $files_id;
  238. $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  239. $filenamewrite = "output_restore_iassign_stepslib_" . $timeWrite . ".txt";
  240. // ./mod/iassign/ilm_debug/escreva.php: writeContent($filetype1, $pathbase, $outputFile, $msgToRegister)
  241. require_once($CFG->dirroot . "/mod/iassign/ilm_debug/escreva.php"); //leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$
  242. $resp = writeContent("", "", $filenamewrite, $msgD); // ($filetype1, $pathbase, $outputFile, $msgToRegister) //leo REMOVER!
  243. //$ss=0; for ($i=0; $i<10000; $i++) $ss = $ss+$i;
  244. //TODO: em instalacoes distintas o 'iassign_ilmid' precisa ser buscado para encontrar o {iassign_ilm} adequado (se existir)!
  245. try {
  246. // Insert in table {iassign_statement} and get its ID {iassign_statement}.id
  247. $newitemid = $DB->insert_record('iassign_statement', $new_iassign_statement);
  248. // /backup/moodle2/restore_subplugin.class.php!set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null)
  249. // * To send ids pairs to backup_ids_table and to store them into paths to table {files}
  250. //2025 $this->set_mapping('iassign_statement', $oldid, $newitemid, true); // Has related fileareas
  251. $this->set_mapping('iassign_statement', $oldid, $newitemid, false, null, $this->task->get_old_contextid());
  252. // Note - the old contextid is required in order to be able to restore files stored in sub plugin file areas attached to the iassign_statementid.
  253. } catch (Exception $e) {
  254. $error_msg = "Error on restore_iassign_stepslib.php!process_iassign_statement(.): " . $e->getMessage() . "<br/>\n";
  255. // With any of the bellow warning, the restoring process is interrupted!
  256. // throw new restore_step_exception($error_msg);
  257. debugging('Error in restore_iassign_stepslib.php!process_iassign_statement(.): the iLM ' . $ilm_name . '!', DEBUG_DEVELOPER);
  258. }
  259. activity::add_calendar($newitemid);
  260. } // protected function process_iassign_statement($data)
  261. // Process a iassign_submission restore.
  262. // @param object $data The data in object form
  263. protected function process_iassign_submission ($data) {
  264. global $DB, $CFG;
  265. $data = (object) $data;
  266. $oldid = $data->id; // from source {course}.id: old {iassign_statement}.id
  267. if ($CFG->debugdisplay) { // debug messages is turned on
  268. if (isset($this->source_course_id)) //DEBUG
  269. print "restore_iassign_stepslib.php!process_iassign_submission(.): this->source_course_id=" . $this->source_course_id . "; old {issign}.id=" . $oldid . "<br/>"; //exit;
  270. }
  271. $data->iassign_statementid = $this->get_new_parentid('iassign_statement');
  272. $data->userid = $this->get_mappingid('user', $data->userid);
  273. $data->teacher = $this->get_mappingid('user', $data->teacher);
  274. $newitemid = $DB->insert_record('iassign_submission', $data); // insert in 'iassign_submission'
  275. $this->set_mapping('iassign_submission', $oldid, $newitemid, true); // Has related fileareas
  276. }
  277. // Process a iassign_submission_comment restore.
  278. // @param object $data The data in object form
  279. protected function process_iassign_submission_comment ($data) {
  280. global $DB;
  281. $data = (object) $data;
  282. $oldid = $data->id; // from source {course}.id: old {iassign_statement}.id
  283. if ($CFG->debugdisplay) { // debug messages is turned on
  284. if (isset($this->source_course_id)) //DEBUG
  285. print "restore_iassign_stepslib.php!process_iassign_submission_comment(.): this->source_course_id=" . $this->source_course_id . "; old {issign}.id=" . $oldid . "<br/>"; //exit;
  286. }
  287. $data->iassign_submissionid = $this->get_new_parentid('iassign_submission');
  288. $data->comment_authorid = $this->get_mappingid('user', $data->comment_authorid);
  289. $newitemid = $DB->insert_record('iassign_submission_comment', $data); // insert in 'iassign_submission_comment'
  290. $this->set_mapping('iassign_submission_comment', $oldid, $newitemid, true); // Has related fileareas
  291. }
  292. // Try current and old association {files} and {iassign_statemnt}
  293. // Initially try to recover with {files}.itemid = {iassign_statement}.id, after with {iassign_statement}.file and then with {iassign_statement}.filesid
  294. protected function get_all_files_ia ($DB, $ias_id, $ias_file, $ias_filesid, $ias_name) {
  295. // Try to get all associated {files} using: {files}.itemid = {iassign_statement}.id
  296. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_id));
  297. if ($files)
  298. return $files;
  299. // Now try with: {files}.itemid = {iassign_statement}.file
  300. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_file));
  301. if ($files)
  302. return $files;
  303. // Now try with: {files}.itemid = {iassign_statement}.file
  304. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_filesid));
  305. return $files;
  306. }
  307. // An extension of Levenshtein to avoid wrong association, as $name="6.3 Laco simples: Dado N, determinar a soma dos N primeiros pares"
  308. // with $filename1="aula5-circuncentro_EF_web.geo", instead of $filename2="ex6_3_n_prim_pares.ivph",
  309. // since Levenshtein($name,$filename1)=57 and Levenshtein($name,$filename2)=59
  310. static function extend_levenshtein_distance ($name, $filename) { // Levenshtein distance
  311. $itens = explode(" ", $name);
  312. $dist = 0;
  313. foreach ($itens as $item) // use all word in {iassign_statement}.name
  314. $dist += Levenshtein($item, $filename);
  315. return $dist;
  316. }
  317. // Once the database tables have been fully restored, restore the files.
  318. // The current code is only necessary to eventually change {files}.itemid and {iassign_statement}.filesid to
  319. // the current standard: {files}.itemid = {iassign_statement}.id AND {iassign_statement}.filesid={files}.id
  320. // @calledby ./backup/util/plan/restore_structure_step.class.php: restore_iassign_activity_structure_step->after_execute()
  321. // @calls ./backup/moodle2/restore_subplugin.class.php: protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null)
  322. protected function after_execute () {
  323. global $CFG, $DB;
  324. //RR $oldcontextid = $this->task->get_old_contextid(); // $data->oldcontextid; // The old context ID (from the backup)
  325. //xxxxxxxxxxxxxxx adicionei: , 'files', $this->old_id
  326. //RR $this->add_related_files('mod_iassign', 'exercise', 'files', $oldcontextid, $this->old_id); // $this->add_related_files('mod_iassign', 'exercise', null);
  327. //RR $this->add_related_files('mod_iassign', 'activity', 'files', $oldcontextid, $this->old_id); // $this->add_related_files('mod_iassign', 'activity', null); //2025
  328. $this->add_related_files('mod_iassign', 'exercise', null);
  329. $this->add_related_files('mod_iassign', 'activity', null); //2025/04
  330. $this->add_related_files('mod_iassign', 'intro', null); //2025/04
  331. // $fs = get_file_storage();
  332. $msgD = "\n----------------------\nrestore_iassign_stepslib.php!after_execute():\n"; //Dxxx
  333. $countF = 0; // count the total of iAssign activities file //Dxxx
  334. // Get this {course}.id, note that {course_modules}.course={course}.id AND {course_modules}.instance={iassign}.id AND {course_modules}.module={module}.id of iAssign
  335. // $course_id = $this->get_courseid(); // ERROR! This returns the {course}.id of the NEW {course}.id!!!
  336. // $course_id = $this->source_course_id; // get the source {iassign}.course = {course}.id
  337. $course_id = $this->get_courseid(); // get the destination {course}.id
  338. if ($CFG->debugdisplay) { // debug messages is turned on
  339. if (isset($this->source_course_id)) //DEBUG
  340. print "restore_iassign_stepslib.php!after_execute(): original {course}.id=" . $this->source_course_id . "; here courseid=" . $course_id . "<br/>"; //exit;
  341. }
  342. // @see /lib/accesslib.php : CONTEXT_SYSTEM=10; CONTEXT_USER=30; CONTEXT_COURSECAT=40; CONTEXT_COURSE=50; CONTEXT_MODULE=70
  343. $coursecontext = context_course::instance($course_id); //REMOVE: auxiliary to debug {context}.contextlevel = CONTEXT_MODULE = 70
  344. // Get all "iAssignment block" inside the source course ($this->source_course_id): $list_all_instances_of_iassign
  345. // ERROR: here $course_id is the {course}.id of the NEW course!!!
  346. $list_all_instances_of_iassign = $DB->get_records('iassign', array('course' => $course_id));
  347. $msgD .= "The target course.id=" . $course_id . "\n" . "And its coursecontext.id=" . $coursecontext->id . "\n" .
  348. "#list_all_instances_of_iassign=" . count($list_all_instances_of_iassign) . " : from course.id=" . $course_id . "\n"; //XXX
  349. foreach ($list_all_instances_of_iassign as $iassign) {
  350. // Get all activities (course.moudules) inside each "iAssignment block": $list_all_iassign_statement
  351. $list_all_iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id)); // get all activities inside this block
  352. $msgD .= "#list_all_iassign_statement = " . count($list_all_iassign_statement) . " : from iassign.id=iassign_statement.iassignid=" . $iassign->id . "\n"; //Dxxx
  353. foreach ($list_all_iassign_statement as $iassign_statement) {
  354. // Get all associated {files} to this iAssign activity: $files
  355. // Initial iAssign/files association: {files}.itemid = {iassign_statement}.file
  356. // Current (after 2022): {files}.itemid = {iassign_statement}.id AND {iassign_statement}.filesid = {files}.id
  357. $msgD .= "+ {iassign_statement} : id=" . $iassign_statement->id . ", file=" . $iassign_statement->file . ", filesid=" . $iassign_statement->filesid . ", " . $iassign_statement->name . "\n"; //Dxxx
  358. // Try recover {files} using : {files}.itemid == {iassign_statement}.id
  359. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // new 2022 association
  360. if ($files) { // success
  361. $msgD .= " - (1) {files}.itemid=={iassign_statement}.id==" . $iassign_statement->id . "\n"; //Dxxx
  362. }
  363. else { // try old association : {files}.itemid == {iassign_statement}.file
  364. $msgD .= " - (1) {files}.itemid=={iassign_statement}.id==" . $iassign_statement->id . " falhou!\n"; //Dxxx
  365. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file)); //version until 2021:
  366. if ($files) { // success
  367. $msgD .= " - (2) {files}.itemid=={iassign_statement}.file==" . $iassign_statement->file . "\n"; //Dxxx
  368. }
  369. else { // try association : {files}.itemid == {iassign_statement}.filesid
  370. $msgD .= " - (2) {files}.itemid=={iassign_statement}.file==" . $iassign_statement->file . " falhou!\n"; //Dxxx
  371. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->filesid)); //version until 2021:
  372. if ($files) {
  373. $msgD .= " - (3) {files}.itemid=={iassign_statement}.filesid==" . $iassign_statement->filesid . "\n"; //Dxxx
  374. }
  375. //2025
  376. else { // try association : {files}.id == {iassign_statement}.filesid
  377. $msgD .= " - (3) {files}.itemid=={iassign_statement}.filesid==" . $iassign_statement->filesid . " falhou!\n"; //Dxxx
  378. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'id' => $iassign_statement->filesid)); //2025
  379. if ($files) {
  380. $msgD .= " - (4) {files}.id=={iassign_statement}.filesid==" . $iassign_statement->filesid . "\n"; //Dxxx
  381. }
  382. else $msgD .= " - (4) {files}.id=={iassign_statement}.filesid==" . $iassign_statement->filesid . " falhou!\n"; //Dxxx
  383. } // else {files}.itemid == {iassign_statement}.filesid
  384. //2025
  385. } // else {files}.itemid == {iassign_statement}.filesid
  386. } // else {files}.itemid == {iassign_statement}.file
  387. $choosen_file = null;
  388. if ($files) { // got some {files}
  389. $msgD .= " - #files = " . count($files) . "\n"; //D
  390. $countF += count($files); //D
  391. // $distance = strlen($iassign_statement->name) + 1; // use Levenshtein distance to get the {files} with smallest distance from {iassign_statement}.name
  392. $distance = 100*strlen($iassign_statement->name) + 1; // use extended Levenshtein distance to get the {files} with smallest distance from {iassign_statement}.name
  393. foreach ($files as $element_file) { // analyse each '$element_file' of 'files'
  394. if ($element_file->filename != '.') { // get the '$element_file' that is not only "path"
  395. $dist = restore_iassign_activity_structure_step::extend_levenshtein_distance($iassign_statement->name, $element_file->filename); // Levenshtein distance
  396. $msgD .= " -- files: id=" . $element_file->id . ", itemid=" . $element_file->itemid . ", filename=" . $element_file->filename . " : dist=" . $dist . "\n"; //D
  397. if ($dist<$distance) { // new chosen file (name nearest from {iassign_statement}.name
  398. $choosen_file = $element_file;
  399. $distance = $dist;
  400. }
  401. } // if
  402. } // foreach ($files as $element_file)
  403. if ($choosen_file != null) { // there is {files} to associate
  404. if ($iassign_statement->filesid != $choosen_file->id) { // need to update 'iassign_statement.filesid'?
  405. // {iassign_statement}.filesid' <- 'files.id' (update bellow) //2022
  406. $newentry = new stdClass();
  407. $newentry->id = $iassign_statement->id;
  408. $newentry->filesid = $choosen_file->id;
  409. //2023--- line bellow was commented!
  410. if (!$DB->update_record("iassign_statement", $newentry)) print_error('error_update', 'iassign'); // update 'iassign_statement' : filesid <- {files}.id'
  411. $msgD .= " " . " - Update {iassign_statement}.id=" . $newentry->id . ", filesid=" . $iassign_statement->filesid . " <- " . $newentry->filesid . "; \n"; //D
  412. }
  413. else $msgD .= " " . " - OK! To {iassign_statement}.id=" . $newentry->id . " : filesid=" . $iassign_statement->filesid . " = " . $choosen_file->id . "; \n"; //D
  414. //2023 Removido o bloco para redefinir {iassign_statement}.iassign_ilmid baseado na extensao do arquivo encontrado //XXX
  415. //2023 Vide: /home/leo/projetos/iMA/lms/itarefa/novo/alteracoes_2023/alteracoes_backup_moodle2.txt //XXX
  416. if ($choosen_file->itemid != $iassign_statement->id) {
  417. // Adjust to new 2022 {files}/{iassign_statement} relation: if {files}.itemid!=iassign_statement}.id then {files}.itemid <- {iassign_statement}.id
  418. $newentry = new stdClass();
  419. $newentry->id = $choosen_file->id;
  420. $newentry->itemid = $iassign_statement->id;
  421. $msgD .= " " . " - Update {files.itemid}: id=" . $newentry->id . ", itemid=" . $choosen_file->itemid . "<-" . $newentry->itemid . "\n"; //D
  422. //2023--- line bellow was commented!
  423. if (!$DB->update_record('files', $newentry)) print_error('error_update', 'iassign'); // atualiza {files}.itemid := {iassign_statement}.id
  424. }
  425. else $msgD .= " " . " - OK! To {iassign_statement}.id=" . $newentry->id . " : itemid=" . $choosen_file->itemid . " = " . $iassign_statement->id . "; \n"; //D
  426. } // if ($choosen_file != null)
  427. } // if ($files)
  428. if ($choosen_file == null) $msgD .= " " . " - NAO encontrado {files} para associar!<br/>\n";
  429. else $msgD .= " " . " - Encontrado {files} : " . $choosen_file->id . ", " . $choosen_file->itemid . ", " . $choosen_file->filename . ", distance=" . $distance . "\n";
  430. } // foreach ($list_all_iassign_statement as $iassign_statement)
  431. } // foreach ($list_all_instances_of_iassign as $iassign)
  432. //DEBUG
  433. // /var/www/html/moodle/mod/iassign/ilm_debug/escreva.php
  434. $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  435. $filenamewrite = "output_restore_after_execute_" . $timeWrite . ".txt";
  436. // ./mod/iassign/ilm_debug/escreva.php: writeContent($filetype1, $pathbase, $outputFile, $msgToRegister)
  437. // ./mod/iassign/ilm_debug/saida_restore_after_execute.txt
  438. // require_once(__DIR__ . "/../../ilm_debug/escreva.php"); //leo REMOVER!
  439. require_once($CFG->dirroot . "/mod/iassign/ilm_debug/escreva.php"); //leo REMOVER!
  440. $resp = writeContent("", "", $filenamewrite, $msgD); // ($filetype1, $pathbase, $outputFile, $msgToRegister) //leo REMOVER!
  441. //D echo "restore_iassign_stepslib: resp=" . $resp . ", #files=" . $countF . "<br/>\n";
  442. //DEBUG
  443. } // protected function after_execute()
  444. } // class restore_iassign_activity_structure_step extends restore_activity_structure_step