html5.php 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. <?php
  2. /**
  3. * Class that implements ilm_handle, in order to allow manipulation and management of HTML5 iLM
  4. *
  5. * @author Igor Moreira Fe'lix
  6. * @author Patricia Alves Rodrigues
  7. * @author Leo^nidas O. Branda~o
  8. * @version v 1 2017/17/10
  9. * @package mod_iassign_ilm_handlers
  10. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  11. *
  12. * <b>License</b>
  13. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  14. */
  15. global $CFG;
  16. require_once $CFG->dirroot . '/mod/iassign/ilm_handle.php';
  17. class html5 implements ilm_handle {
  18. /// Auxiliary function to check is an directory is empty
  19. public static function is_empty_dir ($dir) {
  20. if (!is_dir($dir))
  21. return FALSE;
  22. $handle = opendir($dir);
  23. while (false !== ($entry = readdir($handle))) {
  24. if ($entry != "." && $entry != "..") {
  25. closedir($handle); // close end return from here
  26. return FALSE;
  27. }
  28. }
  29. closedir($handle); // close end return
  30. return TRUE;
  31. }
  32. /// Produce HTML code to load iLM
  33. // @calledby locallib.php: class ilm_settings: function build_ilm_tags($ilm_id, $options=array()): $retorno=$typec::build_ilm_tags($ilm_id, $options);
  34. public static function build_ilm_tags ($ilm_id, $options = array()) {
  35. global $DB, $OUTPUT;
  36. global $CONF_WWW; //TODO 1 => use iLM under WWW; otherwise use under MoodleData
  37. $html = "";
  38. if (empty($options['Proposition']))
  39. $options['Proposition'] = "";
  40. if (empty($options['addresPOST']))
  41. $options['addresPOST'] = "";
  42. if (empty($options['student_answer']))
  43. $options['student_answer'] = "";
  44. if (empty($options['notSEND']))
  45. $options['notSEND'] = "";
  46. else // Case it is authoring put 'notSEND' (important to iVProgH5 to present authoring tool)
  47. if ($options['type'] == "editor_update")
  48. $options['notSEND'] = "true";
  49. if (empty($options['id_iLM_security'])) // if defined, it is from 'iassign_security'
  50. $options['id_iLM_security'] = "";
  51. $id_iLM_security = $options['id_iLM_security'];
  52. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  53. if ($iassign_ilm) {
  54. // md_files : filename
  55. $ilm_extension = $iassign_ilm->extension; // use local variavel to efficiency (several use)
  56. if ($ilm_extension) { // avoid problems
  57. $ilm_extension = strtolower($ilm_extension);
  58. }
  59. // Attention: in iAssign 2014 on, all the iLM is located on the Moodle filesystem (usually /var/moodledata/filedir/).
  60. // This means that '$iassign_ilm->file_jar' = '*_files.id'
  61. $file_url = array();
  62. $fs = get_file_storage();
  63. $files_jar = explode(",", $iassign_ilm->file_jar);
  64. $url = $iassign_ilm->file_class; // to HTML5 package, this 'file_class' must have the main HTML file
  65. array_push($file_url, $url);
  66. $lang = substr(current_language(), 0, 2);
  67. if ($options['type'] == "filter") { //leo
  68. $iassign_ilm_width = $options['width']; // or use? $iassign_ilm->width
  69. $iassign_ilm_height = $options['height']; // or use? $iassign_ilm->height
  70. } else { //leo
  71. $iassign_ilm_width = $iassign_ilm->width;
  72. $iassign_ilm_height = $iassign_ilm->height; // or use? $iassign_ilm->height
  73. }
  74. if (!empty($file_url)) { // There is an iLM file
  75. //TODO iLM_HTML5 :: Change to 'object', tag 'applet' was deprecated.
  76. $paramsStr = "?1=1";
  77. $html .= html5::show_ilm_commands();
  78. switch ($options['type']) {
  79. case "view":
  80. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']); //leo
  81. $paramsStr .= "&iLM_PARAM_SendAnswer=true";
  82. //TODO: REVIEW: this code is to insert iLM as HTML5 and to allow general parameter to any iLM
  83. //TODO: For now, 'iassign_ilm_config' is empty... let comment these lines
  84. //n $iassign_ilm_config = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id));
  85. //n foreach ($iassign_ilm_config as $ilm_config) {
  86. //n if (array_key_exists($ilm_config->param_name, $options)) {
  87. //n $ilm_config->param_value = $options[$ilm_config->param_name];
  88. //n $paramsStr .= "&" . $ilm_config->param_name . "=" . urlencode($ilm_config->param_value);
  89. //n }
  90. //n }
  91. break;
  92. case "filter":
  93. if ($options['toolbar'] == "disable")
  94. $paramsStr .= "&SOH_ADD=ADD";
  95. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  96. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  97. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  98. $paramsStr .= "&iLM_PARAM_ServerToGetAnswerURL=" . urlencode($ilm_config->param_value);
  99. break; // static function build_ilm_tags($ilm_id, $options=array())
  100. case "activity": // build_ilm_tags
  101. //TODO To generalize to any HTML5 iLM, it is necessary to use 'iLM_PARAM_Assignment' and 'iLM_PARAM_SendAnswer'
  102. //TODO iLM_PARAM_Assignment=Proposition ; iLM_PARAM_SendAnswer=notSEND
  103. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  104. // if ($options['special_param'] == 1) { }
  105. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  106. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  107. $paramsStr .= "&iLM_PARAM_ServerToGetAnswerURL=" . urlencode($options['addresPOST']);
  108. //TODO iLM_HTML5 :: To extend to any iLM in HTML5
  109. //TODO iLM_HTML5 :: it will allow to load dynamic parameters
  110. //T $iassign_activity_item_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $options['iassign_statement'] ));
  111. //T if ($iassign_activity_item_configs) {
  112. //T foreach ($iassign_activity_item_configs as $iassign_activity_item_config)
  113. //T $paramsStr .= "&" . $iassign_activity_item_config->param_name . "=" . urlencode($iassign_activity_item_config->param_value);
  114. //T }
  115. break;
  116. case "editor_new":
  117. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  118. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  119. $paramsStr .= "&iLM_PARAM_Authoring=true";
  120. break;
  121. case "editor_update":
  122. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  123. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  124. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  125. $paramsStr .= "&iLM_PARAM_Authoring=true";
  126. break;
  127. default:
  128. $html .= iassign::warning_message_iassign('error_view_without_actiontype'); // $OUTPUT->notification(get_string('error_view_without_actiontype', 'iassign'), 'notifyproblem'); // The API allows for creation of four types of notification: error, warning, info, and success.
  129. } // switch($options['type'])
  130. $paramsStr .= "&lang=" . $lang; // get the language defined in Moodle
  131. $sty_aux = "overflow-x:hidden !important; overflow-y:hidden !important;";
  132. $parameters = ' style="width: ' . $iassign_ilm_width . 'px; height: ' . $iassign_ilm_height . 'px;' . $sty_aux . '" ';
  133. // $html .= "\n" . '<iframe frameborder="0" seamless="seamless" name="iLM" id="iLM"' . "\n";
  134. $html .= "\n" . '<iframe frameborder="0" name="iLM" id="iLM" scrolling="no"' . "\n";
  135. $html .= ' src="' . $iassign_ilm->file_jar . $iassign_ilm->file_class . $paramsStr . '" ' . $parameters . '>' . "\n";
  136. $html .= "</iframe>\n";
  137. } // if (!empty($file_url))
  138. } // if ($iassign_ilm)
  139. return $html;
  140. } // public static function build_ilm_tags($ilm_id, $options = array())
  141. public static function show_ilm_commands () {
  142. $html = "<script>function full_screen() {
  143. if ('fullscreenEnabled' in document || 'webkitFullscreenEnabled' in document || 'mozFullScreenEnabled' in document || 'msFullscreenEnabled' in document) {
  144. if (document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled) {
  145. var element = document.getElementsByName('iLM').item(0);
  146. //requestFullscreen is used to display an element in full screen mode.
  147. if ('requestFullscreen' in element) {
  148. element.requestFullscreen();
  149. }
  150. else if ('webkitRequestFullscreen' in element) {
  151. element.webkitRequestFullscreen();
  152. }
  153. else if ('mozRequestFullScreen' in element) {
  154. element.mozRequestFullScreen();
  155. }
  156. else if ('msRequestFullscreen' in element) {
  157. element.msRequestFullscreen();
  158. }
  159. }
  160. } else {
  161. $('.expand_button').addClass('disabled');
  162. }
  163. }</script>";
  164. $html .= "<div id='fullscreen' style='width: 800px;text-align: right;'><div onClick='full_screen()' style='color: blue; cursor: pointer; display: contents;'>".get_string('full_screen', 'iassign')."</div></div>";
  165. return $html;
  166. } // public static function show_ilm_commands()
  167. //TODO: Remover em favor do 'files_functions.php ! get_from_files(.)'
  168. // Try to get file from {files} using fields 'id', 'file' and 'filesid' in this order
  169. // The current convention is {files}.itemid = {iassign_statement}.id (however previously the others were used)
  170. public static function recover_from_files ($fs, $context_id, $iassign_statement_id, $iassign_statement_file, $iassign_statement_files) {
  171. // {iassign_statement} = table 'iassign_statement'
  172. $answer = [-1,-1]; // indicate error
  173. $files = $fs->get_area_files($context_id, 'mod_iassign', 'exercise', $iassign_statement_id);
  174. //D if ($files) { $msg_aux = ""; foreach ($files as $one_files) if ($one_files!='.') $msg_aux = "{files}=[id=" . $one_files->get_id() . ", itemid=" . $one_files->get_itemid() . "]<br/>"; }
  175. //D echo "ilm_handlers/html5.php: recover_from_files(.): tentou com 'iassign_statement_id'=" . $iassign_statement_id . ": files=" . ($files?"OK":"vazio!") . "<br/>" . $msg_aux . "<br/>";
  176. if (!$files) {
  177. //D echo " * html5.php: falhou com {iassign_statement}: id=" . $iassign_statement_id . " - tente com file=" . $iassign_statement_file . "<br/>\n";
  178. $files = $fs->get_area_files($context_id, 'mod_iassign', 'exercise', $iassign_statement_file);
  179. //D echo "ilm_handlers/html5.php: recover_from_files(.): tentou com 'iassign_statement_file'=" . $iassign_statement_file . ": files=" . ($files?"OK":"vazio!") . "<br/>";
  180. if (!$files) {
  181. //D echo " * html5.php: falhou com {iassign_statement}: file=" . $iassign_statement_file . " - tente com filesid=" . $iassign_statement_filesid . "<br/>\n";
  182. // $files = $fs->get_area_files($context_id, 'mod_iassign', 'exercise', $iassign_statement_filesid);
  183. $files = $fs->get_file_by_id($iassign_statement_filesid);
  184. //D echo "ilm_handlers/html5.php: recover_from_files(.): tentou com 'iassign_statement_filesid'=" . $iassign_statement_filesid . ": files=" . ($files?"OK":"vazio!") . "<br/>";
  185. if (!$files) {
  186. //D echo " * html5.php: falhou com {iassign_statement}: filesid=" . $iassign_statement_fileid . " - estou perdido!<br/>\n";
  187. }
  188. }
  189. }
  190. if ($files) {
  191. //D echo " * html5.php: deu certo com {iassign_statement}: id=" . $iassign_statement_id . ", {files}.id=";
  192. //TODO To be revised? 'files.filename' has '.' is only path (not the file)
  193. foreach ($files as $one_files) {
  194. //D echo $one_files->get_id() . ", ";
  195. if ($one_files->get_filename() != '.') {
  196. $files_id = $one_files->get_id();
  197. $answer[0] = $one_files; // return the {files}
  198. $answer[1] = $one_files->get_id(); // return the {files}.id
  199. break; // found => finish
  200. }
  201. }
  202. //D echo "<br/>\n";
  203. }
  204. return $answer;
  205. }
  206. /// Presents the iLM content (content inside iLM)
  207. // @calledby locallib.php : view_iLM(...)
  208. public static function show_activity_in_ilm ($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) {
  209. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  210. $special_param1 = $iassign_statement_activity_item->special_param1;
  211. $ilm = $DB->get_record('iassign_ilm', array('id' => $iassign_statement_activity_item->iassign_ilmid));
  212. $context = context_module::instance($USER->cm);
  213. //D if ($USER->id==3) echo "<br/> * html5.php: show_activity_in_ilm(.): {context}.id=" . $context->id . ", iassign_statement_activity_item.id=" . $iassign_statement_activity_item->id . ", view_teacherfileversion=" . $view_teacherfileversion . "<br/>\n";
  214. //TODO Given an activity => find its correspondent file in Moodle data. Bad solution!
  215. //TODO Change the meaning of 'iassign_statement.file' from insertion order to the ID in table 'files'.
  216. //TODO This demands update to each 'iassign_statement', find its corresponding on in 'files', and update 'iassign_statement.file = files.id'
  217. if ($view_teacherfileversion) { // get the exercise in Moodle data (teacher file)
  218. //D if ($USER->id==3) echo " * html5.php: show_activity_in_ilm(.): iassign_statement_activity_item.id=" . $iassign_statement_activity_item->id . ": carrega " . $CFG->dirroot . '/mod/iassign/files_functions.php' . "<br/>\n";
  219. $fileid = "";
  220. $fs = get_file_storage();
  221. //$files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement_activity_item->id); // iassign_statement_activity_item = table 'iassign_statement'
  222. // $answer_files = html5::recover_from_files($fs, $context->id, $iassign_statement_activity_item->id, $iassign_statement_activity_item->file, $iassign_statement_activity_item->filesid);
  223. require_once($CFG->dirroot . '/mod/iassign/files_functions.php');
  224. // echo "html5.php: {iassign_statement}.id=" . $iassign_statement_activity_item->id . ", file=" . $iassign_statement_activity_item->file . ", filesid=" . $iassign_statement_activity_item->filesid . "<br/>\n";
  225. $answer_files = NULL;
  226. if ($iassign_statement_activity_item->file!=0 || $iassign_statement_activity_item->filesid!=0) { // problem with restore?
  227. $answer_files = get_from_files(
  228. $iassign_statement_activity_item->id, $iassign_statement_activity_item->file, $iassign_statement_activity_item->filesid,
  229. $fs, $context->id, 'mod_iassign', 'exercise');
  230. if ($answer_files == NULL) { // Error: no {files} were found in this context with these 3 itemid
  231. $answer_files = get_from_files(
  232. $iassign_statement_activity_item->id, $iassign_statement_activity_item->filesid, $iassign_statement_activity_item->filesid,
  233. $fs, $context->id, 'mod_iassign', 'exercise');
  234. }
  235. }
  236. //D if ($USER->id==3) echo " * html5.php: show_activity_in_ilm(.): #answer_files=" . count($answer_files) . "<br/>\n"; //exit;
  237. $one_files = NULL;
  238. $one_files_id = -1;
  239. $one_files_filename = "<vazio>";
  240. if ($answer_files == NULL) { // Error: no {files} were found in this context with these 3 itemid
  241. print "Error: {iassign_statement}.id=" . $iassign_statement_activity_item->id . " associated file not found! file=" .
  242. $iassign_statement_activity_item->file . ", filesid=" . $iassign_statement_activity_item->filesid . "<br/>\n";
  243. }
  244. else {
  245. $one_files = $answer_files[0]; // get $f1_obj from 'files_functions.php!get_from_files(.)'
  246. if (!empty($one_files)) {
  247. $one_files_id = $one_files->get_id(); // write_iLM_security(.) will use this ID to get file content
  248. $one_files_filename = $one_files->get_filename();
  249. }
  250. //D if ($USER->id==3) echo " &nbsp; + {files} : id=" . $one_files_id . ", itemid=" . $one_files->get_itemid() . ", filename=" . $one_files_filename . "<br/>\n"; //leo
  251. }
  252. //D echo " * voltou de 'recover_from_files' com one_files_id=" . $one_files_id . "<br/>\n";
  253. if ($one_files_id == -1) { // 'Something is wrong. Maybe your teacher withdrew this exercise file. Please, inform your teacher.';
  254. //D echo "ERRO: one_files_id=-1<br/>\n";
  255. print iassign::warning_message_iassign('error_exercise_removed') . "<br/>\n"; // I couldn't find the file in table 'files'!
  256. }
  257. } // if ($view_teacherfileversion)
  258. $ilm_name = strtolower($ilm->name);
  259. $extension = iassign_utils::filename_extension($ilm_name);
  260. if ($view_teacherfileversion) { // $view_teacherfileversion==1 => load the exercise ('activity') from the 'moodledata' (id in 'files')
  261. // $content_or_id_from_ilm_security = $this->context->id;
  262. // The content is here: $one_files->get_content(); // since it is teacher get the file contents at once
  263. // however the iLM must requires the content (using ./mod/iassign/ilm_security.php)
  264. $content_or_id_from_ilm_security = $one_files_id; // $iassign_statement_activity_item->file;
  265. }
  266. else { // $view_teacherfileversion==null => load the learner answer from the data base (iassign_submission)
  267. $content_or_id_from_ilm_security = $student_answer;
  268. }
  269. $allow_submission = false; // There is permission to 'submission' button?
  270. // Student already send one answer and the iLM allow to edit the submissiont
  271. // For instance, the Java version of iGeom does not allow editting (because the send answer do not has the target objects)
  272. // However, the iVProg JavaScript version allows editing
  273. // if ($USER->iassignEdit == 1 && $student_answer) { // for now, only iVProg2 and iVProgH5 allows editions of exercise already sent
  274. if ($ilm->editingbehavior == 1 && $student_answer) { // if iLM allow to edit previous solution
  275. $allow_submission = true; // yes!
  276. if ($enderecoPOST!="") {
  277. // In "locallib.php!view_iassign_current()", if due date is excedeed (even with allow test {iassign_statement}.test==1): $enderecoPOST = "";
  278. $write_solution = 1;
  279. $enderecoPOST .= "&write_solution=1"; // complement POST address indicating that the learner could send edited solution
  280. }
  281. }
  282. // Prepare JS to the button "send" to the iAssign addres in $enderecoPOST
  283. // $enderecoPOST is prepared in 'locallib.php:view_iassign_current()'
  284. // with "http://.../mod/iassign/view.php?action=get_answer&iassign_submission_current=ID1&id=ID2&iassign_current=ID3&write_solution=1&userid_iassign=ID4&write_solution=1
  285. // ID1 = {iassign_submission}.id
  286. // ID2 = {course_modules}.id with {course_modules}.instance = {iassign}.id
  287. // ID3 = {iassign_statement}.id
  288. // ID4 = {user}.id
  289. $iassign = "
  290. <script type='text/javascript'>
  291. //<![CDATA[
  292. var strAnswer = '';
  293. var evaluationResult = '';
  294. var comment = '';
  295. function jsAnalyseAnswer () { // this function will call the iLM function 'getEvaluation()'
  296. document.getElementById('spinner-loading').style.visibility = 'visible';
  297. setTimeout(function(){
  298. document.getElementById('spinner-loading').style.visibility = 'hidden';
  299. }, 5000);
  300. // iVProg will call function 'getEvaluationCallback(...)': /var/www/html/.../<iLM_name>/<iLM_version>/<iLM_dir>/js/iassign-integration-functions.js
  301. // - 'getEvaluation()' calls 'js/iassign-integration-functions.js : endTest function(index)' that calls 'getEvaluationCallback(apro/100);'
  302. // iFractions: sumEval = getSummation(); alert('mod/iassign/ilm/ifractions_5/index.html: sumEval = ' + sumEval);
  303. //ATTENTION 2017/11/22 - the use of 'window.frames.iLM' here, results in error 'TypeError: window.frames.iLM.getEvaluation is not a function'
  304. //ATTENTION answerGrade = window.frames.iLM.getEvaluation();
  305. var answerGrade = window.frames[0].getEvaluation(); // with the first frame (can not have another frame!), get the iLM student content
  306. console.log('./iassign/ilm_handlers/html5.php: jsAnalyseAnswer(): getEvaluation() returned value = ' + answerGrade);
  307. // document.getElementsByTagName('frame')[0].src
  308. // here answerGrade will return 'undefined' always
  309. if (answerGrade == 'undefined' || answerGrade == undefined) {
  310. console.log('./iassign/ilm_handlers/html5.php: jsAnalyseAnswer(): Ups! answerGrade undefined!');
  311. return false;
  312. }
  313. strAnswer = window.frames[0].getAnswer(); // get from iLM the student answer
  314. document.formEnvio.iLM_PARAM_ArchiveContent.value = strAnswer;
  315. document.formEnvio.iLM_PARAM_ActivityEvaluation.value = answerGrade;
  316. document.formEnvio.iLM_PARAM_RealGrade.value = answerGrade;
  317. console.log('./iassign/ilm_handlers/html5.php: jsAnalyseAnswer(): iLM_PARAM_RealGrade<-' + answerGrade);
  318. console.log(' * iLM_PARAM_ArchiveContent=' + strAnswer);
  319. console.log('-----');
  320. document.formEnvio.submit();
  321. return true; // success
  322. }
  323. window.flagclick = false;
  324. // ./mod/iassign/ilm/<iLM-name>/js/iassign-integration-functions.js : call this to define the variable 'evaluationResult'
  325. function getEvaluationCallback (evaluation) {
  326. let submissionbehavior = " . $ilm->submissionbehavior . ";
  327. // console.log('getEvaluationCallback(): evaluation=' + evaluation + ', submissionbehavior=' + submissionbehavior);
  328. // console.log('getEvaluationCallback(): enderecoPOST=" . $enderecoPOST . "');
  329. evaluationResult = evaluation;
  330. // Before 2017/11/22: strAnswer = window.frames.iLM.getAnswer();
  331. strAnswer = window.frames[0].getAnswer();
  332. // comment = document.formEnvio.submission_comment.value;
  333. //D alert('getEvaluationCallback(...)' + evaluation + ', strAnswer=' + strAnswer);
  334. //D alert('getEvaluationCallback: enviando evaluationResult=' + evaluation + ', strAnswer=' + strAnswer);
  335. if ((strAnswer==null || strAnswer=='' || strAnswer==-1)) { // undefined // //R && (comment==null || comment=='')
  336. alert('" . get_string('activity_empty', 'iassign') . "'); // 'Activity sent without content.'
  337. return false; // error...
  338. }
  339. else if (submissionbehavior == 1) { // iLM has no submition button, use the form one
  340. document.formEnvio.iLM_PARAM_ArchiveContent.value = strAnswer;
  341. document.formEnvio.iLM_PARAM_ActivityEvaluation.value = evaluationResult;
  342. document.formEnvio.iLM_PARAM_RealGrade.value = evaluation;
  343. //D alert(strAnswer);
  344. document.formEnvio.submit();
  345. return true; // success
  346. }
  347. else if (submissionbehavior == 0) { // iLM has submition button - then, use it to submit answer
  348. var formData = new FormData();
  349. formData.append('iLM_PARAM_ArchiveContent', strAnswer);
  350. formData.append('iLM_PARAM_ActivityEvaluation', evaluationResult);
  351. formData.append('iLM_PARAM_RealGrade', evaluation);
  352. formData.append('iLM_ajax', 1);
  353. var request = new XMLHttpRequest();
  354. request.open('POST', '" . $enderecoPOST . "');
  355. request.onload = function (e) {
  356. if (!window.flagclick) return;
  357. window.flagclick = false;
  358. document.getElementById('spinner-loading').style.visibility = 'hidden';
  359. if (request.readyState === 4) {
  360. if (request.status === 200) {
  361. document.getElementById('check-message-success-submission').style.visibility = 'visible';
  362. setTimeout(function(){
  363. document.getElementById('check-message-success-submission').style.visibility = 'hidden';
  364. }, 3000);
  365. } else {
  366. document.getElementById('error-message-submission').style.visibility = 'visible';
  367. }
  368. } else {
  369. document.getElementById('error-message-submission').style.visibility = 'visible';
  370. }
  371. };
  372. request.send(formData);
  373. }
  374. }
  375. //]]>
  376. </script>\n";
  377. // Security: this avoid the student get a second access to the file content (usually an exercise)
  378. // Data are registered in the table '*_iassign_security' bellow and is erased by function 'view()' above.
  379. // IMPORTANT: the '$end_file' will receive the iLM content URL using the security filter './mod/iassign/ilm_security.php'
  380. // the iLM must request the content using this URL. Data are registered in the table '*_iassign_security'.
  381. // Attention : using iVProgH5 there are lot of " and the use of slashes (as '\"') will imply in iVProgH5 do not read the file!
  382. // do not use: $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, addslashes($content_or_id_from_ilm_security));
  383. //2017 $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, $content_or_id_from_ilm_security); // insert in 'iassign_security'
  384. //2017 $this->remove_old_iLM_security_entries($USER->id, $iassign_statement_activity_item->id); // additional security: erase eventually old entries
  385. require_once $CFG->dirroot . '/mod/iassign/ilm_security.php';
  386. $timecreated = time();
  387. $token = md5($timecreated); // iassign_iLM_security->timecreated);
  388. //D if ($USER->id==3) echo " * html5.php: antes de 'write_iLM_security': timecreated=".$timecreated.", iassign_statement_activity_item->id=".$iassign_statement_activity_item->id.", content_or_id_from_ilm_security=".$content_or_id_from_ilm_security."<br/>\n"; //leo
  389. // Additional security to avoid error on "INSERT INTO {iassign_security} (...)"
  390. if (!$content_or_id_from_ilm_security || (is_numeric($content_or_id_from_ilm_security) && !($content_or_id_from_ilm_security>0)))
  391. $content_or_id_from_ilm_security = -1; // to avoid 'write_iLM_security(.)' try to load non existent file
  392. $id_iLM_security = ilm_security::write_iLM_security($USER->id, $timecreated, $iassign_statement_activity_item->id, $content_or_id_from_ilm_security); // insert in 'iassign_security'
  393. // $iassign_iLM_security = $DB->get_record("iassign_security", array("id" => $id_iLM_security));
  394. $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $id_iLM_security . '&token=' . $token . '&view=' . $view_teacherfileversion; // need full path...
  395. // $iassign = "..."
  396. $iassign .= "\n<center>\n<form name='formEnvio' id='formEnvio' method='post' action='" . $enderecoPOST . "' enctype='multipart/form-data'>\n";
  397. // Attention: The actual iLM content will be provided by the indirect access in './mod/iassign/ilm_security.php',
  398. // bellow only the 'token' to the content will be shown in URL (by security reason). The iLM must use this URL on
  399. // 'MA_PARAM_Proposition' to request the content.
  400. // Calls static function bellow: parameters are data to store in table '*_iassign_submission'
  401. // In 'locallib.php ! class ilm_settings ! static function build_ilm_tags($ilm_id, $options = array())' that calls 'build_ilm_tags(.)' from here
  402. $iassign .= ilm_settings::build_ilm_tags($ilm->id, array(
  403. "type" => "activity",
  404. "notSEND" => "false",
  405. "addresPOST" => $enderecoPOST,
  406. "Proposition" => $end_file,
  407. "special_param" => $special_param1,
  408. "student_answer" => $student_answer,
  409. "id_iLM_security" => $id_iLM_security,
  410. "iassign_statement" => $iassign_statement_activity_item->id // MOOC 2016
  411. ));
  412. //DEBUG To verify iLM
  413. //D $iassign .= "
  414. //D <script type='text/javascript'>
  415. //D //alert('./mod/iassign/ilm_handlers/html5.php: 1 window.frames[0].getEvaluation()=' + window.frames[0].getEvaluation()); //
  416. //D alert('./mod/iassign/ilm_handlers/html5.php: 1 window.frames[0].getAnswer()=' + window.frames[0].getAnswer()); //
  417. //D </script>\n";
  418. if ($ilm->editingbehavior == 1 || !$student_answer) // the iLM allows editing old submission or has no previous solution send
  419. $allow_submit_button = 1;
  420. else
  421. $allow_submit_button = 0;
  422. //D echo "./iassign/ilm_handlers/html5.php: show_activity_in_ilm(.): ilm->action_buttons=" . $ilm->action_buttons . "<br/>";
  423. //D echo " - iassign_statement_activity_item->type_iassign=" . $iassign_statement_activity_item->type_iassign . ", _GET['action']=" . $_GET['action'] . "<br/>";
  424. // "iassign_statement_activity_item.type_iassign" == 1 => is activity of type "example" - not submit button for submission
  425. // {iassign_ilm}.action_buttons==1 <=> do not use the submition button of the form
  426. if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1 && $allow_submit_button == 1) {
  427. // ($ilm->editingbehavior == 1 || ($ilm->editingbehavior == 0 && !in_array($_GET['action'], array('viewsubmission', 'view')))))
  428. //D echo " - entrou 1<br/>\n";
  429. $iassign .= " <input type='hidden' name='iLM_PARAM_ArchiveContent' value=''>\n";
  430. $iassign .= " <input type='hidden' name='iLM_PARAM_ActivityEvaluation' value=''>\n";
  431. $iassign .= " <input type='hidden' name='iLM_PARAM_RealGrade' value=''>\n";
  432. // Default: enable button to submit activity - need to avoid "Warning: Undefined variable $disabled_button in..."
  433. $disabled_button = ""; // {iassign_ilm}.action_buttons==1 <=> do not use the submition button of the form
  434. //2023/05/08 Desabilitado para evitar ficar sem o botao!
  435. //2023/05/08 if ($ilm->action_buttons == 1) $disabled_button = "";
  436. //2023/05/08 elseif ($ilm->action_buttons == 0) $disabled_button = " disabled='true' ";
  437. // $iassign .= "<p><textarea rows='2' cols='60' name='submission_comment'></textarea></p>\n";
  438. // Prepare button to send answer (calling the iLM getEvaluation())
  439. // In "locallib.php!view_iassign_current()", if due date is excedeed (even with allow test {iassign_statement}.test==1): $enderecoPOST = "";
  440. // For this reason, if $enderecoPOST == "", do NOT present submission button!
  441. // {iassign_statement}.preventlate==0 <=> do NOT allow submission after due date
  442. if ($enderecoPOST && $enderecoPOST!="&write_solution=1" && $enderecoPOST!="&write_solution=0") {
  443. // There is a valid address - caution with $iassign_statement_activity_item->preventlate==1 <=> allow submission after due date
  444. $iassign .= "<center>\n<!-- load button -->\n" .
  445. " <br> <br> <button " . $disabled_button . " class='btn btn-primary' type=button value='" . get_string('submit_iassign', 'iassign') . "' " .
  446. " onClick = 'window.flagclick = true; window.jsAnalyseAnswer();' title='" . get_string('message_submit_iassign', 'iassign') . "'>" .
  447. get_string('submit_iassign', 'iassign') . "</button> <i id='check-message-success-submission' class='fa fa-check' aria-hidden='true' " .
  448. " style='position: absolute; color: green; font-size: 2.0rem; margin-left: 1rem; visibility: hidden;'><span style='font-size: 1.0rem;'>" .
  449. get_string('get_answer', 'iassign') . "</span></i><i id='error-message-submission' class='fa fa-times' aria-hidden='true' " .
  450. " style='position: absolute; color: red; font-size: 2.0rem; margin-left: 1rem; visibility: hidden;'><span style='font-size: 1.0rem;'>" .
  451. get_string('error_insert_submissions', 'iassign') . "</span></i>\n" . " <div class='spinner-border' style='position: absolute; margin-left: 1rem; visibility: hidden;' " .
  452. " id='spinner-loading' role='status'><span class='sr-only'>Loading...</span>\n </div> </center>\n";
  453. }
  454. } // if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1 && $allow_submit_button == 1)
  455. elseif ($ilm->editingbehavior == 0 && $student_answer) { // already send some answer
  456. // Button to redo the activity
  457. //D echo " - entrou 2<br/>\n";
  458. $iassign .= "<center><br><a href=\"view.php?action=repeat&id=" . $_GET['id'] . "&iassign_current=" . $_GET['iassign_current'] . "\"> \n" .
  459. " <button type='button' class='btn btn-success' value='" . get_string('repeat', 'iassign') . "'>" . get_string('repeat', 'iassign') . "</button></a></center>\n";
  460. }
  461. //D else echo " - NAO entrou <br/>\n";
  462. $iassign .= "</form></center>\n\n";
  463. return $iassign;
  464. } // public static function show_activity_in_ilm($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion)
  465. /// Presents iLM information
  466. public static function view_ilm ($ilmid, $from) {
  467. global $DB;
  468. $url = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign'));
  469. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilmid));
  470. $str = "";
  471. $str .= '<table id="outlinetable" cellpadding="5" width="100%" >' . "\n";
  472. $str .= '<tr>';
  473. $str .= '<td colspan=3 align=right>';
  474. if ($from != 'admin') {
  475. $str .= '<input type=button value="' . get_string('return', 'iassign') . '" onclick="javascript:window.location = \'' . $_SERVER['HTTP_REFERER'] . '\';">' . "\n";
  476. }
  477. $str .= '<input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close();">';
  478. $str .= '</td>' . "\n";
  479. $str .= '</tr>' . "\n";
  480. if ($iassign_ilm) {
  481. $iassign_statement_activity_item = $DB->get_records('iassign_statement', array("iassign_ilmid" => $iassign_ilm->id));
  482. if ($iassign_statement_activity_item) {
  483. $total = count($iassign_statement_activity_item);
  484. } else {
  485. $total = 0;
  486. }
  487. if ($from == 'admin') {
  488. $str .= '<tr><td colspan=2>' . "\n";
  489. $str .= '<table width="100%" class="generaltable boxaligncenter" >' . "\n";
  490. $str .= '<tr>' . "\n";
  491. $str .= '<td class=\'cell c0 actvity\' ><strong>' . get_string('activities', 'iassign') . ':</strong>&nbsp;' . $total . '</td>' . "\n";
  492. $str .= '<td><strong>' . get_string('url_ilm', 'iassign') . '</strong>&nbsp;<a href="' . $iassign_ilm->url . '">' . $iassign_ilm->url . '</a></td>' . "\n";
  493. $str .= '</tr>' . "\n";
  494. $str .= '<tr><td colspan=2><strong>' . get_string('description', 'iassign') . ':</strong>&nbsp;' . iassign_language::get_description_lang(current_language(), $iassign_ilm->description) . '</td></tr>' . "\n";
  495. $str .= '<tr><td width="50%"><strong>' . get_string('type_ilm', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->type . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>' . get_string('extension', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->extension . '</td>' . "\n";
  496. $str .= '<td width="50%"><strong>' . get_string('width', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->width;
  497. $str .= '&nbsp;&nbsp;<strong>' . get_string('height', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->height . '</td></tr>' . "\n";
  498. $date_jar = $iassign_ilm->file_jar;
  499. $str .= '<tr><td><strong>' . get_string('file_jar', 'iassign') . ':</strong>&nbsp;' . $date_jar . '</td>' . "\n";
  500. $str .= '<td ><strong>' . get_string('file_class', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->file_class . '</td></tr>' . "\n";
  501. if ($iassign_ilm->evaluate == 1) {
  502. $evaluate = get_string('yes', 'iassign');
  503. } else {
  504. $evaluate = get_string('no', 'iassign');
  505. }
  506. $str .= '<tr><td width="50%"><strong>' . get_string('evaluate', 'iassign') . ':</strong>&nbsp;' . $evaluate . '</td>' . "\n";
  507. if ($iassign_ilm->enable == 1) {
  508. $enable = get_string('yes', 'iassign');
  509. } else {
  510. $enable = get_string('no', 'iassign');
  511. }
  512. $str .= '<td width="50%"><strong>' . get_string('enable', 'iassign') . ':</strong>&nbsp;' . $enable . '</td></tr>' . "\n";
  513. $str .= '<tr>' . "\n";
  514. $str .= '<td width="50%"><strong>' . get_string('file_created', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timecreated) . '</td>' . "\n";
  515. $str .= '<td width="50%"><strong>' . get_string('file_modified', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timemodified) . '</td>' . "\n";
  516. $str .= '</tr>' . "\n";
  517. $user_ilm = $DB->get_record('user', array('id' => $iassign_ilm->author));
  518. if ($user_ilm) {
  519. $str .= '<tr>' . "\n";
  520. $str .= '<td colspan=2><strong>' . get_string('author', 'iassign') . ':</strong>&nbsp;' . $user_ilm->firstname . '&nbsp;' . $user_ilm->lastname . '</td>' . "\n";
  521. $str .= '</tr>' . "\n";
  522. }
  523. $str .= '</table>' . "\n";
  524. $str .= '</td></tr>' . "\n";
  525. }
  526. if (!empty($iassign_ilm->file_jar)) {
  527. //TODO: REVIEW: to be used for parameters of "applet" from DB
  528. $options = array("type" => "view"); //MOOC2014: start
  529. $str .= '<tr class=\'cell c0 actvity\'><td colspan=3 align=center bgcolor="#F5F5F5">' . "\n";
  530. // Second parameter null since 'iassign_security' are not define yet
  531. $str .= ilm_settings::build_ilm_tags($iassign_ilm->id, $options);
  532. //TODO: REVIEW: missing code to manage parameters
  533. //MOOC2014: tem este codigo!
  534. } else {
  535. $str .= '<tr class=\'cell c0 actvity\'>' . "\n";
  536. $str .= '<td colspan=2 align=center>' . get_string('null_file', 'iassign') . '</td>' . "\n";
  537. $str .= '<td align=center><a href="' . $url . '</a></td>' . "\n";
  538. $str .= '</tr>' . "\n";
  539. }
  540. $str .= '</td></tr>' . "\n";
  541. }
  542. $str .= '</table>' . "\n";
  543. return $str;
  544. } // public static function view_ilm($ilmid, $from)
  545. /// Make a copy or produce a new version of an iLM (using a ZIP package)
  546. // Interface: Administration > plugins > iAssign : after select the iLM and the option 'Add new iLM version'
  547. // @see settings_ilm.php: $action == 'new_version'
  548. // @calledby locallib.php!copy_new_version_ilm($param): with $action=='new_version' or $action=='copy'
  549. public static function copy_new_version_ilm ($param, $files_extract) {
  550. global $DB, $CFG;
  551. $iassign_ilm = new stdClass();
  552. $iassign_ilm->name = $param->name;
  553. $iassign_ilm->version = $param->version;
  554. $iassign_ilm->file_jar = null;
  555. //D echo "html5.php!copy_new_version_ilm(.): <br/>"; // . $type_ipz_zip . "<br/>";
  556. //D // echo "param="; print_r($param);
  557. //D echo "Construir 'application_xml'<br/>";
  558. // Since we are using the same code that process IPZ package (that uses tags from "ilm-application.xml"), lets build it!
  559. $application_xml = new stdClass(); // build object with fields equivalent to those in "ilm-application.xml" from IPZ importer
  560. foreach ($param as $field => $value) {
  561. $application_xml->$field = $value;
  562. //D echo $field . "=" . $value . "; ";
  563. }
  564. //D $application_xml = @simplexml_load_file($CFG->dataroot . '/temp/' . 'ilm-application.xml', null, LIBXML_NOCDATA); // get XML fields
  565. //D echo "<br/>file_jar=" . $file_jar . "<br/>";
  566. $paramcopy_zip = "copy";
  567. if ($param->action == "new_version")
  568. $paramcopy_zip = "zip";
  569. // It creates directory and iLM files on it, will return the iLM path (after iAssign dir.): ./ilm/...
  570. $dir_and_file_jar = self::save_ilm_by_xml($paramcopy_zip, $application_xml, $files_extract);
  571. $ilm_final_directory = $dir_and_file_jar[0]; // {iassign_ilm}.file_jar is the first element (not used here)
  572. $file_jar = $dir_and_file_jar[1]; // {iassign_ilm}.file_jar is the second element (if null then occorred error, do not insert in {iassign_ilm})
  573. //D echo "file_jar=" . $file_jar . "<br/>";
  574. if ($file_jar == null) { // 'save_ilm_by_xml(...)' detected a problem (perhaps no write permission)
  575. //D echo "Erro! file_jar==null<br/>"; exit;
  576. return false;
  577. }
  578. $file_jar = str_replace("./", "", $file_jar);
  579. if ($file_jar[strlen($file_jar)-1]!=DIRECTORY_SEPARATOR)
  580. $file_jar .= DIRECTORY_SEPARATOR; // add final separator "/"
  581. //D echo "file_jar=" . $file_jar . "<br/>";
  582. //D exit; // to debug, stop here - before insert in {iassign_ilm} table
  583. // From a single text in default language => generate multi-language
  584. //R $description = json_decode($param->description_lang);
  585. //R $description->{$param->set_lang} = $param->description;
  586. $description_json_lang = $param->description_lang;
  587. $newentry = new stdClass();
  588. $newentry->name = $param->name;
  589. $newentry->version = $param->version;
  590. $newentry->type = 'HTML5';
  591. $newentry->url = $param->url;
  592. $newentry->description = $description_json_lang; //R strip_tags(json_encode($description));
  593. $newentry->extension = strtolower($param->extension);
  594. $newentry->file_jar = $file_jar;
  595. $newentry->file_class = $param->file_class;
  596. $newentry->width = $param->width;
  597. $newentry->height = $param->height;
  598. $newentry->enable = 0;
  599. $newentry->timemodified = $param->timemodified;
  600. $newentry->timecreated = $param->timecreated;
  601. $newentry->evaluate = $param->evaluate;
  602. $newentry->author = $param->author;
  603. $newentry->parent = $param->parent;
  604. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  605. // log event --------------------------------------------------------------------------------------
  606. iassign_log::add_log('copy_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $newentry->id);
  607. // log event --------------------------------------------------------------------------------------
  608. } // public static function copy_new_version_ilm($param, $files_extract)
  609. /// Export the iLM to the IPZ package
  610. // @calledby locallib.php : export_ilm($ilm_id) : require_once 'ilm_handlers/' . $typec . '.php'; $typec::export_ilm($ilm_id);
  611. public static function export_ilm ($ilm_id) {
  612. global $DB, $CFG;
  613. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  614. $iassign_ilm_configs = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  615. //if (!$iassign_ilm_configs) return;
  616. $files_jar = $iassign_ilm->file_jar;
  617. $zip_filename = $CFG->dataroot . '/temp/ilm-' . iassign_utils::format_pathname($iassign_ilm->name . '-v' . $iassign_ilm->version) . '_' . date("Ymd-Hi") . '.ipz';
  618. $zip = new zip_archive;
  619. $zip->open($zip_filename);
  620. $rootdir = $CFG->dirroot . '/mod/iassign/' . $files_jar;
  621. $first_folder = str_replace($CFG->dirroot . '/mod/iassign/ilm/', "", $rootdir);
  622. $zip->add_directory($first_folder);
  623. $allfiles = self::list_directory($rootdir);
  624. $i = 0;
  625. foreach ($allfiles as $file) {
  626. $mini = str_replace($CFG->dirroot . '/mod/iassign/ilm/', "", $file);
  627. $mini = str_replace('//', "/", $mini);
  628. if (is_dir($file)) {
  629. $zip->add_directory($mini);
  630. } else {
  631. $zip->add_file_from_pathname($mini, $file);
  632. }
  633. }
  634. $folder = str_replace('ilm/', "", $files_jar);
  635. $application_descriptor = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  636. $application_descriptor .= '<application xmlns="http://line.ime.usp.br/application/1.5">' . "\n"; //TODO: o que colocar aqui???
  637. $application_descriptor .= ' <name>' . $iassign_ilm->name . '</name>' . "\n";
  638. $application_descriptor .= ' <url>' . $iassign_ilm->url . '</url>' . "\n";
  639. $application_descriptor .= ' <version>' . $iassign_ilm->version . '</version>' . "\n";
  640. $application_descriptor .= ' <type>' . $iassign_ilm->type . '</type>' . "\n";
  641. $application_descriptor .= ' <description>' . html_entity_decode(str_replace(array('<p>', '</p>'), array('', ''), $iassign_ilm->description)) . '</description>' . "\n";
  642. $application_descriptor .= ' <extension>' . $iassign_ilm->extension . '</extension>' . "\n";
  643. $application_descriptor .= ' <file_jar>' . $folder . '</file_jar>' . "\n";
  644. $application_descriptor .= ' <file_class>' . $iassign_ilm->file_class . '</file_class>' . "\n";
  645. $application_descriptor .= ' <width>' . $iassign_ilm->width . '</width>' . "\n";
  646. $application_descriptor .= ' <height>' . $iassign_ilm->height . '</height>' . "\n";
  647. $application_descriptor .= ' <evaluate>' . $iassign_ilm->evaluate . '</evaluate>' . "\n";
  648. $application_descriptor .= ' <reevaluate>' . $iassign_ilm->reevaluate . '</reevaluate>' . "\n";
  649. $application_descriptor .= ' <editingbehavior>' . $iassign_ilm->editingbehavior . '</editingbehavior>' . "\n";
  650. $application_descriptor .= ' <submissionbehavior>' . $iassign_ilm->submissionbehavior . '</submissionbehavior>' . "\n"; // 0 => iLM has no submition button, use the form one
  651. $application_descriptor .= ' <action_buttons>' . $iassign_ilm->action_buttons . '</action_buttons>' . "\n"; // {iassign_ilm}.ction_buttons==1 <=> do not use the submition button of the form
  652. if ($iassign_ilm_configs) { //MOOC 2016
  653. $application_descriptor .= ' <params>' . "\n";
  654. foreach ($iassign_ilm_configs as $iassign_ilm_config) {
  655. $application_descriptor .= ' <param>' . "\n";
  656. $application_descriptor .= ' <type>' . $iassign_ilm_config->param_type . '</type>' . "\n";
  657. $application_descriptor .= ' <name>' . $iassign_ilm_config->param_name . '</name>' . "\n";
  658. $application_descriptor .= ' <value>' . $iassign_ilm_config->param_value . '</value>' . "\n";
  659. $application_descriptor .= ' <description>' . htmlentities(str_replace("\n", "", $iassign_ilm_config->description)) . '</description>' . "\n";
  660. $application_descriptor .= ' <visible>' . $iassign_ilm_config->visible . '</visible>' . "\n";
  661. $application_descriptor .= ' </param>' . "\n";
  662. }
  663. $application_descriptor .= ' </params>' . "\n";
  664. } //MOOC 2016
  665. $application_descriptor .= '</application>' . "\n";
  666. $zip->add_file_from_string('ilm-application.xml', $application_descriptor);
  667. $zip->close();
  668. header("Pragma: public");
  669. header("Expires: 0");
  670. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  671. header("Cache-Control: private", false);
  672. header("Content-Type: application/zip");
  673. header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
  674. header("Content-Transfer-Encoding: binary");
  675. header("Content-Length: " . (filesize($zip_filename)));
  676. set_time_limit(0);
  677. readfile($zip_filename) || die("File not found.");
  678. unlink($zip_filename);
  679. exit;
  680. } // public static function export_ilm($ilm_id)
  681. /// Function for list the directory where iLM is allocated.
  682. // @param type $dir
  683. // @return type
  684. static function list_directory ($dir) {
  685. $ds = DIRECTORY_SEPARATOR; // to use short name of directory separator (on Linux is "/")
  686. $files = array();
  687. $cont = 0;
  688. $ffs = scandir($dir);
  689. unset($ffs[array_search('.', $ffs, true)]);
  690. unset($ffs[array_search('..', $ffs, true)]);
  691. if (count($ffs) < 1) {
  692. return;
  693. }
  694. foreach ($ffs as $ff) {
  695. $files[$cont] = $dir . $ds . $ff;
  696. $cont++;
  697. if (is_dir($dir . $ds . $ff)) {
  698. $temp = self::list_directory($dir . $ds . $ff);
  699. foreach ($temp as $t) {
  700. $files[$cont] = $t;
  701. $cont++;
  702. }
  703. }
  704. }
  705. return $files;
  706. }
  707. /// Function to really remove files from the iLM remove from interface to remove iLM)
  708. // @calledy locallib.php!delete_ilm($ilm_id) : $return_ilm_parent_id = $typec::delete_ilm($ilm_id);
  709. // @param int $ilm_id is the iLM ID ({iassign_ilm}.id)
  710. // @return int Return ID of parent iLM ({iassign_ilm}.id)
  711. public static function delete_ilm ($ilm_id) {
  712. global $DB, $CFG, $OUTPUT;
  713. echo "ilm_handlers/html5.php!delete_ilm(.): remove www do " . $ilm_id. "<br/>\n"; //2025/04/11 leo
  714. $ds = DIRECTORY_SEPARATOR; // to use short name of directory separator (on Linux is "/")
  715. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  716. //D echo "ilm_handlers/html5.php!delete_ilm(.): ilm_id=" . $ilm_id . ", file_jar=" . $iassign_ilm->file_jar . "<br/>\n"; //exit; //2025/04/11 leo
  717. // In case iLM has error in its path (perhaps during its creation or by misunderstanding edition)
  718. if (!file_exists($iassign_ilm->file_jar)) {
  719. //D echo "ilm_handlers/html5.php!delete_ilm(.): " . $iassign_ilm->file_jar . " nao existe, tenta completar<br/>\n"; //exit; //2025/04/11 leo
  720. // iLM directory under which the new version will be created
  721. $ilm_base_directory = "ilm" . $ds . $iassign_ilm->name; // ./mod/iassign/ilm/<iLM name>
  722. $ilm_directory_version = $ilm_base_directory . $ds . $iassign_ilm->version; // ./mod/iassign/ilm/<iLM name>/<iLM version>
  723. $iassign_ilm->file_jar = $ilm_directory_version;
  724. //D
  725. echo "ilm_handlers/html5.php!delete_ilm(.): completado para: " . $iassign_ilm->file_jar . "<br/>\n"; //exit; //2025/04/11 leo
  726. }
  727. // Prepare the path of directory to be removed
  728. $path_w = rtrim($iassign_ilm->file_jar, $ds); // remove white space and "/" from the end of the string
  729. $folder_to_remove = $path_w; // substr($path_w, 0, strrpos($path_w, '/') + 1); - ja tinha removido "/" final, com "rtrim(.)"
  730. echo "ilm_handlers/html5.php!delete_ilm(.): path_w="; print_r($path_w); echo "<br/> - folder_to_remove=" . $folder_to_remove . "<br/>\n"; //exit; //2025/04/11 leo
  731. // ilm_handlers/html5.php!delete_ilm(.): path_w=ilm/iHanoi/1.0.20240815
  732. // - folder_to_remove=ilm/iHanoi/
  733. // Check if the iLM directory is writable
  734. if (!is_writable($iassign_ilm->file_jar)) {
  735. //D echo "ilm_handlers/html5.php!delete_ilm(.): ilm_id=" . $ilm_id . ", ops, file_jar=" . $iassign_ilm->file_jar . " nao permite escrita<br/>\n"; //exit; //2025/04/11 leo
  736. return null;
  737. }
  738. self::delete_dir("www", $folder_to_remove); // from WWW of Moodle
  739. $ilm_folder = "ilm" . $ds . $iassign_ilm->name . $ds;
  740. $k = 0;
  741. // Verify if iLM parent directory is empty, if yes, remove it
  742. foreach (glob($ilm_folder . "*", GLOB_ONLYDIR) as $dir) {
  743. $k ++;
  744. break; // it is enough 1 (implies not empty, implies do not remove "ilm/<iLM name>")
  745. }
  746. if ($k == 0) { // parent directory is empty => this is the last iLM version, so also remove the it root ./ilm/<iLM name>
  747. echo "ilm_handlers/html5.php!delete_ilm(.): if k==0, ilm_id=" . $ilm_id . ", k=" . $k . ", delete_dir(" . $ilm_folder . ")<br/>\n"; //exit; //2025/04/11 leo
  748. self::delete_dir("www", $ilm_folder); // from WWW of Moodle
  749. }
  750. else echo "ilm_handlers/html5.php!delete_ilm(.): else k==0, ilm_id=" . $ilm_id . ", k=" . $k . ", delete_dir(" . $ilm_folder . ")<br/>\n"; //exit; //2025/04/11 leo
  751. $DB->delete_records("iassign_ilm", array('id' => $ilm_id));
  752. $DB->delete_records("iassign_ilm_config", array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  753. // log event --------------------------------------------------------------------------------------
  754. iassign_log::add_log('delete_iassign_ilm', 'name: ' . $iassign_ilm->name . ' ' . $iassign_ilm->version, 0, $iassign_ilm->id);
  755. // log event --------------------------------------------------------------------------------------
  756. return $iassign_ilm->parent;
  757. } // public static function delete_ilm($ilm_id)
  758. /// Receive the updated data from an iLM to process it
  759. // @calledby locallib.php: static function edit_ilm($param,$itemid): $typec::edit_ilm($param,$itemid,$files_extract,$contextuser);
  760. public static function edit_ilm ($param, $itemid, $files_extract, $contextuser) {
  761. global $DB, $CFG;
  762. $ds = DIRECTORY_SEPARATOR; // to use short name of directory separator (on Linux is "/")
  763. $iassign_ilm = new stdClass();
  764. $iassign_ilm->name = $param->name;
  765. $iassign_ilm->version = $param->version;
  766. $iassign_ilm->file_jar = $param->file_jar;
  767. $file_jar = null;
  768. if (!is_null($files_extract)) { // if there is a new iLM (from IPZ pachage)
  769. $dir_and_file_jar = self::save_ilm_by_xml("edit", null, $files_extract);
  770. $ilm_final_directory = $dir_and_file_jar[0]; // {iassign_ilm}.file_jar is the first element (not used here)
  771. $file_jar = $dir_and_file_jar[1]; // {iassign_ilm}.file_jar is the second element
  772. if ($file_jar == null) {
  773. return false;
  774. }
  775. $file_jar = str_replace("." . $ds, "", $file_jar) . $ds; // "./"
  776. }
  777. if (is_null($file_jar)) {
  778. // there is not definition to 'file_jar' in the IPZ packege, use eventually something entered by the form
  779. $file_jar = $param->file_jar;
  780. }
  781. $description = json_decode($param->description_lang);
  782. $description->{$param->set_lang} = $param->description;
  783. $updentry = new stdClass();
  784. $updentry->id = $param->id;
  785. $updentry->version = $param->version;
  786. $updentry->url = $param->url;
  787. // $updentry->description = json_encode($description); //R strip_tags(json_encode($description)); // will replace any special character by tags
  788. // $updentry->description = html_entity_decode((String)$param->description_lang); - the same as above
  789. $updentry->description = json_encode($description, JSON_UNESCAPED_UNICODE); // encode using UTF8
  790. $updentry->extension = strtolower($param->extension);
  791. if (!is_null($file_jar)) {
  792. $updentry->file_jar = $file_jar;
  793. }
  794. $updentry->file_class = $param->file_class;
  795. $updentry->width = $param->width;
  796. $updentry->height = $param->height;
  797. $updentry->enable = $param->enable;
  798. $updentry->timemodified = $param->timemodified;
  799. $updentry->evaluate = $param->evaluate;
  800. $updentry->reevaluate = $param->reevaluate; // reevaluate = 1 => allows to the teacher calls method to re-evaluate (does it in batch, to all students)
  801. $updentry->editingbehavior = $param->editingbehavior; // editingbehavior = 1 => iLM auto-evaluation remains working over a solution sent by the student (iGeom does not work, iVProg does)
  802. $updentry->submissionbehavior = $param->submissionbehavior; // 0 => iLM has no submition button, use the form one; 1 => it has, use it
  803. //D echo "./ilm_handlers/html5.php: edit_ilm(...): file_jar=$file_jar, file_class=" . $updentry->file_class . "<br/>"; //D exit;
  804. $DB->update_record("iassign_ilm", $updentry);
  805. // log event --------------------------------------------------------------------------------------
  806. iassign_log::add_log('update_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $param->id);
  807. // log event --------------------------------------------------------------------------------------
  808. } // public static function edit_ilm($param, $itemid, $files_extract, $contextuser)
  809. //2023/05/30
  810. // Removi funcao NAO mais usada: static function new_file_ilm ($itemid, $fs, $contextuser, $contextsystem, $files_ilm)
  811. // Tambem removi homonima de './iassign/locallib.php': static function new_file_ilm ($itemid, $iassign_ilm)
  812. // Para : /home/leo/projetos/iMA/lms/itarefa/novo/removidos/locallib_html_2023_05_30.php
  813. // Help 'new_ilm(.)' to recover {iassign_ilm}.name with no more than 20 characters
  814. public static function get_xml_name ($app_xml) {
  815. if (!isset($app_xml)) return NULL;
  816. $name = (String) $app_xml->name;
  817. if (isset($name)) {
  818. $itens = explode(":", $name); // try "iVProg: something"
  819. $name = str_replace(" ", "_", $itens[0]); // avoid blanks in iLM name (to ./mod/iassign/ilm/<iLM name>)
  820. if (strlen($name)>20) {
  821. $name = substr($name, 0, 20); // {iassign}.name is char(20)
  822. }
  823. }
  824. return $name;
  825. }
  826. /// Add a new iLM using form (and ZIP package)
  827. // Admin user: arrive here trhough admin interface to manage "Plugins | Activity modules" then choosing "Add iLM".
  828. // The admin must fill a form (with all iLM meta-data) and load a ZIP package, then 'save_ilm_by_xml(.)'
  829. // will create the directory ./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>
  830. // and using files and meta-data in 'ilm-application.xml' will insert iLM data into {iassign_ilm} table
  831. // Before reach here, it came from 'locallib.php!new_ilm($itemid)' that is called by 'settings_ilm.php'
  832. // Admin user in iLM manager interface (iassign/settings_ilm.php) clicks on the button "Add iLM".
  833. // After this, "iassign/settings_ilm.php" calls "locallib.php!new_ilm($itemid)" that
  834. // calls this function (to insert new iLM in database and in proper directory)
  835. // @calledby ./mod/iassign/locallib.php!new_ilm($itemid): $retorno = $typec::new_ilm($itemid, $files_extract, $iassign_ilm_instance, $contextuser, $fs);
  836. public static function new_ilm ($itemid, $files_extract, $iassign_ilm_instance, $contextuser, $fs) {
  837. global $DB, $CFG, $USER, $OUTPUT;
  838. $description_str = $iassign_ilm_instance->description; // to {iassign_ilm}.description
  839. // If necessary truncate (String)$application_xml->name, since {iassign_ilm}.name has at most 20 characters!
  840. //TODO: also can use 'iassign_utils::filename_from_iLM_name($iassign_ilm_instance->name, false);'
  841. $ilm_trunc_name = html5::get_xml_name($iassign_ilm_instance); // send all data, standardizer 'get_xml_name($app_xml)' will get the field 'name'
  842. $array_ilm = array("name" => $ilm_trunc_name, "version" => $iassign_ilm_instance->version, "type" => $iassign_ilm_instance->type);
  843. $iassign_ilm = $DB->get_record('iassign_ilm', $array_ilm);
  844. $auxm = ": " . $ilm_trunc_name . ", " . $iassign_ilm_instance->version . ", " . $iassign_ilm_instance->type;
  845. //D //xxxxxxxxxxxxxxxxxxx remover
  846. //D $msg = " * new_ilm(.): files_extract: <br/>";
  847. //D foreach ($files_extract as $key => $value) $msg .= " - diretorio = " . $diretorio . " (" . $key . "," . $value . ")<br/>\n";
  848. //D echo $msg;
  849. //D if ($iassign_ilm) echo "Entrara 1<br/>";
  850. //D else echo "Entrara 2<br/>";
  851. //D exit;
  852. if ($iassign_ilm) { // there is such iLM (same name, version and type!)
  853. foreach ($files_extract as $key => $value) {
  854. $rootfolder = $CFG->dataroot . '/temp/' . $key;
  855. self::delete_dir("data", $rootfolder); // from Moodle data
  856. break;
  857. }
  858. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign') . $auxm, 'notifyproblem')); // There is another iLM with the same version!
  859. return false;
  860. }
  861. else { // if ($iassign_ilm)
  862. //D $msg = " * new_ilm(.): files_extract: <br/>";
  863. //D foreach ($files_extract as $key => $value) $msg .= " - diretorio = " . $diretorio . " (" . $key . "," . $value . ")<br/>\n";
  864. //D echo $msg; exit;
  865. // It creates directory and iLM files on it, will return the iLM path (after iAssign dir.): ./ilm/...
  866. $dir_and_file_jar = self::save_ilm_by_xml("zip", $iassign_ilm_instance, $files_extract);
  867. $ilm_final_directory = $dir_and_file_jar[0]; // {iassign_ilm}.file_jar is the first element (not used here)
  868. $file_jar = $dir_and_file_jar[1]; // {iassign_ilm}.file_jar is the second element
  869. if ($file_jar == null) {
  870. return false;
  871. }
  872. //2022/05/13
  873. //D echo "/var/www/html/saw2021_2/mod/iassign/ilm_handlers/html5.php: file_jar=" . $file_jar . "<br/>\n";
  874. $file_jar = str_replace("./", "", $file_jar);
  875. //D echo "/var/www/html/saw2021_2/mod/iassign/ilm_handlers/html5.php: file_jar=" . $file_jar . "<br/>\n";
  876. if (empty($file_jar)) {
  877. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In new_ilm: file_jar empty, files_extract=" . $files_extract . "<br/>\n";
  878. print_error($msg_error);
  879. //xx print_error('error_add_ilm', 'iassign');
  880. //print("New file = " . file_jar . "<br/>");
  881. }
  882. else { // if (empty($file_jar))
  883. //TODO {iassign_ilm}.name has at most 20 characters!
  884. // $ilm_trunc_name = (String) $iassign_ilm_instance->name;
  885. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => $ilm_trunc_name));
  886. if (!$iassign_ilm) {
  887. $iassign_ilm = new stdClass(); //MOOC 2016
  888. $iassign_ilm->id = 0;
  889. }
  890. $newentry = new stdClass();
  891. $newentry->name = $ilm_trunc_name; // html5::get_xml_name($iassign_ilm_instance); // be sure {iassign_ilm}.name is up to char(20)
  892. $newentry->version = (String) $iassign_ilm_instance->version;
  893. $newentry->type = (String) $iassign_ilm_instance->type;
  894. $newentry->url = (String) $iassign_ilm_instance->url;
  895. $newentry->description = strip_tags($description_str);
  896. $newentry->extension = strtolower((String) $iassign_ilm_instance->extension);
  897. $newentry->file_jar = $file_jar . "/"; //DEBUG need to use DIRECTORY_SEPARATOR (instead "/")?
  898. $newentry->file_class = (String) $iassign_ilm_instance->file_class;
  899. $newentry->width = (String) $iassign_ilm_instance->width;
  900. $newentry->height = (String) $iassign_ilm_instance->height;
  901. $newentry->enable = 0;
  902. $newentry->timemodified = time();
  903. $newentry->author = $USER->id;
  904. $newentry->timecreated = time();
  905. $newentry->evaluate = (String) $iassign_ilm_instance->evaluate;
  906. $newentry->reevaluate = (String) $iassign_ilm_instance->reevaluate;
  907. $newentry->parent = $iassign_ilm->id;
  908. $newentry->editingbehavior = (String) $iassign_ilm_instance->editingbehavior;
  909. $newentry->submissionbehavior = (String) $iassign_ilm_instance->submissionbehavior; // 0 => iLM has no submition button, use the form one; 1 => it has, use it
  910. $newentry->action_buttons = (String) $iassign_ilm_instance->action_buttons; // {iassign_ilm}.action_buttons==1 <=> do not use the submition button of the form
  911. $newentry->id = $DB->insert_record("iassign_ilm", $newentry); //leo voltar
  912. // log event --------------------------------------------------------------------------------------
  913. iassign_log::add_log('add_iassign_ilm', 'name: ' . $newentry->name . ' ' . $newentry->version, 0, $newentry->id); //2023/05/08 //leo voltar
  914. // log event --------------------------------------------------------------------------------------
  915. if ($iassign_ilm_instance->params->param) {
  916. foreach ($iassign_ilm_instance->params->param as $value) {
  917. $newentry = new stdClass();
  918. $newentry->iassign_ilmid = $iassign_ilmid;
  919. $newentry->param_type = (String) $value->type;
  920. $newentry->param_name = (String) $value->name;
  921. $newentry->param_value = (String) $value->value;
  922. $newentry->description = html_entity_decode((String) $value->description);
  923. $newentry->visible = (String) $value->visible;
  924. $newentry->id = $DB->insert_record("iassign_ilm", $newentry); //2023/05/08 //leo voltar
  925. if (!$newentry->id) {
  926. print_error('error_add_param', 'iassign');
  927. }
  928. }
  929. } // if ($iassign_ilm_instance->params->param)
  930. } // else // if (empty($file_jar))
  931. } // else // if ($iassign_ilm)
  932. $resp = $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  933. //exit; //xxxxxxxxxxxxxxxxxxx remover
  934. return true;
  935. } // public static function new_ilm($itemid, $files_extract, $iassign_ilm_instance, $contextuser, $fs)
  936. // Adjust the cases in which 'ilm-application.xml' presents 'file_jar' starting with './' and does not have '/' in its final
  937. // @return $file_jar cleaned and with '/' final
  938. public static function adjust_File_jar ($file_jar) {
  939. $file_jar = trim($file_jar); // strip whitespace (or other characters) from the beginning and end of a string
  940. $file_jar = str_replace("./", "", $file_jar); // do not use tag 'file_jar' from 'ilm-application.xml', use actual directory name
  941. $size_file_jar = strlen($file_jar);
  942. if ($size_file_jar>0) {
  943. if ($file_jar[0] == '/') {
  944. $file_jar = substr($file_jar, 1, $size_file_jar-1); // erase initial '/'
  945. $size_file_jar--;
  946. }
  947. if ($file_jar[$size_file_jar-1] != "/") // does not end with "/" => insert it
  948. $file_jar .= '/'; // add final '/'
  949. }
  950. return $file_jar;
  951. }
  952. /// Import a iLM from IPZ package
  953. // Admin user: arrive here trhough admin interface to manage "Plugins | Activity modules" then choosing "Import iLM".
  954. // The admin must load the IPZ package, then 'save_ilm_by_xml(.)' will create the directory ./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>
  955. // and using files and meta-data in 'ilm-application.xml' will insert iLM data into {iassign_ilm} table
  956. // @calledby locallib.php!import_ilm($itemid)
  957. public static function import_ilm ($itemid, $files_extract, $application_xml, $contextuser, $fs) {
  958. global $DB, $CFG, $USER, $OUTPUT;
  959. $ds = DIRECTORY_SEPARATOR; // to use short name of directory separator (on Linux is "/")
  960. $description_str = str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML());
  961. //D- echo "html5.php!import_ilm(...): application_xml=|"; print_r($application_xml); echo "|<br/>\n"; //exit;
  962. //TODO {iassign_ilm}.name has at most 20 characters!
  963. $iassign_ilm = $DB->get_record('iassign_ilm', array("name" => (String)$application_xml->name, "version" => (String)$application_xml->version));
  964. if ($iassign_ilm) { // There is (at least) one iLM with this 'name' and this 'version'
  965. foreach ($files_extract as $key0 => $value0) {
  966. //D- echo " self::delete_dir - " . $key0 . " , " . $value0. "<br/>\n";
  967. $rootfolder = $CFG->dataroot . $ds . 'temp' . $ds . $key0;
  968. self::delete_dir("data", $rootfolder); // from Moodle data
  969. break;
  970. }
  971. unlink($CFG->dataroot . $ds . 'temp' . $ds . "ilm-application.xml"); // also remove the "ilm-application.xml" file
  972. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  973. }
  974. else {
  975. // This explode IPZ package coping all of its file/directories to ./mod/iassign/ilm/<iLM name>/<iLM version>/<base_name>
  976. // The <base_name> will be deduced from IPZ package, it must be the directory name containing files, i.e.
  977. // the directory at the same level of 'ilm-application.xml' descriptor file.
  978. // ATENTION: the tag 'file_jar' must coincide <base_name>, i.e. descriptor must have "<file_jar>base_name</file_jar>"
  979. $dir_and_file_jar = self::save_ilm_by_xml("ipz", $application_xml, $files_extract); // really creates iLM directory under ilm/<iLM name>/<iLM version>/<file_jar>
  980. $ilm_final_directory = $dir_and_file_jar[0]; // {iassign_ilm}.file_jar is the first element (not used here)
  981. $file_jar = $dir_and_file_jar[1]; // {iassign_ilm}.file_jar is the second element
  982. echo "dir_and_file_jar : ilm_final_directory="; print_r($ilm_final_directory); echo "<br/>file_jar="; print_r($file_jar); //exit;
  983. if (!is_dir($file_jar)) { // If the admin does not includes "ilm/<iLM name>" add it to the $file_jar (since this path is necessary)
  984. // $items = explode("/", $ilm_final_directory); // get [<iLM name>, <iLM version>]
  985. // $iLMname = $items[0];
  986. $file_jar = "ilm" . $ds . $ilm_final_directory . $ds . $file_jar;
  987. echo " - completado file_jar=" . $file_jar. "<br/>\n"; //2025/04/11 leo
  988. }
  989. // ilm_final_directory=iHanoi/1.0.20240815, file_jar=ihanoi/
  990. if ($file_jar == null) {
  991. exit; // this allow to keep the error message into the interface
  992. // return false;
  993. }
  994. // If necessary truncate (String)$application_xml->name, since {iassign_ilm}.name has at most 20 characters!
  995. $ilm_xml_name = html5::get_xml_name($application_xml);
  996. // Adjust the cases in which 'ilm-application.xml' presents 'file_jar' starting with './' and does not have '/' in its final
  997. // $file_jar = html5::adjust_File_jar($file_jar); // do not use tag 'file_jar' from 'ilm-application.xml', use actual directory name
  998. if (empty($file_jar)) {
  999. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In import_ilm: file_jar empty, files_extract=" . $files_extract . "<br/>\n";
  1000. print_error($msg_error); //xx print_error('error_add_ilm', 'iassign'); //D print("Import file = " . file_jar . "<br/>");
  1001. }
  1002. else { // if (empty($file_jar))
  1003. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => $ilm_xml_name));
  1004. if (!$iassign_ilm) { // New iLM (it does not have any parent)
  1005. $iassign_ilm = new stdClass();
  1006. $iassign_ilm->id = 0;
  1007. }
  1008. //D- echo " - html5.php!import_ilm(.): ilm_xml_name=" . $ilm_xml_name . ", parent = iassign_ilm->id = " . $iassign_ilm->id . "<br/>\n"; //xxxxxxxxxx //2023/05/08 //leo remover
  1009. $newentry = new stdClass();
  1010. $newentry->name = $ilm_xml_name;
  1011. $newentry->version = (String) $application_xml->version;
  1012. $newentry->type = (String) $application_xml->type;
  1013. $newentry->url = (String) $application_xml->url;
  1014. $newentry->description = strip_tags($description_str);
  1015. $newentry->extension = strtolower((String) $application_xml->extension);
  1016. $newentry->file_jar = $file_jar; // it must ends with '/' - see 'adjust_File_jar(.)'
  1017. $newentry->file_class = (String) $application_xml->file_class;
  1018. $newentry->width = (String) $application_xml->width;
  1019. $newentry->height = (String) $application_xml->height;
  1020. $newentry->enable = 0;
  1021. $newentry->timemodified = time();
  1022. $newentry->author = $USER->id;
  1023. $newentry->timecreated = time();
  1024. $newentry->evaluate = (String) $application_xml->evaluate;
  1025. $newentry->reevaluate = (String) $application_xml->reevaluate;
  1026. $newentry->editingbehavior = (String) $application_xml->editingbehavior;
  1027. $newentry->submissionbehavior = (String) $application_xml->submissionbehavior; // 0 => iLM has no submition button, use the form one; 1 => it has, use it
  1028. $newentry->action_buttons = (String) $application_xml->action_buttons; // {iassign_ilm}.action_buttons==1 <=> do not use the submition button of the form
  1029. $newentry->parent = $iassign_ilm->id;
  1030. $iassign_ilmid = $DB->insert_record("iassign_ilm", $newentry); // insert in {iassign_ilm} table
  1031. if ($application_xml->params->param) {
  1032. foreach ($application_xml->params->param as $value) {
  1033. $newentry = new stdClass();
  1034. $newentry->iassign_ilmid = $iassign_ilmid;
  1035. $newentry->param_type = (String) $value->type;
  1036. $newentry->param_name = (String) $value->name;
  1037. $newentry->param_value = (String) $value->value;
  1038. $newentry->description = html_entity_decode((String) $value->description);
  1039. $newentry->visible = (String) $value->visible;
  1040. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  1041. if (!$newentry->id) {
  1042. print_error('error_add_param', 'iassign');
  1043. }
  1044. }
  1045. } // if ($application_xml->params->param)
  1046. } // else if (empty($file_jar))
  1047. print $OUTPUT->notification(get_string('ok_import_ilm_version', 'iassign'), 'notifysuccess'); // The iLM was successfully imported!
  1048. }
  1049. $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  1050. //D- echo " - import_ilm(.): newentry ="; print_r($newentry); echo "<br/>\n"; //xxxxxxxxxx //2023/05/08 //leo remover
  1051. } // public static function import_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs)
  1052. /// Function for register iLM from XML descriptor (the 'ilm-application.xml' file in IPZ package)
  1053. // It creates the correspondent directory (the insertion in {iassin_ilm} table is under 'import_ilm(.)' responsability)
  1054. // @see : self::copy_new_version_ilm($param,$files_extract); edit_ilm($param,$itemid,$files_extract,$contextuser); new_ilm($itemid,$files_extract,$application_xml,$contextuser,$fs); import_ilm($itemid,$files_extract,$application_xml,$contextuser,$fs);
  1055. // @calledby : self::new_ilm($itemid,$files_extract,$application_xml,$contextuser,$fs) : $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
  1056. // @param string $str_from Define the event origin ("copy", "edit", "zip" or "ipz")
  1057. // @param array $application_xml Data of XML descriptor (with all {iassign_ilm} field and something more)
  1058. // @param array $files_extract Filenames of extract files
  1059. // @return string with the iLM path (since ./ilm/...)
  1060. static function save_ilm_by_xml ($str_from, $application_xml, $files_extract) {
  1061. global $CFG, $USER, $OUTPUT;
  1062. $ds = DIRECTORY_SEPARATOR; // to short name (to directory separator, on Linux "/")
  1063. $source = "";
  1064. $temp_directory = "";
  1065. //D echo "html5.php!save_ilm_by_xml(.): " .$str_from . "<br/>";
  1066. $html_header = "";
  1067. // ./lib/outputrenderers.php: public function notification($message, $type = null, $closebutton = true)
  1068. $html_header_end = "</html></body>"; // in case of erro, to close HTML format window frame (to be used with '$OUTPUT->notification(.)"
  1069. // Check if the iLM directory is writable: ./mod/iassign/ilm
  1070. if (!is_writable("ilm")) { // is writable the directory "/moodle/mod/iassign/ilm/"?
  1071. // Error: There are no permission to modify files in iLM directory. Please, you need to verify iLM directory permissions (Moodle admin prerogative)!
  1072. $err_msg = $html_header . get_string('error_folder_permission_denied', 'iassign') . "<br/>\n";
  1073. $err_msg .= get_string('error_folder_permission_dir', 'iassign') . " ./mod/iassign/ilm"; // Directory:
  1074. print $OUTPUT->header() . $OUTPUT->heading(get_string('error_folder_permission_create', 'iassign')); // "iLM creation error: directory without permission"
  1075. print $OUTPUT->notification($err_msg, 'notifyproblem') . $html_header_end;
  1076. exit; // ensure do not follow processing...
  1077. }
  1078. // The filter 'filename_from_iLM_name(...)' is essential to clear the name for directory from the iLM original name,
  1079. // it is used to define the field {iassign_ilm}.name has at most 20 characters!
  1080. // ./mod/iassign/locallib.php!(class iassign_utils) filename_from_iLM_name($ilm_name,$is_lowercase=true): remove special char; find initial name
  1081. $application_file_name = iassign_utils::filename_from_iLM_name($application_xml->name, false); //TODO: also can use html5::get_xml_name($application_xml);
  1082. //D //2025/04/09 echo "html5.php!save_ilm_by_xml(.): application_file_name=" . $application_file_name . ", str_from=" . $str_from . "<br/>"; //exit;
  1083. if ($str_from == "ipz") { // come from "ilm-application.xml" (IPZ)
  1084. // From XML arrives only one directory (iLM base), we must complete the path to create iLM directory under WWW
  1085. // Apply the same filter to avoid special character in the internal directory name {iassign_ilm}.file_jar
  1086. // We need to produze {iassign_ilm}.file_jar with a complete path (e.g., to iFraction: "ilm/iFractions/3.1.0/ifractions/")
  1087. $file_jar = iassign_utils::filename_from_iLM_name($application_xml->file_jar, false); // in "locallib.php": static function filename_from_iLM_name($ilm_name, $is_lowercase = true)
  1088. // iLM directory under which the new version will be created: the final directory will also have the suffix given by $file_jar
  1089. $ilm_base_directory = "ilm" . $ds . $application_file_name; // ./mod/iassign/ilm/<iLM name>
  1090. $ilm_directory_version = $ilm_base_directory . $ds . $application_xml->version; // ./mod/iassign/ilm/<iLM name>/<iLM version>
  1091. if ($CFG->debugdisplay) { // debug messages is turned on
  1092. print "ilm_handlers/html5.php!save_ilm_by_xml(.): file_jar=" . $file_jar . ", ilm_base_directory=" . $ilm_base_directory . ", version=" . $application_xml->version . "<br/>\n"; //DEBUG
  1093. }
  1094. // Final directory of the new iLM: in ./mod/iassign/ilm/
  1095. // $ilm_final_directory = "ilm" . $ds . $application_file_name . $ds . $application_xml->version; // ilm/<iLM name>/<iLM version>
  1096. // $ilm_final_directory .= $ds . $file_jar; // ilm/<iLM name>/<iLM version>/<base_name>/
  1097. //2025/04/09 echo "html5.php!save_ilm_by_xml(.): file_jar=" . $file_jar . "<br/>"; //
  1098. $ilm_final_directory = $file_jar; // ilm/<iLM name>/<iLM version>/<base_name>/
  1099. //2025/04/09 static function save_ilm_by_xml($str_from, $application_xml, $files_extract)
  1100. $sizedir = strlen($ilm_final_directory);
  1101. if ($sizedir>0 && $ilm_final_directory[$sizedir-1] == $ds) // remove last "/"
  1102. $ilm_final_directory = substr($ilm_final_directory, 0, $sizedir-1);
  1103. }
  1104. else { // come from form (ZIP) or "ilm-application.xml" (IPZ)
  1105. //2025/04/09 echo "html5.php!save_ilm_by_xml(.): opa, NAO esperava chegar nesse ponto!<br/>"; //exit;
  1106. $file_jar = $application_xml->file_jar;
  1107. $ilm_base_directory = "ilm" . $ds . $application_file_name; // ./mod/iassign/ilm/<iLM name>
  1108. $ilm_directory_version = $ilm_base_directory . $ds . $application_xml->version; // ilm/<iLM name>/<iLM version>
  1109. // Final directory of the new iLM: in ./mod/iassign/ilm/
  1110. // $ilm_final_directory = $ilm_directory_version . $ds . $application_xml->version; // ilm/<iLM name>/<iLM version>
  1111. $ilm_final_directory = $ilm_directory_version . $ds . $application_file_name; // ilm/<iLM name>/<iLM version>/<base_name>/
  1112. //D echo "file_jar=" . $file_jar . ", ilm_directory=" . $ilm_base_directory . ", ilm_directory_version=" . $ilm_directory_version . "<br/>";
  1113. //D echo "ilm_final_directory=" . $ilm_final_directory . "<br/>";
  1114. }
  1115. //D echo "save_ilm_by_xml(.): file_jar=" . $file_jar . ", ilm_directory=" . $ilm_base_directory . ", ilm_directory_version=" . $ilm_directory_version . "<br/>"; //exit;
  1116. // Moodle use "temp/" directory in MoodleData area to keep the IPZ package sent
  1117. $source_in_moodledata = null; // to get the directory on the IPZ file (on MoodleData)
  1118. $source_in_moodledata_root = $CFG->dataroot . $ds . "temp"; // to get the "ilm-application.xml" file (to remotion at the end)
  1119. // Package files been sending is under MoodleData: MoodleData/temp/<entry>
  1120. // Find the initial directory to be used as {iassign_ilm}.file_jar: looking at each <entry> em IPZ package
  1121. // ATTENTION: the ZIP or IPZ package must have all of its files inside a single directory, to be recovery bellow
  1122. $countDir = 0; // must be a single
  1123. $error = 0; // if finished with positive implies error
  1124. $actual_file_jar = ""; // base directory with all iLM files - ideally must be equals to above $application_file_name
  1125. $second_file_jar = ""; // if was detect more than one directory at the root level of package
  1126. $temp_files = array(); // if package has not a single directory this will help to remove all the wrong files
  1127. $temp_dir = array(); // if package has not a single directory this will help to remove all the wrong directories
  1128. foreach ($files_extract as $key1 => $value1) {
  1129. $file_name = $CFG->dataroot . $ds . "temp" . $ds . $key1; // look at MoodleData/temp/<entry>
  1130. if (is_dir($file_name)) { // Is it a directory? (stop with the first directory)
  1131. if ($source_in_moodledata == null) { // not yet defined, use the first directory
  1132. $items = explode($ds, $key1);
  1133. if (count($items)>0) {
  1134. $actual_file_jar = $items[0];
  1135. $source_in_moodledata = $CFG->dataroot . $ds . "temp" . $ds . $items[0]; // to avoid the first be a sub-directory...
  1136. $countDir++;
  1137. }
  1138. else {
  1139. $actual_file_jar = $key1;
  1140. $source_in_moodledata = $file_name; // to avoid the first be a sub-directory...
  1141. }
  1142. $temp_dir[] = $source_in_moodledata; // if package has not a single directory this will help to remove all the wrong directories
  1143. }
  1144. else { // already defined "initial" file in $source_in_moodledata
  1145. $items = explode($ds, $key1);
  1146. $newdir = $items[0]; // eventually is sub-directory of $source_in_moodledata (which is allowed)
  1147. $newdir_complete = $CFG->dataroot . $ds . "temp" . $ds . $newdir;
  1148. if (!in_array($newdir_complete, $temp_dir)) // new dir to the list of directories to be remove
  1149. $temp_dir[] = $newdir_complete; // if package has not a single directory this will help to remove all the wrong directories
  1150. if ($newdir != $actual_file_jar) { // ups, other main directory!
  1151. $countDir++;
  1152. $second_file_jar = $newdir;
  1153. $error = 1; // more than one directory inside package: $newdir, $actual_file_jar
  1154. }
  1155. }
  1156. } // if (is_dir($file_name))
  1157. else { // is file
  1158. $items = explode($ds, $key1); // eventually is sub-directory of $source_in_moodledata (which is allowed)
  1159. if (count($items)>1) // it is allowed (files under the root level directory)
  1160. continue;
  1161. $filename = $items[0]; // get the first
  1162. $temp_files[] = $CFG->dataroot . $ds . "temp" . $ds . $filename; // if package has not a single directory this will help to remove all the wrong files
  1163. if ($filename != "ilm-application.xml") {
  1164. $error = 2; // error, all files (but 'ilm-application.xml') must be under a single directory!
  1165. //D $items = explode($ds, $key1); echo " +++ key1=" . $key1 . " count=" . count($items) . "<br/>"; if (count($items)==1)
  1166. }
  1167. }
  1168. } // foreach
  1169. if ($error>0) {
  1170. if ($CFG->debugdisplay) { // debug messages is turned on
  1171. print "ilm_handlers/html5.php!save_ilm_by_xml(.): Error more files at the root level?<br/> - actual_file_jar=" . $actual_file_jar . ", second_file_jar=" . $second_file_jar . "<br/>temp_dir=\n"; //DEBUG
  1172. print_r($temp_dir); print "<br/>";
  1173. }
  1174. $param = new stdClass(); $param->dir1 = $actual_file_jar; $param->dir2 = $second_file_jar;
  1175. $msg1 = get_string('error_zip_ipz_single_directory', 'iassign'); // Error! The ZIP or IPZ package must have all of its files inside a single directory.
  1176. $msg2 = get_string('error_zip_ipz_more_directories', 'iassign', $param); // Was found at least two sub-directories inside the package: "{$a}" and "{$b}"
  1177. $msg3 = get_string('error_zip_ipz_file_in_pack', 'iassign'); // Was found at least one file (other than "ilm-application.xml") at the root level of the package:
  1178. $err_msg = $html_header . $msg1 . "<br/>\n";
  1179. if ($error == 1) {
  1180. $err_msg .= $msg2; // "Was found at least two sub-directories inside the package: {$actual_file_jar} and {$key1}
  1181. }
  1182. else { // other file than 'ilm-application.xml' inside the package at root level
  1183. $err_msg .= $msg3 . ' ' . $key1; // Was found at least one file (other than 'ilm-application.xml') at the root level of the package:
  1184. }
  1185. foreach ($temp_files as $one_file) { // $temp_files // if package has not a single directory this will help to remove all the wrong files
  1186. //D echo $one_file . "<br/>";
  1187. unlink($one_file); // $source_in_moodledata_root . $ds . $one_file
  1188. }
  1189. foreach ($temp_dir as $one_dir) { // $temp_files // if package has not a single directory this will help to remove all the wrong directories
  1190. //D echo $one_dir . "<br/>";
  1191. self::delete_dir("data", $one_dir); // try to remove source directories from MoodleData - $source_in_moodledata_root . $ds . $one_dir
  1192. }
  1193. self::delete_dir("data", $CFG->dataroot . $ds . "temp" . $ds . "filestorage"); // remove auxiliary Moodle directory (created during the process)
  1194. print $OUTPUT->header() . $OUTPUT->heading(get_string('error_zip_ipz', 'iassign')); // "iLM creation error: fail in package ZIP or IPZ
  1195. print $OUTPUT->notification($err_msg, 'notifyproblem') . $html_header_end;
  1196. exit; // ensure do not follow processing...
  1197. }
  1198. //D echo "html5.php!save_ilm_by_xml(.): (1) ilm_directory=" . $ilm_base_directory . "<br/>"; // html5.php!save_ilm_by_xml(.): (1) ilm_directory=ilm/iHanoi
  1199. // Check if iLM root directory already exists
  1200. if (!file_exists($ilm_base_directory)) { // If the root iLM directory does exist, create "./mod/iassign/ilm/<LM name>"
  1201. //D echo "html5.php!save_ilm_by_xml(.): (2) ilm_directory=" . $ilm_base_directory . "<br/>";
  1202. if (!is_writable("ilm")) { // can be created "./mod/iassign/ilm/<iLM name>" in "./mod/iassign/ilm/"?
  1203. //D echo "html5.php!save_ilm_by_xml(.): (3) is_writable(ilm)<br/>";
  1204. // Error: There are no permission to modify files in iLM directory. Please, you need to verify iLM directory permissions (Moodle admin prerogative)!
  1205. $err_msg = $html_header . get_string('error_folder_permission_denied', 'iassign') . "<br/>\n";
  1206. $err_msg .= get_string('error_folder_permission_ilmdir', 'iassign') . " " . $ilm_base_directory; // iLM directory:
  1207. print $OUTPUT->header() . $OUTPUT->heading(get_string('error_folder_permission_create', 'iassign')); // "iLM creation error: directory without permission"
  1208. print $OUTPUT->notification($err_msg, 'notifyproblem') . $html_header_end;
  1209. exit; // ensure do not follow processing...
  1210. }
  1211. mkdir($ilm_base_directory, 0755, true); // create directory - 755 = (111)(101)(101) = rwxr-xr-x
  1212. if (!file_exists($ilm_base_directory . $ds . "index.html")) // if does not already exist "index.html" create an empty one
  1213. touch($ilm_base_directory . $ds . "index.html");
  1214. //D echo "html5.php!save_ilm_by_xml(.): (4) mkdir " . $ilm_base_directory . "<br/>";
  1215. }
  1216. // Check if does note already exists the directory to this iLM version
  1217. if (!file_exists($ilm_directory_version)) {
  1218. if (!is_writable($ilm_base_directory)) { // ./mod/iassign/ilm/<iLM name>: can not create ./mod/iassign/ilm/<iLM name>/<iLM version>
  1219. $err_msg = $html_header . get_string('error_folder_permission_denied', 'iassign') . "<br/>\n"; // Error: There are no permission to modify files in iLM directory. Please,...
  1220. $err_msg .= get_string('error_folder_permission_ilmver', 'iassign') . " " . $ilm_directory_version; // "iLM version directory: "
  1221. print $OUTPUT->header() . $OUTPUT->heading(get_string('error_folder_permission_create', 'iassign')); // "iLM creation error: directory without permission"
  1222. print $OUTPUT->notification($err_msg, 'notifyproblem') . $html_header_end;
  1223. return [$ilm_directory_version, null]; // null will indicate to 'import_ilm(.)' an error (avoid to write in table {iassign_ilm})
  1224. exit; // ensure do not follow processing...
  1225. }
  1226. mkdir($ilm_directory_version, 0755, true);
  1227. if (!file_exists($ilm_directory_version . $ds . "index.html")) // if does not already exist "index.html" create an empty one
  1228. touch($ilm_directory_version . $ds . "index.html");
  1229. }
  1230. else { // the iLM directory and version already exists
  1231. // $ilm_trunc_name, $iassign_ilm_instance
  1232. $auxm = "<br/>" . get_string('error_import_ilm_version_type', 'iassign'). ": " .
  1233. $application_xml->name . ", " . // {iassign_ilm}.name
  1234. $application_xml->version . ", " . // {iassign_ilm}.version
  1235. "html5"; // {iassign_ilm}.type
  1236. //D echo "save_ilm_by_xml(.): Erro! Diretorio '" . $ilm_directory_version . "' ja existe!<br/>(name,version,type)=" . $auxm;
  1237. $temp_directory = "." . $ds . $ilm_directory_version;
  1238. $err_msg = $html_header . get_string('error_import_ilm_version', 'iassign') . $auxm . "<br/>\n"; // ERROR: this iLM version is already installed
  1239. $err_msg .= get_string('error_folder_permission_ilmver', 'iassign') . " " . $ilm_directory_version; // "iLM version directory: "
  1240. if (!$OUTPUT->has_started())
  1241. print $OUTPUT->header();
  1242. print $OUTPUT->heading(get_string('error_import_ilm_version_exist', 'iassign')); // "There is already a iLM with this name and version"
  1243. print $OUTPUT->notification($err_msg, 'notifyproblem') . $html_header_end;
  1244. exit; // Better stop here! Otherwise the message in 'copy_new_version_ilm(.)' will use empty field "file_jar"
  1245. // return [$temp_directory, null]; // null will indicate to 'import_ilm(.)' an error (avoid to write in table {iassign_ilm})
  1246. }
  1247. // Write from MoodleData to the final directory (in the WWW area): './mod/iassign/ilm/'
  1248. $rec_dir_md = new RecursiveDirectoryIterator($source_in_moodledata, RecursiveDirectoryIterator::SKIP_DOTS);
  1249. $iterator = new RecursiveIteratorIterator($rec_dir_md, RecursiveIteratorIterator::SELF_FIRST);
  1250. $wwwpath = getcwd(); // usually "/var/www/html/moodle/mod/iassign" considering "http://localhost/moodle/mod/iassign"
  1251. $ilm_complete_path_base = $wwwpath . $ds . $ilm_final_directory; // base iLM directory: /mod/iassign/ihanoi
  1252. if ($CFG->debugdisplay) { // debug messages is turned on
  1253. print "ilm_handlers/html5.php!save_ilm_by_xml(.): wwwpath=" . $wwwpath . ", ilm_complete_path_base=" . $ilm_complete_path_base . "<br/>\n"; //DEBUG
  1254. // ilm_handlers/html5.php!save_ilm_by_xml(.): wwwpath=/var/www/html/saw_limpo/mod/iassign, ilm_base=/var/www/html/saw_limpo/mod/iassign/ihanoi
  1255. }
  1256. // Here, above: $application_file_name = iassign_utils::filename_from_iLM_name($application_xml->name, false); //TODO: also can use html5::get_xml_name($application_xml);
  1257. $ilm_final_directory = $application_file_name; // base iLM directory: iHanoi
  1258. // In case iLM has error in its path (perhaps during its creation or by misunderstanding edition)
  1259. if (!file_exists($ilm_complete_path_base)) { // perhaps reach here from IPZ with 'file_jar' with a single name (e.g. file_jar='ihanoi/')
  1260. if ($CFG->debugdisplay) { // debug messages is turned on
  1261. print "ilm_handlers/html5.php!save_ilm_by_xml(.): " . $ilm_complete_path_base . " does not exist! Try to complete<br/>\n"; //DEBUG
  1262. }
  1263. $ilm_complete_path_base = $wwwpath . $ds . "/ilm/" . $application_file_name;
  1264. $ilm_complete_path_base = $wwwpath . $ds . "ilm" . $ds . $application_file_name; // use $ds to "/" on Linux or inverted barr in Win...
  1265. if ($CFG->debugdisplay) { // debug messages is turned on
  1266. print "ilm_handlers/html5.php!save_ilm_by_xml(.): completed path base=" . $ilm_complete_path_base . "<br/>\n"; //DEBUG
  1267. }
  1268. }
  1269. //D echo "html5.php!save_ilm_by_xml(.): (5) wwwpath=" . $wwwpath . ", mkdir ilm_complete_path_base=" . $ilm_complete_path_base . "<br/>"; // html5.php!save_ilm_by_xml(.): (5) wwwpath=/var/www/html/saw_limpo/mod/iassign, mkdir ilm_base=/var/www/html/saw_limpo/mod/iassign//ilm/iHanoi
  1270. //D mkdir ilm_base=/var/www/html/saw_limpo/mod/iassign//ilm/iHanoi
  1271. if (!is_dir($ilm_complete_path_base)) { // create base directory: ./mod/iassign/ilm/<iLM name>
  1272. mkdir($ilm_complete_path_base, 0755, true);
  1273. touch($ilm_complete_path_base . $ds . "index.html"); // if does not appear any other "index"...
  1274. }
  1275. $ilm_path_version_filejar = $ilm_complete_path_base . $ds . $application_xml->version . $ds . $file_jar; // ./mod/iassign/ilm/<iLM name>/<iLM version>
  1276. $ilm_final_directory = $ilm_final_directory . $ds . $application_xml->version; // now base iLM directory: <iLM name>/<version>
  1277. if (!is_dir($ilm_path_version_filejar)) { // create base directory
  1278. //D echo "html5.php!save_ilm_by_xml(.): (6) mkdir ilm_path_version_filejar=" . $ilm_path_version_filejar . "<br/>";
  1279. mkdir($ilm_path_version_filejar, 0755, true); // create directory version: ./mod/iassign/ilm/<iLM name>/<iLM version>
  1280. touch($ilm_path_version_filejar . $ds . "index.html"); // if does not appear any other "index"...
  1281. }
  1282. $err_msg = "";
  1283. //D echo "html5.php: copiar<br/>";
  1284. //D foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source_in_moodledata, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item
  1285. foreach ($iterator as $item) {
  1286. $directory_name = $ilm_path_version_filejar . $ds . $iterator->getSubPathName(); // = $wwwpath . $ds . $ilm_final_directory . $ds . $iterator->getSubPathName();
  1287. if ($item->isDir()) {
  1288. mkdir($directory_name, 0755); // explicitly create with permission 0755 (others only can read and execute)
  1289. //D echo " + mkdir(" . $directory_name . ", 0755)<br/>";
  1290. touch($directory_name . $ds . "index.html"); // it creates empty "index" to avoid users to see all files directly
  1291. }
  1292. else {
  1293. //D echo "html5.php!save_ilm_by_xml(.): (6) copy item para directory_name=" . $directory_name . "<br/>";
  1294. if (!copy($item, $directory_name)) //aqui
  1295. $err_msg .= "Error copy " . $item . " to " . $directory_name . "<br/>\n";
  1296. else { // Change permission: everything for owner, read and execute for others
  1297. chmod($directory_name, 0755); // (0)(111)(101)(101)
  1298. //D echo " + chmod(" . $directory_name . ", 0755)<br/>";
  1299. }
  1300. }
  1301. }
  1302. //D if ($err_msg!="") echo " + Erros:<br/>" . $err_msg;
  1303. self::delete_dir("data", $source_in_moodledata); // remove source file in the MoodleData
  1304. unlink($source_in_moodledata_root . $ds . "ilm-application.xml"); // also remove the "ilm-application.xml" file
  1305. rmdir($source_in_moodledata); // now remove the directory
  1306. //D echo "html5.php!save_ilm_by_xml(.): (7) devolva ilm_final_directory/file_jar, sendo: ilm_final_directory=" . $ilm_final_directory . ", file_jar=" . $file_jar . "<br/>";
  1307. //D // html5.php!save_ilm_by_xml(.): (7) devolva ilm_final_directory/file_jar, sendo: ilm_final_directory=iHanoi, file_jar=ihanoi/
  1308. // return [$ilm_final_directory . $ds, $file_jar];
  1309. return [$ilm_final_directory, $file_jar]; // [<iLM name>, <internal path>]
  1310. } // static function save_ilm_by_xml($str_from, $application_xml, $files_extract)
  1311. /// Function for delete directory where the iLM is allocated.
  1312. // Remove iLM directories under WWW and temporary files of MoodleData (MoodleData/temp/)
  1313. // @calledby save_ilm_by_xml(.): self::delete_dir($source_in_moodledata); // remove source file in the MoodleData
  1314. // @calledby self::delete_dir($source_in_moodledata); // remove source file in the MoodleData
  1315. // @calledby some more
  1316. // @param string $dirPath
  1317. // @throws InvalidArgumentException
  1318. public static function delete_dir ($www_or_data, $dirPath) {
  1319. global $CFG;
  1320. $ds = DIRECTORY_SEPARATOR; // to short name (to directory separator, on Linux "/")
  1321. var_dump(debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); //2025/04/11 leo
  1322. echo "ilm_handlers/html5.php!delete_dir(.): www_or_data=" . $www_or_data . ", original dirPath=" . $dirPath . "<br/>\n"; //2025/04/11 leo
  1323. $dh = opendir($dirPath);
  1324. if (!$dh) {
  1325. echo "ilm_handlers/html5.php!delete_dir(.): dirPath=" . $dirPath . " NAO e' diretorio, esqueca!<br/>\n"; //2025/04/11 leo
  1326. // Error...
  1327. return false;
  1328. }
  1329. // To use 'unlink(.)' needs complete path:
  1330. if ($www_or_data == "www") { // came from here delete_ilm(.), then add path prefix
  1331. $dirPathC = $CFG->dirroot . DIRECTORY_SEPARATOR . "mod" . DIRECTORY_SEPARATOR . "iassign" . DIRECTORY_SEPARATOR . $dirPath;
  1332. if (is_dir($dirPathC)) { // came here with an incomplete path, complete it
  1333. $dirPath = $dirPathC;
  1334. echo "ilm_handlers/html5.php!delete_dir(.): novo dirPath=" . $dirPath . "<br/>\n"; //2025/04/11 leo
  1335. }
  1336. }
  1337. while (($file_name = readdir($dh)) !== false) {
  1338. if (!$file_name || $file_name == ".." || $file_name == ".")
  1339. continue; // ignore empty or subdirectories
  1340. if ($dirPath[strlen($dirPath) - 1] != $ds) // avoid append when it already has final "/" (ou "\")
  1341. $file_name = $dirPath . DIRECTORY_SEPARATOR . $file_name;
  1342. else
  1343. $file_name = $dirPath . $file_name;
  1344. if (is_dir($file_name)) { // recursivelly remove
  1345. self::delete_dir($www_or_data, $file_name);
  1346. $result = rmdir($file_name); // after removed its files, remove the directory
  1347. if ($CFG->debugdisplay) { // debug messages is turned on
  1348. if ($result) print "html5.php!delete_dir(.): after removed its files, remove the directory, file_name=" . $file_name . " successfully removed with 'unlink(file_name)'!<br/>";
  1349. else print "html5.php!delete_dir(.): after removed its files, remove the directory, file_name=" . $file_name . ", occurred an ERROR with 'unlink(file_name)'!<br/>";
  1350. }
  1351. }
  1352. else {
  1353. // ilm_handlers/html5.php!save_ilm_by_xml(.): www_or_data=www, original dirPath=ilm/iHanoi/
  1354. // ilm_handlers/html5.php!save_ilm_by_xml(.): novo dirPath=/var/www/html/saw_limpo/mod/iassign/ilm/iHanoi/
  1355. // ilm_handlers/html5.php!save_ilm_by_xml(.): www_or_data=www, original dirPath=/var/www/html/saw_limpo/mod/iassign/ilm/iHanoi/1.0.20200803
  1356. // ilm_handlers/html5.php!save_ilm_by_xml(.): novo dirPath=/var/www/html/saw_limpo/mod/iassign//var/www/html/saw_limpo/mod/iassign/ilm/iHanoi/1.0.20200803
  1357. //
  1358. // /var/www/html/saw_limpo/mod/iassign//var/www/html/saw_limpo/mod/iassign/ilm/iHanoi/1.0.20200803/index.html
  1359. if (!is_dir($file_name) || self::is_empty_dir($file_name)) { // Not directory or is empty directory, then remove it
  1360. $result = unlink($file_name); // remove this file
  1361. //x $result = unlink($file_nameC); // remove this file
  1362. if ($CFG->debugdisplay) { // debug messages is turned on
  1363. // dirPath = ilm/iHanoi/1.0.20230525/ihanoi
  1364. // file_name = ilm/iHanoi/1.0.20230525/ihanoi/index_ihanoi.html
  1365. if ($result) print "html5.php!delete_dir(.): in dirPath=" . $dirPath . ", file_name=" . $file_name . " successfully removed with 'unlink(file_name)'!<br/>";
  1366. else print "html5.php!delete_dir(.): in dirPath=" . $dirPath . ", occurred an ERROR to remove " . $file_name . " with 'unlink(file_name)'!<br/>";
  1367. }
  1368. }
  1369. else echo "ilm_handlers/html5.php!delete_dir(.): file_name=" . $file_name . ", NAO e' diretorio limpo, esqueca<br/>\n";
  1370. }
  1371. } // while (($file_name = readdir($dh)) !== false)
  1372. closedir($dh);
  1373. if (is_dir($dirPath)) {
  1374. if (self::is_empty_dir($file_name)) { // If there is another iLM under this name, do not even try to remove!
  1375. $result = rmdir($dirPath); //TODO Warning: rmdir(/var/data/moodle_data_saw/temp/ivprog-html/js/semantic/): Directory not empty in /var/www/html/saw/...
  1376. // Esta sobrando diretorio no Moodle data! /var/data/saw_limpo/temp/ihanoi/img/*
  1377. //x $result = rmdir($dirPathC);
  1378. if ($CFG->debugdisplay) { // debug messages is turned on
  1379. if ($result) print "html5.php!delete_dir(.): in dirPath=" . $dirPath . " successfully removed with 'unlink(dirPath)'!<br/>\n";
  1380. else print "html5.php!delete_dir(.): occurred an ERROR to remove " . $dirPath . " with 'unlink(dirPath)'!<br/>dirPath=" . $dirPath . "<br/>\n";
  1381. }
  1382. }
  1383. } // if (is_dir($dirPath))
  1384. // if (error) {
  1385. // print($OUTPUT->notification(get_string('error_file_jar_exists', 'iassign'), 'notifyproblem')); // just warning the user
  1386. // //D echo "./mod/iassign/ilm_handlers/html5.php: delete_dir(" . $dirPath . "): " . $error_msg . "<br/>\n";
  1387. // $error_msg = get_string('error_file_jar_exists', 'iassign') . "<br/>\nThere was no directory " . $dirPath . " to be removed";
  1388. // print($OUTPUT->notification($error_msg, 'notifyproblem')); // just warning the user"
  1389. // If the directory does not exist, simply ignore it!
  1390. // }
  1391. } // public static function delete_dir($www_or_data, $dirPath)
  1392. } // class html5 implements ilm_handle
  1393. ?>