html5.php 43 KB

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