html5.php 48 KB

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