1
0

html5.php 48 KB

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