html5.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. /**
  3. * Class that implements ilm_handle, in order to allow manipulation and management of HTML5 iLM
  4. *
  5. * @author Igor Moreira Félix
  6. * @author Patricia Alves Rodrigues
  7. * @author Leônidas O. Brandã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. /// Produce HTML code to load iLM
  19. public static function build_ilm_tags ($ilm_id, $options = array()) {
  20. global $DB, $OUTPUT;
  21. global $CONF_WWW; //TODO 1 => use iLM under WWW; otherwise use under MoodleData
  22. $html = "";
  23. if (empty($options['Proposition']))
  24. $options['Proposition'] = "";
  25. if (empty($options['addresPOST']))
  26. $options['addresPOST'] = "";
  27. if (empty($options['student_answer']))
  28. $options['student_answer'] = "";
  29. if (empty($options['notSEND']))
  30. $options['notSEND'] = "";
  31. else // Case it is authoring put 'notSEND' (important to iVProgH5 to present authoring tool)
  32. if ($options['type'] == "editor_update")
  33. $options['notSEND'] = "true";
  34. if (empty($options['id_iLM_security'])) // if defined, it is from 'iassign_security'
  35. $options['id_iLM_security'] = "";
  36. $id_iLM_security = $options['id_iLM_security'];
  37. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  38. if ($iassign_ilm) {
  39. // md_files : filename
  40. $ilm_extension = $iassign_ilm->extension; // use local variavel to efficiency (several use)
  41. if ($ilm_extension) { // avoid problems
  42. $ilm_extension = strtolower($ilm_extension);
  43. }
  44. // Attention: in iAssign 2014 on, all the iLM is located on the Moodle filesystem (usually /var/moodledata/filedir/).
  45. // This means that '$iassign_ilm->file_jar' = '*_files.id'
  46. $file_url = array();
  47. $fs = get_file_storage();
  48. $files_jar = explode(",", $iassign_ilm->file_jar);
  49. $url = $iassign_ilm->file_class; // to HTML5 package, this 'file_class' must have the main HTML file
  50. array_push($file_url, $url);
  51. $lang = substr(current_language(), 0, 2);
  52. if ($options['type'] == "filter") { //leo
  53. $iassign_ilm_width = $options['width']; // or use? $iassign_ilm->width
  54. $iassign_ilm_height = $options['height']; // or use? $iassign_ilm->height
  55. } else { //leo
  56. $iassign_ilm_width = $iassign_ilm->width;
  57. $iassign_ilm_height = $iassign_ilm->height; // or use? $iassign_ilm->height
  58. }
  59. if (!empty($file_url)) { // There is an iLM file
  60. //TODO iLM_HTML5 :: Change to 'object', tag 'applet' was deprecated.
  61. $paramsStr = "?1=1";
  62. switch ($options['type']) {
  63. case "view":
  64. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']); //leo
  65. $paramsStr .= "&iLM_PARAM_SendAnswer=true";
  66. //TODO: REVIEW: this code is to insert iLM as HTML5 and to allow general parameter to any iLM
  67. //TODO: For now, 'iassign_ilm_config' is empty... let comment these lines
  68. //n $iassign_ilm_config = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id));
  69. //n foreach ($iassign_ilm_config as $ilm_config) {
  70. //n if (array_key_exists($ilm_config->param_name, $options)) {
  71. //n $ilm_config->param_value = $options[$ilm_config->param_name];
  72. //n $paramsStr .= "&" . $ilm_config->param_name . "=" . urlencode($ilm_config->param_value);
  73. //n }
  74. //n }
  75. break;
  76. case "filter":
  77. if ($options['toolbar'] == "disable")
  78. $paramsStr .= "&SOH_ADD=ADD";
  79. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  80. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  81. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  82. $paramsStr .= "&iLM_PARAM_ServerToGetAnswerURL=" . urlencode($ilm_config->param_value);
  83. break; // static function build_ilm_tags($ilm_id, $options=array())
  84. case "activity": // build_ilm_tags
  85. //TODO To generalize to any HTML5 iLM, it is necessary to use 'iLM_PARAM_Assignment' and 'iLM_PARAM_SendAnswer'
  86. //TODO iLM_PARAM_Assignment=Proposition ; iLM_PARAM_SendAnswer=notSEND
  87. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  88. // if ($options['special_param'] == 1) { }
  89. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  90. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  91. $paramsStr .= "&iLM_PARAM_ServerToGetAnswerURL=" . urlencode($options['addresPOST']);
  92. //TODO iLM_HTML5 :: To extend to any iLM in HTML5
  93. //TODO iLM_HTML5 :: it will allow to load dynamic parameters
  94. //T $iassign_activity_item_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $options['iassign_statement'] ));
  95. //T if ($iassign_activity_item_configs) {
  96. //T foreach ($iassign_activity_item_configs as $iassign_activity_item_config)
  97. //T $paramsStr .= "&" . $iassign_activity_item_config->param_name . "=" . urlencode($iassign_activity_item_config->param_value);
  98. //T }
  99. break;
  100. case "editor_new":
  101. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  102. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  103. $paramsStr .= "&iLM_PARAM_Authoring=true";
  104. break;
  105. case "editor_update":
  106. $paramsStr .= "&iLM_PARAM_AssignmentURL=true";
  107. $paramsStr .= "&iLM_PARAM_Assignment=" . urlencode($options['Proposition']);
  108. $paramsStr .= "&iLM_PARAM_SendAnswer=" . urlencode($options['notSEND']);
  109. $paramsStr .= "&iLM_PARAM_Authoring=true";
  110. break;
  111. default:
  112. $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.
  113. } // switch($options['type'])
  114. $paramsStr .= "&lang=" . $lang; // get the language defined in Moodle
  115. $parameters = ' style="width: ' . $iassign_ilm_width . 'px; height: ' . $iassign_ilm_height . 'px;" ';
  116. $html .= '<iframe frameborder="0" name="iLM" src="' . $iassign_ilm->file_jar . $iassign_ilm->file_class . $paramsStr . '" ' . $parameters . ' id="iLM">' . "\n";
  117. $html .= '</iframe>' . "\n";
  118. } // if (!empty($file_url))
  119. } // if ($iassign_ilm)
  120. return $html;
  121. } // public static function build_ilm_tags($ilm_id, $options = array())
  122. /// Exibe a atividade no iLM
  123. // @calledby locallib.php : view_iLM(...)
  124. public static function show_activity_in_ilm ($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) {
  125. global $USER, $CFG, $COURSE, $DB, $OUTPUT;
  126. $special_param1 = $iassign_statement_activity_item->special_param1;
  127. $ilm = $DB->get_record('iassign_ilm', array('id' => $iassign_statement_activity_item->iassign_ilmid));
  128. $context = context_module::instance($USER->cm);
  129. //TODO Given an activity => find its correspondent file in Moodle data. Bad solution!
  130. //TODO Change the meaning of 'iassign_statement.file' from insertion order to the ID in table 'files'.
  131. //TODO This demands update to each 'iassign_statement', find its corresponding on in 'files', and update 'iassign_statement.file = files.id'
  132. if ($view_teacherfileversion) { // get the exercise in Moodle data (teacher file)
  133. $fileid = "";
  134. $fs = get_file_storage();
  135. $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement_activity_item->file); // iassign_statement_activity_item = table 'iassign_statement'
  136. if ($files) {
  137. foreach ($files as $value) {
  138. if ($value->get_filename() != '.')
  139. $fileid = $value->get_id();
  140. }
  141. }
  142. if (!$fileid) { // 'Something is wrong. Maybe your teacher withdrew this exercise file. Please, inform your teacher.';
  143. print iassign::warning_message_iassign('error_exercise_removed') . "<br/>\n"; // I couldn't find the file in table 'files'!
  144. }
  145. }
  146. $ilm_name = strtolower($ilm->name);
  147. $extension = iassign_utils::filename_extension($ilm_name);
  148. if ($view_teacherfileversion) { // $view_teacherfileversion==1 => load the exercise ('activity') from the 'moodledata' (id in 'files')
  149. // $content_or_id_from_ilm_security = $this->context->id;
  150. $content_or_id_from_ilm_security = $fileid; // $iassign_statement_activity_item->file;
  151. } else { // $view_teacherfileversion==null => load the learner answer from the data base (iassign_submission)
  152. $content_or_id_from_ilm_security = $student_answer;
  153. }
  154. $allow_submission = false; // There is permission to 'submission' button?
  155. //VERIFICAR ESTE IF
  156. if ($USER->iassignEdit == 1 && $student_answer) { // for now, only iVProg2 and iVProgH5 allows editions of exercise already sent
  157. $allow_submission = true; // yes!
  158. $write_solution = 1;
  159. $enderecoPOST .= "&write_solution=1"; // complement POST address indicating that the learner could send edited solution
  160. }
  161. // Security: this avoid the student get a second access to the file content (usually an exercise)
  162. // Data are registered in the table '*_iassign_security' bellow and is erased by function 'view()' above.
  163. // IMPORTANT: the '$end_file' will receive the iLM content URL using the security filter './mod/iassign/ilm_security.php'
  164. // the iLM must request the content using this URL. Data are registered in the table '*_iassign_security'.
  165. // Attention : using iVProgH5 there are lot of " and the use of slashes (as '\"') will imply in iVProgH5 do not read the file!
  166. // do not use: $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, addslashes($content_or_id_from_ilm_security));
  167. //2017 $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, $content_or_id_from_ilm_security); // insert in 'iassign_security'
  168. //2017 $this->remove_old_iLM_security_entries($USER->id, $iassign_statement_activity_item->id); // additional security: erase eventually old entries
  169. require_once ($CFG->dirroot . '/mod/iassign/ilm_security.php');
  170. $timecreated = time();
  171. $token = md5($timecreated); // iassign_iLM_security->timecreated);
  172. $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'
  173. // $iassign_iLM_security = $DB->get_record("iassign_security", array("id" => $id_iLM_security));
  174. $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $id_iLM_security . '&token=' . $token . '&view=' . $view_teacherfileversion; // need full path...
  175. //
  176. //
  177. $iassign = "
  178. <script type='text/javascript'>
  179. //<![CDATA[
  180. var strAnswer = '';
  181. var evaluationResult = '';
  182. var comment = '';
  183. //alert('./mod/iassign/ilm_handlers/html5.php: 1 window.frames.iLM.getEvaluation=' + window.frames.iLM.getEvaluation); // sem efeito!
  184. function jsAnalyseAnswer () {
  185. // iVProgH5 will call function 'getEvaluationCallback(...)': /var/www/html/ivprogh5/js/services.js
  186. // 'getEvaluation()' calls 'js/services.js : endTest function(index)' that calls 'getEvaluationCallback(apro/100);'
  187. // sumEval = getSummation(); alert('mod/iassign/ilm/ifractions_5/index.html: sumEval = ' + sumEval);
  188. //CUIDADO 2017/11/22 - usar 'window.frames.iLM' resultava neste ponto 'TypeError: window.frames.iLM.getEvaluation is not a function'
  189. //CUIDADO resp = window.frames.iLM.getEvaluation();
  190. resp = window.frames[0].getEvaluation(); // pegar diretamente a primeir janela (nao pode haver outra!)
  191. if (resp == 'undefined') // in './mod/iassign/ilm/ivprog-html/js/services.js'; './mod/iassign/ilm/ivprog-html/main.html'
  192. return false;
  193. return true;
  194. }
  195. // ./mod/iassign/ilm/ivprog-html/js/services.js : call this to define the variable 'evaluationResult'
  196. function getEvaluationCallback (evaluation) {
  197. evaluationResult = evaluation;
  198. //leo 2017/11/22 strAnswer = window.frames.iLM.getAnswer();
  199. strAnswer = window.frames[0].getAnswer();
  200. // alert('getEvaluationCallback(...)' + evaluation + ', strAnswer=' + strAnswer);
  201. comment = document.formEnvio.submission_comment.value;
  202. //leo alert('getEvaluationCallback: enviando evaluationResult=' + evaluation + ', strAnswer=' + strAnswer);
  203. //leo
  204. if ((strAnswer==null || strAnswer=='' || strAnswer==-1) && (comment==null || comment=='')) { // undefined
  205. alert('" . get_string('activity_empty', 'iassign') . "'); // 'Activity sent without content.'
  206. return false; // error...
  207. }
  208. else {
  209. document.formEnvio.iLM_PARAM_ArchiveContent.value = strAnswer;
  210. document.formEnvio.iLM_PARAM_ActivityEvaluation.value = evaluationResult;
  211. //D alert(strAnswer);
  212. document.formEnvio.submit();
  213. return true; // success
  214. }
  215. }
  216. //]]>
  217. </script>\n";
  218. $iassign .= "\n<center>\n<form name='formEnvio' id='formEnvio' method='post' action='$enderecoPOST' enctype='multipart/form-data'>\n";
  219. // Attention: The actual iLM content will be provided by the indirect access in './mod/iassign/ilm_security.php',
  220. // bellow only the 'token' to the content will be shown in URL (by security reason). The iLM must use this URL on
  221. // 'MA_PARAM_Proposition' to request the content.
  222. // Calls static function bellow: parameters are data to store in table '*_iassign_submission'
  223. //leo $iassign .= ilm_settings::build_ilm_tags($this->ilm->id, array(
  224. $iassign .= ilm_settings::build_ilm_tags($ilm->id, array(//leo para testar 'iassign_security'
  225. "type" => "activity",
  226. "notSEND" => "false",
  227. "addresPOST" => $enderecoPOST,
  228. "Proposition" => $end_file,
  229. "special_param" => $special_param1,
  230. "student_answer" => $student_answer,
  231. "id_iLM_security" => $id_iLM_security,
  232. "iassign_statement" => $iassign_statement_activity_item->id // MOOC 2016
  233. ));
  234. if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1) {
  235. $iassign .= " <input type='hidden' name='iLM_PARAM_ArchiveContent' value=''>\n";
  236. $iassign .= " <input type='hidden' name='iLM_PARAM_ActivityEvaluation' value=''>\n";
  237. if (!has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
  238. $iassign .= "<p><textarea rows='2' cols='60' name='submission_comment'></textarea></p>\n";
  239. else
  240. $iassign .= "<input type='hidden' name='submission_comment'>\n";
  241. if ($allow_submission) { // it is not iGeom
  242. $iassign .= "<center>\n<!-- load button -->\n" .
  243. " <input type=button value='" . get_string('submit_iassign', 'iassign') . "' onClick = 'javascript:window.jsAnalyseAnswer();' title='" .
  244. get_string('message_submit_iassign', 'iassign') . "'>\n" . "</center>\n";
  245. } else {
  246. // Works with 'javascript:window.jsAnalyseAnswer()' or simply 'jsAnalyseAnswer()'
  247. $iassign .= "<center>\n<!-- load button -->\n" .
  248. " <input type=button value='" . get_string('submit_iassign', 'iassign') . "' onClick = 'javascript:window.jsAnalyseAnswer();' title='" .
  249. get_string('message_submit_iassign', 'iassign') . "'>\n" . "</center>\n";
  250. }
  251. } // if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1)
  252. $iassign .= "</form></center>\n\n";
  253. return $iassign;
  254. } // public static function show_activity_in_ilm($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion)
  255. /// Presents iLM information
  256. public static function view_ilm ($ilmid, $from) {
  257. global $DB;
  258. $url = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign'));
  259. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilmid));
  260. $str = "";
  261. $str .= '<table id="outlinetable" cellpadding="5" width="100%" >' . "\n";
  262. $str .= '<tr>';
  263. $str .= '<td colspan=3 align=right>';
  264. if ($from != 'admin') {
  265. $str .= '<input type=button value="' . get_string('return', 'iassign') . '" onclick="javascript:window.location = \'' . $_SERVER['HTTP_REFERER'] . '\';">' . "\n";
  266. }
  267. $str .= '<input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close();">';
  268. $str .= '</td>' . "\n";
  269. $str .= '</tr>' . "\n";
  270. if ($iassign_ilm) {
  271. $iassign_statement_activity_item = $DB->get_records('iassign_statement', array("iassign_ilmid" => $iassign_ilm->id));
  272. if ($iassign_statement_activity_item) {
  273. $total = count($iassign_statement_activity_item);
  274. } else {
  275. $total = 0;
  276. }
  277. if ($from == 'admin') {
  278. $str .= '<tr><td colspan=2>' . "\n";
  279. $str .= '<table width="100%" class="generaltable boxaligncenter" >' . "\n";
  280. $str .= '<tr>' . "\n";
  281. $str .= '<td class=\'cell c0 actvity\' ><strong>' . get_string('activities', 'iassign') . ':</strong>&nbsp;' . $total . '</td>' . "\n";
  282. $str .= '<td><strong>' . get_string('url_ilm', 'iassign') . '</strong>&nbsp;<a href="' . $iassign_ilm->url . '">' . $iassign_ilm->url . '</a></td>' . "\n";
  283. $str .= '</tr>' . "\n";
  284. $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";
  285. $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";
  286. $str .= '<td width="50%"><strong>' . get_string('width', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->width;
  287. $str .= '&nbsp;&nbsp;<strong>' . get_string('height', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->height . '</td></tr>' . "\n";
  288. $date_jar = $iassign_ilm->file_jar;
  289. $str .= '<tr><td><strong>' . get_string('file_jar', 'iassign') . ':</strong>&nbsp;' . $date_jar . '</td>' . "\n";
  290. $str .= '<td ><strong>' . get_string('file_class', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->file_class . '</td></tr>' . "\n";
  291. if ($iassign_ilm->evaluate == 1) {
  292. $evaluate = get_string('yes', 'iassign');
  293. } else {
  294. $evaluate = get_string('no', 'iassign');
  295. }
  296. $str .= '<tr><td width="50%"><strong>' . get_string('evaluate', 'iassign') . ':</strong>&nbsp;' . $evaluate . '</td>' . "\n";
  297. if ($iassign_ilm->enable == 1) {
  298. $enable = get_string('yes', 'iassign');
  299. } else {
  300. $enable = get_string('no', 'iassign');
  301. }
  302. $str .= '<td width="50%"><strong>' . get_string('enable', 'iassign') . ':</strong>&nbsp;' . $enable . '</td></tr>' . "\n";
  303. $str .= '<tr>' . "\n";
  304. $str .= '<td width="50%"><strong>' . get_string('file_created', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timecreated) . '</td>' . "\n";
  305. $str .= '<td width="50%"><strong>' . get_string('file_modified', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timemodified) . '</td>' . "\n";
  306. $str .= '</tr>' . "\n";
  307. $user_ilm = $DB->get_record('user', array('id' => $iassign_ilm->author));
  308. if ($user_ilm) {
  309. $str .= '<tr>' . "\n";
  310. $str .= '<td colspan=2><strong>' . get_string('author', 'iassign') . ':</strong>&nbsp;' . $user_ilm->firstname . '&nbsp;' . $user_ilm->lastname . '</td>' . "\n";
  311. $str .= '</tr>' . "\n";
  312. }
  313. $str .= '</table>' . "\n";
  314. $str .= '</td></tr>' . "\n";
  315. }
  316. if (!empty($iassign_ilm->file_jar)) {
  317. //TODO: REVIEW: to be used for parameters of "applet" from DB
  318. $options = array("type" => "view"); //MOOC2014: start
  319. $str .= '<tr class=\'cell c0 actvity\'><td colspan=3 align=center bgcolor="#F5F5F5">' . "\n";
  320. // Second parameter null since 'iassign_security' are not define yet
  321. $str .= ilm_settings::build_ilm_tags($iassign_ilm->id, $options);
  322. //TODO: REVIEW: missing code to manage parameters
  323. //MOOC2014: tem este codigo!
  324. } else {
  325. $str .= '<tr class=\'cell c0 actvity\'>' . "\n";
  326. $str .= '<td colspan=2 align=center>' . get_string('null_file', 'iassign') . '</td>' . "\n";
  327. $str .= '<td align=center><a href="' . $url . '</a></td>' . "\n";
  328. $str .= '</tr>' . "\n";
  329. }
  330. $str .= '</td></tr>' . "\n";
  331. }
  332. $str .= '</table>' . "\n";
  333. return $str;
  334. } // public static function view_ilm($ilmid, $from)
  335. /// Make a copy or produce a new version of an iLM
  336. // @see locallib.php : copy_new_version_ilm($param)
  337. public static function copy_new_version_ilm ($param, $files_extract) {
  338. global $DB, $CFG;
  339. $iassign_ilm = new stdClass();
  340. $iassign_ilm->name = $param->name;
  341. $iassign_ilm->version = $param->version;
  342. $iassign_ilm->file_jar = null;
  343. $application_xml = @simplexml_load_file($CFG->dataroot . '/temp/' . 'ilm-application.xml', null, LIBXML_NOCDATA); //2019
  344. $file_jar = self::save_ilm_by_xml($application_xml, $files_extract); // iLM ṕath (since iAssign)> ./ilm/...
  345. if ($file_jar == null) { // 'save_ilm_by_xml(...)' detected a problem (perhaps no write permission)
  346. return false;
  347. }
  348. $file_jar = str_replace("./", "", $file_jar) . "/";
  349. // From a single text in default language => generate multi-language
  350. //R $description = json_decode($param->description_lang);
  351. //R $description->{$param->set_lang} = $param->description;
  352. $description_json_lang = $param->description_lang;
  353. $newentry = new stdClass();
  354. $newentry->name = $param->name;
  355. $newentry->version = $param->version;
  356. $newentry->type = 'HTML5';
  357. $newentry->url = $param->url;
  358. $newentry->description = $description_json_lang; //R strip_tags(json_encode($description));
  359. $newentry->extension = strtolower($param->extension);
  360. $newentry->file_jar = $file_jar;
  361. $newentry->file_class = $param->file_class;
  362. $newentry->width = $param->width;
  363. $newentry->height = $param->height;
  364. $newentry->enable = 0;
  365. $newentry->timemodified = $param->timemodified;
  366. $newentry->timecreated = $param->timecreated;
  367. $newentry->evaluate = $param->evaluate;
  368. $newentry->author = $param->author;
  369. $newentry->parent = $param->parent;
  370. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  371. // log event --------------------------------------------------------------------------------------
  372. iassign_log::add_log('copy_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $newentry->id);
  373. // log event --------------------------------------------------------------------------------------
  374. } // public static function copy_new_version_ilm($param, $files_extract)
  375. /// Export the iLM to the IPZ package
  376. public static function export_ilm ($ilm_id) {
  377. global $DB, $CFG;
  378. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  379. $iassign_ilm_configs = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  380. $files_jar = $iassign_ilm->file_jar;
  381. $zip_filename = $CFG->dataroot . '/temp/ilm-' . iassign_utils::format_pathname($iassign_ilm->name . '-v' . $iassign_ilm->version) . '_' . date("Ymd-Hi") . '.ipz';
  382. $zip = new zip_archive;
  383. $zip->open($zip_filename);
  384. $rootdir = $CFG->dirroot . '/mod/iassign/' . $files_jar;
  385. $first_folder = str_replace($CFG->dirroot . '/mod/iassign/ilm/', "", $rootdir);
  386. $zip->add_directory($first_folder);
  387. $allfiles = self::list_directory($rootdir);
  388. $i = 0;
  389. foreach ($allfiles as $file) {
  390. $mini = str_replace($CFG->dirroot . '/mod/iassign/ilm/', "", $file);
  391. $mini = str_replace('//', "/", $mini);
  392. if (is_dir($file)) {
  393. $zip->add_directory($mini);
  394. } else {
  395. $zip->add_file_from_pathname($mini, $file);
  396. }
  397. }
  398. $folder = str_replace('ilm/', "", $files_jar);
  399. $application_descriptor = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  400. $application_descriptor .= '<application xmlns="http://line.ime.usp.br/application/1.5">' . "\n";
  401. $application_descriptor .= ' <name>' . $iassign_ilm->name . '</name>' . "\n";
  402. $application_descriptor .= ' <url>' . $iassign_ilm->url . '</url>' . "\n";
  403. $application_descriptor .= ' <version>' . $iassign_ilm->version . '</version>' . "\n";
  404. $application_descriptor .= ' <type>' . $iassign_ilm->type . '</type>' . "\n";
  405. $application_descriptor .= ' <description>' . html_entity_decode(str_replace(array('<p>', '</p>'), array('', ''), $iassign_ilm->description)) . '</description>' . "\n";
  406. $application_descriptor .= ' <extension>' . $iassign_ilm->extension . '</extension>' . "\n";
  407. $application_descriptor .= ' <file_jar>' . $folder . '</file_jar>' . "\n";
  408. $application_descriptor .= ' <file_class>' . $iassign_ilm->file_class . '</file_class>' . "\n";
  409. $application_descriptor .= ' <width>' . $iassign_ilm->width . '</width>' . "\n";
  410. $application_descriptor .= ' <height>' . $iassign_ilm->height . '</height>' . "\n";
  411. $application_descriptor .= ' <evaluate>' . $iassign_ilm->evaluate . '</evaluate>' . "\n";
  412. if ($iassign_ilm_configs) { //MOOC 2016
  413. $application_descriptor .= ' <params>' . "\n";
  414. foreach ($iassign_ilm_configs as $iassign_ilm_config) {
  415. $application_descriptor .= ' <param>' . "\n";
  416. $application_descriptor .= ' <type>' . $iassign_ilm_config->param_type . '</type>' . "\n";
  417. $application_descriptor .= ' <name>' . $iassign_ilm_config->param_name . '</name>' . "\n";
  418. $application_descriptor .= ' <value>' . $iassign_ilm_config->param_value . '</value>' . "\n";
  419. $application_descriptor .= ' <description>' . htmlentities(str_replace("\n", "", $iassign_ilm_config->description)) . '</description>' . "\n";
  420. $application_descriptor .= ' <visible>' . $iassign_ilm_config->visible . '</visible>' . "\n";
  421. $application_descriptor .= ' </param>' . "\n";
  422. }
  423. $application_descriptor .= ' </params>' . "\n";
  424. } //MOOC 2016
  425. $application_descriptor .= '</application>' . "\n";
  426. $zip->add_file_from_string('ilm-application.xml', $application_descriptor);
  427. $zip->close();
  428. header("Pragma: public");
  429. header("Expires: 0");
  430. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  431. header("Cache-Control: private", false);
  432. header("Content-Type: application/zip");
  433. header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
  434. header("Content-Transfer-Encoding: binary");
  435. header("Content-Length: " . (filesize($zip_filename) + 1000));
  436. set_time_limit(0);
  437. readfile($zip_filename) || die("File not found.");
  438. unlink($zip_filename);
  439. exit;
  440. } // public static function export_ilm($ilm_id)
  441. /// Function for list the directory where iLM is allocated.
  442. // @param type $dir
  443. // @return type
  444. static function list_directory ($dir) {
  445. $files = array();
  446. $cont = 0;
  447. $ffs = scandir($dir);
  448. unset($ffs[array_search('.', $ffs, true)]);
  449. unset($ffs[array_search('..', $ffs, true)]);
  450. if (count($ffs) < 1) {
  451. return;
  452. }
  453. foreach ($ffs as $ff) {
  454. $files[$cont] = $dir . "/" . $ff;
  455. $cont++;
  456. if (is_dir($dir . '/' . $ff)) {
  457. $temp = self::list_directory($dir . '/' . $ff);
  458. foreach ($temp as $t) {
  459. $files[$cont] = $t;
  460. $cont++;
  461. }
  462. }
  463. }
  464. return $files;
  465. }
  466. public static function delete_ilm($ilm_id) {
  467. global $DB, $CFG, $OUTPUT;
  468. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  469. // Prepare the path of directory to be removed
  470. $path_w = rtrim($iassign_ilm->file_jar, "/");
  471. $folder_to_remove = substr($path_w, 0, strrpos($path_w, '/') + 1);
  472. // Check if the iLM directory is writable
  473. if (!is_writable($iassign_ilm->file_jar)) {
  474. return null;
  475. }
  476. self::delete_dir($folder_to_remove);
  477. $ilm_folder = "ilm/" . $iassign_ilm->name . "/";
  478. $k = 0;
  479. // Verify if iLM parent directory is empty, if yes, remove it
  480. foreach(glob($ilm_folder . "*", GLOB_ONLYDIR) as $dir) {
  481. $k ++;
  482. break;
  483. }
  484. if ($k == 0) {
  485. self::delete_dir($ilm_folder);
  486. }
  487. $DB->delete_records("iassign_ilm", array('id' => $ilm_id));
  488. $DB->delete_records("iassign_ilm_config", array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  489. // log event --------------------------------------------------------------------------------------
  490. iassign_log::add_log('delete_iassign_ilm', 'name: ' . $iassign_ilm->name . ' ' . $iassign_ilm->version, 0, $iassign_ilm->id);
  491. // log event --------------------------------------------------------------------------------------
  492. return $iassign_ilm->parent;
  493. }
  494. /// Receive the updated data from an iLM to process it
  495. // @calledby locallib.php: static function edit_ilm($param,$itemid): $typec::edit_ilm($param,$itemid,$files_extract,$contextuser);
  496. public static function edit_ilm ($param, $itemid, $files_extract, $contextuser) {
  497. global $DB, $CFG;
  498. $iassign_ilm = new stdClass();
  499. $iassign_ilm->name = $param->name;
  500. $iassign_ilm->version = $param->version;
  501. $iassign_ilm->file_jar = $param->file_jar;
  502. $file_jar = null;
  503. if (!is_null($files_extract)) { // if there is a new iLM (from IPZ pachage)
  504. $file_jar = self::save_ilm_by_xml(null, $files_extract);
  505. if ($file_jar == null) {
  506. return false;
  507. }
  508. $file_jar = str_replace("./", "", $file_jar) . "/";
  509. }
  510. if (is_null($file_jar)) {
  511. // there is not definition to 'file_jar' in the IPZ packege, use eventually something entered by the form
  512. $file_jar = $param->file_jar;
  513. }
  514. $description = json_decode($param->description_lang);
  515. $description->{$param->set_lang} = $param->description;
  516. $updentry = new stdClass();
  517. $updentry->id = $param->id;
  518. $updentry->version = $param->version;
  519. $updentry->url = $param->url;
  520. // $updentry->description = json_encode($description); //R strip_tags(json_encode($description)); // will replace any special character by tags
  521. // $updentry->description = html_entity_decode((String)$param->description_lang); - the same as above
  522. $updentry->description = json_encode($description, JSON_UNESCAPED_UNICODE); // encode using UTF8
  523. $updentry->extension = strtolower($param->extension);
  524. if (!is_null($file_jar)) {
  525. $updentry->file_jar = $file_jar;
  526. }
  527. $updentry->file_class = $param->file_class;
  528. $updentry->width = $param->width;
  529. $updentry->height = $param->height;
  530. $updentry->enable = $param->enable;
  531. $updentry->timemodified = $param->timemodified;
  532. $updentry->evaluate = $param->evaluate;
  533. //D echo "./ilm_handlers/html5.php: edit_ilm(...): file_jar=$file_jar, file_class=" . $updentry->file_class . "<br/>"; //D exit;
  534. $DB->update_record("iassign_ilm", $updentry);
  535. // log event --------------------------------------------------------------------------------------
  536. iassign_log::add_log('update_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $param->id);
  537. // log event --------------------------------------------------------------------------------------
  538. } // public static function edit_ilm($param, $itemid, $files_extract, $contextuser)
  539. /// Function for save iLM file in moodledata
  540. // @param int $itemid Itemid of file save in draft (upload file)
  541. // @param int $ilm_id Id of iLM
  542. // @return string Return an string with ids of iLM files
  543. static function new_file_ilm ($itemid, $fs, $contextuser, $contextsystem, $files_ilm) {
  544. global $CFG, $USER, $DB;
  545. if ($files_ilm) {
  546. foreach ($files_ilm as $value) {
  547. // Check if it is an HTML5 iLM
  548. // Copy:
  549. $destination = 'ilm_debug/' . $value->get_filename();
  550. $value->copy_content_to($destination);
  551. // Extract the content:
  552. $zip = new ZipArchive();
  553. $extracted = './ilm';
  554. $dir = "";
  555. if ($zip->open($destination) === TRUE) {
  556. $dir = './ilm/' . trim($zip->getNameIndex(0));
  557. if (is_dir($dir)) {
  558. $i = 1;
  559. $previous = str_replace("/", "", $zip->getNameIndex(0));
  560. while (file_exists('./ilm/' . $previous . "_" . $i)) {
  561. $i ++;
  562. }
  563. $name = $previous . "_" . $i;
  564. $dir = './ilm/' . $name . "/";
  565. $j = 0;
  566. while ($item_name = $zip->getNameIndex($j)) {
  567. $zip->renameIndex($j, str_replace($previous, $name, $item_name));
  568. $j++;
  569. }
  570. $zip->close();
  571. }
  572. $zip->open($destination);
  573. $zip->extractTo($extracted);
  574. $zip->close();
  575. // After extract, remove from debug:
  576. unlink($destination);
  577. } else { // if ($zip->open($destination) === TRUE)
  578. // After trying to extract, occurring error, erase ZIP file:
  579. unlink($destination);
  580. print_error('error_add_ilm_zip', 'iassign');
  581. }
  582. return $dir;
  583. } // foreach ($files_ilm as $value)
  584. } // if ($files_ilm)
  585. } // static function new_file_ilm($itemid, $fs, $contextuser, $contextsystem, $files_ilm)
  586. /// Register data to the new iLM (in database and in proper directory)
  587. public static function new_ilm ($itemid, $files_extract, $application_xml, $contextuser, $fs) {
  588. global $DB, $CFG, $USER, $OUTPUT;
  589. $description_str = str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML());
  590. $iassign_ilm = $DB->get_record('iassign_ilm', array("name" => (String) $application_xml->name, "version" => (String) $application_xml->version));
  591. if ($iassign_ilm) {
  592. foreach ($files_extract as $key => $value) {
  593. $rootfolder = $CFG->dataroot . '/temp/' . $key;
  594. self::delete_dir($rootfolder);
  595. break;
  596. }
  597. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem')); // There is another iLM with the sama version!
  598. return false;
  599. }
  600. else {
  601. $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
  602. if ($file_jar == null) {
  603. return false;
  604. }
  605. $file_jar = str_replace("./", "", $file_jar);
  606. if (empty($file_jar)) {
  607. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In new_ilm: file_jar empty, files_extract=" . $files_extract . "<br/>\n";
  608. print_error($msg_error);
  609. //xx print_error('error_add_ilm', 'iassign');
  610. //print("New file = " . file_jar . "<br/>");
  611. }
  612. else { // if (empty($file_jar))
  613. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => (String) $application_xml->name));
  614. if (!$iassign_ilm) {
  615. $iassign_ilm = new stdClass(); //MOOC 2016
  616. $iassign_ilm->id = 0;
  617. }
  618. $newentry = new stdClass();
  619. $newentry->name = (String) $application_xml->name;
  620. $newentry->version = (String) $application_xml->version;
  621. $newentry->type = (String) $application_xml->type;
  622. $newentry->url = (String) $application_xml->url;
  623. $newentry->description = strip_tags($description_str);
  624. $newentry->extension = strtolower((String) $application_xml->extension);
  625. $newentry->file_jar = $file_jar . "/";
  626. $newentry->file_class = (String) $application_xml->file_class;
  627. $newentry->width = (String) $application_xml->width;
  628. $newentry->height = (String) $application_xml->height;
  629. $newentry->enable = 0;
  630. $newentry->timemodified = time();
  631. $newentry->author = $USER->id;
  632. $newentry->timecreated = time();
  633. $newentry->evaluate = (String) $application_xml->evaluate;
  634. $newentry->parent = $iassign_ilm->id;
  635. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  636. // log event --------------------------------------------------------------------------------------
  637. iassign_log::add_log('add_iassign_ilm', 'name: ' . $newentry->name . ' ' . $newentry->version, 0, $newentry->id);
  638. // log event --------------------------------------------------------------------------------------
  639. if ($application_xml->params->param) {
  640. foreach ($application_xml->params->param as $value) {
  641. $newentry = new stdClass();
  642. $newentry->iassign_ilmid = $iassign_ilmid;
  643. $newentry->param_type = (String) $value->type;
  644. $newentry->param_name = (String) $value->name;
  645. $newentry->param_value = (String) $value->value;
  646. $newentry->description = html_entity_decode((String) $value->description);
  647. $newentry->visible = (String) $value->visible;
  648. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  649. if (!$newentry->id) {
  650. print_error('error_add_param', 'iassign');
  651. }
  652. }
  653. }
  654. }
  655. }
  656. $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  657. return true;
  658. } // public static function new_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs)
  659. /// Import an iLM
  660. public static function import_ilm ($itemid, $files_extract, $application_xml, $contextuser, $fs) {
  661. global $DB, $CFG, $USER, $OUTPUT;
  662. $description_str = str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML());
  663. $iassign_ilm = $DB->get_record('iassign_ilm', array("name" => (String) $application_xml->name, "version" => (String) $application_xml->version));
  664. if ($iassign_ilm) {
  665. foreach ($files_extract as $key => $value) {
  666. $rootfolder = $CFG->dataroot . '/temp/' . $key;
  667. self::delete_dir($rootfolder);
  668. break;
  669. }
  670. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  671. }
  672. else {
  673. $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
  674. if ($file_jar == null) {
  675. return false;
  676. }
  677. $file_jar = str_replace("./", "", $file_jar);
  678. if (empty($file_jar)) {
  679. $msg_error = get_string('error_add_ilm', 'iassign') . "<br/>In import_ilm: file_jar empty, files_extract=" . $files_extract . "<br/>\n";
  680. print_error($msg_error);
  681. //xx print_error('error_add_ilm', 'iassign');
  682. //print("Import file = " . file_jar . "<br/>");
  683. }
  684. else { // if (empty($file_jar))
  685. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => (String) $application_xml->name));
  686. if (!$iassign_ilm) {
  687. $iassign_ilm = new stdClass(); //MOOC 2016
  688. $iassign_ilm->id = 0;
  689. }
  690. $newentry = new stdClass();
  691. $newentry->name = (String) $application_xml->name;
  692. $newentry->version = (String) $application_xml->version;
  693. $newentry->type = (String) $application_xml->type;
  694. $newentry->url = (String) $application_xml->url;
  695. $newentry->description = strip_tags($description_str);
  696. $newentry->extension = strtolower((String) $application_xml->extension);
  697. $newentry->file_jar = $file_jar . "/";
  698. $newentry->file_class = (String) $application_xml->file_class;
  699. $newentry->width = (String) $application_xml->width;
  700. $newentry->height = (String) $application_xml->height;
  701. $newentry->enable = 0;
  702. $newentry->timemodified = time();
  703. $newentry->author = $USER->id;
  704. $newentry->timecreated = time();
  705. $newentry->evaluate = (String) $application_xml->evaluate;
  706. $newentry->parent = $iassign_ilm->id;
  707. //MOOC 2016 $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  708. $iassign_ilmid = $DB->insert_record("iassign_ilm", $newentry);
  709. if ($application_xml->params->param) {
  710. foreach ($application_xml->params->param as $value) {
  711. $newentry = new stdClass();
  712. $newentry->iassign_ilmid = $iassign_ilmid;
  713. $newentry->param_type = (String) $value->type;
  714. $newentry->param_name = (String) $value->name;
  715. $newentry->param_value = (String) $value->value;
  716. $newentry->description = html_entity_decode((String) $value->description);
  717. $newentry->visible = (String) $value->visible;
  718. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  719. if (!$newentry->id) {
  720. print_error('error_add_param', 'iassign');
  721. }
  722. }
  723. } // if ($application_xml->params->param)
  724. } // else if (empty($file_jar))
  725. print($OUTPUT->notification(get_string('ok_import_ilm_version', 'iassign'), 'notifysuccess'));
  726. }
  727. $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  728. } // public static function import_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs)
  729. // static function export_update_ilm($ilm_id) //MOOC 2016
  730. /// Function for save iLM from XML descriptor
  731. // @see html5.php : 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);
  732. // @param array $application_xml Data of XML descriptor
  733. // @param array $files_extract Filenames of extract files
  734. // @return string with the iLM path (since ./ilm/...)
  735. static function save_ilm_by_xml ($application_xml, $files_extract) {
  736. global $CFG, $USER, $OUTPUT;
  737. $source = "";
  738. $diretorio = "";
  739. // Check if the iLM directory is writable
  740. if (!is_writable("ilm/")) {
  741. print($OUTPUT->notification(get_string('error_folder_permission_denied', 'iassign'), 'notifyproblem'));
  742. exit;
  743. // return null; // Is safer to stop the processing in order to allow the reader of this error!
  744. }
  745. // The filter 'filename_from_iLM_name(...)' is essential to clear the name for directory from the iLM original name
  746. $application_file_name = iassign_utils::filename_from_iLM_name($application_xml->name, false); //2019
  747. // Check if iLM directory already exists
  748. if (!file_exists("ilm/" . $application_file_name)) {
  749. mkdir("ilm/" . $application_file_name, 0755, true); // 755 = rwxr-xr-x
  750. touch("ilm/" . $application_file_name . DIRECTORY_SEPARATOR . "index.html");
  751. }
  752. // Check if iLM version already exists in directory
  753. if (!file_exists("ilm/" . $application_file_name . "/" . $application_xml->version)) {
  754. mkdir("ilm/" . $application_file_name . DIRECTORY_SEPARATOR . $application_xml->version, 0755, true);
  755. touch("ilm/" . $application_file_name . DIRECTORY_SEPARATOR . $application_xml->version . DIRECTORY_SEPARATOR . "index.html");
  756. }
  757. else {
  758. $diretorio = "./" . "ilm/" . $application_file_name . DIRECTORY_SEPARATOR . $application_xml->version;
  759. //RR echo $diretorio."<br/>";
  760. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  761. //RR return null;
  762. return $diretorio;
  763. }
  764. $root_ilm = "ilm/" . $application_file_name . "/" . $application_xml->version;
  765. // Extract iLM files to directory
  766. foreach ($files_extract as $key => $value) {
  767. $file = $CFG->dataroot . '/temp/' . $key;
  768. if (is_dir($file)) {
  769. $source = $file;
  770. $diretorio = $root_ilm . "/" . basename($file);
  771. // mkdir($diretorio, 0777, true);
  772. mkdir($diretorio, 0755, true);
  773. touch($diretorio . DIRECTORY_SEPARATOR . "index.html");
  774. break;
  775. }
  776. } // foreach ($files_extract as $key => $value)
  777. // Write in the MoodleData 'temp' directory, also in the WWW 'mod/iassign/ilm/'
  778. //D foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item
  779. foreach ($iterator = new RecursiveIteratorIterator(
  780. new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item) {
  781. $directory_name = $diretorio . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
  782. if ($item->isDir()) {
  783. mkdir($directory_name);
  784. touch($directory_name . DIRECTORY_SEPARATOR . "index.html");
  785. }
  786. else {
  787. copy($item, $directory_name);
  788. }
  789. }
  790. self::delete_dir($source);
  791. return "./" . $diretorio;
  792. } // static function save_ilm_by_xml($application_xml, $files_extract)
  793. /// Function for delete directory where the iLM is allocated.
  794. // @param string $dirPath
  795. // @throws InvalidArgumentException
  796. public static function delete_dir ($dirPath) {
  797. if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
  798. $dirPath .= '/';
  799. }
  800. $files = glob($dirPath . '*', GLOB_MARK); //TODO This option 'glob' do not get file with dot (like '.versions'), chang to 'scandir($dir)'...
  801. foreach ($files as $file) {
  802. if (is_dir($file)) { //TODO if ($file != '.' && $file != '..')
  803. self::delete_dir($file);
  804. }
  805. else {
  806. unlink($file);
  807. }
  808. }
  809. if (is_dir($dirPath))
  810. rmdir($dirPath); //TODO Warning: rmdir(/var/data/moodle_data_saw/temp/ivprog-html/js/semantic/): Directory not empty in /var/www/html/saw_clone/mod/iassign/ilm_handlers/html5.php on line 962
  811. else
  812. print($OUTPUT->notification(get_string('error_file_jar_exists', 'iassign'), 'notifyproblem')); // just warning the user
  813. }
  814. } // class html5 implements ilm_handle
  815. ?>