files_functions.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 - is the module id (example, if iAssign has {module}.id=7 => {course_modules}.module = 7
  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. // Get all iAssign {files}
  141. // Build "$list_all_files" with fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  142. // Initially "$list_iassign[$ii]".ias_id = -1 (in "search_files(.)" we hope "ias_id" := {iassign_statement}.id)
  143. // @calledby get_associated_ias_file
  144. function get_all_files ($DB, $list_iassign) {
  145. global $USER, $SEP; //DEBUG: apenas durante depuracao!
  146. $sizeAC = count($list_iassign);
  147. //L echo "----<br/>get_all_files: #list_iassign=" . $sizeAC . "<br/>\n";
  148. // Usual access to {files} is through:
  149. // $fs = get_file_storage();
  150. // $files = $fs->get_area_files($context_id, 'mod_iassign', 'exercise', $iassign_statement_id);
  151. // using: ./lib/filestorage/file_storage.php: public function get_area_files($contextid, $component, $filearea, $itemid = false,...)
  152. $str_query = "SELECT * FROM {files} WHERE component='mod_iassign'"; // "; // AND filearea='exercise' AND contextid=108
  153. $tmp_list_all_files = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  154. //D print_r($tmp_list_all_files); echo "<br/>"; // Array ( [37] => stdClass Object ( [id] => 3...
  155. $numf = count($tmp_list_all_files);
  156. //L echo "#tmp_list_all_files=" . $numf . "<br/>"; // #tmp_list_all_files=13383
  157. $list_all_files = array(); // use continous array ($tmp_list_all_files is indexed by 'id')
  158. $ii = 0;
  159. foreach ($tmp_list_all_files as $elem) {
  160. //D echo $ii . $SEP . $elem->id . $SEP . $elem->contextid . $SEP . $elem->timecreated . $SEP . $elem->filearea . $SEP . $elem->itemid . $SEP .
  161. //D "'" . $elem->author . "'" . $SEP . "'" . $elem->filename . "'" . $SEP . "<br/>";
  162. $elem->ias_id = -1; // to receive the associated {iassign_statement}.id (defined in "search_lost_files_in_context(.)")
  163. $elem->courseid = -1; // to receive the associated {iassign_statement}.id (defined in "search_lost_files_in_context(.)")
  164. $elem->missing_ias = TRUE; // by default this {files} is associated to one {iassign_statement} - function "search_files(.)" can change it to FALSE
  165. $list_all_files[$ii] = $elem; // transcript array indexed by 'id' to "continous array"
  166. $ii++;
  167. }
  168. //D for ($ii=0; $ii<$numf; $ii++) {
  169. //D $elem = $list_all_files[$ii];
  170. //D echo $ii . $SEP . $elem->id . $SEP . $elem->contextid . $SEP . $elem->timecreated . $SEP . $elem->filearea . $SEP . $elem->itemid . $SEP .
  171. //D "'" . $elem->author . "'" . $SEP . "'" . $elem->filename . "'" . $SEP . "<br/>";
  172. //D }
  173. $sizeof_list_all_files = $ii;
  174. return $list_all_files;
  175. } // function get_associated_ias_file($DB, $iassign_statement, $all_context, &$all_context_final, $ii)
  176. // Search $element_ias.contextid == $list_all_files[.].contextid and ($element_ias.file == $list_all_files[.].itemid or $element_ias.filesid == $list_all_files[.].id)
  177. // Build "$list_all_files" with fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  178. // Initially "$list_iassign[$ii]".ias_id = -1 (in "search_files(.)" we hope "ias_id" := {iassign_statement}.id)
  179. // @param &$list_all_files: passed by reference, "$list_all_files[$jj].ias_id" could receive {iassign_statement}.id
  180. function search_files (&$list_all_files, $element_ias) {
  181. $sizeOfFiles = count($list_all_files);
  182. $sizeC = count($element_ias->contextid);
  183. //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/>"; }
  184. for ($ii=0; $ii<$sizeOfFiles; $ii++) {
  185. //D Decho $ii . " ";
  186. $element_file = $list_all_files[$ii];
  187. for ($jj=0; $jj<$sizeC; $jj++) {
  188. $contextid = $element_ias->contextid[$jj]; // try with $jj-th {context}.id
  189. if ($contextid == $element_file->contextid) {
  190. if ($element_ias->file == $element_file->itemid) {
  191. //echo "<br/>";
  192. //D echo " - Encontrou " . $element_ias->id . " via 'file' (file==itemid): contextid[" . $jj . "]=" . $contextid . ": " . $element_ias->file . "==" . $element_file->itemid . " no files[" . $ii . "]<br/>";
  193. $element_file->ias_id = $element_ias->id;
  194. $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}
  195. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  196. }
  197. else
  198. if ($element_ias->filesid == $element_file->itemid) {
  199. //echo "<br/>";
  200. //D echo " - Encontrou " . $element_ias->id . " via 'file' (filesid==itemid): contextid[" . $jj . "]=" . $contextid . ": " . $element_ias->filesid . "==" . $element_file->itemid . " no files[" . $ii . "]<br/>";
  201. $element_file->ias_id = $element_ias->id;
  202. $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}
  203. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  204. }
  205. else
  206. if ($element_ias->id == $element_file->itemid) { // Com esse
  207. // Total of {iassign_statement} without any {files}: 771 -> 739 = 32
  208. // Total of {files} without any {iassign_statement}: 10638 -> 10606 = 32
  209. // Total of {files} associated with iAssign: 880 -> 880
  210. //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";
  211. // - Encontrou 5658 via 'id' (id==itemid): contextid[1]=52001: 5658==5658 no files[12521] - {files}.id=301156
  212. // 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
  213. // 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=
  214. $element_file->ias_id = $element_ias->id;
  215. $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}
  216. return [$ii, $jj]; // found the file of {iassign_statement} at {files} index $ii with the context {iassign_statement}.contextid[$jj]
  217. }
  218. }
  219. } // for ($jj=0; $jj<$sizeC; $jj++)
  220. }
  221. //D if ($element_ias->id==5793 || $element_ias->id==5794) echo " - " . $element_ias->id . ", " . $element_ias->name . ": jj=" . $jj . ", ERRO!<br/>";
  222. return -1;
  223. } // function search_files($list_all_files, $element_ias)
  224. // Get all {files} in one specific {course}.id
  225. // Get all of its context, then all of their {files}
  226. // @calledby locallib.php ! view_files_ilm($iassign_ilm_class, $extension)
  227. function get_all_files_course ($DB, $courseid, $fs) {
  228. $modules_iassing_id = get_iassign_id($DB); // get 'id, name, visible' from 'modules' with upper(name)='IASSIGN'
  229. //ATTENTION: since 'get_records_sql(.)' will return only the first element (first field), it is necessary to use as first column {context}.id
  230. // {course_modules}.instance = {context}.instanceid = {iassign}.id
  231. $str_query = "SELECT {context}.id AS contextid, {course_modules}.id, {course_modules}.instance, {context}.instanceid FROM {course_modules},{context} ".
  232. " WHERE {course_modules}.id = {context}.instanceid AND {course_modules}.course = " . $courseid . " AND {course_modules}.module = " . $modules_iassing_id;
  233. //TODO? $array_timecreated = array(); //TODO order by {files}.timecreated?
  234. $array_all_files = array();
  235. //D echo "files_functions.php ! get_all_files_course(.)<br/>\n";
  236. //ATTENTION: 'get_records_sql(.)' return only the first element (first field)
  237. $array_course_modules_context = $DB->get_records_sql($str_query);
  238. foreach ($array_course_modules_context as $item_cm) {
  239. $files_tree = $fs->get_area_files($item_cm->contextid, 'mod_iassign', 'exercise');
  240. foreach ($files_tree as $onefile)
  241. if ($onefile->get_filename()!='.') {
  242. $array_all_files[] = $onefile;
  243. //TODO? $array_timecreated[] = $item; //TODO Use "asort($array_timecreated);" to order?
  244. //D echo " - id=" . $onefile->get_id() . ", contextid=" . $onefile->get_contextid() . ", filename=" . $onefile->get_filename() . "<br/>\n";
  245. }
  246. }
  247. //D echo "files_functions.php ! get_all_files_course(.): #array_all_files=" . count($array_all_files) . "<br/>\n";
  248. return $array_all_files;
  249. }
  250. // Get {iassign_statement} or {files}
  251. // @calledby settings_process_files_form.php
  252. function get_iassignstatement_or_files ($DB, $table, $element_id) {
  253. //D echo "files_functions.php|get_iassignstatement_or_files: " . $element_id . "<br/>\n";
  254. if ($table != 'iassign_statement' && $table != 'files') return NULL; // security
  255. $ia_obj = $DB->get_record($table, array('id' => $element_id));
  256. //D if ($table=='iassign_statement) echo $ia_obj->iassignid . ", " . $ia_obj->id . ", " . $ia_obj->file . ", " . $ia_obj->filesid . ", " . $ia_obj->name . "<br/>\n";
  257. return $ia_obj;
  258. //$files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); //
  259. }
  260. // To be used in restoring backup
  261. // @calledby ./mod/iassign/backup/moodle2/restore_iassign_stepslib.php
  262. function get_all_files_ia ($DB, $ias_id, $ias_file, $ias_filesid, $ias_name) {
  263. // Try to get all associated {files} using: {files}.itemid = {iassign_statement}.id
  264. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_id));
  265. if ($files)
  266. return $files;
  267. // Now try with: {files}.itemid = {iassign_statement}.file
  268. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_file));
  269. if ($files)
  270. return $files;
  271. // Now try with: {files}.itemid = {iassign_statement}.file
  272. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $ias_filesid));
  273. return $files;
  274. }
  275. //TODO: remover funcao 'get_from_files(.)' do locallib.php (linha: 358)
  276. // Get the element associated to {iassign_statement} in {files}
  277. // Since the association was changed in 2020/12/05, but was upgrade non consistent,
  278. // this try to recover with: {files}.itemid = {iassign_statement}.id, after with {iassign_statement}.file and then with {iassign_statement}.filesid
  279. // @return array({files}, {files}) or NULL
  280. // @calledby locallib.php ! add_edit_iassign() ; duplicate_activity()
  281. // @calledby ilm_handlers/html5.php !show_activity_in_ilm(.)
  282. function get_from_files ($ia_id, $ia_file, $ia_filesid, $fs, $contextid, $component, $filearea) {
  283. global $USER;
  284. //D if ($USER->id==3) echo " USER->id==3: files_function.php: get_from_files(.) - {user}.id=3: component=" .$component . "<br/>\n"; //leo
  285. $files_list = false;
  286. if ($ia_id) // try association: {files}.itemid = {iassign_statement}.id
  287. $files_list = $fs->get_area_files($contextid, $component, $filearea, $ia_id);
  288. //D if ($USER->id==3) echo " USER->id==3: + get_area_files com ia_id=" . $ia_id . " => #files_list=" . count($files_list). "<br/>\n"; //leo remover!
  289. if (!$files_list) { // not found with {files}.itemid = {iassign_statement}.id
  290. if ($ia_file) // try association: {files}.itemid = {iassign_statement}.file
  291. $files_list = $fs->get_area_files($contextid, $component, $filearea, $ia_file); // try with {iassign_statement}.file
  292. //D if ($USER->id==3) echo " USER->id==3: + get_area_files com ia_file=" . $ia_file . "<br/>\n"; //leo remover!
  293. if (!$files_list) { // not found with {files}.itemid = {iassign_statement}.file
  294. if ($ia_filesid) // try association: {files}.itemid = {iassign_statement}.filesid
  295. $files_list = $fs->get_area_files($contextid, $component, $filearea, $ia_filesid); // try with {iassign_statement}.filesid
  296. //D if ($USER->id==3) echo " USER->id==3: + get_area_files com ia_filesid=" . $ia_filesid . "<br/>\n"; //leo remover!
  297. if (!$files_list) return NULL; // error!
  298. }
  299. }
  300. $f1_obj = NULL; // will receive the element of {files} with regular 'filename'
  301. $f2_obj = NULL; // will receive the correspondent element of $f1_obj with directory, i.e, filename=='.'
  302. foreach ($files_list as $one_files) { // when activity is duplicated => it has only the regular 'filename'
  303. if ($one_files->get_filename()!=".") $f1_obj = $one_files; // $files_list[0]; // from {files}
  304. else $f2_obj = $one_files; // $files_list[1]; // from {files}
  305. }
  306. // if $f2_obj!=NULL but $f2_obj==NULL, then probably $f1_obj was recovered by 'files_functions.php!get_from_filesAll(.)' in the previous edition
  307. //D if ($USER->id==3) echo " USER->id==3: * &nbsp; id=" . $f1_obj->get_id() . ", filename=" . $f1_obj->get_filename() . "<br/>\n"; //REMOVER
  308. return array($f1_obj, $f2_obj);
  309. } // function get_from_files($ia_id, $ia_file, $ia_filesid, $fs, $contextid, $component, $filearea)
  310. // Debug function: list all {files} considering only {context}.id and {files}.component='mod_iassign' and ({files}.filearea='exercise' or {files}.filearea='activity')
  311. // Using: ./lib/filestorage/file_storage.php: public function get_area_files($contextid, $component, $filearea, $itemid = false,...)
  312. // @return array({files}, {files}) or NULL
  313. // @calledby locallib.php ! add_edit_iassign()
  314. function get_from_filesAll ($fs, $contextid, $component, $filearea) {
  315. global $USER, $DB;
  316. if ($USER->id==3) echo "-------<br/>USER->id==3: files_function.php: get_from_filesAll(.) - {user}.id=3 : contextid=" . $contextid . ", component=" . $component . ", filearea=" . $filearea . "<br/>\n"; //leo
  317. // SELECT id,contextid,component,filearea,itemid,filename,author FROM s_files WHERE contextid=51250 AND (filearea='exercise' OR filearea='activity')
  318. $files_list = false;
  319. //x $files_list = $fs->get_area_files($contextid, $component, $filearea, false, $sort = "itemid, filepath, filename"); // volta vazio!
  320. $files_list = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => $filearea)); //
  321. if ($USER->id==3) {
  322. echo " USER->id==3: + #files_list=" . count($files_list) . "<br/>\n"; //leo remover!
  323. }
  324. if (!$files_list) return NULL; // error!
  325. return $files_list;
  326. } // function get_from_filesAll($fs, $contextid, $component, $filearea)
  327. // Update {files}.itemid to {iassign_statement}.id
  328. // Came here when teacher send the form to update one activity and {files} already exists!
  329. // @calledby locallib.php: update_iassign($param):
  330. function update_files ($DB, $one_file_id, $ia_id) {
  331. $newf = new stdClass();
  332. $newf->id = $one_file_id;
  333. $newf->itemid = $ia_id;
  334. //D echo "files_functions.php: update_files(.): {files}.id=" . $one_file_id . ", {iassign_statement}.id=" . $ia_id . ": {files}.itemid <- " . $ia_id . "<br/>";
  335. //D exit; //REMOVER exit
  336. if (!$DB->update_record('files', $newf)) return FALSE; // not error
  337. return TRUE;
  338. }
  339. // Update {iassign_statement}.filesid and {files}.itemid
  340. // Temporary function to restore relation {iassign_statement}/{files}
  341. // First version of iAssign used a random value to associate 'files.itemid' with 'iassign_statement.file'
  342. // Came here when teacher try to edit any activity, during the form preparation
  343. // @param $DB = database
  344. // @param $ia_id = {iassign_statement}->id
  345. // @param $filesid = {iassign_statement} object
  346. // @param $is_obj = {iassign_statement} object
  347. // @param $f1_obj = {files} obj with id=$filesid ($filesid an specific {files}->id)
  348. // @param $f2_obj = {files} obj with id=$filesid+1
  349. // @calledby settings_process_files_form.php: $answer = update_iassignstatement_files($DB, $iassign_statement_id, $filesid, $is_obj, $f1_obj, $f2_obj);
  350. // @calledby locallib.php:add_edit_iassign(): $result = update_iassignstatement_files($DB, $is_obj->id, $f1_obj->get_id(), $is_obj, $f1_obj, $f2_obj);
  351. function update_iassignstatement_files ($DB, $ia_id, $filesid, $is_obj, $f1_obj, $f2_obj = NULL, $context = NULL) {
  352. global $USER;
  353. // Came here with something like: $ia_obj = $DB->get_record($table, array('id' => $element_id));
  354. $ma = "{iassign_statement}: id=" . $ia_id . " : filesid=" . $is_obj->filesid; //D
  355. $mf1 = "{files}: id=" . $f1_obj->get_id() . ": itemid=" . $f1_obj->get_itemid(); //D
  356. $mf2 = "{files}: [empty]"; //D
  357. $is_obj_filesid = $is_obj->filesid; // {iassign_statement}.filesid initial
  358. $is_obj->filesid = $filesid; // {iassign_statement}.filesid := $f1_obj->id = {files}.id
  359. $is_obj->file = $filesid; // {iassign_statement}.file := $f1_obj->id = {files}.id (update also the old field 'file')
  360. $f1_obj->id = $f1_obj->get_id();
  361. $f1_obj_itemid = $f1_obj->get_itemid(); // first {files}.itemid initial
  362. $f1_obj->itemid = $ia_id; // Necessary to force {files}.id, otherwise will occurs the error:
  363. $ma .= " -> " . $is_obj->filesid; //D
  364. $mf1 .= " -> " . $f1_obj->itemid . " (id=" . $f1_obj->get_id() . ")"; //D
  365. $error = FALSE;
  366. // If {iassign_statement}.filesid != {files}.id update {iassign_statement}
  367. if ($is_obj_filesid != $filesid) // update {iassign_statement} defining: {iassign_statement}.filesid = {files}.id
  368. if (!$DB->update_record('iassign_statement', $is_obj)) $error = TRUE; // print_error('error_update', 'iassign');
  369. // If {files}.itemid != {iassign_statement}.id != update {filesid} (for file with 'filename')
  370. if ($f1_obj_itemid != $ia_id) // update first {files}.item with {iassign_statement}.id
  371. if (!$DB->update_record('files', $f1_obj)) $error = TRUE;
  372. if (is_null($f1_obj_itemid)) $f1_obj_itemid = -1; // to avoid error when {files} is not found
  373. $f2_obj_itemid = -1; // to avoid error when {files} is not found
  374. if ($f2_obj!=NULL) {
  375. $mf2 = "{files}: id=" . $f2_obj->get_id() . ": itemid=" . $f2_obj->get_itemid(); //D
  376. $mf2 .= " -> " . $f2_obj->get_itemid() . " (id=" . $f2_obj->get_id() . ")"; //D
  377. $f2_obj->id = $f2_obj->get_id();
  378. $f2_obj_itemid = $f2_obj->get_itemid(); // second {files}.itemid initial
  379. $f2_obj->itemid = $ia_id; // moodle_database::update_record_raw() id field must be specified.
  380. // If {files}.itemid != {iassign_statement}.id != update {filesid} (for file with directory, filename='.')
  381. if ($f2_obj_itemid != $ia_id) // update second {files}.item with {iassign_statement}.id
  382. if (!$DB->update_record('files', $f2_obj)) $error = TRUE;
  383. }
  384. //D echo "files_functions.php!update_iassignstatement_files(.): 4<br/>"; //exit;//leo
  385. //DEBUG
  386. if ($USER->id==3) { //D REMOVER
  387. $troca = 0; // locallib.php!add_edit_iassign(): $context = context_module::instance($this->cm->id);
  388. echo "<br/><br/><br/><table><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
  389. echo "<td>USER->id==3: files_functions.php: update_iassignstatement_files(): USER==3</td></tr>\n";
  390. echo "<tr><td></td><td>\$COURSE context: id=" .$context->id . "</td></tr>\n";
  391. echo "<tr><td></td><td>{iassign_statement}: id=" .$ia_id . ", file=" . $is_obj->file . ", filesid=" . $is_obj->filesid . "</td></tr>\n";
  392. echo "<tr><td></td><td>{files} 1: id=" . $f1_obj->get_id() . ", contextid=" . $f1_obj->get_contextid() . ", itemid=" . $f1_obj->itemid . ", filearea=" . $f1_obj->get_filearea() .
  393. ", filepath=" . $f1_obj->get_filepath() . ", filename=" . $f1_obj->get_filename() . ", userid=" . $f1_obj->get_userid() . "</td></tr>\n";
  394. echo "<tr><td></td><td>{files} 1 : " .$mf1 . "</td></tr>\n";
  395. echo "<tr><td></td><td>{files} 2 : " .$mf2 . "</td></tr>\n";
  396. if ($is_obj_filesid != $filesid) { echo "<tr><td></td><td>&nbsp; - tenta: update {iassign_statement}.filesid with {files}.id ******* deveria TROCAR!</td></tr>\n"; $troca = 1; }
  397. else echo "<tr><td></td><td>&nbsp; - NAO troca iassign_statement</td></tr>\n";
  398. if ($f1_obj_itemid != $ia_id) { echo "<tr><td></td><td>&nbsp; - tenta: f1: update first {files}.item with {iassign_statement}.id f1.itemid=" . $f1_obj_itemid . "<-" . $ia_id . " ******* deveria TROCAR!</td></tr>\n"; $troca = 1; }
  399. else echo "<tr><td></td><td>&nbsp; - NAO troca files f1</td></tr>\n";
  400. if ($f2_obj_itemid != $ia_id) { echo "<tr><td></td><td>&nbsp; - tenta: f2: update second {files}.item with {iassign_statement}.id: f2.itemid=" . $f2_obj_itemid . "<-" . $ia_id . " ******* deveria TROCAR!</td></tr>\n"; $troca = 1; }
  401. else echo "<tr><td></td><td>&nbsp; - NAO troca files f1</td></tr>\n";
  402. echo "<tr><td></td><td>&nbsp; * " . $ma . " / " . $mf1 . " / " . $mf2 . "</td></tr>\n\n";
  403. if ($troca == 1) {
  404. if (!$error) echo "<tr><td></td><td>&nbsp; - atualizou {iassign_statement} e {files} com sucesso!</td></tr>\n<tr><td></td><td>Depuracao: USER->id==3</td></tr>\n";
  405. else echo "<tr><td></td><td>&nbsp; - ERRO! NAO atualizou {iassign_statement} e {files}!</td></tr>\nDepuracao: USER->id==3</td></tr>\n";
  406. // exit; //REMOVER exit
  407. }
  408. }
  409. if (!$error) {
  410. if ($USER->id==3) echo "<tr><td></td><td>USER->id==3: &nbsp; * " . $ma . " / " . $mf1 . " / " . $mf2 . "</td></tr></table>\n\n"; //D
  411. return TRUE;
  412. }
  413. if ($USER->id==3) echo "<tr><td></td><td>USER->id==3: &nbsp; x " . $ma . " / " . $mf1 . " / " . $mf2 . " --- ERROR!</td></tr></table>\n\n"; //D
  414. return FALSE;
  415. } // function update_iassignstatement_files($DB, $ia_id, $filesid, $is_obj, $f1_obj, $f2_obj)
  416. // List all "$list_all_files[$jj]" with field "ias_id" == -1 (candidate to be the missing file)
  417. // Prepare HTML to "settings_process_files_form.php" then send to this->update_iassign_statement_files(.) function to update {iassign_statement} and {files}
  418. // The "settings_verify.php" calls "see_all_iass_files(.)" and "list_all_iass_files(.)"
  419. // @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
  420. // @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,
  421. // @param $contextid : is array of context in this {iassign_statement}; $ias_id = {iassign_statement}.id
  422. // @calledby this->list_all_iass_files($DB, $modules_iassignid): [$msgCandidates, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_filesbyID] = search_lost_files_in_context(2,...)
  423. // @calledby this->see_all_iass_files($DB, $modules_iassignid): [$msgCandidates, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_filesbyID] = search_lost_files_in_context(1, ...)
  424. function search_lost_files_in_context ($type, $ii, $courseid, $ias_id, &$list_all_files, $array_contextid, $countErr) {
  425. $sizeOfFiles = count($list_all_files);
  426. $sizeC = count($array_contextid);
  427. $files_index_candidates = array();
  428. $files_filename_candidates = array();
  429. $contextid_candidates = array(); // {context}.id associated to the {files}
  430. $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}
  431. $msg = "ERRO: <select name='selected_filesid[" . $countErr . "]'>\n <option value=''>select one file</option>\n";
  432. $msgHidden = "";
  433. for ($jj=0; $jj<$sizeOfFiles; $jj++) { // list all lost {files}
  434. $sizeC = count($array_contextid); // all {context}.id of this {iassign_statement}
  435. $hascandidate = 0; // has {files} candidate with one of {iassign_statement}.contextid[]?
  436. for ($kk=0; $kk<$sizeC; $kk++) { // list all {context}.id associated to this {iassign_statement}
  437. $contextid = $array_contextid[$kk];
  438. 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])
  439. $hascandidate = 1; // has {files} candidate with one of {iassign_statement}.contextid[$kk]
  440. if ($type == 2) // can change field "ias_id" ($type==1 nedd it continous as -1 ao list {files} with all possible {iassign_statement}
  441. $list_all_files[$jj]->ias_id = $ias_id; // this {files} could be of {iassign_statement}.id = $ias_id
  442. $list_all_files[$jj]->courseid = $courseid; // this {files} could be on the same course as {iassign_statement}.id = $ias_id
  443. break; // stop search to the other {iassign_statement}.contextid[]
  444. }
  445. }
  446. if ($hascandidate == 1) { // there is one {files}.contextid = {iassign_statement}.contextid[$kk]
  447. $files_index_candidates[] = $jj;
  448. $files_filename_candidates[] = $list_all_files[$jj]->filename;
  449. $contextid_candidates[] = $contextid;
  450. $fileid = $list_all_files[$jj]->id;
  451. $list_candidate_files[$fileid] = $jj;
  452. if ($list_all_files[$jj]->filename != ".") { // consider only file (not path)
  453. $msg .= " <option value='" . $fileid . "'>" . $fileid . " - " . $list_all_files[$jj]->filename . " ii=" . $list_candidate_files[$fileid] . "</option>\n";
  454. $msgHidden .= " <input value='" . $list_all_files[$jj]->filename . "' name='list_filesname[" . $ii . "][]' type='hidden' />\n";
  455. $msgHidden .= " <input value='" . $list_all_files[$jj]->id . "' name='list_filesid[" . $ii . "][]' type='hidden' />\n";
  456. $msgHidden .= " <input value='" . $contextid . "' name='list_contextid[" . $ii . "][]' type='hidden' title='#array_contextid=" . count($array_contextid) . "'/>\n";
  457. $msgHidden .= " <input value='" . $ias_id . "' name='list_iassign_statement_id[" . $ii . "][]' type='hidden' />\n"; // {iassign_statement}.id
  458. }
  459. }
  460. } // for ($jj=0; $jj<$sizeOfFiles; $jj++)
  461. $msg .= "</select><table>\n";
  462. $sizeCandidates = sizeof($files_index_candidates);
  463. for ($jj=0; $jj<$sizeCandidates; $jj++) {
  464. if ($files_filename_candidates[$jj] == ".") continue; // ignore filename==".", next $jj
  465. $kk = $files_index_candidates[$jj];
  466. $contextid = $contextid_candidates[$jj]; // $array_contextid[$kk];
  467. $fileid = $list_all_files[$kk]->id;
  468. $msg .= " <tr><td title='coourse.id'>" . $list_all_files[$kk]->courseid . "</td>"; // defined in "search_lost_files_in_context(.)"
  469. $msg .= " <td title='context.id'>" . $contextid . "</td>";
  470. $msg .= " <td title='files.id'>" . $list_all_files[$kk]->id . "</td>";
  471. $msg .= " <td title='files.name'>" .
  472. $list_all_files[$kk]->filename . "</td><td title='files.timecreated=" . date("d/m/Y H:i:s", $list_all_files[$kk]->timecreated) . "'>" .
  473. $list_all_files[$kk]->timecreated . "</td><td title='files.author'>" . $list_all_files[$kk]->author . "</td></tr>\n";
  474. } // for ($jj=0; $jj<$sizeOfFiles; $jj++)
  475. $msg .= "</table>\n";
  476. return [$msg, $msgHidden, $files_index_candidates, $contextid_candidates, $list_candidate_files];
  477. } // function search_lost_files_in_context($ii, $courseid, $ias_id, &$list_all_files, $array_contextid, $countErr)
  478. // Find in array $list_iassign[] $list_iassign[$ii]->ia_id = $iassignid
  479. // Return [ {countext}.id array , {course}.id ]
  480. // @calledby see_all_iass_files($DB, $course_modules_iassign)
  481. function find_course_context ($list_iassign, $iassignid) {
  482. $sizeI = count($list_iassign);
  483. for ($ii=0; $ii<$sizeI; $ii++)
  484. if ($list_iassign[$ii]->ia_id == $iassignid)
  485. return [$list_iassign[$ii]->id, $list_iassign[$ii]->course]; // array of contextid
  486. print "*** ERRROR: coudn't find {iassign}.id = " . $iassignid . "<br/>\n";
  487. return [-1, -1]; // error!
  488. }
  489. /// Search for {files} associated to each "iassign_statement" ("settings_verify.php?action=list")
  490. /// 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}
  491. // @calledby settings_verify.php?action=list : $list_iassign = see_all_iass_files($DB, 9);
  492. // @param $modules_iassignid is iAssign ID in {modules}
  493. function list_all_iass_files ($DB, $modules_iassignid) {
  494. global $SEP;
  495. // Problema: como encontrar {files} com itemid errado?
  496. // 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)
  497. // 2. Vetor com todos os {iassign_statement} : $list_ias : id ; name ; iassignid ; author_name ; iassign_ilmid ; file ; fileid ; timecreated
  498. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  499. // 4. Vetor com todos os {iassign_statement} sem {files} correspondentes:
  500. /// Get {iassign} :: $list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  501. ///
  502. //D print "---<br/>Get {iassign} :: list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  503. //ATTENTION: 'get_records_sql(.)' return only the first element (first field)
  504. //$str_query = "SELECT {context}.id AS contextid, {course_modules}.id AS cmid, {iassign}.id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  505. $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 " .
  506. "{course_modules}.module=" . $modules_iassignid . " AND {iassign}.course={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance={iassign}.id";
  507. $tmp_list_iassign = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  508. //D print " - #tmp_list_iassign=" . count($tmp_list_iassign). "<br/>";
  509. // Process $tmp_list_iassign to produce the array $list_iassign continous (starting from $list_iassign[0]),
  510. // with only one entry to each {iassign}.id and keeping all of its associated {context}.id under the array $list_iassign[$ii]->id
  511. // The {iassign}.id is $list_iassign[$ii]->ia_id
  512. $list_iassign = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  513. $ii = 0;
  514. foreach ($tmp_list_iassign as $element) { // one {iassign} could have several contextid
  515. if ($ii==0 || $list_iassign[$ii-1]->ia_id != $element->ia_id) { // new element
  516. $item = array(); // change to array
  517. $item[] = $element->id; // contextid
  518. $element->id = $item; // change to array - contextid = $item
  519. $list_iassign[$ii] = $element; // transcript array indexed by 'id' to "continous array"
  520. $ii++;
  521. }
  522. else { // just add new contextid (extends array $list_iassign[$ii-1]->id[])
  523. $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  524. }
  525. }
  526. $sizeOf_list_iassign = $ii;
  527. //D print " - #list_iassign=" . $sizeOf_list_iassign . "<br/>";
  528. /// 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 }
  529. ///
  530. //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";
  531. $str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  532. " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated, {iassign}.course AS courseid, {iassign}.name AS ias_name " .
  533. " FROM {iassign}, {iassign_statement} WHERE {iassign_statement}.iassignid = {iassign}.id ORDER BY {iassign}.course, {iassign_statement}.iassignid, {iassign_statement}.id";
  534. $list_ias = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  535. $tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  536. $jj = 0;
  537. foreach ($tmp_list_ias as $element) {
  538. $answer = find_course_context($list_iassign, $element->iassignid); // to receive the array with all {context}.id associated with its {iassign}
  539. $element->contextid = $answer[0]; // -1 => error; otherwise is an array with all associated {context}.id
  540. if ($element->courseid != $answer[1]) echo " x Error: (" . $jj . ", id=" . $element->id . ") courseid=" . $element->courseid . "!=" . $answer[1] . "<br/>\n";
  541. $list_ias[$jj] = $element; // transcript array indexed by 'id' to "continous array"
  542. $jj++;
  543. }
  544. $sizeOf_list_ias = $jj;
  545. //D print " - #list_ias=" . $sizeOf_list_ias . "<br/>";
  546. /// Get {files} :: $list_all_files[] = { id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id }
  547. ///
  548. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  549. //D print "---<br/>Get {files} :: list_all_files[] = { id ; contenthash ; contextid ; component ; filearea ; itemid ; filepath ; filename ; userid ; author ; timecreated ; ias.id }<br/>\n";
  550. // 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")
  551. // Fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id files_id
  552. // - "$list_all_files[$ii]->ias_id" will receive the {iassign_statement}.id with the same {context}.id
  553. // - "$list_all_files[$ii]->missing_ias" will receive FALSE only if its {iassign_statement} are correctly associated (in function "search_files(.)"
  554. // If $list_iassign[$ii] wasn't associated to any {iassign_statement} then "ias_id" := -1 (otherwise "ias_id" := {iassign_statement}.id
  555. $list_all_files = get_all_files($DB, $list_iassign);
  556. //D echo " - #list_files=" . count($list_all_files) . "<br/>\n"; // &nbsp; *
  557. // List al {iassign_statement} with no {files} (from "$list_ias[.]") and then list all {files} with no {iassign_statement} (from "$list_all_files[.]")
  558. $countErr = 0;
  559. $countIA = 0;
  560. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  561. $element = $list_ias[$jj]; // array of { id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; contextid (array of context.id) }
  562. $answerSF = search_files($list_all_files, $element); // return [$ii, $jj] = {files}[$ii] is the associated file using context {iassign_statement}.contextid[$jj]
  563. if ($answerSF == -1) { // don't find!
  564. $element->missingfile = TRUE; // couldn't find {files} associated!
  565. // List all unmatched {files} under the same context
  566. // It will return tag SELECT 'filesid[]' with sizeof($files_index_candidates) options
  567. [$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);
  568. if (count($files_index_candidates)==0) $msgCandidates = "";
  569. else $msgCandidates = "<br/>" . $msgCandidates;
  570. // $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  571. //R $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  572. //R print $msgHidden;
  573. $countErr++;
  574. }
  575. else {
  576. $countIA++;
  577. $element->missingfile = FALSE;
  578. }
  579. }
  580. print "<style> .error { background-color: #f4d6d2; color: #ca3120; }</style>\n";
  581. $msgErr = " class='error' "; // "color='#aa0000'";
  582. $msgTErr = ""; // " title='Not found' ";
  583. print "List of all {iassign_statement} with no {files} associated<br/>\n";
  584. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  585. 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>" .
  586. " <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>" .
  587. " <td title='{iassign}.name'>ia.name</td> <td title='{iassign_statement}.name'>ias.name</td></tr>\n";
  588. $countMIAS = 0;
  589. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  590. $element = $list_ias[$jj];
  591. if ($element->missingfile) {
  592. $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  593. print "<tr " . $msgTErr . "><td>" . $countMIAS . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  594. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  595. " <td title='not found file possible array of context.id'>" . $msgC . "</td><td title='iassign_statement.file'>" . $element->file . "</td>\n" .
  596. " <td title='iassign_statement.filesid'>" . $element->filesid . "</td><td>" .
  597. date("d/m/Y H:i:s", $element->timecreated) . "</td><td>" . $element->author_name . "</td>" .
  598. " <td title='{iassign}.name'>" . $element->ias_name . "</td> <td title='{iassign_statement}.name'>" . $element->name . "</td></tr>\n";
  599. $countMIAS++; // next {iassign_statement} with no associated {files}
  600. }
  601. }
  602. // SELECT * FROM s_files WHERE id=314077 OR id=313730 OR id=301156
  603. // {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])
  604. // 301156 52001 5658 vetor_soma_x_y.ivph | 5658 8.1 Vetor - Soma das posicoes x e y
  605. // 313730 54821 5658 vetor_soma_x_y.ivph <- course.id = 487
  606. // 314077 54391 0 soma_2_inteiros.ivph
  607. // - Encontrou 5658 via 'id' (id==itemid): contextid[1]=52001: 5658==5658 no files[12521] - {files}.id=301156
  608. print "</table><br/><br/>----------------------------<br/><br/>\n";
  609. print "List of all {files} with no {iassign_statement} associated<br/>\n";
  610. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  611. 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>" .
  612. " <td title='files.id'>id</td> <td title='{files}.itemid'>itemid</td> <td title='date(timecreated)'>date(timecreated)</td>" .
  613. " <td title='author'>author</td> <td title='{file}.filename'>filename</td></tr>\n";
  614. $countErrF = 0;
  615. $sizeOf_files = count($list_all_files);
  616. for ($jj=0; $jj<$sizeOf_files; $jj++) {
  617. $element = $list_all_files[$jj];
  618. if ($element->missing_ias) { // "$element->missing_ias = FALSE" in funcion "search_files(.)"
  619. print "<tr " . $msgTErr . "><td>" . $countErrF . "</td><td title='course.id'>" . $element->courseid . "</td> <td title='iassign.id=iassign_statement.iassignid'>" . $element->ias_id . "</td>" .
  620. " <td title='context.id''>" . $element->contextid . "</td> <td title='files.id'>" . $element->id . "</td> " .
  621. " <td title='{files}.itemid'>" . $element->itemid . "</td> <td title='date(timecreated)'>" . date("d/m/Y H:i:s", $element->timecreated) . "</td>" .
  622. " <td title='author'>" . $element->author . "</td> <td title='{file}.filename'>" . $element->filename . "</td></tr>\n";
  623. $countErrF++;
  624. }
  625. }
  626. print "</table>\n";
  627. print "Total of {iassign_statement}: " . $countIA . "<br/>\n";
  628. print "Total of {iassign_statement} without any {files}: " . $countErr . "<br/>\n";
  629. print "Total of {files} without any {iassign_statement}: " . $countErrF . "<br/>\n";
  630. return $list_iassign;
  631. } // function list_all_iass_files($DB, $modules_iassignid)
  632. /// Search for files associated to each "iassign_statement"
  633. /// Must be used to produce a form with all {iassign_statement} with no {files} and their candidate {files} (with same {context}.id
  634. // @calledby settings_verify.php: $list_iassign = see_all_iass_files($DB, 9);
  635. // @param $modules_iassignid is iAssign ID in {modules}
  636. function see_all_iass_files ($DB, $modules_iassignid) {
  637. global $SEP;
  638. // Problema: como encontrar {files} com itemid errado?
  639. // 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)
  640. // 2. Vetor com todos os {iassign_statement} : $list_ias : id ; name ; iassignid ; author_name ; iassign_ilmid ; file ; fileid ; timecreated
  641. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  642. // 4. Vetor com todos os {iassign_statement} sem {files} correspondentes:
  643. // $list_iassign = $DB->get_records('iassign');
  644. // echo "#list_iassign=" . count($list_iassign) . "<br/>"; // 880
  645. // $ii = 0; foreach ($list_iassign as $element) { echo $ii . $SEP . $element->id . $SEP . $element->course . $SEP . $element->name . "<br/>"; $ii++; }
  646. // 880
  647. /// Get {iassign} :: $list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }
  648. ///
  649. print "---<br/>Get {iassign} :: list_iassign[] = { id (context.id) ; cmid ; ia_id (iassign.id) ; course (course.id) ; name ({iassign}.name) }<br/>\n";
  650. //ATTENTION: 'get_records_sql(.)' return only the first element (first field)
  651. //$str_query = "SELECT {context}.id AS contextid, {course_modules}.id AS cmid, {iassign}.id, {iassign}.course, {iassign}.name FROM {iassign}, {context}, {course_modules} WHERE " .
  652. $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 " .
  653. "{course_modules}.module=" . $modules_iassignid . " AND {iassign}.course={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance={iassign}.id";
  654. //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
  655. // s_iassign.course=s_course_modules.course AND s_course_modules.id=s_context.instanceid AND s_course_modules.instance=s_iassign.id
  656. $tmp_list_iassign = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  657. print " - #tmp_list_iassign=" . count($tmp_list_iassign). "<br/>";
  658. //D echo "#" . $SEP . "id (context.id)" . $SEP . "ia_id (iassign.id)" . $SEP . "course" . $SEP . "cmid" . $SEP . "name" . $SEP . "contextid" . "<br/>"; $ii++;
  659. // Process $tmp_list_iassign to produce the array $list_iassign continous (starting from $list_iassign[0]),
  660. // with only one entry to each {iassign}.id and keeping all of its associated {context}.id under the array $list_iassign[$ii]->id
  661. // The {iassign}.id is $list_iassign[$ii]->ia_id
  662. $list_iassign = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  663. $ii = 0;
  664. foreach ($tmp_list_iassign as $element) { // one {iassign} could have several contextid
  665. if ($ii==0 || $list_iassign[$ii-1]->ia_id != $element->ia_id) { // new element
  666. //D echo $ii . $SEP . $element->id . $SEP . $element->ia_id . $SEP . $element->course . $SEP . $element->cmid . $SEP . "'" . $element->name . "'" . $SEP . "<br/>"; // id=contextid
  667. $item = array(); // change to array
  668. $item[] = $element->id; // contextid
  669. $element->id = $item; // change to array - contextid = $item
  670. $list_iassign[$ii] = $element; // transcript array indexed by 'id' to "continous array"
  671. $ii++;
  672. }
  673. else { // just add new contextid (extends array $list_iassign[$ii-1]->id[])
  674. //_ $item = $list_iassign[$ii-1]->id; // is array -- contextid
  675. //_ $item[] = $element->id; // contextid
  676. //_ $list_iassign[$ii-1]->id = $item; // contextid
  677. $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  678. //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
  679. }
  680. }
  681. $sizeOf_list_iassign = $ii;
  682. print " - #list_iassign=" . $sizeOf_list_iassign . "<br/>";
  683. //D for ($jj=0; $jj<$sizeOf_list_iassign; $jj++) {
  684. //D $element = $list_iassign[$jj];
  685. //D echo $jj . $SEP . $element->ia_id . $SEP . $element->iassign_ilmid . $SEP . $element->iassignid . $SEP . $element->course . $SEP . $element->file . $SEP . $element->fileid .
  686. //D $SEP . $element->timecreated . $SEP . "'" . $element->author_name . "'" . $SEP . "'" . $element->name . "': contextid=[";
  687. //D $sizeC = count($element->id); // ($element->contextid);
  688. //D for ($kk=0; $kk<$sizeC; $kk++) echo $element->id[$kk] . ","; // contextid[$kk]
  689. //D if ($sizeC>1) echo "] ****************************************<br/>";
  690. //D else echo "]<br/>";
  691. //D }
  692. //L print "====================================================================================<br/>";
  693. /// 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 }
  694. ///
  695. //ATTENTION: 'get_records_sql(.)' return only the first element (first field) => use unique first identifier {iassign_statement}.id
  696. 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";
  697. //$str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  698. // " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated " .
  699. // " FROM {iassign_statement} ORDER BY {iassign_statement}.iassignid, {iassign_statement}.id";
  700. $str_query = "SELECT {iassign_statement}.id, {iassign_statement}.name, {iassign_statement}.iassignid, {iassign_statement}.author_name, {iassign_statement}.iassign_ilmid, " .
  701. " {iassign_statement}.file, {iassign_statement}.filesid, {iassign_statement}.timecreated, " .
  702. " {iassign}.course AS courseid FROM {iassign}, {iassign_statement} WHERE {iassign_statement}.iassignid = {iassign}.id ORDER BY {iassign}.course, {iassign_statement}.iassignid, {iassign_statement}.id";
  703. //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,
  704. // s_iassign_statement.filesid, s_iassign_statement.timecreated, s_iassign.course AS courseid FROM s_iassign, s_iassign_statement
  705. //WHERE s_iassign_statement.iassignid = s_iassign.id ORDER BY s_iassign.course, s_iassign_statement.iassignid, s_iassign_statement.id
  706. //SELECT s_iassign_statement.id, s_iassign_statement.name, s_iassign_statement.iassignid, s_iassign_statement.author_name, s_iassign_statement.iassign_ilmid,
  707. // s_iassign_statement.file, s_iassign_statement.filesid, s_iassign_statement.timecreated FROM s_iassign_statement
  708. $list_ias = array(); // use continous array ($tmp_list_ias is indexed by 'id')
  709. $tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  710. $jj = 0;
  711. foreach ($tmp_list_ias as $element) {
  712. $answer = find_course_context($list_iassign, $element->iassignid); // to receive the array with all {context}.id associated with its {iassign}
  713. $element->contextid = $answer[0]; // -1 => error; otherwise is an array with all associated {context}.id
  714. //$element->courseid = $answer[1]; // -1 => error; otherwise {iassicn}.course = {course}.id
  715. if ($element->courseid != $answer[1]) echo " x Error: (" . $jj . ", id=" . $element->id . ") courseid=" . $element->courseid . "!=" . $answer[1] . "<br/>\n";
  716. $list_ias[$jj] = $element; // transcript array indexed by 'id' to "continous array"
  717. $jj++;
  718. }
  719. $sizeOf_list_ias = $jj;
  720. //L for ($jj=0; $jj<$sizeOf_list_ias; $jj++) {
  721. //L $element = $list_ias[$jj];
  722. //L //D echo $jj . $SEP . $element->id . $SEP . $element->iassign_ilmid . $SEP . $element->iassignid . $SEP . $element->file . $SEP . $element->fileid .
  723. //L //D $SEP . $element->timecreated . $SEP . "'" . $element->author_name . "'" . $SEP . "'" . $element->name . "'" . $SEP . "[";
  724. //L //D $sizeC = count($element->contextid); // array of {context}.id // $element->contextid . $SEP
  725. //L //D for ($kk=0; $kk<$sizeC; $kk++) echo $element->contextid[$kk] . $SEP;
  726. //L //D echo "]<br/>";
  727. //L }
  728. print " - #list_ias=" . $sizeOf_list_ias . "<br/>";
  729. //L print "====================================================================================<br/>";
  730. /// Get {files} :: $list_all_files[] = { id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id }
  731. ///
  732. // 3. Vetor com todos os {files} com {iassign}.contextid : $list_files : id ; contenthash ; contextid component filearea itemid filepath filename userid author timecreated ias.id
  733. print "---<br/>Get {files} :: list_all_files[] = { id ; contenthash ; contextid ; component ; filearea ; itemid ; filepath ; filename ; userid ; author ; timecreated ; ias.id }<br/>\n";
  734. //$list_files = array(); // $list_files[$ii] = { ia_id, ia_file, ia_filesid, filesid=files.id, filename=files.filename, itemid=files.itemid, contextid=files.contextid }
  735. // Get all {files} associated with {iassign} (here the additional field "ias_id" is "-1")
  736. // Fields: id contenthash contextid component filearea itemid filepath filename userid author timecreated ias_id
  737. // If $list_iassign[$ii] wasn't associated to any {iassign_statement} then "ias_id" := -1 (otherwise "ias_id" := {iassign_statement}.id
  738. $list_all_files = get_all_files($DB, $list_iassign);
  739. echo " - #list_files=" . count($list_all_files) . "<br/>\n"; // &nbsp; *
  740. print "<style> .error { background-color: #f4d6d2; color: #ca3120; }</style>\n";
  741. $msgErr = " class='error' "; // "color='#aa0000'";
  742. $msgTErr = ""; // " title='Not found' ";
  743. print "<form action='settings_process_files_form.php' method='post' class='form-example'>\n"; // form to be processed by 'setting_process_files_form.php'
  744. print "<button type='submit' class='button action has-icon search-button'>Enviar</button>\n"; //TODO: internacionalizar
  745. print "<input type='hidden' name='ilm_id' value='" . $modules_iassignid . "'>\n";
  746. print "<table id='outlinetable' class='generaltable boxaligncenter' width='100%'>\n";
  747. 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>" .
  748. "<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>" .
  749. "<td title='{file}.filename'>{files}.filename</td><td title='{file}.id'>id</td><td title='{files}.itemid'>itemid</td></tr>\n";
  750. // <td title='ID:={course_modules}.id to access mod/iassign/view.php?id=ID'>{course_modules}.id</td><td># files</td>";
  751. // Arrays to be used in "select" to recover missing files: $err_iassignid[], $err_iassignstatementid[], $err_selectedfilesid[]
  752. $err_iassignid = array(); // array with the $jj {iassign}.id with no {iassign_statement} withount any {files}
  753. $err_iassignstatementid = array(); // array with the $jj {iassign_statement}.id with no {files}
  754. $err_selectedfilesid = array(); // array with the $jj {files}.id to be associated with the $jj {iassign_statement}
  755. $countErr = 0;
  756. $countIA = 0;
  757. for ($jj=0; $jj<$sizeOf_list_ias; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  758. //D for ($jj=0; $jj<10; $jj++) { // try to find {files} (in $list_all_files) associated to each $list_ias[$jj]
  759. $element = $list_ias[$jj]; // array of { id (iassign_statement.id) ; name ; iassignid ; author_name ; iassign_ilmid ; file ; filesid ; timecreated ; contextid (array of context.id) }
  760. $answerSF = search_files($list_all_files, $element); // return [$ii, $jj] = {files}[$ii] is the associated file using context {iassign_statement}.contextid[$jj]
  761. if ($answerSF == -1) { // don't find!
  762. // List all unmatched {files} under the same context
  763. // It will return tag SELECT 'filesid[]' with sizeof($files_index_candidates) options
  764. [$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);
  765. //if ($jj==29) $msgCandidates = "<br/>#files_index_candidates=" . count($files_index_candidates) . " " . $msgCandidates; // " contextid=" . $element->contextid .
  766. //else
  767. if (count($files_index_candidates)==0) $msgCandidates = "";
  768. else $msgCandidates = "<br/>" . $msgCandidates;
  769. // $list_iassign[$ii-1]->id[] = $element->id; // new entry to contextid array -- context
  770. $msgC = "["; $sizeC = count($element->contextid); for ($kk=0; $kk<$sizeC; $kk++) $msgC .= $element->contextid[$kk] . $SEP; $msgC .= "]";
  771. print $msgHidden;
  772. print "<tr " . $msgTErr . "><td>" . $countErr . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  773. //RRR print "<tr " . $msgTErr . "><td>" . $jj . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>" .
  774. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  775. " <td title='not found file possible array of context.id'>" . $msgC . "</td><td title='iassign_statement.file'>" . $element->file . "</td>\n" .
  776. " <td title='iassign_statement.filesid'>" . $element->filesid . "</td><td>" .
  777. date("d/m/Y H:i:s", $element->timecreated) . "</td><td>" .
  778. $element->author_name . "</td> <td>" . $element->name . "</td><td colspan='3' " . $msgErr . ">\n" . $msgCandidates . "</td></tr>\n"; // ERRO
  779. $err_iassignid[] = $element->iassignid; // array with the $jj {iassign}.id with no {iassign_statement} withount any {files}
  780. $err_iassignstatementid[] = $element->id; // array with the $jj {iassign_statement}.id with no {files}
  781. $err_selectedfilesid[] = countErr; // array with the $jj {files}.id to be associated with the $jj {iassign_statement}
  782. $countErr++;
  783. }
  784. else {
  785. /*
  786. [$indexF, $indexC] = $answerSF; // {files}[$indexF] is the associated file using context {iassign_statement}.contextid[$indexC]
  787. print "<tr><td>" . $jj . "</td><td title='course.id'>" . $element->courseid . "</td><td title='iassign.id=iassign_statement.iassignid'>" . $element->iassignid . "</td>\n" .
  788. " <td title='iassign_statement.id'>" . $element->id . "</td><td title='iassign_statement.iassign_ilmid'>" . $element->iassign_ilmid . "</td>\n" .
  789. " <td title='[files.contextid]'>" . $element->contextid[$indexC] . "</td><td title='iassign_statement.file'>" . $element->file . "</td><td title='iassign_statement.filesid'>" . $element->filesid . "</td>" .
  790. " <td>" . date("d/m/Y H:i:s", $element->timecreated) .
  791. "</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. "'>" .
  792. $list_all_files[$indexF]->filename . "</td><td title='Found in list_all_files[" . $indexF . "]->id={files}.id'>" . $list_all_files[$indexF]->id . "</td>" .
  793. "<td title='{files}.itemid'>" .
  794. $list_all_files[$indexF]->itemid . "</td></tr>\n";
  795. //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
  796. //TODO Usar $list_ias[$jj]->ia_id = {iassign_statement}.id para redefinir campo {files}.itemid : UPDATE
  797. //$list_all_files[$indexF]->id = {files}.id
  798. *///RRR
  799. $countIA++;
  800. }
  801. }
  802. print "</table>\n";
  803. print "</form>\n";
  804. print "Total of {iassign_statement}: " . $countIA . "<br>";
  805. print "Total of {iassign_statement} without any {files}: " . $countErr . "<br>";
  806. return $list_iassign;
  807. } // function see_all_iass_files($DB, $modules_iassignid)
  808. //====================================================================================================================================
  809. // Create entry with { ia_id, ia_file, ia_filesid, ia_name, filesid, filename, itemid } for each iAssign statement and files
  810. function createNewEntry ($iassign_statement, $element_file) {
  811. $new_file = new stdClass(); // { ia_id, ia_file, ia_filesid, ia_name, filesid, filename, itemid } for each iAssign statement and files
  812. $new_file->ia_id = $iassign_statement->id;
  813. $new_file->ia_name = $iassign_statement->name;
  814. $new_file->ia_file = $iassign_statement->file;
  815. $new_file->ia_filesid = $iassign_statement->filesid;
  816. if ($element_file != NULL) {
  817. $new_file->filesid = $element_file->id;
  818. $new_file->filename = $element_file->filename;
  819. $new_file->itemid = $element_file->itemid;
  820. $new_file->contextid = $element_file->contextid;
  821. }
  822. else {
  823. $new_file->filesid = NULL;
  824. $new_file->filename = NULL;
  825. $new_file->itemid = NULL;
  826. $new_file->contextid = NULL;
  827. }
  828. return $new_file;
  829. } // function createNewEntry($iassign_statement, $element_file)
  830. // Try several method to recover file (MoodleData) associated to the iAssign activity
  831. // Given array $all_context, defined a clean version $all_context_final
  832. // @param &$all_context_final : passed by reference
  833. // @calledby this->get_files($course_id, $DB)
  834. function get_associated_files ($DB, $iassign_statement, $all_context, &$all_context_final, $ii) {
  835. global $USER; //DEBUG: apenas durante depuracao!
  836. $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
  837. $sizeAC = count($array_context_element);
  838. if (!is_array($array_context_element)) { // just in case '$answer_list = $DB->get_records_sql($str_query);' returned only one element
  839. $array_context_elements = array();
  840. $array_context_elements[] = $all_context[$ii];
  841. }
  842. else
  843. $array_context_elements = $all_context[$ii];
  844. $sizeAC = count($array_context_elements);
  845. // 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/>"; }
  846. //D__ echo "----<br/>#array_context_elements=" . $sizeAC . " (get_associated_files(.))<br/>\n";
  847. $countF = 0;
  848. $jj = 0;
  849. while ($jj<$sizeAC) {
  850. $context_element = $array_context_elements[$jj];
  851. $contextid = $context_element->contextid; // context of this 'iassign.id'
  852. //$msgEC .= " &nbsp; * " . $context_element->contextid . ", " . $context_element->id . ", '" . $context_element->shortname . "', " . $context_element->course_module_id . ", " . $context_element->instanceid . "<br/>"; }
  853. //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/>";
  854. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  855. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  856. // To avoid programming mistake on old versions, try to get 'files' with 'iassign_statement.id', 'iassign_statement.file' and 'iassign_statement.filesid'
  857. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  858. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  859. if ($numf == 0) {
  860. if (isset($iassign_statement->file))
  861. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file)); // all 'files' associated
  862. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.file"; //D
  863. if ($numf == 0) {
  864. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.itemid'
  865. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->filesid));
  866. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid"; //D
  867. if ($numf == 0) {
  868. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.id'
  869. $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  870. $numf = count($files); $countF += $numf; $msgO = "files.id=iassign_statement.filesid"; //D
  871. // if ($numf == 0) {
  872. // // try 'files.itemid = iassign_statement.id'
  873. // $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  874. // $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid=files.itemid"; //D
  875. // }
  876. if ($numf == 0) {
  877. //D__ echo " &nbsp; * files: " . $numf . " - ERRO 'id' 'file' 'filesid' to statement id=" . $iassign_statement->id . ": " . $iassign_statement->name . "<br/>\n"; //D
  878. //D__ if ($iassign_statement->id==5118) {
  879. //D__ $context = context_module::instance($USER->cm); // usado no: ilm_handlers/html5.php
  880. //D__ //D__ echo " &nbsp; * {course_module}.id=" . $USER->cm .", USER.{context}.id=" . $context->id . "<br/>\n";
  881. //D__ $fs = get_file_storage();
  882. //D__ $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement->id); $fileid = -1; $filename = "";
  883. //D__ if ($files) { //TODO To be revised? 'files.filename' has '.' is only path (not the file)
  884. //D__ foreach ($files as $value) { if ($value->get_filename() != '.') { $fileid = $value->get_id(); $filename = $value->get_filename(); break; } }
  885. //D__ }
  886. //D__ //D__ echo " &nbsp; * codigo de 'ilm_handlers/html5.php': file.id=" . $fileid . ", filename=" . $filename . "<br/>";
  887. //D__ }
  888. }
  889. }
  890. }
  891. } // if ($numf == 0)
  892. if ($numf>0) { $msgF = ""; foreach ($files as $element_file) $msgF .= "id=" . $element_file->id . ", filename='" . $element_file->filename . "'; ";
  893. //D__ echo " &nbsp; * files: " . $numf . " - encontrei {files}: " . $msgF . "<br/>\n"; //D echo " &nbsp; * files: "; print_r($files); echo "<br/>\n"; //
  894. $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
  895. return [ $files, $contextid, $numf, $msg0 ];
  896. }
  897. $jj++;
  898. }
  899. $all_context_final[$ii] = $context_element; // now only one array
  900. return [ null, -1, 0, $msg0 ];
  901. } // function get_associated_files($DB, $iassign_statement, $all_context, &$all_context_final, $ii)
  902. // @calledby locallib.php : add_edit_iassign(.)
  903. // @calledby settings_activities.php: [$list_all_instances_of_iassign, $all_context, $list_of_ia_files...] = get_files($courseid, $ilm_id, $DB);
  904. function get_files ($course_id, $ilm_id, $DB) { //2022
  905. //__ echo "get_files(.): course_id=" . $course_id . "<br/>";
  906. // Get all iAssignment block inside this course
  907. if (isset($ilm_id) && $ilm_id) {
  908. $str_query = "SELECT DISTINCT {iassign}.* FROM {iassign}, {iassign_statement} WHERE " .
  909. " {iassign}.id={iassign_statement}.iassignid AND {iassign_statement}.iassign_ilmid=" . $ilm_id;
  910. //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=?
  911. $aux_answer_list_iassign = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  912. //D echo "#aux_answer_list_iassign=" . count($aux_answer_list_iassign) . "<br/>";
  913. $answer_list_iassign = array(); // transform the associative array in one that is continous
  914. foreach ($aux_answer_list_iassign as $item) {
  915. $answer_list_iassign[] = $item;
  916. }
  917. //D echo "#answer_list_iassign=" . count($answer_list_iassign) . "<br/>";
  918. //D $sizeIA = count($answer_list_iassign);
  919. //D for ($ii=0; $ii<$sizeIA; $ii++) echo $ii . " : " . $answer_list_iassign[$ii]->id . ", " . $answer_list_iassign[$ii]->name . ", " . $answer_list_iassign[$ii]->author_name . "<br/>\n";
  920. //D exit;
  921. }
  922. else
  923. if (isset($course_id) && $course_id) // not yet used...
  924. $answer_list_iassign = $DB->get_records('iassign', array('course' => $course_id)); //R $this->get_courseid() -> $course_id
  925. else {
  926. print "Attention, parameters {course}.id and {modules}.id of iAssign not defined!<br/>";
  927. exit(1);
  928. // $answer_list_iassign = $DB->get_records('iassign'); // all courses
  929. }
  930. //$tmp_list_ias = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  931. //D echo "#answer_list_iassign = " . sizeof($answer_list_iassign) . "<br/>";
  932. //$list_iassign = $DB->get_records('iassign');
  933. //echo "#list_iassign=" . count($list_iassign) . "<br/>"; $SEP = ", "; // 880
  934. //$ii = 0; foreach ($list_iassign as $element) { echo $ii . $SEP . $element->id . $SEP . $element->course . $SEP . $element->name . "<br/>"; $ii++; }
  935. // Build 3 arrays (list_all_instances_of_iassign[.], all_context[.], list_of_files[.])
  936. // - $list_all_instances_of_iassign[$ii] <-> $all_context[$ii] <-> $list_of_files[$ii]
  937. // Build all context per course
  938. $list_of_contextid_by_courses = array(); // list of courses by ID ({course}.id): $list_of_contextid_by_courses[$i] = array(with all of its context)
  939. $list_of_courses = array(); // list of {course}.id: $list_of_courses[$i] = array(with {course}.id, {course}.fullname, {course}.shortname)
  940. $list_of_coursesid = array(); // auxiliary to easy to generate $list_of_courses[]
  941. // Get all context of each iAssignment block inside this course: $list_all_instances_of_iassign[.] ; $all_context[.]
  942. $list_all_instances_of_iassign = array(); // will return { id, contextid, shortname} = { iassign.id, context.id, course.shortname } <-> $list_of_files[.]
  943. $all_context_final = array(); // will be defined by $all_context considering eventually more then one context to recover {files}
  944. $all_context = array();
  945. $ii = 0;
  946. foreach ($answer_list_iassign as $iassign) { // get all context of each iAssign block: this iAssign block $iassign
  947. //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(.)'!
  948. //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.
  949. //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.
  950. //DEBUG: Then is necessary to use as the first field one that has unique representation considering all rows,
  951. //DEBUG: so we must use '{context}.id' since several of them could be associated to one {course_modules}
  952. $str_query = "SELECT {context}.id AS contextid, {course}.id, {course}.fullname, {course}.shortname, {course_modules}.id AS course_module_id, {context}.instanceid " .
  953. " FROM {course}, {context}, {course_modules} WHERE " .
  954. " {course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.instance=" . $iassign->id . " AND {course}.id=" . $iassign->course .
  955. " ORDER BY {course}.id, {course_modules}.instance"; // Redundance: {course_modules}.instance=" . $iassign->id . " AND {course}.id=" . $iassign->course
  956. // " {course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.course=" . $course_id . " AND {course_modules}.instance=" . $iassign->id;
  957. //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=?
  958. $answer_list = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  959. //D if ($iassign->id==1299) { // ($iassign->id==1) { //
  960. //D echo "query=" . $str_query . "<br/>#answer_list=" . count($answer_list) . ": <br/>"; } // print_r($answer_list); echo "<br/>"; }
  961. $list1 = array();
  962. $strAux = "";
  963. foreach ($answer_list as $item) { // $answer_list[] from {context}, {course} and {course_modules}
  964. $list1[] = $item; // transform the associative array in one that is continous
  965. $courseid = $item->id;
  966. if (!in_array($courseid, $list_of_coursesid)) { // array of contextid not initiated for this course
  967. $list_of_courses[] = array($item->id, $item->shortname, $item->fullname); // list of courses array({course}.id, {course}.shortname, {course}.fullname)
  968. $list_of_coursesid[] = $item->id; // auxiliary to easy to generate $list_of_courses[]
  969. $iassign->courseid = $item->id;
  970. $iassign->fullname = $item->fullname;
  971. //DD echo "(".$item->id.",".$item->shortname .") ";
  972. }
  973. if (!isset($list_of_contextid_by_courses[$courseid])) { // array of contextid not initiated for this course
  974. $list_of_contextid_by_courses[$courseid] = array($item->contextid); // list of courses by ID ({course}.id)
  975. }
  976. else // already initiated this course
  977. array_push($list_of_contextid_by_courses[$courseid], $item->contextid); // append a new contextid
  978. //_ $strAux = "[" . $item->id . "," . $item->contextid . "," . $item->course_module_id . "," . $item->shortname . "," . $item->instanceid . "] ";
  979. //_ if ($iassign->id==1299) { // ($iassign->id==1) { // print_r($item);
  980. //_ echo " - iassign.id=" . $list_all_instances_of_iassign[$ii]->id . ": [id,context.id,course_module.id,shortname,instanceid]=" . $strAux . "<br/>\n";
  981. //_ }
  982. } // foreach ($answer_list as $item)
  983. //DD echo "<br/>";
  984. $list_all_instances_of_iassign[] = $iassign;
  985. $all_context[$ii] = $list1; // $list1[0]; // { course.id, course.fullname, course.shortname, context.id }
  986. //__ 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";
  987. $ii++;
  988. } // foreach $answer_list_iassign
  989. //D echo "Total de atividades: ii = " . $ii . "<br/>";
  990. // Built the list of all iAssign associated with this block of activities ('iassign_statement' and 'files')
  991. // This list is associated with the list of iAssign instances: $list_of_files[$ii] <=> $all_context[$ii]
  992. // $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}
  993. $list_of_ia_files = array(); // as { iassign_statement->id, iassign_statement->name, iassign_statement->filesid, files->id, files->contextid, files->itemid, files->filename}
  994. $sizeofList = $ii; // size of $all_context[]
  995. for ($ii=0; $ii<$sizeofList; $ii++) { // get all iAssign block of activities (course.moudules)
  996. $iassign = $list_all_instances_of_iassign[$ii];
  997. // Get all activities inside this block, only to the iLM selected $ilm_id == {modules}.id
  998. $list_all_iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id, 'iassign_ilmid' => $ilm_id));
  999. $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 }
  1000. //D__ echo "#list_all_iassign_statement = " . count($list_all_iassign_statement) . " de iassign.id=" . $iassign->id . "<br/>\n"; //D2022
  1001. foreach ($list_all_iassign_statement as $iassign_statement) { // get all iAssign activity inside the block '$list_all_iassign_statement'
  1002. // $contextid = $all_context[$ii]->contextid; // context of this 'iassign.id'
  1003. // Build $all_context_final[$ii] as $all_context[$ii][0] or $all_context[$ii][1] - If $contextid==-1 then erro! Could not recover file!
  1004. [$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";
  1005. //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
  1006. if ($numf > 0) { // if $iassign_statement has any file (in {files})
  1007. //__ echo " &nbsp; * files: " . $numf . ": " . $msgO . "<br/>\n"; //D
  1008. $filename = array();
  1009. foreach ($files as $element_file) { // analyse each '$element_file' of 'files'
  1010. //D__ echo " &nbsp; * ii=" . $ii . ": files: id = " . $element_file->id . ", contextid = " . $element_file->contextid . ", itemid=" . $element_file->itemid . " : " . $element_file->filename . "<br/>\n"; //D
  1011. $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
  1012. if ($element_file->filename != '.') { // get the '$element_file' that is not only "path"
  1013. $filename = explode(".", $element_file->filename);
  1014. if ($iassign_statement->filesid != $element_file->id) { // need to update 'iassign_statement.filesid'?
  1015. // echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.filesid'=" . $iassign_statement->filesid . " <- " . $element_file->id . " : " . $element_file->filename . "<br/>"; //2022
  1016. //++ Deixar para outro corrigir! A partir de 'settings_activities.php'
  1017. $iassign_statement->filesid = $element_file->id; // 'iassign_statement.filesid' <- 'files.id' (update bellow) //2022 deixar para outro corrigir!
  1018. }
  1019. }
  1020. if ($element_file->itemid != $iassign_statement->id) { // need to update 'files.itemid'? ('files.itemid <- iassign_statement.id') //2022
  1021. $newentry_be_updated = new stdClass();
  1022. $newentry_be_updated->id = $element_file->id; $newentry_be_updated->itemid = $iassign_statement->id;
  1023. // 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
  1024. //R NAO alterar!
  1025. //R if (!$DB->update_record('files', $newentry_be_updated)) print_error('error_update', 'iassign');
  1026. }
  1027. if (isset($new_file->ia_id) && $new_file->ia_id>0) { // security...
  1028. $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 }
  1029. } // echo " &nbsp; * # = " .
  1030. } // foreach ($files as $element_file)
  1031. $list_of_ia_files[$ii] = $list_of_files;
  1032. $extension = ""; // extension of the current '$element_file'
  1033. if (count($filename) > 1) $extension = strtolower($filename[count($filename) - 1]);
  1034. foreach ($list_all_iassign_ilm as $iassign_ilm) { // find iLM with the same extension (to register its 'iassign_ilm.id')
  1035. $extensions_of_ilm = explode(",", $iassign_ilm->extension);
  1036. if (in_array($extension, $extensions_of_ilm)) { // it was found the iLM with correct extension - get the lastast version
  1037. $iassign_statement->iassign_ilmid = $iassign_ilm->id; // the iLM identification: iassign_ilm.id
  1038. // echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.iassign_ilmid': " . $iassign_statement->iassign_ilmid . " <- " . $iassign_ilm->id . "<br/>\n";
  1039. }
  1040. }
  1041. //D__ echo " - Troca iassign_statement.id=" . $iassign_statement->id . " (" . $iassign_statement->name . ") troca filesid para " . $iassign_statement->filesid . "<br/>";
  1042. //R $DB->update_record("iassign_statement", $iassign_statement); // update 'iassign_statement' (mainly 'iassign_statement.filesid')
  1043. } // if ($numf > 0)
  1044. else { // Error!!! Couldn't find any {files} associated to this statement!
  1045. // echo " &nbsp; * ii=" . $ii . ": files: vazio!<br/>\n";
  1046. $list_of_files[] = createNewEntry($iassign_statement, NULL); // create entry with { ia_id, ia_file, ia_filesid, ia_name, filesid=NULL, filename=NULL, itemid=NULL }
  1047. $list_of_ia_files[$ii] = $list_of_files;
  1048. }
  1049. } // foreach ($list_all_iassign_statement as $iassign_statement)
  1050. } // for ($ii=0; $ii<$sizeofList; $ii++)
  1051. return [$list_all_instances_of_iassign, $all_context_final, $list_of_ia_files, $list_of_contextid_by_courses, $list_of_courses];
  1052. } // function get_files($course_id, $DB)
  1053. // Codigo de teste tinha inserido em 'locallib.php' (era invocado linha 2458/9172): static function test_iassign_statement_files()
  1054. // @calledby locallib.php : add_edit_iassign(.)
  1055. function see_all_files_context ($course_id, $DB) { //2022 testar 'restore_iassign_stepslib.php()' - remover!
  1056. // See mod/iassign/backup/moodle2/restore_iassign_stepslib.php : after_execute()
  1057. // /var/www/html/moo391p/mod/iassign/backup/moodle2/restore_iassign_stepslib.php
  1058. //global $CFG, $DB, $USER;
  1059. //$fs = get_file_storage();
  1060. $context_id = 1;
  1061. // $context = context_module::instance($USER->cm); //2022/02: get 'context.id' = 'files.contextid'
  1062. echo "files_functions.php: context.id=" . $context_id . "<br/>\n"; //D
  1063. $countF = 0;
  1064. // Get all iLM installed in iAssign
  1065. $list_all_iassign_ilm = $DB->get_records('iassign_ilm', array('parent' => 0, 'enable' => 1));
  1066. // Get all iAssignment block inside this course
  1067. $answer_list_iassign = $DB->get_records('iassign', array('course' => $course_id)); //R $this->get_courseid() -> $course_id
  1068. // Get all context of each iAssignment block inside this course
  1069. $list_all_instances_of_iassign = array(); // with { iassign.id, context.id, course.shortname } as { id, contextid, shortname }
  1070. $all_context = array();
  1071. $ii = 0;
  1072. foreach ($answer_list_iassign as $iassign) { // get all context of this iAssign block $iassign
  1073. $list_all_instances_of_iassign[] = $iassign;
  1074. $str_query = "SELECT {course}.id, {course}.shortname, {context}.id AS contextid FROM {course}, {context}, {course_modules} WHERE " .
  1075. "{course}.id={course_modules}.course AND {course_modules}.id={context}.instanceid AND {course_modules}.course=" . $course_id .
  1076. " AND {course_modules}.instance=" . $iassign->id;
  1077. //echo "query=" . $str_query . "<br/>";
  1078. $answer_list = $DB->get_records_sql($str_query); // get array of stdClass (must be only one element)
  1079. $list1 = array();
  1080. $strAux = "";
  1081. foreach ($answer_list as $item) { $list1[] = $item; $strAux .= "[" . $item->id . "," . $item->contextid . "," . $item->shortname . "] "; }
  1082. $all_context[$ii] = $list1[0];
  1083. 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";
  1084. $ii++;
  1085. }
  1086. $sizeofList = $ii;
  1087. for ($ii=0; $ii<$sizeofList; $ii++) { // get all iAssign block of activities (course.moudules)
  1088. $iassign = $list_all_instances_of_iassign[$ii];
  1089. $list_all_iassign_statement = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id)); // get all activities inside this block
  1090. $contextid = $all_context[$ii]->contextid; // context of this 'iassign.id'
  1091. //D echo "#list_all_iassign_statement = " . count($list_all_iassign_statement) . " de iassign.id=" . $iassign->id . "<br/>\n"; //D2022
  1092. foreach ($list_all_iassign_statement as $iassign_statement) { // get all iAssign activity inside the block '$list_all_iassign_statement'
  1093. //D echo " - iassign_statement {id = " . $iassign_statement->id . ", file = " . $iassign_statement->file . ", filesid = " . $iassign_statement->filesid .
  1094. //D "}; contextid=" . $contextid . ", date(timecreated)=" . date("Y/m/d H:i:s", $iassign_statement->timecreated) . " : " . $iassign_statement->name . "<br/>\n"; //D2022
  1095. // To avoid programming mistake on old version try to get 'files' with 'iassign_statement.id', 'iassign_statement.file' and 'iassign_statement.filesid'
  1096. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->id)); // all 'files' associated
  1097. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.id"; //D
  1098. if ($numf == 0) {
  1099. if (isset($iassign_statement->file))
  1100. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file)); // all 'files' associated
  1101. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.file"; //D
  1102. if ($numf == 0) {
  1103. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.id'
  1104. $files = $DB->get_records('files', array('id' => $iassign_statement->filesid)); // all 'files' associated
  1105. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid"; //D
  1106. if ($numf == 0) {
  1107. if (isset($iassign_statement->filesid)) // try 'iassign_statement.filesid = files.itemid'
  1108. $files = $DB->get_records('files', array('contextid' => $contextid, 'component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->filesid));
  1109. $numf = count($files); $countF += $numf; $msgO = "iassign_statement.filesid=files.itemid"; //D
  1110. if ($numf == 0)
  1111. echo " &nbsp; * files: " . $numf . " - ERRO 'id' 'file' 'filesid'<br/>\n"; //D
  1112. }
  1113. }
  1114. }
  1115. if ($numf > 0) { echo " &nbsp; * files: " . $numf . ": " . $msgO . "<br/>\n"; //D
  1116. $filename = array();
  1117. foreach ($files as $element_file) { // analyse each '$element_file' of 'files'
  1118. echo " &nbsp; * files: id = " . $element_file->id . ", contextid = " . $element_file->contextid .
  1119. ", itemid=" . $element_file->itemid . " : " . $element_file->filename . "<br/>\n"; //D
  1120. if ($element_file->filename != '.') { // get the '$element_file' that is not only "path"
  1121. $filename = explode(".", $element_file->filename);
  1122. if ($iassign_statement->filesid != $element_file->id) { // need to update 'iassign_statement.filesid'?
  1123. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.filesid'=" . $iassign_statement->filesid .
  1124. " <- " . $element_file->id . " : " . $element_file->filename . "<br/>"; //2022
  1125. $iassign_statement->filesid = $element_file->id; // 'iassign_statement.filesid' <- 'files.id' (update bellow) //2022
  1126. }
  1127. }
  1128. if ($element_file->itemid != $iassign_statement->id) { // need to update 'files.itemid'? ('files.itemid <- iassign_statement.id') //2022
  1129. //++ Deixar para outro corrigir! A partir de 'settings_activities.php'
  1130. $newentry_be_updated = new stdClass(); $newentry_be_updated->id = $element_file->id; $newentry_be_updated->itemid = $iassign_statement->id;
  1131. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'files.itemid': id=" . $newentry_be_updated->id . ", itemid=" .
  1132. $element_file->itemid . "<-" . $newentry_be_updated->itemid . " : " . $element_file->filename . "<br/>"; //2022
  1133. //R if (!$DB->update_record('files', $newentry_be_updated)) print_error('error_update', 'iassign');
  1134. }
  1135. } // foreach ($files as $element_file)
  1136. $extension = ""; // extension of the current '$element_file'
  1137. if (count($filename) > 1) $extension = strtolower($filename[count($filename) - 1]);
  1138. foreach ($list_all_iassign_ilm as $iassign_ilm) { // find iLM with the same extension (to register its 'iassign_ilm.id')
  1139. $extensions_of_ilm = explode(",", $iassign_ilm->extension);
  1140. if (in_array($extension, $extensions_of_ilm)) { // it was found the iLM with correct extension - get the lastast version
  1141. $iassign_statement->iassign_ilmid = $iassign_ilm->id; // the iLM identification: iassign_ilm.id
  1142. echo " &nbsp; &nbsp; &nbsp; &nbsp; Update 'iassign_statement.iassign_ilmid': " . $iassign_statement->iassign_ilmid . " <- " . $iassign_ilm->id . "<br/>\n";
  1143. }
  1144. }
  1145. //R $DB->update_record("iassign_statement", $iassign_statement); // update 'iassign_statement' (mainly 'iassign_statement.filesid')
  1146. } // if ($numf > 0)
  1147. } // foreach ($list_all_iassign_statement as $iassign_statement)
  1148. } // for ($ii=0; $ii<$sizeofList; $ii++)
  1149. try {
  1150. $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  1151. $filenamewrite = "_output_restore_after_execute_" . $timeWrite . ".txt";
  1152. echo "files_function.php!function see_all_files_context(.): " . $filenamewrite . "<br/>\nUsando: " . __DIR__ .
  1153. "/../../ilm_debug/escreva.php" . "<br/>\n"; //D . str_replace("\n","<br>\n", $msgD) . "<br/>\n"; // __DIR__
  1154. // ./mod/iassign/ilm_debug/escreva.php: writeContent($filetype1, $pathbase, $outputFile, $msgToRegister)
  1155. // ./mod/iassign/ilm_debug/saida_restore_after_execute.txt
  1156. //D require_once(__DIR__ . "/../../ilm_debug/escreva.php"); //leo REMOVER!
  1157. //D Gravar em: ./mod/iassign/ilm_debug/output_restore_after_execute_2022_02_dd_hh_mm_ss.txt
  1158. //D $resp = writeContent("", "", $filenamewrite, $msgD); // ($filetype1, $pathbase, $outputFile, $msgToRegister) //leo tire comentario para gravar
  1159. echo " resp=" . $resp . ", #files=" . $countF . "<br/>\n";
  1160. } catch (Exception $e) {
  1161. print 'files_function.php!function see_all_files_context(.): catch exception: ' . $e->getMessage() . "<br/>\n";
  1162. }
  1163. } // function see_all_files_context($course_id, $DB)