html5.php 53 KB

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