ilm_manager.php 17 KB

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