ilm_manager.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * iLM manager
  4. *
  5. * Release Notes
  6. * - v 2.7 2017/03/10
  7. * + added the first parameter in call 'locallib.php : view_files_ilm($iassign_ilm, $iassign_ilm->extension)'
  8. * locallib.php : function view_files_ilm($iassign_ilm, $extension) 6184/6732
  9. * $link_filter = "&nbsp;&nbsp;<a href='#' onclick='preview_ilm(" . $fileid . "," . $ilmid . ");'>"... 6441/6734
  10. * $link_filter = "&nbsp;&nbsp;<a href='#' onclick='preview_ilm(" . $fileid . "," . $ilmid . ");'>"... 6447/6734
  11. *
  12. * - v 2.6 2016/05/12
  13. * + the 'utils' class was changed to "iassign utils"
  14. * - v 2.5 2016/02/16
  15. * + In 'optional_param(...)', some 'PARAM_ALPHANUMEXT' changed by 'PARAM_TEXT'
  16. * + Fixed bug, now allow to rename iLM files
  17. * - v 2.4 2013/10/24
  18. * + Insert function for recover iassign file in course.
  19. * - v 2.3 2013/08/26
  20. * + Fix bug to upload file from block.
  21. * - v 2.2 2013/08/23
  22. * + Fix bug to import zip files.
  23. * - v 2.1 2013/08/22
  24. * + Merge for import zip files and iassign files.
  25. * + Insert function for rename iassign file.
  26. * - v 2.0 2013/08/21
  27. * + Change title link with message for get file for donwload file.
  28. * + Manage import files.
  29. * + Rename files for format accepted.
  30. * + Change position of close and return buttons.
  31. * - v 1.9 2013/08/15
  32. * + Insert functions for import files, export files and remove selected files.
  33. * - v 1.8 2013/08/02
  34. * + Insert return button for block view.
  35. * + Insert close button for iassign view.
  36. * - v 1.7 2013/07/03
  37. * + Replace var 'DIRECTORY_SEPARATOR' for '/' (Server on Windows error of section)
  38. * + Diferent view of block and iassign in files views.
  39. * + Change button of open online editor ('open_online_ilm()').
  40. * + View modified and created date in files views.
  41. * - v 1.3 2013/06/28
  42. * + Correction function delete and duplicate.
  43. * + Allow copying the file from another user.
  44. * - v 1.1 2013/06/26
  45. * + Filter file extension for permission only compatilbe with iLM and block view all user files.
  46. *
  47. * @author Patricia Alves Rodrigues
  48. * @author Leônidas O. Brandão
  49. * @contributor Luciano Oliveira Borges
  50. * @version v 2.6 2016/05/12
  51. * @package mod_iassign_ilm
  52. * @since 2012/01/10
  53. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  54. *
  55. * <b>License</b>
  56. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  57. */
  58. require_once("../../config.php");
  59. require_once($CFG->dirroot . '/mod/iassign/lib.php');
  60. require_once($CFG->dirroot . '/mod/iassign/locallib.php');
  61. require_once($CFG->dirroot . '/mod/iassign/ilm_manager_form.php');
  62. //DEBUG To exam files in Moodle data
  63. //D $id = optional_param('id', 0, PARAM_INT); // Course Module ID
  64. //D $action = optional_param('action', NULL, PARAM_TEXT); //2016:: PARAM_ALPHANUMEXT
  65. //D $from = optional_param('from', NULL, PARAM_TEXT); //2016:: PARAM_ALPHANUMEXT
  66. //D $ilmid = optional_param('ilmid', 1, PARAM_INT);
  67. //D $fileid = optional_param('fileid', 1, PARAM_INT);
  68. //D $url = "$CFG->wwwroot/mod/iassign/ilm_manager.php?from=$from&id=$id&ilmid=$ilmid";
  69. //D $ilm_manager_instance = new ilm_manager($id, $url, $from);
  70. //D echo "ilm_manager.php: ilmid=$ilmid, fileid=$fileid<br/>" . $ilm_manager_instance->get_file_ilm($ilmid, $fileid); exit;
  71. //D print $ilm_manager_instance->get_file_ilm($ilmid, $fileid);
  72. //D exit;
  73. require_login();
  74. if (isguestuser()) {
  75. die();
  76. }
  77. if (session_id() === "")
  78. session_start();
  79. // Prepare iLM content file in secure are to the iLM access it
  80. function prepare_secure_access ($ilmid, $fileid, $userid) {
  81. require_once ('ilm_security.php');
  82. // ./lib/moodlelib.php : function get_file_storage(): $fs = new file_storage($filedir, $trashdirdir, "$CFG->tempdir/filestorage", $CFG->directorypermissions, $CFG->filepermissions);
  83. // ./lib/filestorage/file_storage.php : class file_storage
  84. $fs = get_file_storage();
  85. $md_file = $fs->get_file_by_id($fileid);
  86. $ilm_content_file = $md_file->get_content();
  87. $timecreated = time(); $token = md5($timecreated);
  88. //D echo "ilm_manager.php: prepare_secure_access($ilmid,$fileid,$userid): <br/>";
  89. //print_r($md_file); echo "<br/>";
  90. $filename = $md_file->get_filename(); // $md_file is instanceof 'class file_storage' from './lib/filestorage/file_storage.php'
  91. //D echo "md_file->file_record->filename=" . $filename . "<br/>";
  92. $id_iLM_security = ilm_security::write_iLM_security($userid, $timecreated, -1, $ilm_content_file); // insert in 'iassign_security': class with: iassign_statementid, userid, file,...
  93. $security = array('filename' => $filename, 'content' => $ilm_content_file, 'token' => $token, 'secure_id' => $id_iLM_security);
  94. //D echo "ilm_manager.php: prepare_secure_access($ilmid,$fileid,$userid): filename=".$security['filename']."; " .$security['content']. " " .$security['token']. " " .$security['secure_id']. "<br/>";
  95. return $security;
  96. }
  97. // Parameters GET or POST
  98. $id = optional_param('id', 0, PARAM_INT); // Course Module ID
  99. $action = optional_param('action', NULL, PARAM_TEXT); //2016:: PARAM_ALPHANUMEXT
  100. $from = optional_param('from', NULL, PARAM_TEXT); //2016:: PARAM_ALPHANUMEXT
  101. $ilmid = optional_param('ilmid', 1, PARAM_INT);
  102. if ($id>0) { // if reach here by iLM get request, id is not defined!
  103. $contextuser = context_user::instance($USER->id);
  104. $context = context_course::instance($id);
  105. }
  106. $url = $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=$from&id=$id&ilmid=$ilmid";
  107. // http://localhost/moodle321p/mod/iassign/ilm_manager.php?id=2&from=iassign&ilmid=7#
  108. // -> http://localhost/moodle321p/mod/iassign/ilm_manager.php?from=iassign&id=2&action=preview&fileid=53
  109. //xx $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); //QUARANTINE it is not necessary to present the course in the header...
  110. $iassign_ilm = $DB->get_record('iassign_ilm', array('id' => $ilmid));
  111. if (empty($iassign_ilm)) {
  112. $iassign_ilm = new stdClass();
  113. $iassign_ilm->name = "";
  114. $iassign_ilm->extension = "";
  115. }
  116. $returnurl = optional_param('returnurl', NULL, PARAM_TEXT);
  117. //d echo "<br/><br/>ilm_manager.php: returnurl=$returnurl<br/>";
  118. if ($returnurl != NULL)
  119. $_SESSION['returnurl'] = optional_param('returnurl', $CFG->wwwroot . "/course/view.php?id=$id&ilmid=$ilmid", PARAM_TEXT); //2016:: PARAM_ALPHANUMEXT
  120. $title = get_string('ilm_manager_title', 'iassign');
  121. if ($id>0) { // if reach here by iLM get request, id is not defined!
  122. $PAGE->set_url($url);
  123. $PAGE->set_context($context);
  124. //xx $PAGE->set_course($course);
  125. $PAGE->blocks->show_only_fake_blocks();
  126. //xx $PAGE->set_title($course->fullname);
  127. if (is_null($action)) // first load: with iLM identification and with all iLM files of this iLM
  128. $PAGE->navbar->add($title);
  129. else
  130. $PAGE->navbar->add($title, $url);
  131. $PAGE->set_heading($title);
  132. }
  133. //d $fileid = optional_param('fileid', 1, PARAM_INT); echo "ilm_manager.php: ilmid=$ilmid, fileid=$fileid, id=$id, user.id=$USER->id<br/>"; //exit;
  134. //Test: if (has_capability('mod/iassign:editiassign', $context, $USER->id)) echo "ilm_manager.php: permissao!<br/>"; else echo "ilm_manager.php: SEM permissao!<br/>";
  135. if (has_capability('mod/iassign:editiassign', $context, $USER->id)) {
  136. $ilm_manager_instance = new ilm_manager($id, $url, $from); // locallib.php: class ilm_manager
  137. $dirid = $ilm_manager_instance->get_dir_ilm('dirid');
  138. // Enter here whenever : teacher is viewing any iLM content (preview), here is called by the iLM do not verify credentials.
  139. if ($action == 'get') {
  140. $fileid = optional_param('fileid', 1, PARAM_INT);
  141. return $ilm_manager_instance->get_file_ilm($ilmid, $fileid);
  142. }
  143. if ($action) { // avoid several tests when empty...
  144. switch ($action) { // '$ilm_manager_instance' instace of 'locallib.php : class ilm_manager'
  145. case 'new':
  146. $ilm_manager_instance->ilm_editor_new(); // ./mod/iassign/locallib.php: function ilm_editor_new()
  147. break;
  148. case 'update':
  149. $fileid = optional_param('fileid', NULL, PARAM_INT);
  150. $security = prepare_secure_access($ilmid, $fileid, $USER->id);
  151. //x $ilm_manager_instance->ilm_editor_update();
  152. $ilm_manager_instance->ilm_editor_update($security['filename'], $security['content'], $security['token'], $security['secure_id']);
  153. break;
  154. case 'delete':
  155. $ilm_manager_instance->delete_file_ilm();
  156. break;
  157. case 'duplicate':
  158. $ilm_manager_instance->duplicate_file_ilm();
  159. break;
  160. case 'rename':
  161. $ilm_manager_instance->rename_file_ilm();
  162. break;
  163. case 'preview':
  164. // The function bellow calls '/filter/iassign_filter/filter.php' function 'filter($text, array $options = array())' and exit (do not continue bellow)
  165. //// prepare_secure_access(): array('content' => $ilm_content_file, 'token' => $token, 'secure_id' => $id_iLM_security)
  166. $ilm_manager_instance->preview_ilm($id, $iassign_ilm, $iassign_statementid); // in '/mod/iassign/locallib.php'
  167. break;
  168. case 'addilm':
  169. $ilm_manager_instance->add_ilm();
  170. break;
  171. case 'tinymceilm':
  172. $fileid = optional_param('fileid', NULL, PARAM_INT);
  173. $ilm_manager_instance->editor_ilm($fileid, 'tinyMCE');
  174. // $ilm_manager_instance->tinymce_ilm($fileid);
  175. break;
  176. case 'attoilm':
  177. $fileid = optional_param('fileid', NULL, PARAM_INT);
  178. $ilm_manager_instance->editor_ilm($fileid, 'atto');
  179. // $ilm_manager_instance->atto_ilm($fileid);
  180. break;
  181. case 'export':
  182. $ilm_manager_instance->export_files_ilm();
  183. break;
  184. case 'import':
  185. $ilm_manager_instance->import_files_ilm();
  186. break;
  187. case 'selected_delete':
  188. $ilm_manager_instance->delete_selected_ilm();
  189. break;
  190. case 'new_dir':
  191. $ilm_manager_instance->new_dir_ilm();
  192. break;
  193. case 'delete_dir':
  194. $ilm_manager_instance->delete_dir_ilm();
  195. break;
  196. case 'rename_dir':
  197. $ilm_manager_instance->rename_dir_ilm();
  198. break;
  199. case 'selected_move':
  200. $ilm_manager_instance->selected_move_ilm();
  201. break;
  202. case 'move':
  203. $ilm_manager_instance->move_files_ilm();
  204. break;
  205. case 'recover':
  206. $ilm_manager_instance->recover_files_ilm();
  207. break;
  208. }
  209. } // if ($action)
  210. $mform = new ilm_manager_form();
  211. $param = new stdClass();
  212. $param->id = $id;
  213. $param->from = $from;
  214. $param->ilmid = $ilmid;
  215. $param->dirid = $dirid;
  216. $mform->set_data($param);
  217. if ($mform->is_cancelled()) {
  218. redirect(new moodle_url("/course/view.php?id=$id"));
  219. }
  220. else if ($formdata = $mform->get_data()) { // if exists '$mform->get_data()' use with '$formdata'
  221. $fs = get_file_storage();
  222. if ($formdata->dirid == 0)
  223. $dir_base = '/';
  224. else {
  225. $dir_base = $fs->get_file_by_id($formdata->dirid);
  226. $dir_base = $dir_base->get_filepath();
  227. }
  228. if ($newfilename = $mform->get_new_filename('file')) {
  229. $url = $CFG->wwwroot . "/mod/iassign/ilm_manager.php?from=$formdata->from&id=$id&ilmid=$ilmid&dirid=$formdata->dirid";
  230. $file_extension_array = explode(".", $newfilename);
  231. $index_last_ext = count($file_extension_array) - 1;
  232. if ($index_last_ext < 0)
  233. $file_extension = "";
  234. else
  235. $file_extension = $file_extension_array[$index_last_ext];
  236. if (strtolower($file_extension) != 'zip') {
  237. $filename = $newfilename;
  238. $files_course = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir_base, false, true, 'filename');
  239. if ($files_course) {
  240. foreach ($files_course as $value) {
  241. if ($value->get_filename() == iassign_utils::format_filename($newfilename))
  242. $filename = iassign_utils::version_filename($value->get_filename());
  243. }
  244. }
  245. $ilm_extensions = explode(",", $iassign_ilm->extension);
  246. if (in_array($file_extension, $ilm_extensions))
  247. $file = $mform->save_stored_file('file', $context->id, 'mod_iassign', 'activity', 0, $dir_base, iassign_utils::format_filename($filename), 0, $USER->id);
  248. else if ($from == 'block' || $from == 'tinymce' || $from == 'atto')
  249. $file = $mform->save_stored_file('file', $context->id, 'mod_iassign', 'activity', 0, $dir_base, iassign_utils::format_filename($filename), 0, $USER->id);
  250. else
  251. $url .= "&error=incompatible_extension_file";
  252. }
  253. else { // if (strtolower($file_extension[1]) != 'zip')
  254. $zip_filename = $CFG->dataroot . '/temp/' . $newfilename;
  255. $zip = new zip_packer();
  256. $mform->save_file('file', $zip_filename, true) or die("Save file not found");
  257. $zip_files = $zip->list_files($zip_filename);
  258. $files = $fs->get_directory_files($context->id, 'mod_iassign', 'activity', 0, $dir_base, false, true, 'filename');
  259. //TODO: --- inicio : linhas abaixo estavam comentadas, mas noutra versao funcional (MOOC) estao ativas
  260. $rename_files = array();
  261. foreach ($zip_files as $zip_file) {
  262. foreach ($files as $file) {
  263. if (iassign_utils::format_filename($zip_file->original_pathname) == $file->get_filename())
  264. $rename_files = array_merge($rename_files, array(iassign_utils::version_filename(iassign_utils::format_filename($zip_file->original_pathname)) => iassign_utils::format_filename($zip_file->original_pathname)));
  265. }
  266. } //TODO: --- final 2016/02/16
  267. $zip->extract_to_storage($zip_filename, $context->id, 'mod_iassign', 'activity', 0, $dir_base, $USER->id);
  268. //TODO: --- inicio : linhas abaixo estavam comentadas, mas noutra versao funcional (MOOC) estao ativas
  269. $files = $fs->get_area_files($context->id, 'mod_iassign', 'activity', 0, 'filename');
  270. foreach ($files as $file) {
  271. if ($file->get_author() == "") {
  272. $file->set_author($USER->firstname . ' ' . $USER->lastname);
  273. if ($new_name = array_search($file->get_filename(), $rename_files))
  274. $file->rename($dir_base, $new_name);
  275. else if ($file->get_filename() != '.' && $file->get_filename() != iassign_utils::format_filename($file->get_filename()))
  276. $file->rename($dir_base, iassign_utils::format_filename($file->get_filename()));
  277. }
  278. } //TODO: --- final 2016/02/16
  279. unlink($zip_filename);
  280. } // else if (strtolower($file_extension[1]) != 'zip')
  281. $fs->delete_area_files($contextuser->id, 'user', 'draft', $formdata->file);
  282. } // if ($newfilename = $mform->get_new_filename('file'))
  283. redirect(new moodle_url($url));
  284. } // else if ($formdata = $mform->get_data()) - 172/271,10
  285. print $OUTPUT->header();
  286. if ($from == 'iassign') { // came from iAssign (./mod/iassign/ilm_manager_form.php) => is selection of iLM activity file
  287. print ' <div width=100% align=right style="margin: 20px 20px 20px 20px;">' . "\n" .
  288. ' <input type=button value="' . get_string('close', 'iassign') . '" ' .
  289. ' title="' . get_string('close_alt', 'iassign') . '" onclick="javascript:window.close();" /></div>' . "\n"; // 'Close this window (any change will be lost)'
  290. }
  291. else
  292. if ($from == 'block') {
  293. //returnurl
  294. if (isset($_SERVER['HTTP_REFERER']))
  295. $strh=$_SERVER['HTTP_REFERER']; else $strh="<>, from=$from<br/>";
  296. if (isset($_SERVER['HTTP_REFERER']))
  297. print '<div width=100% align=right style="margin: 20px 20px 20px 20px;"><input type=button value="' . get_string('return', 'iassign') . '" onclick="javascript:window.location = \'' . $_SESSION['returnurl'] . '\';"></div>' . "\n";
  298. }
  299. else if ($from == 'tinymce' || $from == 'atto') {
  300. print '<div width=100% align=right style="margin: 20px 20px 20px 20px;"><input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close ();"></div>' . "\n";
  301. }
  302. else if ($from == 'qtype') {
  303. // print $OUTPUT->header();
  304. print $OUTPUT->heading($title);
  305. print '<div width=100% align=right style="margin: 20px 20px 20px 20px;"><input type=button value="' . get_string('close', 'iassign') . '" onclick="javascript:window.close ();"></div>' . "\n";
  306. }
  307. if (!is_null($error = optional_param('error', NULL, PARAM_TEXT)))
  308. print $OUTPUT->notification(get_string($error, 'iassign'), 'notifyproblem');
  309. $mform->display();
  310. $ilm_manager_instance->view_files_ilm($iassign_ilm, $iassign_ilm->extension); // locallib.php : function view_files_ilm($iassign_ilm, $extension)
  311. print $OUTPUT->footer();
  312. die;
  313. } // if (has_capability('mod/iassign:editiassign', $context, $USER->id))