document_lite.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Light version of documents manager (currently disabled)
  5. * @package chamilo.document
  6. */
  7. /**
  8. * Code
  9. */
  10. exit;
  11. // Language files that need to be included
  12. $language_file = array('document', 'slideshow', 'gradebook', 'create_course');
  13. require_once '../inc/global.inc.php';
  14. $this_section = SECTION_COURSES;
  15. require_once 'document.inc.php';
  16. $lib_path = api_get_path(LIBRARY_PATH);
  17. /* Libraries */
  18. require_once $lib_path.'document.lib.php';
  19. api_protect_course_script(true);
  20. $htmlHeadXtra[] = api_get_jqgrid_js();
  21. $course_info = api_get_course_info();
  22. $course_dir = $course_info['path'].'/document';
  23. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  24. $base_work_dir = $sys_course_path.$course_dir;
  25. $http_www = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document';
  26. $dbl_click_id = 0; // Used for avoiding double-click
  27. /* Constants and variables */
  28. $session_id = api_get_session_id();
  29. $course_code = api_get_course_id();
  30. $to_group_id = api_get_group_id();
  31. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  32. $group_member_with_upload_rights = false;
  33. // If the group id is set, we show them group documents
  34. $group_properties = array();
  35. $group_properties['directory'] = null;
  36. // For sessions we should check the parameters of visibility
  37. if (api_get_session_id() != 0) {
  38. $group_member_with_upload_rights = $group_member_with_upload_rights && api_is_allowed_to_session_edit(false, true);
  39. }
  40. //Actions
  41. $document_id = intval($_REQUEST['id']);
  42. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  43. switch ($action) {
  44. case 'download':
  45. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  46. // Check whether the document is in the database
  47. if (empty($document_data)) {
  48. // File not found!
  49. header('HTTP/1.0 404 Not Found');
  50. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  51. $error404 .= '<html><head>';
  52. $error404 .= '<title>404 Not Found</title>';
  53. $error404 .= '</head><body>';
  54. $error404 .= '<h1>Not Found</h1>';
  55. $error404 .= '<p>The requested URL was not found on this server.</p>';
  56. $error404 .= '<hr>';
  57. $error404 .= '</body></html>';
  58. echo $error404;
  59. exit;
  60. }
  61. // Launch event
  62. event_download($document_data['url']);
  63. // Check visibility of document and paths
  64. if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible_by_id(
  65. $document_id,
  66. $course_info,
  67. api_get_session_id(),
  68. api_get_user_id()
  69. )
  70. ) {
  71. api_not_allowed(true);
  72. }
  73. $full_file_name = $base_work_dir.$document_data['path'];
  74. if (Security::check_abs_path($full_file_name, $base_work_dir.'/')) {
  75. DocumentManager::file_send_for_download($full_file_name, true);
  76. }
  77. exit;
  78. break;
  79. case 'downloadfolder' :
  80. if (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin(
  81. )
  82. ) {
  83. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  84. //filter when I am into shared folder, I can donwload only my shared folder
  85. if (is_any_user_shared_folder($document_data['path'], $session_id)) {
  86. if (is_my_shared_folder(
  87. api_get_user_id(),
  88. $document_data['path'],
  89. $session_id
  90. ) || api_is_allowed_to_edit() || api_is_platform_admin()
  91. ) {
  92. require 'downloadfolder.inc.php';
  93. }
  94. } else {
  95. require 'downloadfolder.inc.php';
  96. }
  97. exit;
  98. }
  99. break;
  100. }
  101. //If no actions we proceed to show the document (Hack in order to use document.php?id=X)
  102. if (isset($document_id)) {
  103. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  104. //If the document is not a folder we show the document
  105. if ($document_data) {
  106. $parent_id = $document_data['parent_id'];
  107. //$visibility = DocumentManager::is_visible_by_id($document_id, $course_info, api_get_session_id(), api_get_user_id());
  108. $visibility = DocumentManager::check_visibility_tree(
  109. $document_id,
  110. api_get_course_id(),
  111. api_get_session_id(),
  112. api_get_user_id()
  113. );
  114. if (!empty($document_data['filetype']) && $document_data['filetype'] == 'file') {
  115. if ($visibility && api_is_allowed_to_session_edit()) {
  116. $url = api_get_path(
  117. WEB_COURSE_PATH
  118. ).$course_info['path'].'/document'.$document_data['path'].'?'.api_get_cidreq();
  119. header("Location: $url");
  120. }
  121. exit;
  122. } else {
  123. if (!$visibility && !api_is_allowed_to_edit()) {
  124. api_not_allowed();
  125. }
  126. }
  127. $_GET['curdirpath'] = $document_data['path'];
  128. }
  129. // What's the current path?
  130. // We will verify this a bit further down
  131. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  132. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  133. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  134. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  135. } else {
  136. $curdirpath = '/';
  137. }
  138. $curdirpathurl = urlencode($curdirpath);
  139. } else {
  140. // What's the current path?
  141. // We will verify this a bit further down
  142. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  143. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  144. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  145. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  146. } else {
  147. $curdirpath = '/';
  148. }
  149. $curdirpathurl = urlencode($curdirpath);
  150. // Check the path
  151. // If the path is not found (no document id), set the path to /
  152. $document_id = DocumentManager::get_document_id($course_info, $curdirpath);
  153. if (!$document_id) {
  154. $document_id = DocumentManager::get_document_id($course_info, $curdirpath);
  155. }
  156. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  157. $parent_id = $document_data['parent_id'];
  158. }
  159. $current_folder_id = $document_id;
  160. // Is the document tool visible?
  161. // Check whether the tool is actually visible
  162. $table_course_tool = Database::get_course_table(TABLE_TOOL_LIST);
  163. $course_id = api_get_course_int_id();
  164. $tool_sql = 'SELECT visibility FROM '.$table_course_tool.' WHERE c_id = '.$course_id.' AND name = "'.TOOL_DOCUMENT.'" LIMIT 1';
  165. $tool_result = Database::query($tool_sql);
  166. $tool_row = Database::fetch_array($tool_result);
  167. $tool_visibility = $tool_row['visibility'];
  168. if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $group_member_with_upload_rights)) {
  169. api_not_allowed(true);
  170. }
  171. $htmlHeadXtra[] =
  172. "<script type=\"text/javascript\">
  173. function confirmation (name) {
  174. if (confirm(\" ".get_lang("AreYouSureToDelete")." \"+ name + \" ?\"))
  175. {return true;}
  176. else
  177. {return false;}
  178. }
  179. </script>";
  180. // If they are looking at group documents they can't see the root
  181. if ($to_group_id != 0 && $curdirpath == '/') {
  182. $curdirpath = $group_properties['directory'];
  183. $curdirpathurl = urlencode($group_properties['directory']);
  184. }
  185. // Check visibility of the current dir path. Don't show anything if not allowed
  186. //@todo check this validation for coaches
  187. //if (!$is_allowed_to_edit || api_is_coach()) { before
  188. if (!$is_allowed_to_edit && api_is_coach()) {
  189. if ($curdirpath != '/' && !(DocumentManager::is_visible($curdirpath, $_course, api_get_session_id(), 'folder'))) {
  190. api_not_allowed(true);
  191. }
  192. }
  193. /* MAIN SECTION */
  194. // Slideshow inititalisation
  195. $_SESSION['image_files_only'] = '';
  196. $image_files_only = '';
  197. /* Header */
  198. if ($is_certificate_mode) {
  199. $interbreadcrumb[] = array('url' => '../gradebook/index.php', 'name' => get_lang('Gradebook'));
  200. } else {
  201. if ((isset($_GET['id']) && $_GET['id'] != 0) || isset($_GET['curdirpath']) || isset($_GET['createdir'])) {
  202. $interbreadcrumb[] = array('url' => 'document.php', 'name' => get_lang('Documents'));
  203. } else {
  204. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Documents'));
  205. }
  206. }
  207. // Interbreadcrumb for the current directory root path
  208. if (empty($document_data['parents'])) {
  209. if (isset($_GET['createdir'])) {
  210. $interbreadcrumb[] = array('url' => $document_data['document_url'], 'name' => $document_data['title']);
  211. } else {
  212. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  213. }
  214. } else {
  215. foreach ($document_data['parents'] as $document_sub_data) {
  216. if (!isset($_GET['createdir']) && $document_sub_data['id'] == $document_data['id']) {
  217. $document_sub_data['document_url'] = '#';
  218. }
  219. $interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']);
  220. }
  221. }
  222. if (isset($_GET['createdir'])) {
  223. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('CreateDir'));
  224. }
  225. $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
  226. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.$js_path.'jquery-jplayer/skins/chamilo/jplayer.blue.monday.css" type="text/css">';
  227. $htmlHeadXtra[] = '<script type="text/javascript" src="'.$js_path.'jquery-jplayer/jquery.jplayer.min.js"></script>';
  228. $mediaplayer_path = api_get_path(WEB_LIBRARY_PATH).'mediaplayer/player.swf';
  229. $docs_and_folders = DocumentManager::get_all_document_data(
  230. $_course,
  231. $curdirpath,
  232. $to_group_id,
  233. null,
  234. $is_allowed_to_edit || $group_member_with_upload_rights,
  235. false
  236. );
  237. $file_list = $format_list = '';
  238. $count = 1;
  239. if (!empty($docs_and_folders)) {
  240. foreach ($docs_and_folders as $file) {
  241. if ($file['filetype'] == 'file') {
  242. $path_info = pathinfo($file['path']);
  243. $extension = strtolower($path_info['extension']);
  244. //@todo use a js loop to autogenerate this code
  245. if (in_array($extension, array('ogg', 'mp3', 'wav'))) {
  246. $document_data = DocumentManager::get_document_data_by_id($file['id'], api_get_course_id());
  247. if ($extension == 'ogg') {
  248. $extension = 'oga';
  249. }
  250. $jquery .= ' $("#jquery_jplayer_'.$count.'").jPlayer({
  251. ready: function() {
  252. $(this).jPlayer("setMedia", {
  253. '.$extension.' : "'.$document_data['direct_url'].'"
  254. });
  255. },
  256. swfPath: "'.$js_path.'jquery-jplayer",
  257. supplied: "mp3, m4a, oga, ogv, wav",
  258. solution: "flash, html", // Do not change this setting otherwise
  259. cssSelectorAncestor: "#jp_interface_'.$count.'",
  260. });'."\n\n";
  261. $count++;
  262. }
  263. }
  264. }
  265. }
  266. $htmlHeadXtra[] = '<script type="text/javascript">
  267. $(document).ready( function() {
  268. //Experimental changes to preview mp3, ogg files
  269. '.$jquery.'
  270. //Keep this down otherwise the jquery player will not work
  271. for (i=0;i<$(".actions").length;i++) {
  272. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  273. $(".actions:eq("+i+")").hide();
  274. }
  275. }
  276. });
  277. </script>';
  278. // Lib for event log, stats & tracking & record of the access
  279. event_access_tool(TOOL_DOCUMENT);
  280. /* DISPLAY */
  281. if ($to_group_id != 0) { // Add group name after for group documents
  282. $add_group_to_title = ' ('.$group_properties['name'].')';
  283. }
  284. /* Introduction section (editable by course admins) */
  285. if (!empty($_SESSION['_gid'])) {
  286. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  287. } else {
  288. Display::display_introduction_section(TOOL_DOCUMENT);
  289. }
  290. // ACTION MENU
  291. // Copy a file to general my files user's
  292. if (isset($_GET['action']) && $_GET['action'] == 'copytomyfiles' && api_get_setting(
  293. 'users_copy_files'
  294. ) == 'true' && api_get_user_id() != 0
  295. ) {
  296. $clean_get_id = Security::remove_XSS($_GET['id']);
  297. $my_path = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'system');
  298. $user_folder = $my_path['dir'].'my_files/';
  299. $my_path = null;
  300. if (!file_exists($user_folder)) {
  301. $perm = api_get_permissions_for_new_directories();
  302. @mkdir($user_folder, $perm, true);
  303. }
  304. $file = $sys_course_path.$_course['path'].'/document'.$clean_get_id;
  305. $copyfile = $user_folder.basename($clean_get_id);
  306. if (file_exists($copyfile)) {
  307. $message = get_lang('CopyAlreadyDone').'</p><p>'.'<a href="'.api_get_self().'?'.api_get_cidreq(
  308. ).'&amp;curdirpath='.$curdirpath.'">'.get_lang("No").'</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'.api_get_self(
  309. ).'?'.api_get_cidreq(
  310. ).'&amp;curdirpath='.$curdirpath.'&amp;action=copytomyfiles&amp;id='.$clean_get_id.'&amp;copy=yes">'.get_lang(
  311. 'Yes'
  312. ).'</a></p>';
  313. if (!isset($_GET['copy'])) {
  314. Display::display_warning_message($message, false);
  315. }
  316. if (Security::remove_XSS($_GET['copy']) == 'yes') {
  317. if (!copy($file, $copyfile)) {
  318. Display::display_error_message(get_lang('CopyFailed'));
  319. } else {
  320. Display::display_confirmation_message(get_lang('OverwritenFile'));
  321. }
  322. }
  323. } else {
  324. if (!copy($file, $copyfile)) {
  325. Display::display_error_message(get_lang('CopyFailed'));
  326. } else {
  327. Display::display_confirmation_message(get_lang('CopyMade'));
  328. }
  329. }
  330. }
  331. /* MOVE FILE OR DIRECTORY */
  332. //Only teacher and all users into their group and each user into his/her shared folder
  333. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  334. api_get_user_id(),
  335. $curdirpath,
  336. $session_id
  337. ) || is_my_shared_folder(api_get_user_id(), Security::remove_XSS($_POST['move_to']), $session_id)
  338. ) {
  339. if (isset($_GET['move']) && $_GET['move'] != '') {
  340. $my_get_move = intval($_REQUEST['move']);
  341. if (api_is_coach()) {
  342. if (!DocumentManager::is_visible_by_id(
  343. $my_get_move,
  344. $course_info,
  345. api_get_session_id(),
  346. api_get_user_id()
  347. )
  348. ) {
  349. api_not_allowed();
  350. }
  351. }
  352. if (!$is_allowed_to_edit) {
  353. if (DocumentManager::check_readonly($_course, api_get_user_id(), $my_get_move)) {
  354. api_not_allowed();
  355. }
  356. }
  357. $document_to_move = DocumentManager::get_document_data_by_id($my_get_move, api_get_course_id());
  358. $move_path = $document_to_move['path'];
  359. if (!empty($document_to_move)) {
  360. $folders = DocumentManager::get_all_document_folders(
  361. $_course,
  362. $to_group_id,
  363. $is_allowed_to_edit || $group_member_with_upload_rights
  364. );
  365. //filter if is my shared folder. TODO: move this code to build_move_to_selector function
  366. if (is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id) && !$is_allowed_to_edit) {
  367. $main_user_shared_folder_main = '/shared_folder/sf_user_'.api_get_user_id(
  368. ); //only main user shared folder
  369. $main_user_shared_folder_sub = '/shared_folder\/sf_user_'.api_get_user_id().'\//'; //all subfolders
  370. $user_shared_folders = array();
  371. foreach ($folders as $fold) {
  372. if ($main_user_shared_folder_main == $fold || preg_match($main_user_shared_folder_sub, $fold)) {
  373. $user_shared_folders[] = $fold;
  374. }
  375. }
  376. echo '<legend>'.get_lang('Move').'</legend>';
  377. echo build_move_to_selector(
  378. $user_shared_folders,
  379. $move_path,
  380. $my_get_move,
  381. $group_properties['directory']
  382. );
  383. } else {
  384. echo '<legend>'.get_lang('Move').'</legend>';
  385. echo build_move_to_selector($folders, $move_path, $my_get_move, $group_properties['directory']);
  386. }
  387. }
  388. }
  389. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  390. if (!$is_allowed_to_edit) {
  391. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_POST['move_file'])) {
  392. api_not_allowed();
  393. }
  394. }
  395. if (api_is_coach()) {
  396. if (!DocumentManager::is_visible_by_id(
  397. $_POST['move_file'],
  398. $_course,
  399. api_get_session_id(),
  400. api_get_user_id()
  401. )
  402. ) {
  403. api_not_allowed();
  404. }
  405. }
  406. $document_to_move = DocumentManager::get_document_data_by_id($_POST['move_file'], api_get_course_id());
  407. // Security fix: make sure they can't move files that are not in the document table
  408. if (!empty($document_to_move)) {
  409. $real_path_target = $base_work_dir.$_POST['move_to'].'/'.basename($document_to_move['path']);
  410. $fileExist = false;
  411. if (file_exists($real_path_target)) {
  412. $fileExist = true;
  413. }
  414. if (FileManager::move($base_work_dir.$document_to_move['path'], $base_work_dir.$_POST['move_to'])) {
  415. //if (1) {
  416. //$contents = DocumentManager::replace_urls_inside_content_html_when_moving_file(basename($document_to_move['path']), $base_work_dir.dirname($document_to_move['path']), $base_work_dir.$_POST['move_to']);
  417. //exit;
  418. FileManager::update_db_info(
  419. 'update',
  420. $document_to_move['path'],
  421. $_POST['move_to'].'/'.basename($document_to_move['path'])
  422. );
  423. //update database item property
  424. $doc_id = $_POST['move_file'];
  425. if (is_dir($real_path_target)) {
  426. api_item_property_update(
  427. $_course,
  428. TOOL_DOCUMENT,
  429. $doc_id,
  430. 'FolderMoved',
  431. api_get_user_id(),
  432. $to_group_id,
  433. null,
  434. null,
  435. null,
  436. $session_id
  437. );
  438. Display::display_confirmation_message(get_lang('DirMv'));
  439. } elseif (is_file($real_path_target)) {
  440. api_item_property_update(
  441. $_course,
  442. TOOL_DOCUMENT,
  443. $doc_id,
  444. 'DocumentMoved',
  445. api_get_user_id(),
  446. $to_group_id,
  447. null,
  448. null,
  449. null,
  450. $session_id
  451. );
  452. Display::display_confirmation_message(get_lang('DocMv'));
  453. }
  454. // Set the current path
  455. $curdirpath = $_POST['move_to'];
  456. $curdirpathurl = urlencode($_POST['move_to']);
  457. } else {
  458. if ($fileExist) {
  459. if (is_dir($real_path_target)) {
  460. Display::display_error_message(get_lang('DirExists'));
  461. } elseif (is_file($real_path_target)) {
  462. Display::display_error_message(get_lang('FileExists'));
  463. }
  464. } else {
  465. Display::display_error_message(get_lang('Impossible'));
  466. }
  467. }
  468. } else {
  469. Display::display_error_message(get_lang('Impossible'));
  470. }
  471. }
  472. }
  473. /* DELETE FILE OR DIRECTORY */
  474. //Only teacher and all users into their group
  475. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  476. api_get_user_id(),
  477. $curdirpath,
  478. $session_id
  479. )
  480. ) {
  481. if (isset($_GET['delete'])) {
  482. if (!$is_allowed_to_edit) {
  483. if (api_is_coach()) {
  484. if (!DocumentManager::is_visible($_GET['delete'], $_course, api_get_session_id())) {
  485. api_not_allowed();
  486. }
  487. }
  488. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_GET['delete'], '', true)) {
  489. api_not_allowed();
  490. }
  491. }
  492. if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
  493. if (isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(
  494. intval($_GET['delete_certificate_id'])
  495. )
  496. ) {
  497. $default_certificate_id = $_GET['delete_certificate_id'];
  498. DocumentManager::remove_attach_certificate(api_get_course_id(), $default_certificate_id);
  499. }
  500. Display::display_confirmation_message(get_lang('DocDeleted'));
  501. } else {
  502. Display::display_error_message(get_lang('DocDeleteError'));
  503. }
  504. }
  505. if (isset($_POST['action'])) {
  506. switch ($_POST['action']) {
  507. case 'delete':
  508. foreach ($_POST['path'] as $index => & $path) {
  509. if (!$is_allowed_to_edit) {
  510. if (DocumentManager::check_readonly($_course, api_get_user_id(), $path)) {
  511. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  512. break 2;
  513. }
  514. }
  515. }
  516. foreach ($_POST['path'] as $index => & $path) {
  517. if (in_array(
  518. $path,
  519. array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files', '/certificates')
  520. )
  521. ) {
  522. continue;
  523. } else {
  524. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  525. }
  526. }
  527. if (!empty($delete_document)) {
  528. Display::display_confirmation_message(get_lang('DocDeleted'));
  529. }
  530. break;
  531. }
  532. }
  533. }
  534. /* CREATE DIRECTORY */
  535. //Only teacher and all users into their group and any user into his/her shared folder
  536. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  537. api_get_user_id(),
  538. $curdirpath,
  539. $session_id
  540. )
  541. ) {
  542. // Create directory with $_POST data
  543. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  544. // Needed for directory creation
  545. $post_dir_name = $_POST['dirname'];
  546. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  547. Display::display_error_message(get_lang('CannotCreateDir'));
  548. } else {
  549. if (!empty($_POST['dir_id'])) {
  550. $document_data = DocumentManager::get_document_data_by_id($_POST['dir_id'], api_get_course_id());
  551. $curdirpath = $document_data['path'];
  552. }
  553. $added_slash = ($curdirpath == '/') ? '' : '/';
  554. $dir_name = $curdirpath.$added_slash.api_replace_dangerous_char($post_dir_name);
  555. $dir_name = FileManager::disable_dangerous_file($dir_name);
  556. $dir_check = $base_work_dir.$dir_name;
  557. if (!is_dir($dir_check)) {
  558. $created_dir = FileManager::create_unexisting_directory(
  559. $_course,
  560. api_get_user_id(),
  561. api_get_session_id(),
  562. $to_group_id,
  563. $to_user_id,
  564. $base_work_dir,
  565. $dir_name,
  566. $post_dir_name
  567. );
  568. if ($created_dir) {
  569. Display::display_confirmation_message(
  570. '<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>',
  571. false
  572. );
  573. // Uncomment if you want to enter the created dir
  574. //$curdirpath = $created_dir;
  575. //$curdirpathurl = urlencode($curdirpath);
  576. } else {
  577. Display::display_error_message(get_lang('CannotCreateDir'));
  578. }
  579. } else {
  580. Display::display_error_message(get_lang('CannotCreateDir'));
  581. }
  582. }
  583. }
  584. // Show them the form for the directory name
  585. if (isset($_GET['createdir'])) {
  586. echo create_dir_form($document_id);
  587. }
  588. }
  589. /* VISIBILITY COMMANDS */
  590. //Only teacher
  591. if ($is_allowed_to_edit) {
  592. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  593. // Make visible or invisible?
  594. if (isset($_GET['set_visible'])) {
  595. $update_id = intval($_GET['set_visible']);
  596. $visibility_command = 'visible';
  597. } else {
  598. $update_id = intval($_GET['set_invisible']);
  599. $visibility_command = 'invisible';
  600. }
  601. if (!$is_allowed_to_edit) {
  602. if (api_is_coach()) {
  603. if (!DocumentManager::is_visible_by_id($update_id, $_course, api_get_session_id(), api_get_user_id())) {
  604. api_not_allowed();
  605. }
  606. }
  607. if (DocumentManager::check_readonly($_course, api_get_user_id(), '', $update_id)) {
  608. api_not_allowed();
  609. }
  610. }
  611. // Update item_property to change visibility
  612. if (api_item_property_update(
  613. $_course,
  614. TOOL_DOCUMENT,
  615. $update_id,
  616. $visibility_command,
  617. api_get_user_id(),
  618. null,
  619. null,
  620. null,
  621. null,
  622. $session_id
  623. )
  624. ) {
  625. Display::display_confirmation_message(
  626. get_lang('VisibilityChanged')
  627. ); //don't use ViMod because firt is load ViMdod (Gradebook). VisibilityChanged (trad4all)
  628. } else {
  629. Display::display_error_message(get_lang('ViModProb'));
  630. }
  631. }
  632. }
  633. /* TEMPLATE ACTION */
  634. //Only teacher and all users into their group
  635. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  636. api_get_user_id(),
  637. $curdirpath,
  638. $session_id
  639. )
  640. ) {
  641. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  642. $document_id_for_template = intval($_GET['add_as_template']);
  643. // Create the form that asks for the directory name
  644. $template_text = '<form name="set_document_as_new_template" enctype="multipart/form-data" action="'.api_get_self(
  645. ).'?add_as_template='.$document_id_for_template.'" method="post">';
  646. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  647. $template_text .= '<table><tr><td>';
  648. $template_text .= get_lang('TemplateName').' : </td>';
  649. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  650. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  651. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  652. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  653. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  654. $template_text .= '</table>';
  655. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang(
  656. 'CreateTemplate'
  657. ).'</button>';
  658. $template_text .= '</form>';
  659. // Show the form
  660. Display::display_normal_message($template_text, false);
  661. }
  662. }
  663. // END ACTION MENU
  664. // Attach certificate in the gradebook
  665. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate'] == strval(
  666. intval($_GET['set_certificate'])
  667. )
  668. ) {
  669. if (isset($_GET['cidReq'])) {
  670. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  671. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  672. DocumentManager::attach_gradebook_certificate($course_id, $document_id);
  673. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  674. }
  675. }
  676. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  677. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  678. $docs_and_folders = DocumentManager::get_all_document_data(
  679. $_course,
  680. $curdirpath,
  681. $to_group_id,
  682. null,
  683. $is_allowed_to_edit || $group_member_with_upload_rights,
  684. true
  685. );
  686. } else {
  687. $docs_and_folders = DocumentManager::get_all_document_data(
  688. $_course,
  689. $curdirpath,
  690. $to_group_id,
  691. null,
  692. $is_allowed_to_edit || $group_member_with_upload_rights,
  693. false
  694. );
  695. }
  696. $folders = DocumentManager::get_all_document_folders(
  697. $_course,
  698. $to_group_id,
  699. $is_allowed_to_edit || $group_member_with_upload_rights
  700. );
  701. if ($folders === false) {
  702. $folders = array();
  703. }
  704. $table_footer = '';
  705. $total_size = 0;
  706. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  707. // Create a sortable table with our data
  708. $sortable_data = array();
  709. $count = 1;
  710. foreach ($docs_and_folders as $key => $document_data) {
  711. $row = array();
  712. $row['id'] = $document_data['id'];
  713. //$row['type'] = $document_data['filetype'];
  714. $row['type'] = create_document_link($document_data, true, $count, $is_visible);
  715. // If the item is invisible, wrap it in a span with class invisible
  716. $is_visible = DocumentManager::is_visible_by_id(
  717. $document_data['id'],
  718. $course_info,
  719. api_get_session_id(),
  720. api_get_user_id(),
  721. false
  722. );
  723. $invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : '';
  724. $invisibility_span_close = ($is_visible == 0) ? '</span>' : '';
  725. // Size (or total size of a directory)
  726. $size = $document_data['filetype'] == 'folder' ? FileManager::get_total_folder_size(
  727. $document_data['path'],
  728. $is_allowed_to_edit
  729. ) : $document_data['size'];
  730. $row['size'] = Text::format_file_size($size);
  731. // Get the title or the basename depending on what we're using
  732. if ($document_data['title'] != '') {
  733. $document_name = $document_data['title'];
  734. } else {
  735. $document_name = basename($document_data['path']);
  736. }
  737. $row['name'] = $document_name;
  738. $row['name'] = create_document_link(
  739. $document_data,
  740. false,
  741. null,
  742. $is_visible
  743. ).$session_img.'<br />'.$invisibility_span_open.'<i>'.nl2br(
  744. htmlspecialchars($document_data['comment'], ENT_QUOTES, $charset)
  745. ).'</i>'.$invisibility_span_close.$user_link;
  746. // Data for checkbox
  747. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  748. $row[] = $document_data['path'];
  749. }
  750. // Hide HotPotatoes Certificates and all css folders
  751. if ($document_data['path'] == '/HotPotatoes_files' || $document_data['path'] == '/certificates' || basename(
  752. $document_data['path']
  753. ) == 'css'
  754. ) {
  755. continue;
  756. }
  757. //Admin setting for Hide/Show the folders of all users
  758. if (api_get_setting('show_users_folders') == 'false' && ($document_data['path'] == '/shared_folder' || strstr(
  759. $document_data['path'],
  760. 'shared_folder_session_'
  761. ))
  762. ) {
  763. continue;
  764. }
  765. //Admin setting for Hide/Show Default folders to all users
  766. if (api_get_setting(
  767. 'show_default_folders'
  768. ) == 'false' && ($document_data['path'] == '/images' || $document_data['path'] == '/flash' || $document_data['path'] == '/audio' || $document_data['path'] == '/video')
  769. ) {
  770. continue;
  771. }
  772. //Admin setting for Hide/Show chat history folder
  773. if (api_get_setting('show_chat_folder') == 'false' && $document_data['path'] == '/chat_files') {
  774. continue;
  775. }
  776. // Show the owner of the file only in groups
  777. $user_link = '';
  778. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  779. if (!empty($document_data['insert_user_id'])) {
  780. $user_info = UserManager::get_user_info_by_id($document_data['insert_user_id']);
  781. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  782. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document(
  783. $document_data['insert_user_id'],
  784. $user_name
  785. ).'</div>';
  786. }
  787. }
  788. // Icons (clickable)
  789. $row[] = create_document_link($document_data, true, $count, $is_visible);
  790. $path_info = pathinfo($document_data['path']);
  791. if (isset($path_info['extension']) && in_array($path_info['extension'], array('ogg', 'mp3', 'wav'))) {
  792. $count++;
  793. }
  794. // Validacion when belongs to a session
  795. $session_img = api_get_session_image($document_data['session_id'], $_user['status']);
  796. // Document title with link
  797. $row[] = create_document_link(
  798. $document_data,
  799. false,
  800. null,
  801. $is_visible
  802. ).$session_img.'<br />'.$invisibility_span_open.'<i>'.nl2br(
  803. htmlspecialchars($document_data['comment'], ENT_QUOTES, $charset)
  804. ).'</i>'.$invisibility_span_close.$user_link;
  805. // Comments => display comment under the document name
  806. $display_size = Text::format_file_size($size);
  807. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  808. // Last edit date
  809. $last_edit_date = $document_data['lastedit_date'];
  810. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  811. //$display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  812. $display_date = date_to_str_ago($last_edit_date);
  813. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  814. // Admins get an edit column
  815. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  816. api_get_user_id(),
  817. $curdirpath,
  818. $session_id
  819. )
  820. ) {
  821. $is_template = isset($document_data['is_template']) ? $document_data['is_template'] : false;
  822. // If readonly, check if it the owner of the file or if the user is an admin
  823. if ($document_data['insert_user_id'] == api_get_user_id() || api_is_platform_admin()) {
  824. $edit_icons = build_edit_icons($document_data, $key, $is_template, 0, $is_visible);
  825. } else {
  826. $edit_icons = build_edit_icons(
  827. $document_data,
  828. $key,
  829. $is_template,
  830. $document_data['readonly'],
  831. $is_visible
  832. );
  833. }
  834. $row[] = $edit_icons;
  835. }
  836. $row[] = $last_edit_date;
  837. $row[] = $size;
  838. $row[] = $document_name;
  839. $total_size = $total_size + $size;
  840. if ((isset($_GET['keyword']) && search_keyword(
  841. $document_name,
  842. $_GET['keyword']
  843. )) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  844. ) {
  845. $sortable_data[] = $row;
  846. }
  847. }
  848. } else {
  849. $sortable_data = '';
  850. $table_footer = get_lang('NoDocsInFolder');
  851. }
  852. //The order is important you need to check the the $column variable in the model.ajax.php file
  853. $columns = array(get_lang('Type'), get_lang('Name'), get_lang('Size'));
  854. //Column config
  855. $column_model = array(
  856. array('name' => 'type', 'index' => 'type', 'width' => '28', 'align' => 'center', 'sortable' => 'false'),
  857. array('name' => 'name', 'index' => 'name', 'width' => '500', 'align' => 'left'),
  858. array('name' => 'size', 'index' => 'size', 'width' => '35', 'align' => 'right', 'sortable' => 'true')
  859. );
  860. //Autowidth
  861. $extra_params['autowidth'] = 'true';
  862. //height auto
  863. $extra_params['height'] = 'auto';
  864. //With this function we can add actions to the jgrid (edit, delete, etc)
  865. $action_links = 'function action_formatter(cellvalue, options, rowObject) {
  866. return \'<a href="?action=edit&id=\'+options.rowId+\'">'.Display::return_icon(
  867. 'edit.png',
  868. get_lang('Edit'),
  869. '',
  870. ICON_SIZE_SMALL
  871. ).'</a>'.
  872. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  873. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  874. )."\'".')) return false;" href="?action=copy&id=\'+options.rowId+\'">'.Display::return_icon(
  875. 'copy.png',
  876. get_lang('Copy'),
  877. '',
  878. ICON_SIZE_SMALL
  879. ).'</a>'.
  880. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  881. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  882. )."\'".')) return false;" href="?action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  883. 'delete.png',
  884. get_lang('Delete'),
  885. '',
  886. ICON_SIZE_SMALL
  887. ).'</a>'.
  888. '\';
  889. }';
  890. $js_content = Display::grid_js(
  891. 'documents',
  892. '',
  893. $columns,
  894. $column_model,
  895. $extra_params,
  896. $sortable_data,
  897. $action_links,
  898. true
  899. );
  900. $htmlHeadXtra[] = '<script>
  901. $(function() {
  902. // grid definition see the $career->display() function
  903. '.$js_content.'
  904. });
  905. </script>';
  906. require_once 'controller.php';
  907. $controller = new DocumentController();
  908. $tpl = $controller->tpl->get_template('layout/layout_2_col.tpl');
  909. $content = Display::grid_html('documents');
  910. if (!is_null($docs_and_folders)) {
  911. // Show download zipped folder icon
  912. global $total_size;
  913. if (!$is_certificate_mode && $total_size != 0 && (api_get_setting(
  914. 'students_download_folders'
  915. ) == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())
  916. ) {
  917. //for student does not show icon into other shared folder, and does not show into main path (root)
  918. if (is_my_shared_folder(
  919. api_get_user_id(),
  920. $curdirpath,
  921. $session_id
  922. ) && $curdirpath != '/' || api_is_allowed_to_edit() || api_is_platform_admin()
  923. ) {
  924. $link = '<a href="'.api_get_self().'?'.api_get_cidreq(
  925. ).'&amp;action=downloadfolder&amp;id='.$document_id.'">'.Display::return_icon(
  926. 'save_pack.png',
  927. get_lang('Save').' (ZIP)',
  928. '',
  929. ICON_SIZE_MEDIUM
  930. ).'</a>';
  931. }
  932. }
  933. }
  934. $content .= Display::div($link, array('class' => 'right'));
  935. $controller->tpl->assign('content', $content);
  936. $controller->tpl->display($tpl);