1
0

files_functions.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. <?php
  2. /*
  3. * Any Moodle activity is directly associated of a Moodle instance activity through {course_modules}.
  4. *
  5. * iAssign is no directly associated of a Moodle instance activity, since it is a block of activities,
  6. * then there is association between {course_modules}.instance = {iassign}.id and {course_modules}.id = {context}.instanceid
  7. * but {files}.contextid = {iassign_statement}.id (not with {iassign}.id) and {iassign_statement}.iassignid = {iassign}.id
  8. * The same with {files}.itemid that usually is associated with {<module>}.id but in iAssing need associate with each block activity
  9. * then {files}.itemid = {iassign_statement}.id (not with {iassign}.id)
  10. *
  11. * Relations between Moodle tables to recover iAssign files:
  12. * {course_modules}.id = {context}.instanceid
  13. * {context} : id ; contextlevel ; instanceid ; path ; depth ; locked
  14. * {course_modules} : id ; course ; module ; instance ...
  15. * + {course_modules}.course = {course}.id
  16. * + {course_modules}.module = {module}.id
  17. * + {course_modules}.instance = {iassign}.id - associate with activities block
  18. *
  19. */
  20. // Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  21. defined('MOODLE_INTERNAL') || die();
  22. $SEP = ";"; // separador para impressao CSV ou tela
  23. //TODO: locallib.php:
  24. //TODO: Remover o codigo de leitura de arquivos do 'locallib.php ! view_iassign_current(): iassign_statement_activity_item = $this->activity->get_activity();' para este PHP
  25. //TODO: function get_files_in_context (&$filesfrommine, &$filesfromothers, $contextid, $extension, $userid): quem pega conteudo para "view.php" - 8222/9167
  26. //TODO: static function save_ilm_by_xml ($application_xml, $files_extract): rever, pois tem comando "'itemid' => rand(1, 999999999)" - 6791/9167
  27. //TODO: function write_file_iassign ($stringArchiveContent, $filename): pois "itemid' => 0, // usually = ID of row in table" - 7533/9167
  28. //TODO: function update_file_iassign ($stringArchiveContent, $filename, $fileid): pois "itemid' => 0, // usually = ID of row in table" - 7575/9167
  29. //TODO: function recover_files_ilm (): 'itemid' => 0, - 8171/9167
  30. //TODO: function get_files_in_context (&$filesfrommine, &$filesfromothers, $contextid, $extension, $userid): quem pega conteudo para "view.php" - 8222/9167
  31. //TODO: function view_files_ilm ($iassign_ilm_class, $extension) - 8469/9167
  32. //TODO: This code are designed to recover partially files lost in wrong updates:
  33. //TODO: if ($oldversion < 2020120500): error considering {iassign_statement}.id != {iassign_statement}.file, {iassign_statement}.filesid:={files}.itemid and erasing correct {files}! //TODO: not good...
  34. //TODO:
  35. // Get iAssign ID in {modules}
  36. // @calledby settings_verify.php
  37. function get_iassign_id ($DB) {
  38. // Get the list of all modules that are properly installed.
  39. // $iassign_module = $DB->get_records_menu('modules', array(''), '', 'name, id');
  40. $str_query = "SELECT id, name, visible FROM {modules} WHERE upper(name) = 'IASSIGN'";
  41. //D echo "query=" . $str_query . "<br/>";
  42. $iassign_module = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  43. // $iassign_module = $DB->execute($str_query); // get array of stdClass (must be only one element)
  44. if (!$iassign_module) return -1;
  45. foreach ($iassign_module as $ia) {
  46. //D echo "iassign_module->id = " . $ia->id . ", name=" . $ia->name . "<br/>iassign_module="; print_r($iassign_module); echo "<br/>";
  47. return $ia->id;
  48. }
  49. return -1; // error... empty array
  50. }
  51. // Temporary: to create new {iassign_statement}.userid
  52. function get_user ($DB, $name1, $name2) {
  53. $str_query = "SELECT id, firstname, lastname FROM {user} WHERE firstname='" . $name1 . "' AND lastname='" . $name2 . "'"; // "; // AND filearea='exercise' AND contextid=108
  54. $tmp_list_all_files = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  55. $num = count($tmp_list_all_files);
  56. //D echo "get_user: " . $name1 . " " . $name2 . "; num=" . $num . "<br/>";
  57. if ($num==0) return -1;
  58. $ii = 0;
  59. foreach ($tmp_list_all_files as $elem) {
  60. echo " - id=" . $elem->id . "; firstname=" . $elem->firstname . "; lastname=" . $elem->lastname . "<br/>";
  61. $ii++;
  62. }
  63. echo "Total: " . $ii . "<br/>";
  64. }
  65. // Temporary: to create new {iassign_statement}.userid
  66. function print_vector ($vec) {
  67. $SEP = ";";
  68. if (!is_array($vec)) print $vec . "<br/>";
  69. $sizeOfVector = count($vec);
  70. if ($sizeOfVector==0) print "[]<br/>";
  71. else {
  72. for ($ii=0; $ii<$sizeOfVector; $ii++) print $vec[$ii] . $SEP . " ";
  73. //print "<br/>";
  74. }
  75. }
  76. // Temporary: to create new {iassign_statement}.userid
  77. function get_name ($name) {
  78. $items = explode("&nbsp;", $name);
  79. $name1 = $items[0].trim();
  80. $name2 = $items[1].trim(); //D echo $name . " :: " . $name1 . " - " . $name2 . "<br/>";
  81. return [$name1, $name2];
  82. }
  83. // Temporary: to create new {iassign_statement}.userid
  84. function binary_search ($vec_elements, $x_element) {
  85. $sizeOfVector = count($vec_elements);
  86. $leftI = 0; $rightI = $sizeOfVector-1;
  87. while ($leftI <= $rightI) {
  88. $middle = floor(($leftI + $rightI)/2);
  89. $elem_file = $vec_elements[$middle]; //D echo "leftI=" . $leftI . " - rightI=" . $rightI . " => med=" . $middle . "<br/>";
  90. // if ($leftI == $rightI && $vec_elements[$leftI]!=$x_element) { return [-1, $leftI ]; }
  91. if ($elem_file < $x_element) { $leftI = $middle + 1; }
  92. else
  93. if ($elem_file > $x_element) { $rightI = $middle - 1; }
  94. else { return $middle; } // found! return index position
  95. }
  96. return [-1, $leftI, $rightI];
  97. } // function binary_search($vec_elements, $x_element)
  98. // Temporary: to create new {iassign_statement}.userid
  99. function insert_ordered_name (&$vec_elements, $name, $i) {
  100. $sizeOfVector = count($vec_elements);
  101. //D echo "insert_ordered_name: " . $name . " em " . $i . " (sizeOfVector=" . $sizeOfVector . ")<br/>";
  102. $j = $sizeOfVector;
  103. while ($j > $i) { // move all after $i one position
  104. $vec_elements[$j] = $vec_elements[$j-1];
  105. $j--;
  106. }
  107. $vec_elements[$i] = $name; // insert new element on $i position
  108. }
  109. // Temporary: to create new {iassign_statement}.userid
  110. function get_all_iassign_statement ($DB) {
  111. $str_query = "SELECT * FROM {iassign_statement}"; //
  112. $tmp_list_all_iassign_statement = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  113. $num_is = count($tmp_list_all_iassign_statement);
  114. print "#tmp_list_all_iassign_statement = " . $num_is . "<br/>\n";
  115. // saw2021_1: #tmp_list_all_iassign_statement = 3519
  116. $vec_all_names_ord = array();
  117. $count1 = 0;
  118. foreach ($tmp_list_all_iassign_statement as $ias) {
  119. [$name1, $name2] = get_name($ias->author_name);
  120. //D echo $ias->author_name . " :: " . $name1 . " - " . $name2 . "<br/>";
  121. if ($count1>=5) break;
  122. $name = $name1 . " " . $name2;
  123. $answer = binary_search($vec_all_names_ord, $name);
  124. if (count($answer)>1) { // echo $x . " -> ["; print_vector($answer); echo "]<br/>\n";
  125. $ind = $answer[0];
  126. }
  127. else { // echo $x . " -> " . $answer . "<br/>\n";
  128. $ind = $answer;
  129. }
  130. if ($ind == -1) {
  131. $userid = get_user($DB, $name1, $name2); //echo "Total: " . $ii . "<br/>";
  132. insert_ordered_name($vec_all_names_ord, $name, $answer[1]);
  133. $count1++;
  134. }
  135. //D else echo " - " . $name . " ja existe em " . $ind . "<br/>";
  136. }
  137. $total = count($vec_all_names_ord);
  138. print "Total: " . $total . "<br/> Vector: "; print_vector($vec_all_names_ord); print "<br/>";
  139. } // function get_all_iassign_statement($DB)
  140. // Patricia Alves (admin) => user.id = 24 ; patriciaadm ; profa.dra.patricia@gmail.com
  141. // Patricia Alves (professora) => user.id = 1332 ; patricia ; profa.pati@gmail.com
  142. // Get all iAssign {files}
  143. // Build "$list_all_files" with fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  144. // Initially "$list_iassign[$ii]".ias_id = -1 (in "search_files(.)" we hope "ias_id" := {iassign_statement}.id)
  145. // @calledby get_associated_ias_file
  146. function get_all_files ($DB, $list_iassign) {
  147. global $USER, $SEP; //DEBUG: apenas durante depuracao!
  148. $sizeAC = count($list_iassign);
  149. //L echo "----<br/>get_all_files: #list_iassign=" . $sizeAC . "<br/>\n";
  150. //'component' => 'mod_iassign', 'filearea' => 'exercise', 'contextid' => 108:
  151. //get_all_files: #list_iassign=772
  152. //#tmp_list_all_files=12
  153. //get_all_files: #list_iassign=772
  154. //#tmp_list_all_files=7382
  155. //$tmp_list_all_files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise')); // #tmp_list_all_files=8324
  156. //$tmp_list_all_files = $DB->get_records('files', array('component' => 'mod_iassign')); // #tmp_list_all_files=7382 could be 'filearea' => 'activity'
  157. //$tmp_list_all_files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'activity')); // #tmp_list_all_files=4991
  158. //$tmp_list_all_files = $DB->get_records('files', array('component' => 'mod_iassign')); // #tmp_list_all_files=8324 // , 'filearea' => 'exercise', 'contextid' => 108
  159. $str_query = "SELECT * FROM {files} WHERE component='mod_iassign'"; // "; // AND filearea='exercise' AND contextid=108
  160. $tmp_list_all_files = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  161. //print_r($tmp_list_all_files); echo "<br/>"; // Array ( [37] => stdClass Object ( [id] => 3...
  162. $numf = count($tmp_list_all_files);
  163. //L echo "#tmp_list_all_files=" . $numf . "<br/>"; // #tmp_list_all_files=13383
  164. $list_all_files = array(); // use continous array ($tmp_list_all_files is indexed by 'id')
  165. $ii = 0;
  166. foreach ($tmp_list_all_files as $elem) {
  167. //D echo $ii . $SEP . $elem->id . $SEP . $elem->contextid . $SEP . $elem->timecreated . $SEP . $elem->filearea . $SEP . $elem->itemid . $SEP .
  168. //D "'" . $elem->author . "'" . $SEP . "'" . $elem->filename . "'" . $SEP . "<br/>";
  169. $elem->ias_id = -1; // to receive the associated {iassign_statement}.id (defined in "search_lost_files_in_context(.)")
  170. $elem->courseid = -1; // to receive the associated {iassign_statement}.id (defined in "search_lost_files_in_context(.)")
  171. $elem->missing_ias = TRUE; // by default this {files} is associated to one {iassign_statement} - function "search_files(.)" can change it to FALSE
  172. $list_all_files[$ii] = $elem; // transcript array indexed by 'id' to "continous array"
  173. $ii++;
  174. }
  175. //D for ($ii=0; $ii<$numf; $ii++) {
  176. //D $elem = $list_all_files[$ii];
  177. //D echo $ii . $SEP . $elem->id . $SEP . $elem->contextid . $SEP . $elem->timecreated . $SEP . $elem->filearea . $SEP . $elem->itemid . $SEP .
  178. //D "'" . $elem->author . "'" . $SEP . "'" . $elem->filename . "'" . $SEP . "<br/>";
  179. //D }
  180. $sizeof_list_all_files = $ii;
  181. return $list_all_files;
  182. } // function get_associated_ias_file($DB, $iassign_statement, $all_context, &$all_context_final, $ii)
  183. // Search $element_ias.contextid == $list_all_files[.].contextid and ($element_ias.file == $list_all_files[.].itemid or $element_ias.filesid == $list_all_files[.].id)
  184. // Build "$list_all_files" with fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  185. // Initially "$list_iassign[$ii]".ias_id = -1 (in "search_files(.)" we hope "ias_id" := {iassign_statement}.id)
  186. // @param &$list_all_files: passed by reference, "$list_all_files[$jj].ias_id" could receive {iassign_statement}.id
  187. function search_files (&$list_all_files, $element_ias) {
  188. $sizeOfFiles = count($list_all_files);
  189. $sizeC = count($element_ias->contextid);
  190. //D if ($element_ias->id==5793 || $element_ias->id==5794) { echo "search_files: " . $element_ias->courseid . ", " . $element_ias->id . ", " . $element_ias->filesid . " with context: ["; for ($jj=0; $jj<$sizeC; $jj++) echo $element_ias->contextid[$jj] . ","; echo "]<br/>"; }
  191. for ($ii=0; $ii<$sizeOfFiles; $ii++) {
  192. //D Decho $ii . " ";
  193. $element_file = $list_all_files[$ii];
  194. for ($jj=0; $jj<$sizeC; $jj++) {
  195. $contextid = $element_ias->contextid[$jj]; // try with $jj-th {context}.id
  196. if ($contextid == $element_file->contextid) {
  197. if ($element_ias->file == $element_file->itemid) {
  198. //echo "<br/>";
  199. //D echo " - Encontrou " . $element_ias->id . " via 'file' (file==itemid): contextid[" . $jj . "]=" . $contextid . ": " . $element_ias->file . "==" . $element_file->itemid . " no files[" . $ii . "]<br/>";
  200. $element_file->ias_id = $element_ias->id;
  201. $element_file->missing_ias = FALSE; // by default this {files} is NOT associated to one {iassign_statement} - here was detected that this {files} is not associated to any {iassign_statement}
  202. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  203. }
  204. else
  205. if ($element_ias->filesid == $element_file->itemid) {
  206. //echo "<br/>";
  207. //D echo " - Encontrou " . $element_ias->id . " via 'file' (filesid==itemid): contextid[" . $jj . "]=" . $contextid . ": " . $element_ias->filesid . "==" . $element_file->itemid . " no files[" . $ii . "]<br/>";
  208. $element_file->ias_id = $element_ias->id;
  209. $element_file->missing_ias = FALSE; // by default this {files} is NOT associated to one {iassign_statement} - here was detected that this {files} is not associated to any {iassign_statement}
  210. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  211. }
  212. else
  213. if ($element_ias->id == $element_file->itemid) { // Com esse
  214. // Total of {iassign_statement} without any {files}: 771 -> 739 = 32
  215. // Total of {files} without any {iassign_statement}: 10638 -> 10606 = 32
  216. // Total of {files} associated with iAssign: 880 -> 880
  217. //D if ($element_ias->id==5658) echo "<br/> - Encontrou " . $element_ias->id . " via 'id' (id==itemid): contextid[" . $jj . "]=" . $contextid . ": " . $element_ias->id . "==" . $element_file->itemid . " no files[" . $ii . "] - {files}.id=" . $element_file->id . "<br/>\n";
  218. // - Encontrou 5658 via 'id' (id==itemid): contextid[1]=52001: 5658==5658 no files[12521] - {files}.id=301156
  219. // course.id = 472 : "EF-8.1" aqui funciona! http://localhost/saw2021_2/mod/iassign/view.php?id=20309&userid_iassign=3&action=view&iassign_current=5658
  220. // ilm_handlers/html5.php: {course_module}.id=20309, {iassign_statement}.id=5658, {iassign_statement}.file=, {iassign_statement}.filesid=, {context}.id=52001, {files}.id=301156, {files}.filename=
  221. $element_file->ias_id = $element_ias->id;
  222. $element_file->missing_ias = FALSE; // by default this {files} is NOT associated to one {iassign_statement} - here was detected that this {files} is not associated to any {iassign_statement}
  223. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  224. }
  225. }
  226. } // for ($jj=0; $jj<$sizeC; $jj++)
  227. }
  228. //D if ($element_ias->id==5793 || $element_ias->id==5794) echo " - " . $element_ias->id . ", " . $element_ias->name . ": jj=" . $jj . ", ERRO!<br/>";
  229. return -1;
  230. } // function search_files($list_all_files, $element_ias)
  231. // Get {iassign_statement} or {files}
  232. // @calledby settings_process_files_form.php
  233. function get_iassignstatement_or_files ($DB, $table, $element_id) {
  234. //D echo "files_functions.php|get_iassignstatement_or_files: " . $element_id . "<br/>\n";
  235. if ($table != 'iassign_statement' && $table != 'files') return NULL; // security
  236. $ia_obj = $DB->get_record($table, array('id' => $element_id));
  237. //D if ($table=='iassign_statement) echo $ia_obj->iassignid . ", " . $ia_obj->id . ", " . $ia_obj->file . ", " . $ia_obj->filesid . ", " . $ia_obj->name . "<br/>\n";
  238. return $ia_obj;
  239. //$files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); //
  240. }
  241. // Update {iassign_statement}.filesid and {files}.itemid
  242. // @calledby settings_process_files_form.php
  243. function update_iassignstatement_files ($DB, $ia_id, $filesid, $is_obj, $f1_obj, $f2_obj) {
  244. //D echo "files_functions.php|update_iassignstatement_files: ia_id=" . $ia_id . ", files.id=" . $filesid . "<br/>\n";
  245. //$ia_obj = $DB->get_record($table, array('id' => $element_id));
  246. $ma = "{iassign_statement}.filesid=" . $is_obj->filesid;
  247. $mf1 = "{files}: id=" . $f1_obj->id . ": itemid=" . $f1_obj->itemid;
  248. $mf2 = "{files}: id=" . $f2_obj->id . ": itemid=" . $f2_obj->itemid;
  249. $is_obj->filesid = $filesid; // {iassign_statement}.filesid := $f1_obj->ud = {files}.id
  250. $f1_obj->itemid = $ia_id;
  251. $f2_obj->itemid = $ia_id;
  252. $ma .= " -> " . $is_obj->filesid;
  253. $mf1 .= " -> " . $f1_obj->itemid;
  254. $mf2 .= " -> " . $f2_obj->itemid;
  255. $error = FALSE;
  256. if (!$DB->update_record('iassign_statement', $is_obj)) $error = TRUE; // print_error('error_update', 'iassign');
  257. if (!$DB->update_record('files', $f1_obj)) $error = TRUE;
  258. if (!$DB->update_record('files', $f2_obj)) $error = TRUE;
  259. if (!$error) {
  260. echo " * " . $ma . " / " . $mf1 . " / " . $mf2 . "<br/>\n";
  261. return TRUE;
  262. }
  263. echo " x " . $ma . " / " . $mf1 . " / " . $mf2 . " --- ERROR!<br/>\n";
  264. return FALSE;
  265. //$files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); //
  266. } // function update_iassignstatement_files($DB, $ia_id, $filesid, $is_obj, $f1_obj, $f2_obj)
  267. // List all "$list_all_files[$jj]" with field "ias_id" == -1 (candidate to be the missing file)
  268. // Prepare HTML do "settings_process_files_form.php" then send to this->update_iassign_statement_files(.) function to update {iassign_statement} and {files}
  269. // @param $type = 1 => do not edit "$list_all_files[$jj].ias_id" (leave with -1); 2 => edit "$list_all_files[$jj].ias_id" with its first fit {iassign_statement}.ias_id
  270. // @param &$list_all_files: passed by reference, "$list_all_files[$jj].ias_id" could receive {iassign_statement}.id if {files}.contextid = some {context}.id of {iassign_statement}; files[$jj].courseid" := $courseid,
  271. // @param $contextid : is array of context in this {iassign_statement}; $ias_id = {iassign_statement}.id
  272. function search_lost_files_in_context ($type, $ii, $courseid, $ias_id, &$list_all_files, $array_contextid, $countErr) {
  273. $sizeOfFiles = count($list_all_files);
  274. $sizeC = count($array_contextid);
  275. $files_index_candidates = array();
  276. $files_filename_candidates = array();
  277. $contextid_candidates = array(); // {context}.id associated to the {files}
  278. $list_candidate_files = array(); // array with {files} indexed by its ID ({files}.id): $list_candidate_files[ID]==$jj <=> $list_all_files[$jj] is the {files}
  279. $msg = "ERRO: <select name='selected_filesid[" . $countErr . "]'>\n <option value=''>select one file</option>\n";
  280. $msgHidden = "";
  281. for ($jj=0; $jj<$sizeOfFiles; $jj++) { // list all lost {files}
  282. $sizeC = count($array_contextid); // all {context}.id of this {iassign_statement}
  283. $hascandidate = 0; // has {files} candidate with one of {iassign_statement}.contextid[]?
  284. for ($kk=0; $kk<$sizeC; $kk++) { // list all {context}.id associated to this {iassign_statement}
  285. $contextid = $array_contextid[$kk];
  286. if ($list_all_files[$jj]->contextid==$contextid && $list_all_files[$jj]->ias_id==-1) { // found one {files} candidate (its {files}.contextid = {iassign_statement}.contextid[$kk])
  287. $hascandidate = 1; // has {files} candidate with one of {iassign_statement}.contextid[$kk]
  288. if ($type == 2) // can change field "ias_id" ($type==1 nedd it continous as -1 ao list {files} with all possible {iassign_statement}
  289. $list_all_files[$jj]->ias_id = $ias_id; // this {files} could be of {iassign_statement}.id = $ias_id
  290. $list_all_files[$jj]->courseid = $courseid; // this {files} could be on the same course as {iassign_statement}.id = $ias_id
  291. break; // stop search to the other {iassign_statement}.contextid[]
  292. }
  293. }
  294. if ($hascandidate == 1) { // there is one {files}.contextid = {iassign_statement}.contextid[$kk]
  295. $files_index_candidates[] = $jj;
  296. $files_filename_candidates[] = $list_all_files[$jj]->filename;
  297. $contextid_candidates[] = $contextid;
  298. $fileid = $list_all_files[$jj]->id;
  299. $list_candidate_files[$fileid] = $jj;
  300. if ($list_all_files[$jj]->filename != ".") { // consider only file (not path)
  301. $msg .= " <option value='" . $fileid . "'>" . $fileid . " - " . $list_all_files[$jj]->filename . " ii=" . $list_candidate_files[$fileid] . "</option>\n";
  302. $msgHidden .= " <input value='" . $list_all_files[$jj]->filename . "' name='list_filesname[" . $ii . "][]' type='hidden' />\n";
  303. $msgHidden .= " <input value='" . $list_all_files[$jj]->id . "' name='list_filesid[" . $ii . "][]' type='hidden' />\n";
  304. $msgHidden .= " <input value='" . $contextid . "' name='list_contextid[" . $ii . "][]' type='hidden' title='#array_contextid=" . count($array_contextid) . "'/>\n";
  305. $msgHidden .= " <input value='" . $ias_id . "' name='list_iassign_statement_id[" . $ii . "][]' type='hidden' />\n"; // {iassign_statement}.id
  306. }
  307. }
  308. } // for ($jj=0; $jj<$sizeOfFiles; $jj++)
  309. $msg .= "</select><table>\n";
  310. $sizeCandidates = sizeof($files_index_candidates);
  311. for ($jj=0; $jj<$sizeCandidates; $jj++) {
  312. if ($files_filename_candidates[$jj] == ".") continue; // ignore filename==".", next $jj
  313. $kk = $files_index_candidates[$jj];
  314. $contextid = $contextid_candidates[$jj]; // $array_contextid[$kk];
  315. $fileid = $list_all_files[$kk]->id;
  316. $msg .= " <tr><td title='coourse.id'>" . $list_all_files[$kk]->courseid . "</td>"; // defined in "search_lost_files_in_context(.)"
  317. $msg .= " <td title='context.id'>" . $contextid . "</td>";
  318. $msg .= " <td title='files.id'>" . $list_all_files[$kk]->id . "</td>";
  319. $msg .= " <td title='files.name'>" .
  320. $list_all_files[$kk]->filename . "</td><td title='files.timecreated=" . date("d/m/Y H:i:s", $list_all_files[$kk]->timecreated) . "'>" .
  321. $list_all_files[$kk]->timecreated . "</td><td title='files.author'>" . $list_all_files[$kk]->author . "</td></tr>\n";
  322. } // for ($jj=0; $jj<$sizeOfFiles; $jj++)
  323. $msg .= "</table>\n";
  324. return [$msg, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_files];
  325. } // function search_lost_files_in_context($ii, $courseid, $ias_id, &$list_all_files, $array_contextid, $countErr)
  326. // Find in array $list_iassign[] $list_iassign[$ii]->ia_id = $iassignid
  327. // Return [ {countext}.id array , {course}.id ]
  328. // @calledby see_all_iass_files($DB, $course_modules_iassign)
  329. function find_course_context ($list_iassign, $iassignid) {
  330. $sizeI = count($list_iassign);
  331. for ($ii=0; $ii<$sizeI; $ii++)
  332. if ($list_iassign[$ii]->ia_id == $iassignid)
  333. return [$list_iassign[$ii]->id, $list_iassign[$ii]->course]; // array of contextid
  334. print "*** ERRROR: coudn't find {iassign}.id = " . $iassignid . "<br/>\n";
  335. return [-1, -1]; // error!
  336. }
  337. /// Search for {files} associated to each "iassign_statement" ("settings_verify.php?action=list")
  338. /// Must be used to produce a continous list of all {iassign_statement} with no {files} and then a list with all {files} with no {iassign_statement}
  339. // @calledby settings_verify.php?action=list : $list_iassign = see_all_iass_files($DB, 9);
  340. // @param $modules_iassignid is iAssign ID in {modules}
  341. function list_all_iass_files ($DB, $modules_iassignid) {
  342. global $SEP;
  343. // Problema: como encontrar {files} com itemid errado?
  344. // 1. Vetor com todos os {iassign} com todos seus {context}.id : $list_iassign : id ; cmid ({course_modules}.id) ; course ({course}.id); name ; ia_id ({iassign}.id)
  345. // 2. Vetor com todos os {iassign_statement} : $list_ias : id ; name ; iassignid ; author_name ; iassign_ilmid ; file ; fileid ; timecreated
  346. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  347. // 4. Vetor com todos os {iassign_statement} sem {files} correspondentes:
  348. /// Get {iassign} :: $list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  349. ///
  350. //D print "---<br/>Get {iassign} :: list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  351. //ATTENTION: 'get_records_sql(.)' return only the first element (first field)
  352. //$str_query = "SELECT {context}.id AS contextid, {course_modules}.id AS cmid, {iassign}.id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  353. $str_query = "SELECT {context}.id, {course_modules}.id AS cmid, {iassign}.id AS ia_id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  354. "{course_modules}.module=" . $modules_iassignid . " AND {iassign}.course={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance={iassign}.id";
  355. $tmp_list_iassign = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  356. //D print " - #tmp_list_iassign=" . count($tmp_list_iassign). "<br/>";
  357. // Process $tmp_list_iassign to produce the array $list_iassign continous (starting from $list_iassign[0]),
  358. // with only one entry to each {iassign}.id and keeping all of its associated {context}.id under the array $list_iassign[$ii]->id
  359. // The {iassign}.id is $list_iassign[$ii]->ia_id
  360. $list_iassign = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  361. $ii = 0;
  362. foreach ($tmp_list_iassign as $element) { // one {iassign} could have several contextid
  363. if ($ii==0 || $list_iassign[$ii-1]->ia_id != $element->ia_id) { // new element
  364. $item = array(); // change to array
  365. $item[] = $element->id; // contextid
  366. $element->id = $item; // change to array - contextid = $item
  367. $list_iassign[$ii] = $element; // transcript array indexed by 'id' to "continous array"
  368. $ii++;
  369. }
  370. else { // just add new contextid (extends array $list_iassign[$ii-1]->id[])
  371. $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  372. }
  373. }
  374. $sizeOf_list_iassign = $ii;
  375. //D print " - #list_iassign=" . $sizeOf_list_iassign . "<br/>";
  376. /// Get {iassign_statement} :: $list_ias[] = { courseid (iassign.course) ; id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; array of context.id }
  377. ///
  378. //D print "---<br/>Get {iassign_statement} :: list_ias[] = { courseid (iassign.course) ; id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; array of context.id }<br/>\n";
  379. $str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  380. " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated, {iassign}.course AS courseid, {iassign}.name AS ias_name " .
  381. " FROM {iassign}, {iassign_statement} WHERE {iassign_statement}.iassignid = {iassign}.id ORDER BY {iassign}.course, {iassign_statement}.iassignid, {iassign_statement}.id";
  382. $list_ias = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  383. $tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  384. $jj = 0;
  385. foreach ($tmp_list_ias as $element) {
  386. $answer = find_course_context($list_iassign, $element->iassignid); // to receive the array with all {context}.id associated with its {iassign}
  387. $element->contextid = $answer[0]; // -1 => error; otherwise is an array with all associated {context}.id
  388. if ($element->courseid != $answer[1]) echo " x Error: (" . $jj . ", id=" . $element->id . ") courseid=" . $element->courseid . "!=" . $answer[1] . "<br/>\n";
  389. $list_ias[$jj] = $element; // transcript array indexed by 'id' to "continous array"
  390. $jj++;
  391. }
  392. $sizeOf_list_ias = $jj;
  393. //D print " - #list_ias=" . $sizeOf_list_ias . "<br/>";
  394. /// Get {files} :: $list_all_files[] = { id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id }
  395. ///
  396. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  397. //D print "---<br/>Get {files} :: list_all_files[] = { id ; contenthash ; contextid ; component ; filearea ; itemid ; filepath ; filename ; userid ; author ; timecreated ; ias.id }<br/>\n";
  398. // Get all {files} associated with {iassign} (here the additional fields "$list_all_files[]->ias_id" is "-1" and "$list_all_files[]->missing_ias" is "FALSE")
  399. // Fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id files_id
  400. // - "$list_all_files[$ii]->ias_id" will receive the {iassign_statement}.id with the same {context}.id
  401. // - "$list_all_files[$ii]->missing_ias" will receive FALSE only if its {iassign_statement} are correctly associated (in function "search_files(.)"
  402. // If $list_iassign[$ii] wasn't associated to any {iassign_statement} then "ias_id" := -1 (otherwise "ias_id" := {iassign_statement}.id
  403. $list_all_files = get_all_files($DB, $list_iassign);
  404. //D echo " - #list_files=" . count($list_all_files) . "<br/>\n"; // &nbsp; *
  405. // List al {iassign_statement} with no {files} (from "$list_ias[.]") and then list all {files} with no {iassign_statement} (from "$list_all_files[.]")
  406. $countErr = 0;
  407. $countIA = 0;
  408. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  409. $element = $list_ias[$jj]; // array of { id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; contextid (array of context.id) }
  410. $answerSF = search_files($list_all_files, $element); // return [$ii, $jj] = {files}[$ii] is the associated file using context {iassign_statement}.contextid[$jj]
  411. if ($answerSF == -1) { // don't find!
  412. $element->missingfile = TRUE; // couldn't find {files} associated!
  413. // List all unmatched {files} under the same context
  414. // It will return tag SELECT 'filesid[]' with sizeof($files_index_candidates) options
  415. [$msgCandidates, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_filesbyID] = search_lost_files_in_context(2, $countErr, $element->courseid, $element->id, $list_all_files, $element->contextid, $countErr);
  416. if (count($files_index_candidates)==0) $msgCandidates = "";
  417. else $msgCandidates = "<br/>" . $msgCandidates;
  418. // $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  419. //R $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  420. //R print $msgHidden;
  421. $countErr++;
  422. }
  423. else {
  424. $countIA++;
  425. $element->missingfile = FALSE;
  426. }
  427. }
  428. print "<style> .error { background-color: #f4d6d2; color: #ca3120; }</style>\n";
  429. $msgErr = " class='error' "; // "color='#aa0000'";
  430. $msgTErr = ""; // " title='Not found' ";
  431. print "List of all {iassign_statement} with no {files} associated<br/>\n";
  432. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  433. print "<tr><td title='count'>count</td><td>course.id</td><td title='iAssign id'>iassign.id</td><td>iAssign statement id</td><td title='iLM id'>iLM id</td><td title='context.id'>contextid</td>" .
  434. " <td title='iassign_statement.file'>file</td><td title='iassign_statement.filesid'>filesid</td><td title='date(timecreated)'>timecreated</td><td title='author'>Author</td>" .
  435. " <td title='{iassign}.name'>ia.name</td> <td title='{iassign_statement}.name'>ias.name</td></tr>\n";
  436. $countMIAS = 0;
  437. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  438. $element = $list_ias[$jj];
  439. if ($element->missingfile) {
  440. $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  441. print "<tr " . $msgTErr . "><td>" . $countMIAS . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  442. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  443. " <td title='not found file possible array of context.id'>" . $msgC . "</td><td title='iassign_statement.file'>" . $element->file . "</td>\n" .
  444. " <td title='iassign_statement.filesid'>" . $element->filesid . "</td><td>" .
  445. date("d/m/Y H:i:s", $element->timecreated) . "</td><td>" . $element->author_name . "</td>" .
  446. " <td title='{iassign}.name'>" . $element->ias_name . "</td> <td title='{iassign_statement}.name'>" . $element->name . "</td></tr>\n";
  447. $countMIAS++; // next {iassign_statement} with no associated {files}
  448. }
  449. }
  450. // SELECT * FROM s_files WHERE id=314077 OR id=313730 OR id=301156
  451. // {files}.id= ; contextid= ; itemid= ; filename | {iassign_statement} id name (cursos/contextos: -1/51938 ; 475[49895;52998;] ; 485[52174;54306;] ; 486[52376;54615;] ; 487[52676;54821])
  452. // 301156 52001 5658 vetor_soma_x_y.ivph | 5658 8.1 Vetor - Soma das posicoes x e y
  453. // 313730 54821 5658 vetor_soma_x_y.ivph <- course.id = 487
  454. // 314077 54391 0 soma_2_inteiros.ivph
  455. // - Encontrou 5658 via 'id' (id==itemid): contextid[1]=52001: 5658==5658 no files[12521] - {files}.id=301156
  456. print "</table><br/><br/>----------------------------<br/><br/>\n";
  457. print "List of all {files} with no {iassign_statement} associated<br/>\n";
  458. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  459. print "<tr><td title='count'>count</td> <td>course.id</td> <td title='{files}.ias_id=iAssign id'>iassign.id</td> <td title='context.id'>contextid</td>" .
  460. " <td title='files.id'>id</td> <td title='{files}.itemid'>itemid</td> <td title='date(timecreated)'>date(timecreated)</td>" .
  461. " <td title='author'>author</td> <td title='{file}.filename'>filename</td></tr>\n";
  462. $countErrF = 0;
  463. $sizeOf_files = count($list_all_files);
  464. for ($jj=0; $jj<$sizeOf_files; $jj++) {
  465. $element = $list_all_files[$jj];
  466. if ($element->missing_ias) { // "$element->missing_ias = FALSE" in funcion "search_files(.)"
  467. print "<tr " . $msgTErr . "><td>" . $countErrF . "</td><td title='course.id'>" . $element->courseid . "</td> <td title='iassign.id=iassign_statement.iassignid'>" . $element->ias_id . "</td>" .
  468. " <td title='context.id''>" . $element->contextid . "</td> <td title='files.id'>" . $element->id . "</td> " .
  469. " <td title='{files}.itemid'>" . $element->itemid . "</td> <td title='date(timecreated)'>" . date("d/m/Y H:i:s", $element->timecreated) . "</td>" .
  470. " <td title='author'>" . $element->author . "</td> <td title='{file}.filename'>" . $element->filename . "</td></tr>\n";
  471. $countErrF++;
  472. }
  473. }
  474. print "</table>\n";
  475. print "Total of {iassign_statement}: " . $countIA . "<br/>\n";
  476. print "Total of {iassign_statement} without any {files}: " . $countErr . "<br/>\n";
  477. print "Total of {files} without any {iassign_statement}: " . $countErrF . "<br/>\n";
  478. return $list_iassign;
  479. } // function list_all_iass_files($DB, $modules_iassignid)
  480. /// Search for files associated to each "iassign_statement"
  481. /// Must be used to produce a form with all {iassign_statement} with no {files} and their candidate {files} (with same {context}.id
  482. // @calledby settings_verify.php: $list_iassign = see_all_iass_files($DB, 9);
  483. // @param $modules_iassignid is iAssign ID in {modules}
  484. function see_all_iass_files ($DB, $modules_iassignid) {
  485. global $SEP;
  486. // Problema: como encontrar {files} com itemid errado?
  487. // 1. Vetor com todos os {iassign} com todos seus {context}.id : $list_iassign : id ; cmid ({course_modules}.id) ; course ({course}.id); name ; ia_id ({iassign}.id)
  488. // 2. Vetor com todos os {iassign_statement} : $list_ias : id ; name ; iassignid ; author_name ; iassign_ilmid ; file ; fileid ; timecreated
  489. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  490. // 4. Vetor com todos os {iassign_statement} sem {files} correspondentes:
  491. // $list_iassign = $DB->get_records('iassign');
  492. // echo "#list_iassign=" . count($list_iassign) . "<br/>"; // 880
  493. // $ii = 0; foreach ($list_iassign as $element) { echo $ii . $SEP . $element->id . $SEP . $element->course . $SEP . $element->name . "<br/>"; $ii++; }
  494. // 880
  495. /// Get {iassign} :: $list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  496. ///
  497. print "---<br/>Get {iassign} :: list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }<br/>\n";
  498. //ATTENTION: 'get_records_sql(.)' return only the first element (first field)
  499. //$str_query = "SELECT {context}.id AS contextid, {course_modules}.id AS cmid, {iassign}.id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  500. $str_query = "SELECT {context}.id, {course_modules}.id AS cmid, {iassign}.id AS ia_id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  501. "{course_modules}.module=" . $modules_iassignid . " AND {iassign}.course={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance={iassign}.id";
  502. //SELECT s_context.id, s_course_modules.id AS cmid, s_iassign.id AS ia_id, s_iassign.course, s_iassign.name FROM s_iassign, s_context, s_course_modules WHERE s_course_modules.module=9 AND
  503. // s_iassign.course=s_course_modules.course AND s_course_modules.id=s_context.instanceid AND s_course_modules.instance=s_iassign.id
  504. $tmp_list_iassign = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  505. print " - #tmp_list_iassign=" . count($tmp_list_iassign). "<br/>";
  506. //D echo "#" . $SEP . "id (context.id)" . $SEP . "ia_id (iassign.id)" . $SEP . "course" . $SEP . "cmid" . $SEP . "name" . $SEP . "contextid" . "<br/>"; $ii++;
  507. // Process $tmp_list_iassign to produce the array $list_iassign continous (starting from $list_iassign[0]),
  508. // with only one entry to each {iassign}.id and keeping all of its associated {context}.id under the array $list_iassign[$ii]->id
  509. // The {iassign}.id is $list_iassign[$ii]->ia_id
  510. $list_iassign = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  511. $ii = 0;
  512. foreach ($tmp_list_iassign as $element) { // one {iassign} could have several contextid
  513. if ($ii==0 || $list_iassign[$ii-1]->ia_id != $element->ia_id) { // new element
  514. //D echo $ii . $SEP . $element->id . $SEP . $element->ia_id . $SEP . $element->course . $SEP . $element->cmid . $SEP . "'" . $element->name . "'" . $SEP . "<br/>"; // id=contextid
  515. $item = array(); // change to array
  516. $item[] = $element->id; // contextid
  517. $element->id = $item; // change to array - contextid = $item
  518. $list_iassign[$ii] = $element; // transcript array indexed by 'id' to "continous array"
  519. $ii++;
  520. }
  521. else { // just add new contextid (extends array $list_iassign[$ii-1]->id[])
  522. //_ $item = $list_iassign[$ii-1]->id; // is array -- contextid
  523. //_ $item[] = $element->id; // contextid
  524. //_ $list_iassign[$ii-1]->id = $item; // contextid
  525. $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  526. //D echo ($ii-1) . $SEP . $element->id . $SEP . $element->ia_id . $SEP . $element->course . $SEP . $element->cmid . $SEP . "'" . $element->name . "'" . $SEP . " *** #=" . count($list_iassign[$ii-1]->id) . "<br/>"; // id=contextid
  527. }
  528. }
  529. $sizeOf_list_iassign = $ii;
  530. print " - #list_iassign=" . $sizeOf_list_iassign . "<br/>";
  531. //D for ($jj=0; $jj<$sizeOf_list_iassign; $jj++) {
  532. //D $element = $list_iassign[$jj];
  533. //D echo $jj . $SEP . $element->ia_id . $SEP . $element->iassign_ilmid . $SEP . $element->iassignid . $SEP . $element->course . $SEP . $element->file . $SEP . $element->fileid .
  534. //D $SEP . $element->timecreated . $SEP . "'" . $element->author_name . "'" . $SEP . "'" . $element->name . "': contextid=[";
  535. //D $sizeC = count($element->id); // ($element->contextid);
  536. //D for ($kk=0; $kk<$sizeC; $kk++) echo $element->id[$kk] . ","; // contextid[$kk]
  537. //D if ($sizeC>1) echo "] ****************************************<br/>";
  538. //D else echo "]<br/>";
  539. //D }
  540. //L print "====================================================================================<br/>";
  541. /// Get {iassign_statement} :: $list_ias[] = { courseid (iassign.course) ; id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; array of context.id }
  542. ///
  543. //ATTENTION: 'get_records_sql(.)' return only the first element (first field) => use unique first identifier {iassign_statement}.id
  544. print "---<br/>Get {iassign_statement} :: list_ias[] = { courseid (iassign.course) ; id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; array of context.id }<br/>\n";
  545. //$str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  546. // " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated " .
  547. // " FROM {iassign_statement} ORDER BY {iassign_statement}.iassignid, {iassign_statement}.id";
  548. $str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  549. " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated, " .
  550. " {iassign}.course AS courseid FROM {iassign}, {iassign_statement} WHERE {iassign_statement}.iassignid = {iassign}.id ORDER BY {iassign}.course, {iassign_statement}.iassignid, {iassign_statement}.id";
  551. //SELECT s_iassign_statement.id, s_iassign_statement.name, s_iassign_statement.iassignid, s_iassign_statement.author_name, s_iassign_statement.iassign_ilmid, s_iassign_statement.file,
  552. // s_iassign_statement.filesid, s_iassign_statement.timecreated, s_iassign.course AS courseid FROM s_iassign, s_iassign_statement
  553. //WHERE s_iassign_statement.iassignid = s_iassign.id ORDER BY s_iassign.course, s_iassign_statement.iassignid, s_iassign_statement.id
  554. //SELECT s_iassign_statement.id, s_iassign_statement.name, s_iassign_statement.iassignid, s_iassign_statement.author_name, s_iassign_statement.iassign_ilmid,
  555. // s_iassign_statement.file, s_iassign_statement.filesid, s_iassign_statement.timecreated FROM s_iassign_statement
  556. $list_ias = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  557. $tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  558. $jj = 0;
  559. foreach ($tmp_list_ias as $element) {
  560. $answer = find_course_context($list_iassign, $element->iassignid); // to receive the array with all {context}.id associated with its {iassign}
  561. $element->contextid = $answer[0]; // -1 => error; otherwise is an array with all associated {context}.id
  562. //$element->courseid = $answer[1]; // -1 => error; otherwise {iassicn}.course = {course}.id
  563. if ($element->courseid != $answer[1]) echo " x Error: (" . $jj . ", id=" . $element->id . ") courseid=" . $element->courseid . "!=" . $answer[1] . "<br/>\n";
  564. $list_ias[$jj] = $element; // transcript array indexed by 'id' to "continous array"
  565. $jj++;
  566. }
  567. $sizeOf_list_ias = $jj;
  568. //L for ($jj=0; $jj<$sizeOf_list_ias; $jj++) {
  569. //L $element = $list_ias[$jj];
  570. //L //D echo $jj . $SEP . $element->id . $SEP . $element->iassign_ilmid . $SEP . $element->iassignid . $SEP . $element->file . $SEP . $element->fileid .
  571. //L //D $SEP . $element->timecreated . $SEP . "'" . $element->author_name . "'" . $SEP . "'" . $element->name . "'" . $SEP . "[";
  572. //L //D $sizeC = count($element->contextid); // array of {context}.id // $element->contextid . $SEP
  573. //L //D for ($kk=0; $kk<$sizeC; $kk++) echo $element->contextid[$kk] . $SEP;
  574. //L //D echo "]<br/>";
  575. //L }
  576. print " - #list_ias=" . $sizeOf_list_ias . "<br/>";
  577. //L print "====================================================================================<br/>";
  578. /// Get {files} :: $list_all_files[] = { id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id }
  579. ///
  580. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  581. print "---<br/>Get {files} :: list_all_files[] = { id ; contenthash ; contextid ; component ; filearea ; itemid ; filepath ; filename ; userid ; author ; timecreated ; ias.id }<br/>\n";
  582. //$list_files = array(); // $list_files[$ii] = { ia_id, ia_file, ia_filesid, filesid=files.id, filename=files.filename, itemid=files.itemid, contextid=files.contextid }
  583. // Get all {files} associated with {iassign} (here the additional field "ias_id" is "-1")
  584. // Fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  585. // If $list_iassign[$ii] wasn't associated to any {iassign_statement} then "ias_id" := -1 (otherwise "ias_id" := {iassign_statement}.id
  586. $list_all_files = get_all_files($DB, $list_iassign);
  587. echo " - #list_files=" . count($list_all_files) . "<br/>\n"; // &nbsp; *
  588. print "<style> .error { background-color: #f4d6d2; color: #ca3120; }</style>\n";
  589. $msgErr = " class='error' "; // "color='#aa0000'";
  590. $msgTErr = ""; // " title='Not found' ";
  591. print "<form action='settings_process_files_form.php' method='post' class='form-example'>\n"; // form to be processed by 'setting_process_files_form.php'
  592. print "<button type='submit' class='button action has-icon search-button'>Enviar</button>\n"; //TODO: internacionalizar
  593. print "<input type='hidden' name='ilm_id' value='" . $modules_iassignid . "'>\n";
  594. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  595. print "<tr><td title='count'>count</td><td>course.id</td><td title='iAssign id'>iassign.id</td><td>iAssign statement id</td><td title='iLM id'>iLM id</td><td title='context.id'>contextid</td>" .
  596. "<td title='iassign_statement.file'>file</td><td title='iassign_statement.filesid'>filesid</td><td title='date(timecreated)'>timecreated</td><td title='author'>Author</td><td title='name'>ias.name</td>" .
  597. "<td title='{file}.filename'>{files}.filename</td><td title='{file}.id'>id</td><td title='{files}.itemid'>itemid</td></tr>\n";
  598. // <td title='ID:={course_modules}.id to access mod/iassign/view.php?id=ID'>{course_modules}.id</td><td># files</td>";
  599. // Arrays to be used in "select" to recover missing files: $err_iassignid[], $err_iassignstatementid[], $err_selectedfilesid[]
  600. $err_iassignid = array(); // array with the $jj {iassign}.id with no {iassign_statement} withount any {files}
  601. $err_iassignstatementid = array(); // array with the $jj {iassign_statement}.id with no {files}
  602. $err_selectedfilesid = array(); // array with the $jj {files}.id to be associated with the $jj {iassign_statement}
  603. $countErr = 0;
  604. $countIA = 0;
  605. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  606. //D for ($jj=0; $jj<10; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  607. $element = $list_ias[$jj]; // array of { id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; contextid (array of context.id) }
  608. $answerSF = search_files($list_all_files, $element); // return [$ii, $jj] = {files}[$ii] is the associated file using context {iassign_statement}.contextid[$jj]
  609. if ($answerSF == -1) { // don't find!
  610. // List all unmatched {files} under the same context
  611. // It will return tag SELECT 'filesid[]' with sizeof($files_index_candidates) options
  612. [$msgCandidates, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_filesbyID] = search_lost_files_in_context(1, $countErr, $element->courseid, $element->id, $list_all_files, $element->contextid, $countErr);
  613. //if ($jj==29) $msgCandidates = "<br/>#files_index_candidates=" . count($files_index_candidates) . " " . $msgCandidates; // " contextid=" . $element->contextid .
  614. //else
  615. if (count($files_index_candidates)==0) $msgCandidates = "";
  616. else $msgCandidates = "<br/>" . $msgCandidates;
  617. // $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  618. $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  619. print $msgHidden;
  620. print "<tr " . $msgTErr . "><td>" . $countErr . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  621. //RRR print "<tr " . $msgTErr . "><td>" . $jj . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  622. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  623. " <td title='not found file possible array of context.id'>" . $msgC . "</td><td title='iassign_statement.file'>" . $element->file . "</td>\n" .
  624. " <td title='iassign_statement.filesid'>" . $element->filesid . "</td><td>" .
  625. date("d/m/Y H:i:s", $element->timecreated) . "</td><td>" .
  626. $element->author_name . "</td> <td>" . $element->name . "</td><td colspan='3' " . $msgErr . ">\n" . $msgCandidates . "</td></tr>\n"; // ERRO
  627. $err_iassignid[] = $element->iassignid; // array with the $jj {iassign}.id with no {iassign_statement} withount any {files}
  628. $err_iassignstatementid[] = $element->id; // array with the $jj {iassign_statement}.id with no {files}
  629. $err_selectedfilesid[] = countErr; // array with the $jj {files}.id to be associated with the $jj {iassign_statement}
  630. $countErr++;
  631. }
  632. else {
  633. /*
  634. [$indexF, $indexC] = $answerSF; // {files}[$indexF] is the associated file using context {iassign_statement}.contextid[$indexC]
  635. print "<tr><td>" . $jj . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>\n" .
  636. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  637. " <td title='[files.contextid]'>" . $element->contextid[$indexC] . "</td><td title='iassign_statement.file'>" . $element->file . "</td><td title='iassign_statement.filesid'>" . $element->filesid . "</td>" .
  638. " <td>" . date("d/m/Y H:i:s", $element->timecreated) .
  639. "</td><td>" . $element->author_name . " <td>" . $element->name . "</td><td title='Found {files}.id in list_all_files[" . $indexF . "]: ias_id=" . $list_all_files[$indexF]->ias_id. "'>" .
  640. $list_all_files[$indexF]->filename . "</td><td title='Found in list_all_files[" . $indexF . "]->id={files}.id'>" . $list_all_files[$indexF]->id . "</td>" .
  641. "<td title='{files}.itemid'>" .
  642. $list_all_files[$indexF]->itemid . "</td></tr>\n";
  643. //TODO Usar $list_all_files[$indexF]->id = {files}.id para redefinir campo {iassign_statement}.filesid: UPDATE $list_ias[$jj]->filesid = $list_all_files[$indexF]->id
  644. //TODO Usar $list_ias[$jj]->ia_id = {iassign_statement}.id para redefinir campo {files}.itemid : UPDATE
  645. //$list_all_files[$indexF]->id = {files}.id
  646. *///RRR
  647. $countIA++;
  648. }
  649. }
  650. print "</table>\n";
  651. print "</form>\n";
  652. print "Total of {iassign_statement}: " . $countIA . "<br>";
  653. print "Total of {iassign_statement} without any {files}: " . $countErr . "<br>";
  654. return $list_iassign;
  655. } // function see_all_iass_files($DB, $modules_iassignid)
  656. //====================================================================================================================================
  657. // Create entry with { ia_id, ia_file, ia_filesid, ia_name, filesid, filename, itemid } for each iAssign statement and files
  658. function createNewEntry ($iassign_statement, $element_file) {
  659. $new_file = new stdClass(); // { ia_id, ia_file, ia_filesid, ia_name, filesid, filename, itemid } for each iAssign statement and files
  660. $new_file->ia_id = $iassign_statement->id;
  661. $new_file->ia_name = $iassign_statement->name;
  662. $new_file->ia_file = $iassign_statement->file;
  663. $new_file->ia_filesid = $iassign_statement->filesid;
  664. if ($element_file != NULL) {
  665. $new_file->filesid = $element_file->id;
  666. $new_file->filename = $element_file->filename;
  667. $new_file->itemid = $element_file->itemid;
  668. $new_file->contextid = $element_file->contextid;
  669. }
  670. else {
  671. $new_file->filesid = NULL;
  672. $new_file->filename = NULL;
  673. $new_file->itemid = NULL;
  674. $new_file->contextid = NULL;
  675. }
  676. return $new_file;
  677. } // function createNewEntry($iassign_statement, $element_file)
  678. // Try several method to recover file (MoodleData) associated to the iAssign activity
  679. // Given array $all_context, defined a clean version $all_context_final
  680. // @param &$all_context_final : passed by reference
  681. // @calledby this->get_files($course_id, $DB)
  682. function get_associated_files ($DB, $iassign_statement, $all_context, &$all_context_final, $ii) {
  683. global $USER; //DEBUG: apenas durante depuracao!
  684. $array_context_element = $all_context[$ii]; // {context}.id AS contextid, {course}.id, {course}.fullname, {course}.shortname, {course_modules}.id AS course_module_id, {context}.instanceid
  685. $sizeAC = count($array_context_element);
  686. if (!is_array($array_context_element)) { // just in case '$answer_list = $DB->get_records_sql($str_query);' returned only one element
  687. $array_context_elements = array();
  688. $array_context_elements[] = $all_context[$ii];
  689. }
  690. else
  691. $array_context_elements = $all_context[$ii];
  692. $sizeAC = count($array_context_elements);
  693. // for ($jj=0; $jj<$sizeAC; $jj++) { $element_context = $array_context_elements[$jj]; $msgEC .= " &nbsp; * " . $element_context->contextid . ", " . $element_context->id . ", '" . $element_context->shortname . "', " . $element_context->course_module_id . ", " . $element_context->instanceid . "<br/>"; }
  694. //D__ echo "----<br/>#array_context_elements=" . $sizeAC . " (get_associated_files(.))<br/>\n";
  695. $countF = 0;
  696. $jj = 0;
  697. while ($jj<$sizeAC) {
  698. $context_element = $array_context_elements[$jj];
  699. $contextid = $context_element->contextid; // context of this 'iassign.id'
  700. //$msgEC .= " &nbsp; * " . $context_element->contextid . ", " . $context_element->id . ", '" . $context_element->shortname . "', " . $context_element->course_module_id . ", " . $context_element->instanceid . "<br/>"; }
  701. //D__ echo " * Tentar com contextid=" . $contextid . ", iassign_statement.id=" . $iassign_statement->id . ", " . $context_element->id . ", '" . $context_element->shortname . "', " . $context_element->course_module_id . ", " . $context_element->instance . " (jj=" . $jj . ")<br/>";
  702. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  703. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  704. // To avoid programming mistake on old versions, try to get 'files' with 'iassign_statement.id', 'iassign_statement.file' and 'iassign_statement.filesid'
  705. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  706. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  707. if ($numf == 0) {
  708. if (isset($iassign_statement->file))
  709. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file)); // all 'files' associated
  710. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.file"; //D
  711. if ($numf == 0) {
  712. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.itemid'
  713. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->filesid));
  714. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid"; //D
  715. if ($numf == 0) {
  716. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.id'
  717. $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  718. $numf = count($files); $countF += $numf; $msgO = "files.id=iassign_statement.filesid"; //D
  719. // if ($numf == 0) {
  720. // // try 'files.itemid = iassign_statement.id'
  721. // $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  722. // $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid=files.itemid"; //D
  723. // }
  724. if ($numf == 0) {
  725. //D__ echo " &nbsp; * files: " . $numf . " - ERRO 'id' 'file' 'filesid' to statement id=" . $iassign_statement->id . ": " . $iassign_statement->name . "<br/>\n"; //D
  726. //D__ if ($iassign_statement->id==5118) {
  727. //D__ $context = context_module::instance($USER->cm); // usado no: ilm_handlers/html5.php
  728. //D__ //D__ echo " &nbsp; * {course_module}.id=" . $USER->cm .", USER.{context}.id=" . $context->id . "<br/>\n";
  729. //D__ $fs = get_file_storage();
  730. //D__ $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement->id); $fileid = -1; $filename = "";
  731. //D__ if ($files) { //TODO To be revised? 'files.filename' has '.' is only path (not the file)
  732. //D__ foreach ($files as $value) { if ($value->get_filename() != '.') { $fileid = $value->get_id(); $filename = $value->get_filename(); break; } }
  733. //D__ }
  734. //D__ //D__ echo " &nbsp; * codigo de 'ilm_handlers/html5.php': file.id=" . $fileid . ", filename=" . $filename . "<br/>";
  735. //D__ }
  736. }
  737. }
  738. }
  739. } // if ($numf == 0)
  740. if ($numf>0) { $msgF = ""; foreach ($files as $element_file) $msgF .= "id=" . $element_file->id . ", filename='" . $element_file->filename . "'; ";
  741. //D__ echo " &nbsp; * files: " . $numf . " - encontrei {files}: " . $msgF . "<br/>\n"; //D echo " &nbsp; * files: "; print_r($files); echo "<br/>\n"; //
  742. $all_context_final[$ii] = $context_element; // now only one array with {context}.id AS contextid, {course}.id, {course}.fullname, {course}.shortname, {course_modules}.id AS course_module_id, {context}.instanceid
  743. return [ $files, $contextid, $numf, $msg0 ];
  744. }
  745. $jj++;
  746. }
  747. $all_context_final[$ii] = $context_element; // now only one array
  748. return [ null, -1, 0, $msg0 ];
  749. } // function get_associated_files($DB, $iassign_statement, $all_context, &$all_context_final, $ii)
  750. // @calledby locallib.php : add_edit_iassign(.)
  751. // @calledby settings_activities.php: [$list_all_instances_of_iassign, $all_context, $list_of_ia_files] = get_files($courseid, $DB); // inside 'files_functions.php'
  752. function get_files ($course_id, $DB) { //2022
  753. //__ echo "get_files(.): course_id=" . $course_id . "<br/>";
  754. // Get all iAssignment block inside this course
  755. if (isset($course_id) && $course_id)
  756. $answer_list_iassign = $DB->get_records('iassign', array('course' => $course_id)); //R $this->get_courseid() -> $course_id
  757. else
  758. $answer_list_iassign = $DB->get_records('iassign'); // all courses
  759. //$tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  760. //D echo "#answer_list_iassign = " . sizeof($answer_list_iassign) . "<br/>";
  761. //$list_iassign = $DB->get_records('iassign');
  762. //echo "#list_iassign=" . count($list_iassign) . "<br/>"; $SEP = ", "; // 880
  763. //$ii = 0; foreach ($list_iassign as $element) { echo $ii . $SEP . $element->id . $SEP . $element->course . $SEP . $element->name . "<br/>"; $ii++; }
  764. // Build 3 arrays (list_all_instances_of_iassign[.], all_context[.], list_of_files[.])
  765. // - $list_all_instances_of_iassign[$ii] <-> $all_context[$ii] <-> $list_of_files[$ii]
  766. // Get all context of each iAssignment block inside this course: $list_all_instances_of_iassign[.] ; $all_context[.]
  767. $list_all_instances_of_iassign = array(); // will return { id, contextid, shortname} = { iassign.id, context.id, course.shortname } <-> $list_of_files[.]
  768. $all_context_final = array(); // will be defined by $all_context considering eventually more then one context to recover {files}
  769. $all_context = array();
  770. $ii = 0;
  771. foreach ($answer_list_iassign as $iassign) { // get all context of each iAssign block: this iAssign block $iassign
  772. $list_all_instances_of_iassign[] = $iassign;
  773. //DEBUG: the abstract function of "/lib/dml/moodle_database.php!get_records_sql(.)" are returning only half of rows! Than is recommended to use 'get_in_or_equal(.)'!
  774. //DEBUG: ATTENTION, get_records_XX() family of functions inside Moodle always returns one associative array, i.e. one array of records where the key is the first field in the SELECT clause.
  775. //DEBUG: So, if you get two records with the same value in the 1st field the functions will "group" them, returning the last one in the real recordset.
  776. //DEBUG: Then is necessary to use as the first field one that has unique representation considering all rows,
  777. //DEBUG: so we must use '{context}.id' since several of them could be associated to one {course_modules}
  778. $str_query = "SELECT {context}.id AS contextid, {course}.id, {course}.fullname, {course}.shortname, {course_modules}.id AS course_module_id, {context}.instanceid " .
  779. " FROM {course}, {context}, {course_modules} WHERE " .
  780. " {course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance=" . $iassign->id;
  781. // " {course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.course=" . $course_id . " AND {course_modules}.instance=" . $iassign->id;
  782. //D echo "str_query=" . $str_query . "<br/>"; // SELECT {context}.id AS contextid, {course}.id, {course}.fullname, {course}.shortname, {course_modules}.id AS course_module_id, {context}.instanceid FROM {course}, {context}, {course_modules} WHERE {course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance=?
  783. $answer_list = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  784. //D if ($iassign->id==1299) { // ($iassign->id==1) { //
  785. //D echo "query=" . $str_query . "<br/>#answer_list=" . count($answer_list) . ": <br/>"; } // print_r($answer_list); echo "<br/>"; }
  786. $list1 = array();
  787. $strAux = "";
  788. foreach ($answer_list as $item) {
  789. $list1[] = $item;
  790. //_ $strAux = "[" . $item->id . "," . $item->contextid . "," . $item->course_module_id . "," . $item->shortname . "," . $item->instanceid . "] ";
  791. //_ if ($iassign->id==1299) { // ($iassign->id==1) { // print_r($item);
  792. //_ echo " - iassign.id=" . $list_all_instances_of_iassign[$ii]->id . ": [id,context.id,course_module.id,shortname,instanceid]=" . $strAux . "<br/>\n";
  793. //_ }
  794. }
  795. $all_context[$ii] = $list1; // $list1[0]; // { course.id, course.fullname, course.shortname, context.id }
  796. //__ echo " - iassign.id=" . $list_all_instances_of_iassign[$ii]->id . ", contextid=" . $all_context[$ii]->contextid . ", : " . $strAux . "<br/>\n"; //print_r($answer_list); echo "<br/>\n";
  797. $ii++;
  798. } // foreach $answer_list_iassign
  799. //D echo "Total de atividades: ii = " . $ii . "<br/>";
  800. // Built the list of all iAssign associated with this block of activities ('iassign_statement' and 'files')
  801. // This list is associated with the list of iAssign instances: $list_of_files[$ii] <=> $all_context[$ii]
  802. // $list_of_files[$ii] is an array of all {iassign.id, iassign.name, iassign_statement.id, iassign_statement.filesid, iassign_statement.name, files.id, files.contextid, files.itemid, files.name}
  803. $list_of_ia_files = array(); // as { iassign_statement->id, iassign_statement->name, iassign_statement->filesid, files->id, files->contextid, files->itemid, files->filename}
  804. $sizeofList = $ii; // size of $all_context[]
  805. for ($ii=0; $ii<$sizeofList; $ii++) { // get all iAssign block of activities (course.moudules)
  806. $iassign = $list_all_instances_of_iassign[$ii];
  807. $list_all_iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id)); // get all activities inside this block
  808. $list_of_files = array(); // $list_of_files[$ii] = { ia_id, ia_file, ia_filesid, filesid=files.id, filename=files.filename, itemid=files.itemid, contextid=files.contextid }
  809. //D__ echo "#list_all_iassign_statement = " . count($list_all_iassign_statement) . " de iassign.id=" . $iassign->id . "<br/>\n"; //D2022
  810. foreach ($list_all_iassign_statement as $iassign_statement) { // get all iAssign activity inside the block '$list_all_iassign_statement'
  811. // $contextid = $all_context[$ii]->contextid; // context of this 'iassign.id'
  812. // Build $all_context_final[$ii] as $all_context[$ii][0] or $all_context[$ii][1] - If $contextid==-1 then erro! Could not recover file!
  813. [$files, $contextid, $numf, $msg0 ] = get_associated_files($DB, $iassign_statement, $all_context, $all_context_final, $ii);//echo " &nbsp; * ii=" . $ii . ": #all_context_final[" . $ii . "]=" . count($all_context_final[$ii]) . "<br/>\n";
  814. //D__ echo " - ii=" . $ii . ": iassign_statement {id = " . $iassign_statement->id . ", file = " . $iassign_statement->file . ", filesid = " . $iassign_statement->filesid . "}; contextid=" . $contextid . "=" . $all_context_final[$ii]->contextid . ", date(timecreated)=" . date("Y/m/d H:i:s", $iassign_statement->timecreated) . " : " . $iassign_statement->name . "<br/>\n"; //D2022
  815. if ($numf > 0) { // if $iassign_statement has any file (in {files})
  816. //__ echo " &nbsp; * files: " . $numf . ": " . $msgO . "<br/>\n"; //D
  817. $filename = array();
  818. foreach ($files as $element_file) { // analyse each '$element_file' of 'files'
  819. //D__ echo " &nbsp; * ii=" . $ii . ": files: id = " . $element_file->id . ", contextid = " . $element_file->contextid . ", itemid=" . $element_file->itemid . " : " . $element_file->filename . "<br/>\n"; //D
  820. $new_file = createNewEntry($iassign_statement, $element_file); // create entry with { ia_id, ia_file, ia_filesid, ia_name, filesid, filename, itemid } for each iAssign statement and files
  821. if ($element_file->filename != '.') { // get the '$element_file' that is not only "path"
  822. $filename = explode(".", $element_file->filename);
  823. if ($iassign_statement->filesid != $element_file->id) { // need to update 'iassign_statement.filesid'?
  824. // echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.filesid'=" . $iassign_statement->filesid . " <- " . $element_file->id . " : " . $element_file->filename . "<br/>"; //2022
  825. //++ Deixar para outro corrigir! A partir de 'settings_activities.php'
  826. $iassign_statement->filesid = $element_file->id; // 'iassign_statement.filesid' <- 'files.id' (update bellow) //2022 deixar para outro corrigir!
  827. }
  828. }
  829. if ($element_file->itemid != $iassign_statement->id) { // need to update 'files.itemid'? ('files.itemid <- iassign_statement.id') //2022
  830. $newentry_be_updated = new stdClass();
  831. $newentry_be_updated->id = $element_file->id; $newentry_be_updated->itemid = $iassign_statement->id;
  832. // echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'files.itemid': id=" . $newentry_be_updated->id . ", itemid=" . $element_file->itemid . "<-" . $newentry_be_updated->itemid . " : " . $element_file->filename . "<br/>"; //2022
  833. //R NAO alterar!
  834. //R if (!$DB->update_record('files', $newentry_be_updated)) print_error('error_update', 'iassign');
  835. }
  836. if (isset($new_file->ia_id) && $new_file->ia_id>0) { // security...
  837. $list_of_files[] = $new_file; // $list_of_files[$ii] = { ia_id, ia_file, ia_filesid, filesid=files.id, filename=files.filename, itemid=files.itemid, contextid=files.contextid }
  838. } // echo " &nbsp; * # = " .
  839. } // foreach ($files as $element_file)
  840. $list_of_ia_files[$ii] = $list_of_files;
  841. $extension = ""; // extension of the current '$element_file'
  842. if (count($filename) > 1) $extension = strtolower($filename[count($filename) - 1]);
  843. foreach ($list_all_iassign_ilm as $iassign_ilm) { // find iLM with the same extension (to register its 'iassign_ilm.id')
  844. $extensions_of_ilm = explode(",", $iassign_ilm->extension);
  845. if (in_array($extension, $extensions_of_ilm)) { // it was found the iLM with correct extension - get the lastast version
  846. $iassign_statement->iassign_ilmid = $iassign_ilm->id; // the iLM identification: iassign_ilm.id
  847. // echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.iassign_ilmid': " . $iassign_statement->iassign_ilmid . " <- " . $iassign_ilm->id . "<br/>\n";
  848. }
  849. }
  850. //D__ echo " - Troca iassign_statement.id=" . $iassign_statement->id . " (" . $iassign_statement->name . ") troca filesid para " . $iassign_statement->filesid . "<br/>";
  851. //R $DB->update_record("iassign_statement", $iassign_statement); // update 'iassign_statement' (mainly 'iassign_statement.filesid')
  852. } // if ($numf > 0)
  853. else { // Error!!! Couldn't find any {files} associated to this statement!
  854. // echo " &nbsp; * ii=" . $ii . ": files: vazio!<br/>\n";
  855. $list_of_files[] = createNewEntry($iassign_statement, NULL); // create entry with { ia_id, ia_file, ia_filesid, ia_name, filesid=NULL, filename=NULL, itemid=NULL }
  856. $list_of_ia_files[$ii] = $list_of_files;
  857. }
  858. } // foreach ($list_all_iassign_statement as $iassign_statement)
  859. } // for ($ii=0; $ii<$sizeofList; $ii++)
  860. return [$list_all_instances_of_iassign, $all_context_final, $list_of_ia_files];
  861. } // function get_files($course_id, $DB)
  862. // Codigo de teste tinha inserido em 'locallib.php' (era invocado linha 2458/9172): static function test_iassign_statement_files()
  863. // @calledby locallib.php : add_edit_iassign(.)
  864. function see_all_files_context ($course_id, $DB) { //2022 testar 'restore_iassign_stepslib.php()' - remover!
  865. // See mod/iassign/backup/moodle2/restore_iassign_stepslib.php : after_execute()
  866. // /var/www/html/moo391p/mod/iassign/backup/moodle2/restore_iassign_stepslib.php
  867. //global $CFG, $DB, $USER;
  868. //$fs = get_file_storage();
  869. $context_id = 1;
  870. // $context = context_module::instance($USER->cm); //2022/02: get 'context.id' = 'files.contextid'
  871. echo "files_functions.php: context.id=" . $context_id . "<br/>\n";
  872. $countF = 0;
  873. // Get all iLM installed in iAssign
  874. $list_all_iassign_ilm = $DB->get_records('iassign_ilm', array('parent' => 0, 'enable' => 1));
  875. // Get all iAssignment block inside this course
  876. $answer_list_iassign = $DB->get_records('iassign', array('course' => $course_id)); //R $this->get_courseid() -> $course_id
  877. // Get all context of each iAssignment block inside this course
  878. $list_all_instances_of_iassign = array(); // with { iassign.id, context.id, course.shortname } as { id, contextid, shortname }
  879. $all_context = array();
  880. $ii = 0;
  881. foreach ($answer_list_iassign as $iassign) { // get all context of this iAssign block $iassign
  882. $list_all_instances_of_iassign[] = $iassign;
  883. $str_query = "SELECT {course}.id, {course}.shortname, {context}.id AS contextid FROM {course}, {context}, {course_modules} WHERE " .
  884. "{course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.course=" . $course_id . " AND {course_modules}.instance=" . $iassign->id;
  885. //echo "query=" . $str_query . "<br/>";
  886. $answer_list = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  887. $list1 = array();
  888. $strAux = "";
  889. foreach ($answer_list as $item) { $list1[] = $item; $strAux .= "[" . $item->id . "," . $item->contextid . "," . $item->shortname . "] "; }
  890. $all_context[$ii] = $list1[0];
  891. echo " - iassign.id=" . $list_all_instances_of_iassign[$ii]->id . ", contextid=" . $all_context[$ii]->contextid . " : " . $strAux . "<br/>\n"; //print_r($answer_list); echo "<br/>\n";
  892. $ii++;
  893. }
  894. $sizeofList = $ii;
  895. for ($ii=0; $ii<$sizeofList; $ii++) { // get all iAssign block of activities (course.moudules)
  896. $iassign = $list_all_instances_of_iassign[$ii];
  897. $list_all_iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id)); // get all activities inside this block
  898. $contextid = $all_context[$ii]->contextid; // context of this 'iassign.id'
  899. //D echo "#list_all_iassign_statement = " . count($list_all_iassign_statement) . " de iassign.id=" . $iassign->id . "<br/>\n"; //D2022
  900. foreach ($list_all_iassign_statement as $iassign_statement) { // get all iAssign activity inside the block '$list_all_iassign_statement'
  901. //D echo " - iassign_statement {id = " . $iassign_statement->id . ", file = " . $iassign_statement->file . ", filesid = " . $iassign_statement->filesid .
  902. //D "}; contextid=" . $contextid . ", date(timecreated)=" . date("Y/m/d H:i:s", $iassign_statement->timecreated) . " : " . $iassign_statement->name . "<br/>\n"; //D2022
  903. // To avoid programming mistake on old version try to get 'files' with 'iassign_statement.id', 'iassign_statement.file' and 'iassign_statement.filesid'
  904. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  905. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  906. if ($numf == 0) {
  907. if (isset($iassign_statement->file))
  908. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file)); // all 'files' associated
  909. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.file"; //D
  910. if ($numf == 0) {
  911. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.id'
  912. $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  913. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid"; //D
  914. if ($numf == 0) {
  915. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.itemid'
  916. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->filesid));
  917. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid=files.itemid"; //D
  918. if ($numf == 0)
  919. echo " &nbsp; * files: " . $numf . " - ERRO 'id' 'file' 'filesid'<br/>\n"; //D
  920. }
  921. }
  922. }
  923. if ($numf > 0) { echo " &nbsp; * files: " . $numf . ": " . $msgO . "<br/>\n"; //D
  924. $filename = array();
  925. foreach ($files as $element_file) { // analyse each '$element_file' of 'files'
  926. echo " &nbsp; * files: id = " . $element_file->id . ", contextid = " . $element_file->contextid . ", itemid=" . $element_file->itemid . " : " . $element_file->filename . "<br/>\n"; //D
  927. if ($element_file->filename != '.') { // get the '$element_file' that is not only "path"
  928. $filename = explode(".", $element_file->filename);
  929. if ($iassign_statement->filesid != $element_file->id) { // need to update 'iassign_statement.filesid'?
  930. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.filesid'=" . $iassign_statement->filesid . " <- " . $element_file->id . " : " . $element_file->filename . "<br/>"; //2022
  931. $iassign_statement->filesid = $element_file->id; // 'iassign_statement.filesid' <- 'files.id' (update bellow) //2022
  932. }
  933. }
  934. if ($element_file->itemid != $iassign_statement->id) { // need to update 'files.itemid'? ('files.itemid <- iassign_statement.id') //2022
  935. //++ Deixar para outro corrigir! A partir de 'settings_activities.php'
  936. $newentry_be_updated = new stdClass(); $newentry_be_updated->id = $element_file->id; $newentry_be_updated->itemid = $iassign_statement->id;
  937. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'files.itemid': id=" . $newentry_be_updated->id . ", itemid=" . $element_file->itemid . "<-" . $newentry_be_updated->itemid . " : " . $element_file->filename . "<br/>"; //2022
  938. //R if (!$DB->update_record('files', $newentry_be_updated)) print_error('error_update', 'iassign');
  939. }
  940. } // foreach ($files as $element_file)
  941. $extension = ""; // extension of the current '$element_file'
  942. if (count($filename) > 1) $extension = strtolower($filename[count($filename) - 1]);
  943. foreach ($list_all_iassign_ilm as $iassign_ilm) { // find iLM with the same extension (to register its 'iassign_ilm.id')
  944. $extensions_of_ilm = explode(",", $iassign_ilm->extension);
  945. if (in_array($extension, $extensions_of_ilm)) { // it was found the iLM with correct extension - get the lastast version
  946. $iassign_statement->iassign_ilmid = $iassign_ilm->id; // the iLM identification: iassign_ilm.id
  947. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.iassign_ilmid': " . $iassign_statement->iassign_ilmid . " <- " . $iassign_ilm->id . "<br/>\n";
  948. }
  949. }
  950. //R $DB->update_record("iassign_statement", $iassign_statement); // update 'iassign_statement' (mainly 'iassign_statement.filesid')
  951. } // if ($numf > 0)
  952. } // foreach ($list_all_iassign_statement as $iassign_statement)
  953. } // for ($ii=0; $ii<$sizeofList; $ii++)
  954. $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  955. $filenamewrite = "_output_restore_after_execute_" . $timeWrite . ".txt";
  956. echo "restore_iassign_stepslib: " . $filenamewrite . "<br/>\nUsando: " . __DIR__ . "/../../ilm_debug/escreva.php" . "<br/>\n"; //D . str_replace("\n","<br>\n", $msgD) . "<br/>\n"; // __DIR__
  957. // ./mod/iassign/ilm_debug/escreva.php: writeContent($filetype1, $pathbase, $outputFile, $msgToRegister)
  958. // ./mod/iassign/ilm_debug/saida_restore_after_execute.txt
  959. //D require_once(__DIR__ . "/../../ilm_debug/escreva.php"); //leo REMOVER! xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  960. //D Grvar em: ./mod/iassign/ilm_debug/output_restore_after_execute_2022_02_dd_hh_mm_ss.txt
  961. //D $resp = writeContent("", "", $filenamewrite, $msgD); // ($filetype1, $pathbase, $outputFile, $msgToRegister) //leo tire comentario para gravar
  962. echo "restore_iassign_stepslib: resp=" . $resp . ", #files=" . $countF . "<br/>\n";
  963. } // function see_all_files_context($course_id, $DB)