document_lite.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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_jquery_ui_js(true);
  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. $htmlHeadXtra[] = api_get_jquery_ui_js();
  205. $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
  206. /*
  207. $htmlHeadXtra[] = '<script type="text/javascript" src="'.$js_path.'yoxview/yox.js"></script>';
  208. $htmlHeadXtra[] = api_get_js('yoxview/yoxview-init.js');
  209. */
  210. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.$js_path.'jquery-jplayer/skins/chamilo/jplayer.blue.monday.css" type="text/css">';
  211. $htmlHeadXtra[] = '<script type="text/javascript" src="'.$js_path.'jquery-jplayer/jquery.jplayer.min.js"></script>';
  212. $mediaplayer_path = api_get_path(WEB_LIBRARY_PATH).'mediaplayer/player.swf';
  213. //automatic loading the course language for yoxview
  214. /*$yoxview_code_translation_table = array('' => 'en', 'pt' => 'pt-Pt', 'sr' => 'sr_latn');
  215. $lang_yoxview = api_get_language_isocode();
  216. $lang_yoxview = isset($yoxview_code_translation_table[$lang_yoxview]) ? $yoxview_code_translation_table[$lang_yoxview] : $lang_yoxview;
  217. */
  218. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, false);
  219. $file_list = $format_list = '';
  220. $count = 1;
  221. if (!empty($docs_and_folders))
  222. foreach ($docs_and_folders as $file) {
  223. if ($file['filetype'] == 'file') {
  224. $path_info = pathinfo($file['path']);
  225. $extension = strtolower($path_info['extension']);
  226. //@todo use a js loop to autogenerate this code
  227. if (in_array($extension, array('ogg', 'mp3', 'wav'))) {
  228. $document_data = DocumentManager::get_document_data_by_id($file['id'], api_get_course_id());
  229. if ($extension == 'ogg') {
  230. $extension = 'oga';
  231. }
  232. $jquery .= ' $("#jquery_jplayer_'.$count.'").jPlayer({
  233. ready: function() {
  234. $(this).jPlayer("setMedia", {
  235. '.$extension.' : "'.$document_data['direct_url'].'"
  236. });
  237. },
  238. swfPath: "'.$js_path.'jquery-jplayer",
  239. supplied: "mp3, m4a, oga, ogv, wav",
  240. solution: "flash, html", // Do not change this setting otherwise
  241. cssSelectorAncestor: "#jp_interface_'.$count.'",
  242. });'."\n\n";
  243. $count++;
  244. }
  245. }
  246. }
  247. $htmlHeadXtra[] = '<script type="text/javascript">
  248. $(document).ready( function() {
  249. /*
  250. $(".yoxview").yoxview({
  251. lang: "'.$lang_yoxview.'",
  252. flashVideoPlayerPath: "'.$mediaplayer_path.'",
  253. allowInternalLinks:true,
  254. defaultDimensions: { iframe: { width: 800}},
  255. });*/
  256. //Experimental changes to preview mp3, ogg files
  257. '.$jquery.'
  258. //Keep this down otherwise the jquery player will not work
  259. for (i=0;i<$(".actions").length;i++) {
  260. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  261. $(".actions:eq("+i+")").hide();
  262. }
  263. }
  264. });
  265. </script>';
  266. // Lib for event log, stats & tracking & record of the access
  267. event_access_tool(TOOL_DOCUMENT);
  268. /* DISPLAY */
  269. if ($to_group_id != 0) { // Add group name after for group documents
  270. $add_group_to_title = ' ('.$group_properties['name'].')';
  271. }
  272. /* Introduction section (editable by course admins) */
  273. if (!empty($_SESSION['_gid'])) {
  274. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  275. } else {
  276. Display::display_introduction_section(TOOL_DOCUMENT);
  277. }
  278. // ACTION MENU
  279. // Copy a file to general my files user's
  280. if (isset($_GET['action']) && $_GET['action'] == 'copytomyfiles' && api_get_setting('users_copy_files') == 'true' && api_get_user_id() != 0) {
  281. $clean_get_id = Security::remove_XSS($_GET['id']);
  282. $my_path = UserManager::get_user_picture_path_by_id(api_get_user_id(),'system');
  283. $user_folder = $my_path['dir'].'my_files/';
  284. $my_path = null;
  285. if (!file_exists($user_folder)) {
  286. $perm = api_get_permissions_for_new_directories();
  287. @mkdir($user_folder, $perm, true);
  288. }
  289. $file = $sys_course_path.$_course['path'].'/document'.$clean_get_id;
  290. $copyfile = $user_folder.basename($clean_get_id);
  291. if (file_exists($copyfile)) {
  292. $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>';
  293. if (!isset($_GET['copy'])){
  294. Display::display_warning_message($message,false);
  295. }
  296. if (Security::remove_XSS($_GET['copy']) == 'yes'){
  297. if (!copy($file, $copyfile)) {
  298. Display::display_error_message(get_lang('CopyFailed'));
  299. } else {
  300. Display::display_confirmation_message(get_lang('OverwritenFile'));
  301. }
  302. }
  303. } else {
  304. if (!copy($file, $copyfile)) {
  305. Display::display_error_message(get_lang('CopyFailed'));
  306. } else {
  307. Display::display_confirmation_message(get_lang('CopyMade'));
  308. }
  309. }
  310. }
  311. /* MOVE FILE OR DIRECTORY */
  312. //Only teacher and all users into their group and each user into his/her shared folder
  313. 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)) {
  314. if (isset($_GET['move']) && $_GET['move'] != '') {
  315. $my_get_move = intval($_REQUEST['move']);
  316. if (api_is_coach()) {
  317. if (!DocumentManager::is_visible_by_id($my_get_move, $course_info, api_get_session_id(), api_get_user_id())) {
  318. api_not_allowed();
  319. }
  320. }
  321. if (!$is_allowed_to_edit) {
  322. if (DocumentManager::check_readonly($_course, api_get_user_id(), $my_get_move)) {
  323. api_not_allowed();
  324. }
  325. }
  326. $document_to_move = DocumentManager::get_document_data_by_id($my_get_move, api_get_course_id());
  327. $move_path = $document_to_move['path'];
  328. if (!empty($document_to_move)) {
  329. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  330. //filter if is my shared folder. TODO: move this code to build_move_to_selector function
  331. if (is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id) && !$is_allowed_to_edit){
  332. $main_user_shared_folder_main = '/shared_folder/sf_user_'.api_get_user_id();//only main user shared folder
  333. $main_user_shared_folder_sub = '/shared_folder\/sf_user_'.api_get_user_id().'\//';//all subfolders
  334. $user_shared_folders=array();
  335. foreach($folders as $fold){
  336. if($main_user_shared_folder_main==$fold || preg_match($main_user_shared_folder_sub, $fold)){
  337. $user_shared_folders[]=$fold;
  338. }
  339. }
  340. echo '<legend>'.get_lang('Move').'</legend>';
  341. echo build_move_to_selector($user_shared_folders, $move_path, $my_get_move, $group_properties['directory']);
  342. } else {
  343. echo '<legend>'.get_lang('Move').'</legend>';
  344. echo build_move_to_selector($folders, $move_path, $my_get_move, $group_properties['directory']);
  345. }
  346. }
  347. }
  348. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  349. if (!$is_allowed_to_edit) {
  350. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_POST['move_file'])) {
  351. api_not_allowed();
  352. }
  353. }
  354. if (api_is_coach()) {
  355. if (!DocumentManager::is_visible_by_id($_POST['move_file'], $_course, api_get_session_id(), api_get_user_id())) {
  356. api_not_allowed();
  357. }
  358. }
  359. $document_to_move = DocumentManager::get_document_data_by_id($_POST['move_file'], api_get_course_id());
  360. require_once $lib_path.'fileManage.lib.php';
  361. // Security fix: make sure they can't move files that are not in the document table
  362. if (!empty($document_to_move)) {
  363. $real_path_target = $base_work_dir.$_POST['move_to'].'/'.basename($document_to_move['path']);
  364. $fileExist=false;
  365. if(file_exists($real_path_target)){
  366. $fileExist=true;
  367. }
  368. if (move($base_work_dir.$document_to_move['path'], $base_work_dir.$_POST['move_to'])) {
  369. //if (1) {
  370. //$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']);
  371. //exit;
  372. update_db_info('update', $document_to_move['path'], $_POST['move_to'].'/'.basename($document_to_move['path']));
  373. //update database item property
  374. $doc_id=$_POST['move_file'];
  375. if(is_dir($real_path_target)){
  376. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderMoved', api_get_user_id(),$to_group_id,null,null,null,$session_id);
  377. Display::display_confirmation_message(get_lang('DirMv'));
  378. }
  379. elseif(is_file($real_path_target)){
  380. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentMoved', api_get_user_id(),$to_group_id,null,null,null,$session_id);
  381. Display::display_confirmation_message(get_lang('DocMv'));
  382. }
  383. // Set the current path
  384. $curdirpath = $_POST['move_to'];
  385. $curdirpathurl = urlencode($_POST['move_to']);
  386. } else {
  387. if($fileExist){
  388. if(is_dir($real_path_target)){
  389. Display::display_error_message(get_lang('DirExists'));
  390. }
  391. elseif(is_file($real_path_target)){
  392. Display::display_error_message(get_lang('FileExists'));
  393. }
  394. }
  395. else{
  396. Display::display_error_message(get_lang('Impossible'));
  397. }
  398. }
  399. } else {
  400. Display::display_error_message(get_lang('Impossible'));
  401. }
  402. }
  403. }
  404. /* DELETE FILE OR DIRECTORY */
  405. //Only teacher and all users into their group
  406. if($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id)){
  407. if (isset($_GET['delete'])) {
  408. if (!$is_allowed_to_edit) {
  409. if (api_is_coach()) {
  410. if (!DocumentManager::is_visible($_GET['delete'], $_course, api_get_session_id())) {
  411. api_not_allowed();
  412. }
  413. }
  414. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_GET['delete'], '', true)) {
  415. api_not_allowed();
  416. }
  417. }
  418. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  419. if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
  420. if ( isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(intval($_GET['delete_certificate_id']))) {
  421. $default_certificate_id = $_GET['delete_certificate_id'];
  422. DocumentManager::remove_attach_certificate(api_get_course_id(), $default_certificate_id);
  423. }
  424. Display::display_confirmation_message(get_lang('DocDeleted'));
  425. } else {
  426. Display::display_error_message(get_lang('DocDeleteError'));
  427. }
  428. }
  429. if (isset($_POST['action'])) {
  430. switch ($_POST['action']) {
  431. case 'delete':
  432. foreach ($_POST['path'] as $index => & $path) {
  433. if (!$is_allowed_to_edit) {
  434. if (DocumentManager::check_readonly($_course, api_get_user_id(), $path)) {
  435. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  436. break 2;
  437. }
  438. }
  439. }
  440. foreach ($_POST['path'] as $index => & $path) {
  441. if (in_array($path, array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files', '/certificates'))) {
  442. continue;
  443. } else {
  444. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  445. }
  446. }
  447. if (!empty($delete_document)) {
  448. Display::display_confirmation_message(get_lang('DocDeleted'));
  449. }
  450. break;
  451. }
  452. }
  453. }
  454. /* CREATE DIRECTORY */
  455. //Only teacher and all users into their group and any user into his/her shared folder
  456. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id)) {
  457. // Create directory with $_POST data
  458. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  459. // Needed for directory creation
  460. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  461. $post_dir_name = $_POST['dirname'];
  462. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  463. Display::display_error_message(get_lang('CannotCreateDir'));
  464. } else {
  465. if (!empty($_POST['dir_id'])) {
  466. $document_data = DocumentManager::get_document_data_by_id($_POST['dir_id'], api_get_course_id());
  467. $curdirpath = $document_data['path'];
  468. }
  469. $added_slash = ($curdirpath == '/') ? '' : '/';
  470. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  471. $dir_name = disable_dangerous_file($dir_name);
  472. $dir_check = $base_work_dir.$dir_name;
  473. if (!is_dir($dir_check)) {
  474. $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);
  475. if ($created_dir) {
  476. Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false);
  477. // Uncomment if you want to enter the created dir
  478. //$curdirpath = $created_dir;
  479. //$curdirpathurl = urlencode($curdirpath);
  480. } else {
  481. Display::display_error_message(get_lang('CannotCreateDir'));
  482. }
  483. } else {
  484. Display::display_error_message(get_lang('CannotCreateDir'));
  485. }
  486. }
  487. }
  488. // Show them the form for the directory name
  489. if (isset($_GET['createdir'])) {
  490. echo create_dir_form($document_id);
  491. }
  492. }
  493. /* VISIBILITY COMMANDS */
  494. //Only teacher
  495. if ($is_allowed_to_edit) {
  496. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  497. // Make visible or invisible?
  498. if (isset($_GET['set_visible'])) {
  499. $update_id = intval($_GET['set_visible']);
  500. $visibility_command = 'visible';
  501. } else {
  502. $update_id = intval($_GET['set_invisible']);
  503. $visibility_command = 'invisible';
  504. }
  505. if (!$is_allowed_to_edit) {
  506. if (api_is_coach()) {
  507. if (!DocumentManager::is_visible_by_id($update_id, $_course, api_get_session_id(), api_get_user_id())) {
  508. api_not_allowed();
  509. }
  510. }
  511. if (DocumentManager::check_readonly($_course, api_get_user_id(), '', $update_id)) {
  512. api_not_allowed();
  513. }
  514. }
  515. // Update item_property to change visibility
  516. if (api_item_property_update($_course, TOOL_DOCUMENT, $update_id, $visibility_command, api_get_user_id(), null, null, null, null, $session_id)) {
  517. Display::display_confirmation_message(get_lang('VisibilityChanged'));//don't use ViMod because firt is load ViMdod (Gradebook). VisibilityChanged (trad4all)
  518. } else {
  519. Display::display_error_message(get_lang('ViModProb'));
  520. }
  521. }
  522. }
  523. /* TEMPLATE ACTION */
  524. //Only teacher and all users into their group
  525. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id)){
  526. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  527. $document_id_for_template = intval($_GET['add_as_template']);
  528. // Create the form that asks for the directory name
  529. $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">';
  530. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  531. $template_text .= '<table><tr><td>';
  532. $template_text .= get_lang('TemplateName').' : </td>';
  533. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  534. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  535. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  536. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  537. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  538. $template_text .= '</table>';
  539. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang('CreateTemplate').'</button>';
  540. $template_text .= '</form>';
  541. // Show the form
  542. Display::display_normal_message($template_text, false);
  543. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  544. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  545. $title = Security::remove_XSS($_POST['template_title']);
  546. //$description = Security::remove_XSS($_POST['template_description']);
  547. $user_id = api_get_user_id();
  548. // Create the template_thumbnails folder in the upload folder (if needed)
  549. if (!is_dir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/')) {
  550. @mkdir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/', api_get_permissions_for_new_directories());
  551. }
  552. // Upload the file
  553. if (!empty($_FILES['template_image']['name'])) {
  554. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  555. $upload_ok = process_uploaded_file($_FILES['template_image']);
  556. if ($upload_ok) {
  557. // Try to add an extension to the file if it hasn't one
  558. $new_file_name = $_course['sysCode'].'-'.add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  559. // Upload dir
  560. $upload_dir = api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/';
  561. // Resize image to max default and end upload
  562. $temp = new Image($_FILES['template_image']['tmp_name']);
  563. $picture_info = $temp->get_image_info();
  564. $max_width_for_picture = 100;
  565. if ($picture_info['width'] > $max_width_for_picture) {
  566. $thumbwidth = $max_width_for_picture;
  567. if (empty($thumbwidth) || $thumbwidth == 0) {
  568. $thumbwidth = $max_width_for_picture;
  569. }
  570. $new_height = round(($thumbwidth/$picture_info['width'])*$picture_info['height']);
  571. $temp->resize($thumbwidth, $new_height, 0);
  572. }
  573. $temp->send_image($upload_dir.$new_file_name);
  574. }
  575. }
  576. DocumentManager::set_document_as_template($title, $description, $document_id_for_template, $course_code, $user_id, $new_file_name);
  577. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  578. }
  579. if (isset($_GET['remove_as_template'])) {
  580. $document_id_for_template = intval($_GET['remove_as_template']);
  581. $user_id = api_get_user_id();
  582. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  583. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  584. }
  585. }
  586. // END ACTION MENU
  587. // Attach certificate in the gradebook
  588. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate'] == strval(intval($_GET['set_certificate']))) {
  589. if (isset($_GET['cidReq'])) {
  590. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  591. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  592. DocumentManager::attach_gradebook_certificate ($course_id,$document_id);
  593. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  594. }
  595. }
  596. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  597. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  598. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, true);
  599. } else {
  600. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, false);
  601. }
  602. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  603. if ($folders === false) {
  604. $folders = array();
  605. }
  606. $table_footer = '';
  607. $total_size = 0;
  608. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  609. // Do we need the title field for the document name or not?
  610. // We get the setting here, so we only have to do it once
  611. $use_document_title = api_get_setting('use_document_title');
  612. // Create a sortable table with our data
  613. $sortable_data = array();
  614. $count = 1;
  615. foreach ($docs_and_folders as $key => $document_data) {
  616. $row = array();
  617. $row['id'] = $document_data['id'];
  618. //$row['type'] = $document_data['filetype'];
  619. $row['type'] = create_document_link($document_data, true, $count, $is_visible);
  620. // If the item is invisible, wrap it in a span with class invisible
  621. $is_visible = DocumentManager::is_visible_by_id($document_data['id'], $course_info, api_get_session_id(), api_get_user_id(), false);
  622. $invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : '';
  623. $invisibility_span_close = ($is_visible == 0) ? '</span>' : '';
  624. // Size (or total size of a directory)
  625. $size = $document_data['filetype'] == 'folder' ? get_total_folder_size($document_data['path'], $is_allowed_to_edit) : $document_data['size'];
  626. $row['size'] = format_file_size($size);
  627. // Get the title or the basename depending on what we're using
  628. if ($use_document_title == 'true' && $document_data['title'] != '') {
  629. $document_name = $document_data['title'];
  630. } else {
  631. $document_name = basename($document_data['path']);
  632. }
  633. $row['name'] = $document_name;
  634. $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;
  635. // Data for checkbox
  636. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  637. $row[] = $document_data['path'];
  638. }
  639. // Hide HotPotatoes Certificates and all css folders
  640. if ($document_data['path']=='/HotPotatoes_files' || $document_data['path']=='/certificates' || basename($document_data['path'])=='css'){
  641. continue;
  642. }
  643. //Admin setting for Hide/Show the folders of all users
  644. if (api_get_setting('show_users_folders') == 'false' && ($document_data['path']=='/shared_folder' || strstr($document_data['path'], 'shared_folder_session_'))){
  645. continue;
  646. }
  647. //Admin setting for Hide/Show Default folders to all users
  648. if (api_get_setting('show_default_folders') == 'false' && ($document_data['path']=='/images' || $document_data['path']=='/flash' || $document_data['path']=='/audio' || $document_data['path']=='/video')){
  649. continue;
  650. }
  651. //Admin setting for Hide/Show chat history folder
  652. if (api_get_setting('show_chat_folder') == 'false' && $document_data['path']=='/chat_files'){
  653. continue;
  654. }
  655. // Show the owner of the file only in groups
  656. $user_link = '';
  657. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  658. if (!empty($document_data['insert_user_id'])) {
  659. $user_info = UserManager::get_user_info_by_id($document_data['insert_user_id']);
  660. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  661. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document($document_data['insert_user_id'], $user_name).'</div>';
  662. }
  663. }
  664. // Icons (clickable)
  665. $row[] = create_document_link($document_data, true, $count, $is_visible);
  666. $path_info = pathinfo($document_data['path']);
  667. if (isset($path_info['extension']) && in_array($path_info['extension'], array('ogg', 'mp3','wav'))) {
  668. $count ++;
  669. }
  670. // Validacion when belongs to a session
  671. $session_img = api_get_session_image($document_data['session_id'], $_user['status']);
  672. // Document title with link
  673. $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;
  674. // Comments => display comment under the document name
  675. $display_size = format_file_size($size);
  676. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  677. // Last edit date
  678. $last_edit_date = $document_data['lastedit_date'];
  679. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  680. //$display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  681. $display_date = date_to_str_ago($last_edit_date);
  682. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  683. // Admins get an edit column
  684. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id)) {
  685. $is_template = isset($document_data['is_template']) ? $document_data['is_template'] : false;
  686. // If readonly, check if it the owner of the file or if the user is an admin
  687. if ($document_data['insert_user_id'] == api_get_user_id() || api_is_platform_admin()) {
  688. $edit_icons = build_edit_icons($document_data, $key, $is_template, 0, $is_visible);
  689. } else {
  690. $edit_icons = build_edit_icons($document_data, $key, $is_template, $document_data['readonly'], $is_visible);
  691. }
  692. $row[] = $edit_icons;
  693. }
  694. $row[] = $last_edit_date;
  695. $row[] = $size;
  696. $row[] = $document_name;
  697. $total_size = $total_size + $size;
  698. if ((isset($_GET['keyword']) && search_keyword($document_name, $_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])) {
  699. $sortable_data[] = $row;
  700. }
  701. }
  702. } else {
  703. $sortable_data = '';
  704. $table_footer = get_lang('NoDocsInFolder');
  705. }
  706. //The order is important you need to check the the $column variable in the model.ajax.php file
  707. $columns = array(get_lang('Type'), get_lang('Name'), get_lang('Size'));
  708. //Column config
  709. $column_model = array(
  710. array('name'=>'type', 'index'=>'type', 'width'=>'28', 'align'=>'center','sortable'=>'false'),
  711. array('name'=>'name', 'index'=>'name', 'width'=>'500', 'align'=>'left'),
  712. array('name'=>'size', 'index'=>'size', 'width'=>'35', 'align'=>'right','sortable'=>'true')
  713. );
  714. //Autowidth
  715. $extra_params['autowidth'] = 'true';
  716. //height auto
  717. $extra_params['height'] = 'auto';
  718. //With this function we can add actions to the jgrid (edit, delete, etc)
  719. $action_links = 'function action_formatter(cellvalue, options, rowObject) {
  720. return \'<a href="?action=edit&id=\'+options.rowId+\'">'.Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'.
  721. '&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>'.
  722. '&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>'.
  723. '\';
  724. }';
  725. $js_content = Display::grid_js('documents', '' ,$columns,$column_model, $extra_params, $sortable_data, $action_links,true);
  726. $htmlHeadXtra[] = '<script>
  727. $(function() {
  728. // grid definition see the $career->display() function
  729. '.$js_content.'
  730. });
  731. </script>';
  732. require_once 'controller.php';
  733. $controller = new DocumentController();
  734. $tpl = $controller->tpl->get_template('layout/layout_2_col.tpl');
  735. $content = Display::grid_html('documents');
  736. if (!is_null($docs_and_folders)) {
  737. // Show download zipped folder icon
  738. global $total_size;
  739. if (!$is_certificate_mode && $total_size != 0 && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  740. //for student does not show icon into other shared folder, and does not show into main path (root)
  741. if (is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id) && $curdirpath!='/' || api_is_allowed_to_edit() || api_is_platform_admin()) {
  742. $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>';
  743. }
  744. }
  745. }
  746. $content .= Display::div($link, array('class'=>'right'));
  747. $controller->tpl->assign('content', $content);
  748. $controller->tpl->display($tpl);
  749. //var_dump($sortable_data);