html5.php 51 KB

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