1
0

java.php 42 KB

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