java.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. <?php
  2. /**
  3. * Class that implements ilm_handle, in order to allow manipulation and management of Java 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 java implements ilm_handle {
  18. /// Build HTML code to present an applet
  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. foreach ($files_jar as $one_file) {
  50. $file = $fs->get_file_by_id($one_file);
  51. if (!$file)
  52. print $OUTPUT->notification(get_string('error_confirms_jar', 'iassign'), 'notifyproblem');
  53. else {
  54. // Get file path (informatin on Moodle table '*_files' - in its field 'contenthash'
  55. // The actual file is under the Moodle data. E.g., if Moodle data is the default '/var/moodledata' and 'contenthash="5641c1a0e1f5b37fa0b74996ead592c77ff027bc"',
  56. // then the JAR file in: '/var/moodledata/fildir/56/41/5641c1a0e1f5b37fa0b74996ead592c77ff027bc'
  57. $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
  58. array_push($file_url, $url);
  59. }
  60. }*/
  61. $lang = substr(current_language(), 0, 2);
  62. if ($options['type'] == "filter") { //leo
  63. $iassign_ilm_width = $options['width']; // or use? $iassign_ilm->width
  64. $iassign_ilm_height = $options['height']; // or use? $iassign_ilm->height
  65. } else { //leo
  66. $iassign_ilm_width = $iassign_ilm->width;
  67. $iassign_ilm_height = $iassign_ilm->height; // or use? $iassign_ilm->height
  68. }
  69. $file_url = explode(",", $iassign_ilm->file_jar);
  70. if (!empty($file_url)) { // There is an iLM file
  71. // Build tag to JAR file
  72. $html .= ' <!-- iAssign - view iLM content / LInE - http://line.ime.usp.br -->' . "\n";
  73. $html .= ' <applet id="iLM_applet" name="iLM" archive="' . new moodle_url("/mod/iassign/". implode(",", $file_url)) . '" code="' . $iassign_ilm->file_class . '" width="' . $iassign_ilm_width . '" height="' . $iassign_ilm_height . '" vspace=10 hspace=10>' . "\n";
  74. $html .= ' <param name="lang" value="' . $lang . '"/>' . "\n";
  75. switch ($options['type']) {
  76. case "view":
  77. $html .= ' <!-- view -->' . "\n";
  78. $html .= ' <param name="iLM_PARAM_SendAnswer" value="' . $options['notSEND'] . '"/>' . "\n"; // ATTENTION: MA_PARAM_notSEND=true => iLM_PARAM_SendAnswer=false
  79. $html .= ' <param name="MA_PARAM_notSEND" value="true"/>' . "\n"; // iLM version 1.0
  80. //MOOC 2016 --- allow to load dynamic parameters
  81. //TODO: REVIEW: this code is to insert iLM as HTML5 and to allow general parameter to any iLM
  82. //n $iassign_ilm_config = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id));
  83. //n foreach ($iassign_ilm_config as $ilm_config) {
  84. //n if (array_key_exists($ilm_config->param_name, $options)) {
  85. //n $ilm_config->param_value = $options[$ilm_config->param_name];
  86. //n $html .= ' <param name="' . $ilm_config->param_name . '" value="' . $ilm_config->param_value . '"/>' . "\n";
  87. //n }
  88. //n }
  89. break;
  90. case "activity":
  91. $html .= ' <!-- activity -->' . "\n";
  92. $html .= ' <param name="iLM_PARAM_Assignment" value="' . $options['Proposition'] . '"/>' . "\n"; // iLM version 2.0
  93. $html .= ' <param name="iLM_PARAM_SendAnswer" value="' . $options['notSEND'] . '"/>' . "\n"; // ATTENTION: MA_PARAM_notSEND=true => iLM_PARAM_SendAnswer=false
  94. $html .= ' <param name="iLM_PARAM_ServerToGetAnswerURL" value="' . $options['addresPOST'] . '"/>' . "\n"; // not necessary in version iLM 2.0
  95. $html .= ' <param name="MA_PARAM_PropositionURL" value="true"/>' . "\n";
  96. $html .= ' <param name="MA_PARAM_Proposition" value="' . $options['Proposition'] . '"/>' . "\n"; // in iLM 2.0 is in 'iLM_PARAM_Assignment' already
  97. $html .= ' <param name="MA_PARAM_notSEND" value="' . $options['notSEND'] . '"/>' . "\n";
  98. $html .= ' <param name="MA_PARAM_addresPOST" value="' . $options['addresPOST'] . '"/>' . "\n"; // not necessary in version iLM 2.0
  99. if ($options['special_param'] == 1) {
  100. $html .= '<param name="TIPO_SCRIPT" value="1"/>' . "\n";
  101. $html .= '<param name="BOTAOSCR1" value="' . $options['student_answer'] . '" />' . "\n";
  102. }
  103. //MOOC 2016 --- allow to load dynamic parameters
  104. //n $iassign_activity_item_configs = $DB->get_records('iassign_statement_config', array('iassign_statementid' => $options['iassign_statement']));
  105. //n if ($iassign_activity_item_configs) {
  106. //n foreach ($iassign_activity_item_configs as $iassign_activity_item_config)
  107. //n $html .= ' <param name="'.$iassign_activity_item_config->param_name.'" value="'.$iassign_activity_item_config->param_value.'"/>' . "\n";
  108. //n }
  109. break;
  110. case "editor_new":
  111. $html .= ' <!-- editor_new -->' . "\n";
  112. $html .= ' <param name="iLM_PARAM_Assignment" value="' . $options['Proposition'] . '"/>' . "\n"; // iLM version 2.0
  113. $html .= ' <param name="iLM_PARAM_SendAnswer" value="' . $options['notSEND'] . '"/>' . "\n"; // ATTENTION: MA_PARAM_notSEND=true => iLM_PARAM_SendAnswer=false
  114. $html .= ' <param name="iLM_PARAM_Authoring" value="true"/>' . "\n";
  115. $html .= ' <param name="MA_PARAM_PropositionURL" value="true"/>' . "\n"; // iLM version 1.0
  116. $html .= ' <param name="MA_PARAM_notSEND" value="' . $options['notSEND'] . '"/>' . "\n";
  117. $html .= ' <param name="MA_PARAM_ArchiveTeacher" value="true"/>' . "\n";
  118. break;
  119. case "editor_update":
  120. $html .= ' <!-- editor_update -->' . "\n";
  121. $html .= ' <param name="iLM_PARAM_Assignment" value="' . $options['Proposition'] . '"/>' . "\n"; // iLM version 2.0
  122. $html .= ' <param name="iLM_PARAM_Authoring" value="true"/>' . "\n";
  123. $html .= ' <param name="iLM_PARAM_SendAnswer" value="' . $options['notSEND'] . '"/>' . "\n"; // ATTENTION: MA_PARAM_notSEND=true => iLM_PARAM_SendAnswer=false
  124. $html .= ' <param name="MA_PARAM_PropositionURL" value="true"/>' . "\n"; // iLM version 1.0
  125. $html .= ' <param name="MA_PARAM_Proposition" value="' . $options['Proposition'] . '"/>' . "\n";
  126. $html .= ' <param name="MA_PARAM_ArchiveTeacher" value="true"/>' . "\n";
  127. $html .= ' <param name="MA_PARAM_notSEND" value="' . $options['notSEND'] . '"/>' . "\n";
  128. break;
  129. default:
  130. print_error('error_view_without_actiontype', 'iassign'); //
  131. }
  132. $html .= '</applet> <br/>' . "\n";
  133. } // if (!empty($file_url))
  134. } // if ($iassign_ilm)
  135. $verify_java = '
  136. <script>var hasJava= navigator.javaEnabled();
  137. if (!hasJava) {
  138. var tableRef = document.getElementById("outlinetable").getElementsByTagName("tbody")[0];
  139. var newRow = tableRef.insertRow();
  140. var newCell = newRow.insertCell(0);
  141. newCell.style.textAlign = "center";
  142. newCell.style.border = "2px solid red";
  143. newCell.style.background = "#ffe8e8";
  144. newCell.style.fontSize = "1.5em";
  145. var newText = document.createTextNode("'.get_string('applet_blocked', 'iassign').'");
  146. newCell.appendChild(newText);
  147. }
  148. </script>';
  149. return $verify_java . $html;
  150. }
  151. /// Presents an activity with its iLM
  152. public static function show_activity_in_ilm ($iassign_statement_activity_item, $student_answer, $enderecoPOST, $view_teacherfileversion) {
  153. global $USER, $CFG, $DB;
  154. $ilm = $DB->get_record('iassign_ilm', array('id' => $iassign_statement_activity_item->iassign_ilmid));
  155. // enderecoPOST: eliminei a tag '&write_solution=0' deixando para este 'view' completar, eventualmente com '&write_solution=1'!
  156. $special_param1 = $iassign_statement_activity_item->special_param1;
  157. $context = context_module::instance($USER->cm);
  158. //TODO Given an activity => find its correspondent file in Moodle data. Bad solution!
  159. //TODO Change the meaning of 'iassign_statement.file' from insertion order to the ID in table 'files'.
  160. //TODO This demands update to each 'iassign_statement', find its corresponding on in 'files', and update 'iassign_statement.file = files.id'
  161. if ($view_teacherfileversion) { // get the exercise in Moodle data (teacher file)
  162. $fileid = "";
  163. $fs = get_file_storage();
  164. $files = $fs->get_area_files($context->id, 'mod_iassign', 'exercise', $iassign_statement_activity_item->file); // iassign_statement_activity_item = table 'iassign_statement'
  165. if ($files) {
  166. foreach ($files as $value) {
  167. if ($value->get_filename() != '.')
  168. $fileid = $value->get_id();
  169. }
  170. }
  171. if (!$fileid) { // 'Something is wrong. Maybe your teacher withdrew this exercise file. Please, inform your teacher.';
  172. print iassign::warning_message_iassign('error_exercise_removed') . "<br/>\n"; // I couldn't find the file in table 'files'!
  173. }
  174. }
  175. $ilm_name = strtolower($ilm->name);
  176. $extension = iassign_utils::filename_extension($ilm_name);
  177. if ($ilm_name == "igeom") { // is iGeom exercise
  178. $is_igeom = true;
  179. $content_or_id_from_ilm_security = $fileid; // $iassign_statement_activity_item->file;
  180. if ($special_param1 == 1 && !empty($student_answer) && (!$view_teacherfileversion)) { // is script
  181. $view_teacherfileversion = true; // here when student is doing/redoing his activity
  182. }
  183. elseif (!$view_teacherfileversion) { // Student seem his solution or teacher it (of one student)
  184. $content_or_id_from_ilm_security = $student_answer;
  185. }
  186. }
  187. else {
  188. if ($view_teacherfileversion) { // $view_teacherfileversion==1 => load the exercise ('activity') from the 'moodledata' (id in 'files')
  189. // $content_or_id_from_ilm_security = $this->context->id;
  190. $content_or_id_from_ilm_security = $fileid; // $iassign_statement_activity_item->file;
  191. } else { // $view_teacherfileversion==null => load the learner answer from the data base (iassign_submission)
  192. $content_or_id_from_ilm_security = $student_answer;
  193. }
  194. }
  195. $allow_submission = false; // There is permission to 'submission' button?
  196. if ($USER->iassignEdit == 1 && $student_answer) { // for now, only iVProg2 and iVProgH5 allows editions of exercise already sent
  197. $allow_submission = true; // yes!
  198. $write_solution = 1;
  199. $enderecoPOST .= "&write_solution=1"; // complement POST address indicating that the learner could send edited solution
  200. }
  201. // Security: this avoid the student get a second access to the file content (usually an exercise)
  202. // Data are registered in the table '*_iassign_security' bellow and is erased by function 'view()' above.
  203. // IMPORTANT: the '$end_file' will receive the iLM content URL using the security filter './mod/iassign/ilm_security.php'
  204. // the iLM must request the content using this URL. Data are registered in the table '*_iassign_security'.
  205. // Attention : using iVProgH5 there are lot of " and the use of slashes (as '\"') will imply in iVProgH5 do not read the file!
  206. // do not use: $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, addslashes($content_or_id_from_ilm_security));
  207. //2017 $id_iLM_security = $this->write_iLM_security($iassign_statement_activity_item->id, $content_or_id_from_ilm_security); // insert in 'iassign_security'
  208. //2017 $this->remove_old_iLM_security_entries($USER->id, $iassign_statement_activity_item->id); // additional security: erase eventually old entries
  209. require_once ($CFG->dirroot . '/mod/iassign/ilm_security.php');
  210. $timecreated = time();
  211. $token = md5($timecreated); // iassign_iLM_security->timecreated);
  212. // Try to get the student answer
  213. // In './ilm_security.php': write_iLM_security($userid, $timecreated, $iassign_statementid = -1, $content_or_id_from_ilm_security)
  214. $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'
  215. // $iassign_iLM_security = $DB->get_record("iassign_security", array("id" => $id_iLM_security));
  216. $end_file = $CFG->wwwroot . '/mod/iassign/ilm_security.php?id=' . $id_iLM_security . '&token=' . $token . '&view=' . $view_teacherfileversion; // need full path...
  217. //TODO iLM_HTML5 :: Need to use '*_iassign_ilm.ilm_type' in { 'jar', 'html5' }
  218. //TODO iLM_HTML5 :: For now I use the particular case of
  219. //leo Ou usar: if ($extension == "html" || $extension == "ivph") // if iLM is HTML5 is inside a frame
  220. $iassign = "
  221. <script type='text/javascript'>
  222. //<![CDATA[
  223. function jsAnalyseAnswer () {
  224. var strAnswer = document.applets[0].getAnswer(); //leo estava comentado!
  225. var evaluationResult = document.applets[0].getEvaluation(); //leo estava comentado!
  226. var comment = document.formEnvio.submission_comment.value;
  227. if ((strAnswer==null || strAnswer=='' || strAnswer==-1) && (comment==null || comment=='')) { // undefined
  228. alert('" . get_string('activity_empty', 'iassign') . "');
  229. return false; // success
  230. }
  231. else {
  232. document.formEnvio.iLM_PARAM_ArchiveContent.value = strAnswer;
  233. document.formEnvio.iLM_PARAM_ActivityEvaluation.value = evaluationResult;
  234. document.formEnvio.submit();
  235. return true; // any error...
  236. }
  237. }
  238. //]]>
  239. </script>\n"; //
  240. $iassign .= "\n<center>\n<form name='formEnvio' id='formEnvio' method='post' action='$enderecoPOST' enctype='multipart/form-data'>\n";
  241. // Attention: The actual iLM content will be provided by the indirect access in './mod/iassign/ilm_security.php',
  242. // bellow only the 'token' to the content will be shown in URL (by security reason). The iLM must use this URL on
  243. // 'MA_PARAM_Proposition' to request the content.
  244. // Calls static function bellow: parameters are data to store in table '*_iassign_submission'
  245. //leo $iassign .= ilm_settings::build_ilm_tags($this->ilm->id, array(
  246. $iassign .= ilm_settings::build_ilm_tags($ilm->id, array(//leo para testar 'iassign_security'
  247. "type" => "activity",
  248. "notSEND" => "false",
  249. "addresPOST" => $enderecoPOST,
  250. "Proposition" => $end_file,
  251. "special_param" => $special_param1,
  252. "student_answer" => $student_answer,
  253. "id_iLM_security" => $id_iLM_security,
  254. "iassign_statement" => $iassign_statement_activity_item->id // MOOC 2016
  255. ));
  256. if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1) {
  257. $iassign .= " <input type='hidden' name='iLM_PARAM_ArchiveContent' value=''>\n";
  258. $iassign .= " <input type='hidden' name='iLM_PARAM_ActivityEvaluation' value=''>\n";
  259. if (!has_capability('mod/iassign:evaluateiassign', $USER->context, $USER->id))
  260. $iassign .= " <p><textarea rows='2' cols='60' name='submission_comment'></textarea></p>\n";
  261. else
  262. $iassign .= " <input type='hidden' name='submission_comment'>\n";
  263. if ($allow_submission) { // it is not iGeom
  264. $iassign .= "<center>\n<!-- load button -->\n" .
  265. " <input type=button value='" . get_string('submit_iassign', 'iassign') . "' onClick = 'javascript:window.jsAnalyseAnswer();' title='" .
  266. get_string('message_submit_iassign', 'iassign') . "'>\n" . "</center>\n";
  267. } else {
  268. // Works with 'javascript:window.jsAnalyseAnswer()' or simply 'jsAnalyseAnswer()'
  269. $iassign .= "<center>\n<!-- load button -->\n" .
  270. " <input type=button value='" . get_string('submit_iassign', 'iassign') . "' onClick = 'javascript:window.jsAnalyseAnswer();' title='" .
  271. get_string('message_submit_iassign', 'iassign') . "'>\n" . "</center>\n";
  272. }
  273. } // if (!isguestuser() && $iassign_statement_activity_item->type_iassign != 1)
  274. $iassign .= "</form></center>\n\n";
  275. return $iassign;
  276. }
  277. /**
  278. * Exibe os dados de um iLM
  279. */
  280. public static function view_ilm($ilmid, $from) {
  281. global $DB;
  282. $url = new moodle_url('/admin/settings.php', array('section' => 'modsettingiassign'));
  283. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilmid));
  284. $str = "";
  285. $str .= '<table id="outlinetable" cellpadding="5" width="100%" >' . "\n";
  286. $str .= '<tr>';
  287. $str .= '<td colspan=3 align=right>';
  288. if ($from != 'admin') {
  289. $str .= '<input type=button value="' . get_string('return', 'iassign') . '" onclick="javascript:window.location = \'' . $_SERVER['HTTP_REFERER'] . '\';">' . "\n";
  290. }
  291. $str .= '<input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close();">';
  292. $str .= '</td>' . "\n";
  293. $str .= '</tr>' . "\n";
  294. if ($iassign_ilm) {
  295. $iassign_statement_activity_item = $DB->get_records('iassign_statement', array("iassign_ilmid" => $iassign_ilm->id));
  296. if ($iassign_statement_activity_item) {
  297. $total = count($iassign_statement_activity_item);
  298. } else {
  299. $total = 0;
  300. }
  301. if ($from == 'admin') {
  302. $str .= '<tr><td colspan=2>' . "\n";
  303. $str .= '<table width="100%" class="generaltable boxaligncenter" >' . "\n";
  304. $str .= '<tr>' . "\n";
  305. $str .= '<td class=\'cell c0 actvity\' ><strong>' . get_string('activities', 'iassign') . ':</strong>&nbsp;' . $total . '</td>' . "\n";
  306. $str .= '<td><strong>' . get_string('url_ilm', 'iassign') . '</strong>&nbsp;<a href="' . $iassign_ilm->url . '">' . $iassign_ilm->url . '</a></td>' . "\n";
  307. $str .= '</tr>' . "\n";
  308. $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";
  309. $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";
  310. $str .= '<td width="50%"><strong>' . get_string('width', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->width;
  311. $str .= '&nbsp;&nbsp;<strong>' . get_string('height', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->height . '</td></tr>' . "\n";
  312. $str .= '<tr><td><strong>' . get_string('file_jar', 'iassign') . ':</strong>&nbsp;' . basename($iassign_ilm->file_jar) . '</td>' . "\n";
  313. $str .= '<td ><strong>' . get_string('file_class', 'iassign') . ':</strong>&nbsp;' . $iassign_ilm->file_class . '</td></tr>' . "\n";
  314. if ($iassign_ilm->evaluate == 1) {
  315. $evaluate = get_string('yes', 'iassign');
  316. } else {
  317. $evaluate = get_string('no', 'iassign');
  318. }
  319. $str .= '<tr><td width="50%"><strong>' . get_string('evaluate', 'iassign') . ':</strong>&nbsp;' . $evaluate . '</td>' . "\n";
  320. if ($iassign_ilm->enable == 1) {
  321. $enable = get_string('yes', 'iassign');
  322. } else {
  323. $enable = get_string('no', 'iassign');
  324. }
  325. $str .= '<td width="50%"><strong>' . get_string('enable', 'iassign') . ':</strong>&nbsp;' . $enable . '</td></tr>' . "\n";
  326. $str .= '<tr>' . "\n";
  327. $str .= '<td width="50%"><strong>' . get_string('file_created', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timecreated) . '</td>' . "\n";
  328. $str .= '<td width="50%"><strong>' . get_string('file_modified', 'iassign') . ':</strong>&nbsp;' . userdate($iassign_ilm->timemodified) . '</td>' . "\n";
  329. $str .= '</tr>' . "\n";
  330. $user_ilm = $DB->get_record('user', array('id' => $iassign_ilm->author));
  331. if ($user_ilm) {
  332. $str .= '<tr>' . "\n";
  333. $str .= '<td colspan=2><strong>' . get_string('author', 'iassign') . ':</strong>&nbsp;' . $user_ilm->firstname . '&nbsp;' . $user_ilm->lastname . '</td>' . "\n";
  334. $str .= '</tr>' . "\n";
  335. }
  336. $str .= '</table>' . "\n";
  337. $str .= '</td></tr>' . "\n";
  338. }
  339. if (!empty($iassign_ilm->file_jar)) {
  340. //TODO: REVIEW: to be used for parameters of "applet" from DB
  341. $options = array("type" => "view"); //MOOC2014: start
  342. $str .= '<tr class=\'cell c0 actvity\'><td colspan=3 align=center bgcolor="#F5F5F5">' . "\n";
  343. // Second parameter null since 'iassign_security' are not define yet
  344. $str .= ilm_settings::build_ilm_tags($iassign_ilm->id, $options);
  345. //TODO: REVIEW: missing code to manage parameters
  346. //MOOC2014: tem este codigo!
  347. } else {
  348. $str .= '<tr class=\'cell c0 actvity\'>' . "\n";
  349. $str .= '<td colspan=2 align=center>' . get_string('null_file', 'iassign') . '</td>' . "\n";
  350. $str .= '<td align=center><a href="' . $url . '</a></td>' . "\n";
  351. $str .= '</tr>' . "\n";
  352. }
  353. $str .= '</td></tr>' . "\n";
  354. }
  355. $str .= '</table>' . "\n";
  356. return $str;
  357. }
  358. /**
  359. * Copia ou gera uma nova versão do iLM
  360. */
  361. public static function copy_new_version_ilm($param, $files_extract) {
  362. global $DB, $CFG;
  363. $iassign_ilm = new stdClass();
  364. $iassign_ilm->name = $param->name;
  365. $iassign_ilm->version = $param->version;
  366. $iassign_ilm->file_jar = null;
  367. $file_jar = null;
  368. if (!is_null($files_extract)) {
  369. foreach ($files_extract as $key => $value) {
  370. if (strpos(strtolower($key), ".jar")) {
  371. $extract = new stdClass();
  372. $extract->file_jar = $key;
  373. $extract->name = $param->name;
  374. $extract->version = $param->version;
  375. echo $key;
  376. $file_jar = self::save_ilm_by_xml($extract, $files_extract)[0];
  377. echo $file_jar;
  378. break;
  379. }
  380. }
  381. }
  382. $description = json_decode($param->description_lang);
  383. $description->{$param->set_lang} = $param->description;
  384. $newentry = new stdClass();
  385. $newentry->name = $param->name;
  386. $newentry->version = $param->version;
  387. $newentry->type = "Java";
  388. $newentry->url = $param->url;
  389. $newentry->description = strip_tags(json_encode($description));
  390. $newentry->extension = strtolower($param->extension);
  391. if (!is_null($file_jar)) {
  392. $newentry->file_jar = $file_jar;
  393. }
  394. $newentry->file_class = $param->file_class;
  395. $newentry->width = $param->width;
  396. $newentry->height = $param->height;
  397. $newentry->enable = 0;
  398. $newentry->timemodified = $param->timemodified;
  399. $newentry->timecreated = $param->timecreated;
  400. $newentry->evaluate = $param->evaluate;
  401. $newentry->author = $param->author;
  402. $newentry->parent = $param->parent;
  403. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  404. // log event --------------------------------------------------------------------------------------
  405. iassign_log::add_log('copy_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $newentry->id);
  406. // log event --------------------------------------------------------------------------------------
  407. }
  408. /// Export an iLM to a package of the iLM (with its descriptor XML)
  409. public static function export_ilm ($ilm_id) {
  410. global $DB, $CFG;
  411. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  412. $iassign_ilm_configs = $DB->get_records('iassign_ilm_config', array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  413. $files_jar = str_replace(basename($iassign_ilm->file_jar), "", $iassign_ilm->file_jar);
  414. $zip_filename = $CFG->dataroot . '/temp/ilm-' . iassign_utils::format_pathname($iassign_ilm->name . '-v' . $iassign_ilm->version) . '_' . date("Ymd-Hi") . '.ipz';
  415. $zip = new zip_archive();
  416. $zip->open($zip_filename);
  417. $rootdir = $CFG->dirroot . '/mod/iassign/' . $files_jar;
  418. $allfiles = self::list_directory($rootdir);
  419. $i = 0;
  420. $base_jar = "";
  421. foreach ($allfiles as $file) {
  422. $mini = basename(str_replace($CFG->dirroot . '/mod/iassign/ilm/', "", $file));
  423. if (!is_dir($file)) {
  424. $zip->add_file_from_pathname($mini, $file);
  425. $base_jar = $mini;
  426. }
  427. }
  428. $application_descriptor = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  429. $application_descriptor .= '<application xmlns="http://line.ime.usp.br/application/1.5">' . "\n";
  430. $application_descriptor .= ' <name>' . $iassign_ilm->name . '</name>' . "\n";
  431. $application_descriptor .= ' <url>' . $iassign_ilm->url . '</url>' . "\n";
  432. $application_descriptor .= ' <version>' . $iassign_ilm->version . '</version>' . "\n";
  433. $application_descriptor .= ' <type>' . $iassign_ilm->type . '</type>' . "\n";
  434. $application_descriptor .= ' <description>' . html_entity_decode(str_replace(array('<p>', '</p>'), array('', ''), $iassign_ilm->description)) . '</description>' . "\n";
  435. $application_descriptor .= ' <extension>' . $iassign_ilm->extension . '</extension>' . "\n";
  436. $application_descriptor .= ' <file_jar>' . $base_jar . '</file_jar>' . "\n";
  437. $application_descriptor .= ' <file_class>' . $iassign_ilm->file_class . '</file_class>' . "\n";
  438. $application_descriptor .= ' <width>' . $iassign_ilm->width . '</width>' . "\n";
  439. $application_descriptor .= ' <height>' . $iassign_ilm->height . '</height>' . "\n";
  440. $application_descriptor .= ' <evaluate>' . $iassign_ilm->evaluate . '</evaluate>' . "\n";
  441. if ($iassign_ilm_configs) { //MOOC 2016
  442. $application_descriptor .= ' <params>' . "\n";
  443. foreach ($iassign_ilm_configs as $iassign_ilm_config) {
  444. $application_descriptor .= ' <param>' . "\n";
  445. $application_descriptor .= ' <type>' . $iassign_ilm_config->param_type . '</type>' . "\n";
  446. $application_descriptor .= ' <name>' . $iassign_ilm_config->param_name . '</name>' . "\n";
  447. $application_descriptor .= ' <value>' . $iassign_ilm_config->param_value . '</value>' . "\n";
  448. $application_descriptor .= ' <description>' . htmlentities(str_replace("\n", "", $iassign_ilm_config->description)) . '</description>' . "\n";
  449. $application_descriptor .= ' <visible>' . $iassign_ilm_config->visible . '</visible>' . "\n";
  450. $application_descriptor .= ' </param>' . "\n";
  451. }
  452. $application_descriptor .= ' </params>' . "\n";
  453. } //MOOC 2016
  454. $application_descriptor .= '</application>' . "\n";
  455. $zip->add_file_from_string('ilm-application.xml', $application_descriptor);
  456. $zip->close();
  457. header("Pragma: public");
  458. header("Expires: 0");
  459. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  460. header("Cache-Control: private", false);
  461. header("Content-Type: application/zip");
  462. header("Content-Disposition: attachment; filename=\"" . basename($zip_filename) . "\";");
  463. header("Content-Transfer-Encoding: binary");
  464. header("Content-Length: " . @filesize($zip_filename));
  465. set_time_limit(0);
  466. @readfile("$zip_filename") || die("File not found.");
  467. unlink($zip_filename);
  468. exit;
  469. }
  470. public static function delete_ilm($ilm_id) {
  471. global $DB, $CFG, $OUTPUT;
  472. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  473. // Prepare the path of directory to be removed
  474. $path_w = rtrim($iassign_ilm->file_jar, "/");
  475. $folder_to_remove = substr($path_w, 0, strrpos($path_w, '/') + 1);
  476. // Check if the iLM directory is writable
  477. if (!is_writable($iassign_ilm->file_jar)) {
  478. return null;
  479. }
  480. self::delete_dir($folder_to_remove);
  481. $ilm_folder = "ilm/" . $iassign_ilm->name . "/";
  482. $k = 0;
  483. // Verify if iLM parent directory is empty, if yes, remove it
  484. foreach(glob($ilm_folder . "*", GLOB_ONLYDIR) as $dir) {
  485. $k ++;
  486. break;
  487. }
  488. if ($k == 0) {
  489. self::delete_dir($ilm_folder);
  490. }
  491. $DB->delete_records("iassign_ilm", array('id' => $ilm_id));
  492. $DB->delete_records("iassign_ilm_config", array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  493. // log event --------------------------------------------------------------------------------------
  494. iassign_log::add_log('delete_iassign_ilm', 'name: ' . $iassign_ilm->name . ' ' . $iassign_ilm->version, 0, $iassign_ilm->id);
  495. // log event --------------------------------------------------------------------------------------
  496. return $iassign_ilm->parent;
  497. }
  498. /*public static function delete_ilm($ilm_id) {
  499. global $DB, $CFG;
  500. $fs = get_file_storage();
  501. $contextsystem = context_system::instance();
  502. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilm_id));
  503. $files_jar = explode(",", $iassign_ilm->file_jar);
  504. foreach ($files_jar as $file_jar) {
  505. $file = $fs->get_file_by_id($file_jar);
  506. if ($file)
  507. $fs->delete_area_files($contextsystem->id, 'mod_iassign', 'ilm', $file->get_itemid());
  508. }
  509. $DB->delete_records("iassign_ilm", array('id' => $ilm_id));
  510. $DB->delete_records("iassign_ilm_config", array('iassign_ilmid' => $ilm_id)); //MOOC 2016
  511. // log event --------------------------------------------------------------------------------------
  512. iassign_log::add_log('delete_iassign_ilm', 'name: ' . $iassign_ilm->name . ' ' . $iassign_ilm->version, 0, $iassign_ilm->id);
  513. // log event --------------------------------------------------------------------------------------
  514. return $iassign_ilm->parent;
  515. }*/
  516. /// Register data from an iLM after to updated it
  517. public static function edit_ilm ($param, $itemid, $files_extract, $contextuser) {
  518. global $DB, $CFG;
  519. $iassign_ilm = new stdClass();
  520. $iassign_ilm->name = $param->name;
  521. $iassign_ilm->version = $param->version;
  522. $iassign_ilm->file_jar = $param->file_jar;
  523. $file_jar = null;
  524. if (!is_null($files_extract)) {
  525. foreach ($files_extract as $key => $value) {
  526. if (strpos(strtolower($key), ".jar")) {
  527. $extract = new stdClass();
  528. $extract->file_jar = $key;
  529. $extract->name = $param->name;
  530. $extract->version = $param->version;
  531. echo $key;
  532. $file_jar = self::save_ilm_by_xml($extract, $files_extract)[0];
  533. echo $file_jar;
  534. break;
  535. }
  536. }
  537. }
  538. $description = json_decode($param->description_lang);
  539. $description->{$param->set_lang} = $param->description;
  540. $updentry = new stdClass();
  541. $updentry->id = $param->id;
  542. $updentry->version = $param->version;
  543. $updentry->url = $param->url;
  544. $updentry->description = strip_tags(json_encode($description));
  545. $updentry->extension = strtolower($param->extension);
  546. if (!is_null($file_jar)) {
  547. $updentry->file_jar = $file_jar;
  548. }
  549. $updentry->file_class = $param->file_class;
  550. $updentry->width = $param->width;
  551. $updentry->height = $param->height;
  552. $updentry->enable = $param->enable;
  553. $updentry->timemodified = $param->timemodified;
  554. $updentry->evaluate = $param->evaluate;
  555. $DB->update_record("iassign_ilm", $updentry);
  556. // log event --------------------------------------------------------------------------------------
  557. iassign_log::add_log('update_iassign_ilm', 'name: ' . $param->name . ' ' . $param->version, 0, $param->id);
  558. // log event --------------------------------------------------------------------------------------
  559. }
  560. /**
  561. * Realiza os procedimentos relacionados ao armazenamento dos dados do iLM e seus arquivos
  562. */
  563. public static function new_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs) {
  564. global $DB, $CFG, $USER, $OUTPUT;
  565. $description_str = str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML());
  566. $iassign_ilm = $DB->get_record('iassign_ilm', array("name" => (String) $application_xml->name, "version" => (String) $application_xml->version));
  567. if ($iassign_ilm) {
  568. foreach ($files_extract as $key => $value) {
  569. $file = $CFG->dataroot . '/temp/' . $key;
  570. if (file_exists($file))
  571. unlink($file);
  572. }
  573. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  574. } else {
  575. $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
  576. if (empty($file_jar))
  577. print_error('error_add_ilm', 'iassign');
  578. else { // if (empty($file_jar))
  579. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => (String) $application_xml->name));
  580. if (!$iassign_ilm) {
  581. $iassign_ilm = new stdClass(); //MOOC 2016
  582. $iassign_ilm->id = 0;
  583. }
  584. $newentry = new stdClass();
  585. $newentry->name = (String) $application_xml->name;
  586. $newentry->version = (String) $application_xml->version;
  587. $newentry->type = (String) $application_xml->type;
  588. $newentry->url = (String) $application_xml->url;
  589. $newentry->description = strip_tags($description_str);
  590. $newentry->extension = strtolower((String) $application_xml->extension);
  591. $newentry->file_jar = $file_jar;
  592. $newentry->file_class = (String) $application_xml->file_class;
  593. $newentry->width = (String) $application_xml->width;
  594. $newentry->height = (String) $application_xml->height;
  595. $newentry->enable = 0;
  596. $newentry->timemodified = time();
  597. $newentry->author = $USER->id;
  598. $newentry->timecreated = time();
  599. $newentry->evaluate = (String) $application_xml->evaluate;
  600. $newentry->parent = $iassign_ilm->id;
  601. //MOOC 2016 $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  602. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  603. // log event --------------------------------------------------------------------------------------
  604. iassign_log::add_log('add_iassign_ilm', 'name: ' . $application_xml->name . ' ' . $application_xml->version, 0, $newentry->id);
  605. // log event --------------------------------------------------------------------------------------
  606. if ($application_xml->params->param) {
  607. foreach ($application_xml->params->param as $value) {
  608. $newentry = new stdClass();
  609. $newentry->iassign_ilmid = $iassign_ilmid;
  610. $newentry->param_type = (String) $value->type;
  611. $newentry->param_name = (String) $value->name;
  612. $newentry->param_value = (String) $value->value;
  613. $newentry->description = html_entity_decode((String) $value->description);
  614. $newentry->visible = (String) $value->visible;
  615. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  616. if (!$newentry->id) {
  617. print_error('error_add_param', 'iassign');
  618. }
  619. }
  620. }
  621. }
  622. }
  623. $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  624. return true;
  625. }
  626. /**
  627. * Realiza os procedimentos relacionados ao armazenamento de um iLM por meio da importação
  628. */
  629. public static function import_ilm($itemid, $files_extract, $application_xml, $contextuser, $fs) {
  630. global $DB, $CFG, $USER, $OUTPUT;
  631. $description_str = str_replace(array('<description>', '</description>'), array('', ''), $application_xml->description->asXML());
  632. $iassign_ilm = $DB->get_record('iassign_ilm', array("name" => (String) $application_xml->name, "version" => (String) $application_xml->version));
  633. if ($iassign_ilm) {
  634. foreach ($files_extract as $key => $value) {
  635. $file = $CFG->dataroot . '/temp/' . $key;
  636. if (file_exists($file))
  637. unlink($file);
  638. }
  639. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  640. } else {
  641. $file_jar = self::save_ilm_by_xml($application_xml, $files_extract);
  642. if (empty($file_jar))
  643. print_error('error_add_ilm', 'iassign');
  644. else { // if (empty($file_jar))
  645. $iassign_ilm = $DB->get_record('iassign_ilm', array("parent" => 0, "name" => (String) $application_xml->name));
  646. if (!$iassign_ilm) {
  647. $iassign_ilm = new stdClass(); //MOOC 2016
  648. $iassign_ilm->id = 0;
  649. }
  650. $newentry = new stdClass();
  651. $newentry->name = (String) $application_xml->name;
  652. $newentry->version = (String) $application_xml->version;
  653. $newentry->type = (String) $application_xml->type;
  654. $newentry->url = (String) $application_xml->url;
  655. $newentry->description = strip_tags($description_str);
  656. $newentry->extension = strtolower((String) $application_xml->extension);
  657. $newentry->file_jar = implode(",", $file_jar);
  658. $newentry->file_class = (String) $application_xml->file_class;
  659. $newentry->width = (String) $application_xml->width;
  660. $newentry->height = (String) $application_xml->height;
  661. $newentry->enable = 0;
  662. $newentry->timemodified = time();
  663. $newentry->author = $USER->id;
  664. $newentry->timecreated = time();
  665. $newentry->evaluate = (String) $application_xml->evaluate;
  666. $newentry->parent = $iassign_ilm->id;
  667. //MOOC 2016 $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  668. $iassign_ilmid = $DB->insert_record("iassign_ilm", $newentry);
  669. if ($application_xml->params->param) {
  670. foreach ($application_xml->params->param as $value) {
  671. $newentry = new stdClass();
  672. $newentry->iassign_ilmid = $iassign_ilmid;
  673. $newentry->param_type = (String) $value->type;
  674. $newentry->param_name = (String) $value->name;
  675. $newentry->param_value = (String) $value->value;
  676. $newentry->description = html_entity_decode((String) $value->description);
  677. $newentry->visible = (String) $value->visible;
  678. $newentry->id = $DB->insert_record("iassign_ilm", $newentry);
  679. if (!$newentry->id) {
  680. print_error('error_add_param', 'iassign');
  681. }
  682. }
  683. }
  684. }
  685. print($OUTPUT->notification(get_string('ok_import_ilm_version', 'iassign'), 'notifysuccess'));
  686. } // else if ($iassign_ilm)
  687. $fs->delete_area_files($contextuser->id, 'user', 'draft', $itemid);
  688. }
  689. /// Function for save iLM from XML descriptor
  690. // @param array $application_xml Data of XML descriptor
  691. // @param array $files_extract Filenames of extract files
  692. // @return array Return an array content id of JAR files
  693. static function save_ilm_by_xml($application_xml, $files_extract) {
  694. global $CFG, $USER, $OUTPUT;
  695. // Check if the iLM directory is writable
  696. if (!is_writable("ilm/")) {
  697. print($OUTPUT->notification(get_string('error_folder_permission_denied', 'iassign'), 'notifyproblem'));
  698. return null;
  699. }
  700. // Check if iLM directory already exists
  701. if (!file_exists("ilm/" . $application_xml->name)) {
  702. // mkdir("ilm/" . $application_xml->name, 0777, true);
  703. mkdir("ilm/" . $application_xml->name, 0755, true); // permissions: drwxr-xr-x
  704. }
  705. // Check if iLM version already exists in directory
  706. if (!file_exists("ilm/" . $application_xml->name . "/" . $application_xml->version)) {
  707. // mkdir("ilm/" . $application_xml->name . "/" . $application_xml->version, 0777, true);
  708. mkdir("ilm/" . $application_xml->name . "/" . $application_xml->version, 0755, true); // drwxr-xr-x
  709. }
  710. else {
  711. print($OUTPUT->notification(get_string('error_import_ilm_version', 'iassign'), 'notifyproblem'));
  712. return null;
  713. }
  714. $root_ilm = "ilm/" . $application_xml->name . "/" . $application_xml->version;
  715. // Extract iLM files to directory
  716. foreach ($files_extract as $key => $value) {
  717. $file = $CFG->dataroot . '/temp/' . $key;
  718. if ($key == $application_xml->file_jar)
  719. copy($file, $root_ilm . "/" . $key);
  720. unlink($file);
  721. } // foreach ($files_extract as $key => $value)
  722. return $root_ilm . "/" . $application_xml->file_jar;
  723. }
  724. /// Function for list the directory where iLM is allocated.
  725. // @param type $dir
  726. // @return type
  727. static function list_directory ($dir) {
  728. $files = array();
  729. $cont = 0;
  730. $ffs = scandir($dir);
  731. unset($ffs[array_search('.', $ffs, true)]);
  732. unset($ffs[array_search('..', $ffs, true)]);
  733. if (count($ffs) < 1) {
  734. return;
  735. }
  736. foreach ($ffs as $ff) {
  737. $files[$cont] = $dir . "/" . $ff;
  738. $cont++;
  739. if (is_dir($dir . '/' . $ff)) {
  740. $temp = self::list_directory($dir . '/' . $ff);
  741. foreach ($temp as $t) {
  742. $files[$cont] = $t;
  743. $cont++;
  744. }
  745. }
  746. }
  747. return $files;
  748. }
  749. /// Function for delete directory where the iLM is allocated.
  750. // @param string $dirPath
  751. // @throws InvalidArgumentException
  752. public static function delete_dir ($dirPath) {
  753. if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
  754. $dirPath .= '/';
  755. }
  756. $files = glob($dirPath . '*', GLOB_MARK);
  757. foreach ($files as $file) {
  758. if (is_dir($file)) {
  759. self::delete_dir($file);
  760. } else {
  761. unlink($file);
  762. }
  763. }
  764. rmdir($dirPath);
  765. }
  766. }
  767. ?>