document_lite.php 40 KB

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