document.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Homepage script for the documents tool
  5. *
  6. * This script allows the user to manage files and directories on a remote http
  7. * server.
  8. * The user can : - navigate through files and directories.
  9. * - upload a file
  10. * - delete, copy a file or a directory
  11. * - edit properties & content (name, comments, html content)
  12. * The script is organised in four sections.
  13. *
  14. * 1) Execute the command called by the user
  15. * Note: somme commands of this section are organised in two steps.
  16. * The script always begins with the second step,
  17. * so it allows to return more easily to the first step.
  18. *
  19. * Note (March 2004) some editing functions (renaming, commenting)
  20. * are moved to a separate page, edit_document.php. This is also
  21. * where xml and other stuff should be added.
  22. * 2) Define the directory to display
  23. * 3) Read files and directories from the directory defined in part 2
  24. * 4) Display all of that on an HTML page
  25. *
  26. * @todo eliminate code duplication with document/document.php, scormdocument.php
  27. *
  28. * @package chamilo.document
  29. */
  30. /* INIT SECTION */
  31. // Name of the language file that needs to be included
  32. $language_file = array('document', 'slideshow');
  33. require_once '../inc/global.inc.php';
  34. $this_section = SECTION_COURSES;
  35. require_once 'document.inc.php';
  36. $lib_path = api_get_path(LIBRARY_PATH);
  37. require_once $lib_path.'usermanager.lib.php';
  38. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  39. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  40. api_protect_course_script(true);
  41. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  42. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/thickbox.js"></script>';
  43. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/thickbox.css" type="text/css" media="projection, screen">';
  44. $htmlHeadXtra[] = '<script type="text/javascript">
  45. $(document).ready( function() {
  46. for (i=0;i<$(".actions").length;i++) {
  47. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  48. $(".actions:eq("+i+")").hide();
  49. }
  50. }
  51. } );
  52. </script>';
  53. //session
  54. if (isset($_GET['id_session'])) {
  55. $_SESSION['id_session'] = Security::remove_XSS($_GET['id_session']);
  56. }
  57. //create directory certificates
  58. $course_id=api_get_course_id();
  59. DocumentManager::create_directory_certificate_in_course($course_id);
  60. //show preview
  61. if (isset($_GET['curdirpath']) && $_GET['curdirpath']=='/certificates' && isset($_GET['set_preview']) && $_GET['set_preview']==strval(intval($_GET['set_preview']))) {
  62. if (isset($_GET['set_preview'])) {
  63. //generate document HTML
  64. $course_id=api_get_course_id();
  65. $content_html=DocumentManager::replace_user_info_into_html($course_id);
  66. $new_content_html=$content_html;
  67. $path_image=api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/gallery';
  68. $new_content_html=str_replace('../images/gallery',$path_image,$new_content_html);
  69. $path_image_in_default_course=api_get_path(WEB_CODE_PATH).'default_course_document';
  70. $new_content_html=str_replace('/main/default_course_document',$path_image_in_default_course,$new_content_html);
  71. $path_image_in_dokeos_main=api_get_path(WEB_IMG_PATH);
  72. $new_content_html=str_replace('/main/img/',$path_image_in_dokeos_main,$new_content_html);
  73. echo '
  74. <style media="print" type="text/css">
  75. #imprimir {
  76. visibility:hidden;
  77. }
  78. </style>';
  79. echo '<a href="javascript:window.print();" style="float:right; padding:4px;" id="imprimir"><img src="../img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
  80. print_r($new_content_html);
  81. exit;
  82. }
  83. }
  84. // Is the document tool visible?
  85. // Check whether the tool is actually visible
  86. $table_course_tool = Database::get_course_table(TABLE_TOOL_LIST, $_course['dbName']);
  87. $tool_sql = 'SELECT visibility FROM ' . $table_course_tool . ' WHERE name = "'. TOOL_DOCUMENT .'" LIMIT 1';
  88. $tool_result = Database::query($tool_sql);
  89. $tool_row = Database::fetch_array($tool_result);
  90. $tool_visibility = $tool_row['visibility'];
  91. if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $group_member_with_upload_rights)) {
  92. api_not_allowed(true);
  93. }
  94. $htmlHeadXtra[] =
  95. "<script type=\"text/javascript\">
  96. function confirmation (name)
  97. {
  98. if (confirm(\" ". get_lang("AreYouSureToDelete") ." \"+ name + \" ?\"))
  99. {return true;}
  100. else
  101. {return false;}
  102. }
  103. </script>";
  104. /*
  105. Variables
  106. - some need defining before inclusion of libraries
  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. $course_dir = $_course['path'].'/document';
  119. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  120. $base_work_dir = $sys_course_path.$course_dir;
  121. $http_www = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document';
  122. $dbl_click_id = 0; // Used for avoiding double-click
  123. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  124. $group_member_with_upload_rights = false;
  125. // If the group id is set, we show them group documents
  126. if (isset($_SESSION['_gid']) && $_SESSION['_gid']!='') {
  127. // Needed for group related stuff
  128. require_once($lib_path. 'groupmanager.lib.php');
  129. // Get group info
  130. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  131. $noPHP_SELF=true;
  132. // Let's assume the user cannot upload files for the group
  133. $group_member_with_upload_rights = false;
  134. if ($group_properties['doc_state'] == 2) { // Documents are private
  135. if ($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'], $_SESSION['_gid'])) { // Only courseadmin or group members (members + tutors) allowed
  136. $to_group_id = $_SESSION['_gid'];
  137. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  138. $interbreadcrumb[] = array('url' => '../group/group.php', 'name' => get_lang('Groups'));
  139. $interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.$_SESSION['_gid'], 'name' => get_lang('GroupSpace').' ('.$group_properties['name'].')');
  140. //they are allowed to upload
  141. $group_member_with_upload_rights = true;
  142. } else {
  143. $to_group_id = 0;
  144. $req_gid = '';
  145. }
  146. } elseif ($group_properties['doc_state'] == 1) { // Documents are public
  147. $to_group_id = $_SESSION['_gid'];
  148. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  149. $interbreadcrumb[] = array ('url' => '../group/group.php', 'name' => get_lang('Groups'));
  150. $interbreadcrumb[] = array ('url' => '../group/group_space.php?gidReq='.$_SESSION['_gid'], 'name' => get_lang('GroupSpace').' ('.$group_properties['name'].')');
  151. //allowed to upload?
  152. if ($is_allowed_to_edit || GroupManager::is_subscribed($_user['user_id'], $_SESSION['_gid'])) { // Only courseadmin or group members can upload
  153. $group_member_with_upload_rights = true;
  154. }
  155. } else { //documents not active for this group
  156. $to_group_id = 0;
  157. $req_gid = '';
  158. }
  159. $_SESSION['group_member_with_upload_rights'] = $group_member_with_upload_rights;
  160. } else {
  161. $to_group_id = 0;
  162. $req_gid = '';
  163. }
  164. // For sessions we should check the parameters of visibility
  165. if (api_get_session_id() != 0) {
  166. $group_member_with_upload_rights = $group_member_with_upload_rights && api_is_allowed_to_session_edit(false, true);
  167. }
  168. /* Libraries */
  169. require_once $lib_path.'fileDisplay.lib.php';
  170. require_once $lib_path.'document.lib.php';
  171. require_once $lib_path.'tablesort.lib.php';
  172. require_once $lib_path.'fileUpload.lib.php';
  173. // Check the path
  174. // If the path is not found (no document id), set the path to /
  175. if (!DocumentManager::get_document_id($_course, $curdirpath)) {
  176. $curdirpath = '/';
  177. // Urlencoded version
  178. $curdirpathurl = '%2F';
  179. }
  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. if (!(DocumentManager::is_visible($curdirpath, $_course)||$is_allowed_to_edit)) {
  187. api_not_allowed();
  188. }
  189. /* Constants and variables */
  190. $course_quota = DocumentManager::get_course_quota();
  191. $current_session_id = api_get_session_id();
  192. /* Create the current user shared folder if no exist */
  193. if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.api_get_user_id())) {
  194. $usf_dir_title = api_get_person_name($_user['firstName'], $_user['lastName']);
  195. $usf_dir_name = '/shared_folder/sf_user_'.api_get_user_id();
  196. $to_group_id = 0;
  197. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title);
  198. }
  199. /* MAIN SECTION */
  200. if (isset($_GET['action']) && $_GET['action'] == 'download') {
  201. $my_get_id = Security::remove_XSS($_GET['id']);
  202. // Check whether the document is in the database
  203. if (!DocumentManager::get_document_id($_course, $my_get_id)) {
  204. // File not found!
  205. header('HTTP/1.0 404 Not Found');
  206. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  207. $error404 .= '<html><head>';
  208. $error404 .= '<title>404 Not Found</title>';
  209. $error404 .= '</head><body>';
  210. $error404 .= '<h1>Not Found</h1>';
  211. $error404 .= '<p>The requested URL was not found on this server.</p>';
  212. $error404 .= '<hr>';
  213. $error404 .= '</body></html>';
  214. echo($error404);
  215. exit;
  216. }
  217. // Launch event
  218. event_download($my_get_id);
  219. // Check visibility of document and paths
  220. if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible($my_get_id, $_course)) {
  221. api_not_allowed();
  222. }
  223. $doc_url = $my_get_id;
  224. $full_file_name = $base_work_dir.$doc_url;
  225. DocumentManager::file_send_for_download($full_file_name, true);
  226. exit;
  227. }
  228. // Download of an completed folder
  229. if (isset($_GET['action']) && $_GET['action'] == 'downloadfolder' && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  230. require 'downloadfolder.inc.php';
  231. }
  232. // Slideshow inititalisation
  233. $_SESSION['image_files_only'] = '';
  234. $image_files_only = '';
  235. /* Header */
  236. $interbreadcrumb[]= array ('url' => '', 'name' => get_lang('ToolDocument'));
  237. // Interbreadcrumb for the current directory root path
  238. $dir_array = explode('/', $curdirpath);
  239. $array_len = count($dir_array);
  240. if ($array_len > 1) {
  241. if (empty($_SESSION['_gid'])) {
  242. $url_dir = 'document.php?&curdirpath=/';
  243. $interbreadcrumb[] = array('url' => $url_dir, 'name' => get_lang('HomeDirectory'));
  244. }
  245. }
  246. $dir_acum = '';
  247. for ($i = 0; $i < $array_len; $i++) {
  248. if ($dir_array[$i] =='shared_folder') {
  249. $dir_array[$i] = get_lang('SharedFolder');
  250. } elseif (strpos($dir_array[$i], 'sf_user_') !== false) {
  251. $userinfo = Database::get_user_info_from_id(substr($dir_array[$i], 8));
  252. $dir_array[$i] = api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  253. }
  254. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  255. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $dir_array[$i]);
  256. $dir_acum.=$dir_array[$i].'/';
  257. }
  258. Display::display_header('','Doc');
  259. // Lib for event log, stats & tracking & record of the access
  260. event_access_tool(TOOL_DOCUMENT);
  261. /* DISPLAY */
  262. if ($to_group_id != 0) { // Add group name after for group documents
  263. $add_group_to_title = ' ('.$group_properties['name'].')';
  264. }
  265. /* Introduction section (editable by course admins) */
  266. if (!empty($_SESSION['_gid'])) {
  267. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  268. } else {
  269. Display::display_introduction_section(TOOL_DOCUMENT);
  270. }
  271. if ($is_allowed_to_edit || $group_member_with_upload_rights) { // TEACHER ONLY
  272. /* MOVE FILE OR DIRECTORY */
  273. $my_get_move = Security::remove_XSS($_GET['move']);
  274. if (isset($_GET['move']) && $_GET['move'] != '') {
  275. if (!$is_allowed_to_edit) {
  276. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  277. api_not_allowed();
  278. }
  279. }
  280. if (DocumentManager::get_document_id($_course, $my_get_move)) {
  281. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  282. echo '<div class="row"><div class="form_header">'.get_lang('Move').'</div></div>';
  283. echo build_move_to_selector($folders, Security::remove_XSS($_GET['curdirpath']), $my_get_move, $group_properties['directory']);
  284. }
  285. }
  286. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  287. if (!$is_allowed_to_edit) {
  288. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  289. api_not_allowed();
  290. }
  291. }
  292. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  293. // This is needed for the update_db_info function
  294. //$dbTable = $_course['dbNameGlu']."document";
  295. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  296. // Security fix: make sure they can't move files that are not in the document table
  297. if (DocumentManager::get_document_id($_course, $_POST['move_file'])) {
  298. if (move($base_work_dir.$_POST['move_file'], $base_work_dir.$_POST['move_to'])) {
  299. update_db_info('update', $_POST['move_file'], $_POST['move_to'].'/'.basename($_POST['move_file']));
  300. // Set the current path
  301. $curdirpath = $_POST['move_to'];
  302. $curdirpathurl = urlencode($_POST['move_to']);
  303. Display::display_confirmation_message(get_lang('DirMv'));
  304. } else {
  305. Display::display_error_message(get_lang('Impossible'));
  306. }
  307. } else {
  308. Display::display_error_message(get_lang('Impossible'));
  309. }
  310. }
  311. /* DELETE FILE OR DIRECTORY */
  312. if (isset($_GET['delete'])) {
  313. if (!$is_allowed_to_edit) {
  314. if (DocumentManager::check_readonly($_course, $_user['user_id'], $_GET['delete'], '', true)) {
  315. api_not_allowed();
  316. }
  317. }
  318. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  319. if(DocumentManager::delete_document($_course,$_GET['delete'],$base_work_dir))
  320. {
  321. if ( isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id']==strval(intval($_GET['delete_certificate_id'])) ) {
  322. $course_id=api_get_course_id();
  323. $default_certificate_id=$_GET['delete_certificate_id'];
  324. DocumentManager::remove_attach_certificate ($course_id,$default_certificate_id);
  325. }
  326. Display::display_confirmation_message(get_lang('DocDeleted'));
  327. } else {
  328. Display::display_error_message(get_lang('DocDeleteError'));
  329. }
  330. }
  331. if (isset($_POST['action'])) {
  332. switch ($_POST['action']) {
  333. case 'delete':
  334. foreach ($_POST['path'] as $index => & $path) {
  335. if (!$is_allowed_to_edit) {
  336. if (DocumentManager::check_readonly($_course, $_user['user_id'], $path)) {
  337. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  338. break 2;
  339. }
  340. }
  341. }
  342. foreach ($_POST['path'] as $index => & $path) {
  343. if (in_array($path, array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files'))) {
  344. continue;
  345. } else {
  346. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  347. }
  348. }
  349. if (!empty($delete_document)) {
  350. Display::display_confirmation_message(get_lang('DocDeleted'));
  351. }
  352. break;
  353. }
  354. }
  355. /* CREATE DIRECTORY */
  356. // Create directory with $_POST data
  357. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  358. // Needed for directory creation
  359. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  360. $post_dir_name = Security::remove_XSS($_POST['dirname']);
  361. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  362. Display::display_error_message(get_lang('CannotCreateDir'));
  363. } else {
  364. $added_slash = ($curdirpath == '/') ? '' : '/';
  365. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  366. $dir_name = disable_dangerous_file($dir_name);
  367. $dir_check = $base_work_dir.$dir_name;
  368. if (!is_dir($dir_check)) {
  369. $created_dir = create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name);
  370. if ($created_dir) {
  371. Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false);
  372. // Uncomment if you want to enter the created dir
  373. //$curdirpath = $created_dir;
  374. //$curdirpathurl = urlencode($curdirpath);
  375. } else {
  376. Display::display_error_message(get_lang('CannotCreateDir'));
  377. }
  378. } else {
  379. Display::display_error_message(get_lang('CannotCreateDir'));
  380. }
  381. }
  382. }
  383. // Show them the form for the directory name
  384. if (isset($_GET['createdir'])) {
  385. // Show the form
  386. echo create_dir_form();
  387. }
  388. /* VISIBILITY COMMANDS */
  389. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  390. // Make visible or invisible?
  391. if (isset($_GET['set_visible'])) {
  392. $update_id = $_GET['set_visible'];
  393. $visibility_command = 'visible';
  394. } else {
  395. $update_id = $_GET['set_invisible'];
  396. $visibility_command = 'invisible';
  397. }
  398. if (!$is_allowed_to_edit) {
  399. if(DocumentManager::check_readonly($_course, $_user['user_id'], '', $update_id)) {
  400. api_not_allowed();
  401. }
  402. }
  403. // Update item_property to change visibility
  404. if (api_item_property_update($_course, TOOL_DOCUMENT, $update_id, $visibility_command, $_user['user_id'], null, null, null, null, $current_session_id)) {
  405. Display::display_confirmation_message(get_lang('ViMod'));
  406. } else {
  407. Display::display_error_message(get_lang('ViModProb'));
  408. }
  409. }
  410. /* TEMPLATE ACTION */
  411. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  412. $document_id_for_template = intval($_GET['add_as_template']);
  413. // Create the form that asks for the directory name
  414. $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">';
  415. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  416. $template_text .= '<table><tr><td>';
  417. $template_text .= get_lang('TemplateName').' : </td>';
  418. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  419. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  420. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  421. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  422. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  423. $template_text .= '</table>';
  424. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang('CreateTemplate').'</button>';
  425. $template_text .= '</form>';
  426. // Show the form
  427. Display::display_normal_message($template_text, false);
  428. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  429. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  430. $title = Security::remove_XSS($_POST['template_title']);
  431. //$description = Security::remove_XSS($_POST['template_description']);
  432. $course_code = api_get_course_id();
  433. $user_id = api_get_user_id();
  434. // Create the template_thumbnails folder in the upload folder (if needed)
  435. if (!is_dir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/')) {
  436. @mkdir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/', api_get_permissions_for_new_directories());
  437. }
  438. // Upload the file
  439. if (!empty($_FILES['template_image']['name'])) {
  440. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  441. $upload_ok = process_uploaded_file($_FILES['template_image']);
  442. if ($upload_ok) {
  443. // Try to add an extension to the file if it hasn't one
  444. $new_file_name = $_course['sysCode'].'-'.add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  445. // Upload dir
  446. $upload_dir = api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/';
  447. // Resize image to max default and end upload
  448. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  449. $temp = new image($_FILES['template_image']['tmp_name']);
  450. $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
  451. $max_width_for_picture = 100;
  452. if ($picture_infos[0] > $max_width_for_picture) {
  453. $thumbwidth = $max_width_for_picture;
  454. if (empty($thumbwidth) || $thumbwidth == 0) {
  455. $thumbwidth = $max_width_for_picture;
  456. }
  457. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  458. $temp->resize($thumbwidth, $new_height, 0);
  459. }
  460. $type = $picture_infos[2];
  461. switch (!empty($type)) {
  462. case 2 : $temp->send_image('JPG', $upload_dir.$new_file_name);
  463. break;
  464. case 3 : $temp->send_image('PNG', $upload_dir.$new_file_name);
  465. break;
  466. case 1 : $temp->send_image('GIF', $upload_dir.$new_file_name);
  467. break;
  468. }
  469. }
  470. }
  471. DocumentManager::set_document_as_template($title, $description, $document_id_for_template, $course_code, $user_id, $new_file_name);
  472. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  473. }
  474. if (isset($_GET['remove_as_template'])) {
  475. $document_id_for_template = intval($_GET['remove_as_template']);
  476. $course_code = api_get_course_id();
  477. $user_id = api_get_user_id();
  478. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  479. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  480. }
  481. } // END is allowed to edit
  482. //attach certificate in the gradebook
  483. if (isset($_GET['curdirpath']) && $_GET['curdirpath']=='/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate']==strval(intval($_GET['set_certificate']))) {
  484. if (isset($_GET['cidReq'])) {
  485. $course_id=Security::remove_XSS($_GET['cidReq']);//course id
  486. $document_id=Security::remove_XSS($_GET['set_certificate']);//document id
  487. DocumentManager::attach_gradebook_certificate ($course_id,$document_id) ;
  488. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  489. }
  490. }
  491. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  492. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights);
  493. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  494. if ($folders === false) {
  495. $folders = array();
  496. }
  497. echo '<div class="actions">';
  498. /* GO TO PARENT DIRECTORY */
  499. if ($curdirpath!= '/' && $curdirpath!=$group_properties['directory']&& !$is_certificate_mode) {
  500. ?>
  501. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath)=='\\')?'/':dirname($curdirpath)).$req_gid ?>">
  502. <img src="../img/folder_up.gif" border="0" title="<?php echo get_lang('Up'); ?>" alt="" />
  503. <?php echo get_lang('Up'); ?></a>&nbsp;
  504. <?php
  505. }
  506. if ($is_certificate_mode & $curdirpath!= '/certificates') {
  507. ?>
  508. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath)=='\\')?'/':dirname($curdirpath)).$req_gid ?>">
  509. <img src="../img/folder_up.gif" border="0" title="<?php echo get_lang('Up'); ?>" alt="" />
  510. <?php echo get_lang('Up'); ?></a>&nbsp;
  511. <?php
  512. }
  513. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  514. //echo('<pre>');
  515. //print_r($docs_and_folders);
  516. //echo('</pre>');
  517. // Do we need the title field for the document name or not?
  518. // We get the setting here, so we only have to do it once
  519. $use_document_title = api_get_setting('use_document_title');
  520. // Create a sortable table with our data
  521. $sortable_data = array();
  522. while (list($key, $id) = each($docs_and_folders)) {
  523. $row = array();
  524. // If the item is invisible, wrap it in a span with class invisible
  525. $invisibility_span_open = ($id['visibility'] == 0) ? '<span class="invisible">' : '';
  526. $invisibility_span_close = ($id['visibility'] == 0) ? '</span>' : '';
  527. // Size (or total size of a directory)
  528. $size = $id['filetype'] == 'folder' ? get_total_folder_size($id['path'], $is_allowed_to_edit) : $id['size'];
  529. // Get the title or the basename depending on what we're using
  530. if ($use_document_title == 'true' && $id['title'] != '') {
  531. $document_name = $id['title'];
  532. } else {
  533. $document_name = basename($id['path']);
  534. //Juan Carlos Raña: Get firstname and lastname when folder is in shared_folder.
  535. //TODO: Check if is also necessary (above else)
  536. if (strstr($document_name, 'sf_user_')) {
  537. $userinfo = Database::get_user_info_from_id(substr($document_name, 8));
  538. $document_name = api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  539. } elseif (strpos($document_name, 'shared_folder') !== false) {
  540. $document_name = get_lang('SharedFolder');
  541. }
  542. }
  543. // Data for checkbox
  544. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  545. $row[] = $id['path'];
  546. }
  547. // Show the owner of the file only in groups
  548. $user_link = '';
  549. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  550. if (!empty($id['insert_user_id'])) {
  551. $user_info = UserManager::get_user_info_by_id($id['insert_user_id']);
  552. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  553. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document($id['insert_user_id'], $user_name).'</div>';
  554. }
  555. }
  556. // Icons (clickable)
  557. //$row[]= build_document_icon_tag($id['filetype'],$id['path']);
  558. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility'], true);
  559. // Validacion when belongs to a session
  560. $session_img = api_get_session_image($id['session_id'], $_user['status']);
  561. // Document title with hyperlink
  562. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility']).$session_img.'<br />'.$invisibility_span_open.nl2br(htmlspecialchars($id['comment'],ENT_QUOTES,$charset)).$invisibility_span_close.$user_link;
  563. // Comments => display comment under the document name
  564. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  565. $display_size = format_file_size($size);
  566. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  567. // Last edit date
  568. $last_edit_date = $id['lastedit_date'];
  569. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  570. $display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  571. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close.'<!--uts='.strtotime($last_edit_date).'-->';
  572. // Admins get an edit column
  573. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  574. $is_template = isset($id['is_template']) ? $id['is_template'] : false;
  575. // If readonly, check if it the owner of the file or if the user is an admin
  576. if ($id['insert_user_id'] == $_user['user_id'] || api_is_platform_admin()) {
  577. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, 0);
  578. } else {
  579. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, $id['readonly']);
  580. }
  581. $row[] = $edit_icons;
  582. }
  583. $row[] = $last_edit_date;
  584. $row[] = $size;
  585. $total_size = $total_size + $size;
  586. $sortable_data[] = $row;
  587. }
  588. } else {
  589. $sortable_data = '';
  590. $table_footer = '<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  591. }
  592. $column_show = array();
  593. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder($_user['user_id'], $curdirpath)) {
  594. // @TODO:check enable more options for shared folders
  595. /* CREATE NEW DOCUMENT OR NEW DIRECTORY / GO TO UPLOAD / DOWNLOAD ZIPPED FOLDER */
  596. ?>
  597. <!-- create new document or directory -->
  598. <?php if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'],$curdirpath))
  599. {?>
  600. <a href="create_document.php?<?php echo api_get_cidreq();?>&dir=<?php echo $curdirpathurl.$req_gid; ?>"><img src="../img/filenew.gif" border="0" alt="" title="<?php echo get_lang('CreateDoc'); ?>" /></a>
  601. <a href="create_document.php?<?php echo api_get_cidreq();?>&dir=<?php echo $curdirpathurl.$req_gid; ?>"><?php echo get_lang("CreateDoc"); ?></a>&nbsp;&nbsp;
  602. <?php }?>
  603. <!--create new certificate -->
  604. <?php if ($is_certificate_mode) { ?>
  605. <a href="create_document.php?<?php echo api_get_cidreq();?>&dir=<?php echo $curdirpathurl.$req_gid; ?>&certificate=true&<?php echo 'selectcat=' . Security::remove_XSS($_GET['selectcat']); ?>"><img src="../img/filenew.gif" border="0" alt="" title="<?php echo get_lang('CreateCertificate'); ?>" /></a>
  606. <a href="create_document.php?<?php echo api_get_cidreq();?>&dir=<?php echo $curdirpathurl.$req_gid; ?>&certificate=true&<?php echo 'selectcat=' . Security::remove_XSS($_GET['selectcat']); ?>"><?php echo get_lang("CreateCertificate"); ?></a>&nbsp;&nbsp;
  607. <?php }?>
  608. <!-- file upload link -->
  609. <?php
  610. $upload_name = get_lang('UplUploadDocument');
  611. if ($is_certificate_mode) {
  612. $upload_name = get_lang('UploadCertificate');
  613. }?>
  614. <a href="upload.php?<?php echo api_get_cidreq();?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>"><img src="../img/submit_file.gif" border="0" title="<?php echo get_lang('UplUploadDocument'); ?>" alt="" /></a>
  615. <a href="upload.php?<?php echo api_get_cidreq();?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>"><?php echo get_lang('UplUploadDocument'); ?></a>&nbsp;
  616. <!-- create directory -->
  617. <?php if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'],$curdirpath))
  618. {?>
  619. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>&amp;createdir=1"><img src="../img/folder_new.gif" border="0" title="<?php echo get_lang('CreateDir'); ?>" alt ="" /></a>
  620. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>&amp;createdir=1"><?php echo get_lang("CreateDir"); ?></a>&nbsp;
  621. <a href="quota.php?<?php echo api_get_cidreq();?>"><?php Display::display_icon('statistics.gif', get_lang("ShowCourseQuotaUse")); ?><?php echo get_lang("ShowCourseQuotaUse"); ?></a>
  622. <?php
  623. }
  624. }
  625. if (!is_null($docs_and_folders)) {
  626. global $total_size;
  627. if (!$is_certificate_mode && $total_size!=0 && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())){ ?>
  628. <!-- download zipped folder -->
  629. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&action=downloadfolder"><img src="../img/zip_save.gif" border="0" title="<?php echo get_lang("Save"); ?> (ZIP)" alt="" /></a>
  630. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&action=downloadfolder"><?php echo get_lang("Save"); ?> (ZIP)</a>&nbsp;
  631. <?php
  632. }
  633. }
  634. // Slideshow by Patrick Cool, May 2004
  635. require 'document_slideshow.inc.php';
  636. if ($image_present) {
  637. echo "<a href=\"slideshow.php?".api_get_cidreq()."&curdirpath=".$curdirpathurl."\"><img src=\"../img/images_gallery.gif\" border=\"0\" title=\"".get_lang('ViewSlideshow')."\"/>&nbsp;". get_lang('ViewSlideshow') . "</a>";
  638. }
  639. echo '</div>';
  640. if (!$is_certificate_mode)
  641. echo build_directory_selector($folders, $curdirpath, (isset($group_properties['directory']) ? $group_properties['directory'] : array()), true);
  642. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  643. $column_show[] = 1;
  644. }
  645. $column_show[] = 1;
  646. $column_show[] = 1;
  647. $column_show[] = 1;
  648. $column_show[] = 1;
  649. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  650. $column_show[] = 1;
  651. }
  652. $column_show[] = 0;
  653. $column_order = array();
  654. if (count($row) == 8) {
  655. $column_order[] = 1;
  656. $column_order[] = 2;
  657. $column_order[] = 7;
  658. $column_order[] = 6;
  659. $column_order[] = 5;
  660. } elseif (count($row) == 6) {
  661. $column_order[] = 1;
  662. $column_order[] = 2;
  663. $column_order[] = 7;
  664. $column_order[] = 4;
  665. }
  666. $default_column = $is_allowed_to_edit ? 2 : 1;
  667. $tablename = $is_allowed_to_edit ? 'teacher_table' : 'student_table';
  668. $table = new SortableTableFromArrayConfig($sortable_data, $default_column, 20, $tablename, $column_show, $column_order, 'ASC');
  669. $query_vars['curdirpath'] = $curdirpath;
  670. if (isset($_SESSION['_gid'])) {
  671. $query_vars['gidReq'] = $_SESSION['_gid'];
  672. }
  673. $query_vars['cidReq'] = api_get_course_id();
  674. $table->set_additional_parameters($query_vars);
  675. $column = 0;
  676. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  677. $table->set_header($column++, '', false);
  678. }
  679. $table->set_header($column++, get_lang('Type'));
  680. $table->set_header($column++, get_lang('Name'));
  681. //$column_header[] = array(get_lang('Comment'), true); // Display comment under the document name
  682. $table->set_header($column++, get_lang('Size'));
  683. $table->set_header($column++, get_lang('Date'));
  684. // Admins get an edit column
  685. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  686. $table->set_header($column++, get_lang('Modify'), false);
  687. }
  688. // Actions on multiple selected documents
  689. // TODO: Currently only delete action -> take only DELETE right into account
  690. if (count($docs_and_folders) > 1) {
  691. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  692. $form_actions = array();
  693. $form_action['delete'] = get_lang('Delete');
  694. $table->set_form_actions($form_action, 'path');
  695. }
  696. }
  697. $table->display();
  698. if (!empty($table_footer)) {
  699. echo $table_footer;
  700. }
  701. // Footer
  702. Display::display_footer();