document.php 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Homepage script for the documents tool
  6. *
  7. * This script allows the user to manage files and directories on a remote http
  8. * server.
  9. * The user can : - navigate through files and directories.
  10. * - upload a file
  11. * - delete, copy a file or a directory
  12. * - edit properties & content (name, comments, html content)
  13. * The script is organised in four sections.
  14. *
  15. * 1) Execute the command called by the user
  16. * Note: somme commands of this section are organised in two steps.
  17. * The script always begins with the second step,
  18. * so it allows to return more easily to the first step.
  19. *
  20. * Note (March 2004) some editing functions (renaming, commenting)
  21. * are moved to a separate page, edit_document.php. This is also
  22. * where xml and other stuff should be added.
  23. * 2) Define the directory to display
  24. * 3) Read files and directories from the directory defined in part 2
  25. * 4) Display all of that on an HTML page
  26. *
  27. * @package chamilo.document
  28. */
  29. require_once __DIR__.'/../inc/global.inc.php';
  30. $allowDownloadDocumentsByApiKey = api_get_setting('allow_download_documents_by_api_key') === 'true';
  31. $current_course_tool = TOOL_DOCUMENT;
  32. $this_section = SECTION_COURSES;
  33. $to_user_id = null;
  34. $parent_id = null;
  35. $lib_path = api_get_path(LIBRARY_PATH);
  36. $actionsRight = '';
  37. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  38. $allowUseTool = false;
  39. if ($allowDownloadDocumentsByApiKey) {
  40. try {
  41. if ($action != 'download') {
  42. throw new Exception(get_lang('SelectAnAction'));
  43. }
  44. $username = isset($_GET['username']) ? Security::remove_XSS($_GET['username']) : null;
  45. $apiKey = isset($_GET['api_key']) ? Security::remove_XSS($_GET['api_key']) : null;
  46. $restApi = Rest::validate($username, $apiKey);
  47. $allowUseTool = $restApi ? true : false;
  48. } catch (Exception $e) {
  49. $allowUseTool = false;
  50. }
  51. }
  52. if (!$allowUseTool) {
  53. api_protect_course_script(true);
  54. api_protect_course_group(GroupManager::GROUP_TOOL_DOCUMENTS);
  55. }
  56. DocumentManager::removeGeneratedAudioTempFile();
  57. $tempRealPath = Session::read('temp_realpath_image');
  58. if (!empty($tempRealPath) &&
  59. file_exists($tempRealPath)
  60. ) {
  61. unlink($tempRealPath);
  62. }
  63. $_user = api_get_user_info();
  64. $courseInfo = api_get_course_info();
  65. $courseId = $courseInfo['real_id'];
  66. $course_dir = $courseInfo['directory'].'/document';
  67. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  68. $base_work_dir = $sys_course_path.$course_dir;
  69. $http_www = api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/document';
  70. $document_path = $base_work_dir;
  71. $usePpt2lp = api_get_setting('service_ppt2lp', 'active') == 'true';
  72. $course_dir = $courseInfo['directory'].'/document';
  73. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  74. $base_work_dir = $sys_course_path.$course_dir;
  75. $http_www = api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/document';
  76. $document_path = $base_work_dir;
  77. $currentUrl = api_get_self().'?'.api_get_cidreq();
  78. // I'm in the certification module?
  79. $is_certificate_mode = false;
  80. if (isset($_GET['curdirpath'])) {
  81. $is_certificate_mode = DocumentManager::is_certificate_mode($_GET['curdirpath']);
  82. }
  83. if (isset($_REQUEST['certificate']) && $_REQUEST['certificate'] == 'true') {
  84. $is_certificate_mode = true;
  85. }
  86. // Removing sessions
  87. Session::erase('draw_dir');
  88. Session::erase('paint_dir');
  89. Session::erase('temp_audio_nanogong');
  90. $plugin = new AppPlugin();
  91. $pluginList = $plugin->get_installed_plugins();
  92. $capturePluginInstalled = in_array('jcapture', $pluginList);
  93. if ($capturePluginInstalled) {
  94. $jcapturePath = api_get_path(WEB_PLUGIN_PATH).'jcapture/plugin_applet.php';
  95. $htmlHeadXtra[]
  96. = '<script>
  97. $(function() {
  98. $("#jcapture").click(function(){
  99. $("#appletplace").load("'.$jcapturePath.'");
  100. });
  101. });
  102. </script>
  103. ';
  104. }
  105. if (empty($courseInfo)) {
  106. api_not_allowed(true);
  107. }
  108. // Create directory certificates.
  109. DocumentManager::create_directory_certificate_in_course($courseInfo);
  110. // Used for avoiding double-click.
  111. $dbl_click_id = 0;
  112. $selectcat = isset($_GET['selectcat']) ? Security::remove_XSS($_GET['selectcat']) : null;
  113. $moveTo = isset($_POST['move_to']) ? Security::remove_XSS($_POST['move_to']) : null;
  114. /* Constants and variables */
  115. $userId = api_get_user_id();
  116. $userInfo = api_get_user_info();
  117. $sessionId = api_get_session_id();
  118. $course_code = api_get_course_id();
  119. $groupId = api_get_group_id();
  120. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  121. $group_member_with_upload_rights = false;
  122. // If the group id is set, we show them group documents
  123. $group_properties = array();
  124. $group_properties['directory'] = null;
  125. // For sessions we should check the parameters of visibility
  126. if (api_get_session_id() != 0) {
  127. $group_member_with_upload_rights = $group_member_with_upload_rights && api_is_allowed_to_session_edit(false, true);
  128. }
  129. // Get group info
  130. $groupIid = 0;
  131. $groupMemberWithEditRights = false;
  132. // Setting group variables.
  133. if (!empty($groupId)) {
  134. $group_properties = GroupManager::get_group_properties($groupId);
  135. $groupIid = isset($group_properties['iid']) ? $group_properties['iid'] : 0;
  136. $isTutorGroup = GroupManager::is_tutor_of_group(
  137. $userId,
  138. $group_properties,
  139. $courseId
  140. );
  141. $groupMemberWithEditRights = $isAllowedToEdit || $isTutorGroup;
  142. // Let's assume the user cannot upload files for the group
  143. $group_member_with_upload_rights = false;
  144. if ($group_properties['doc_state'] == 2) {
  145. // Documents are private
  146. if ($isAllowedToEdit || GroupManager::is_user_in_group($userId, $group_properties)) {
  147. // Only courseadmin or group members (members + tutors) allowed
  148. $interbreadcrumb[] = array(
  149. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  150. 'name' => get_lang('Groups')
  151. );
  152. $interbreadcrumb[] = array(
  153. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  154. 'name' => get_lang('GroupSpace').' '.$group_properties['name']
  155. );
  156. //they are allowed to upload
  157. $group_member_with_upload_rights = true;
  158. } else {
  159. $groupId = 0;
  160. }
  161. } elseif ($group_properties['doc_state'] == 1) {
  162. // Documents are public
  163. $interbreadcrumb[] = array(
  164. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  165. 'name' => get_lang('Groups')
  166. );
  167. $interbreadcrumb[] = array(
  168. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  169. 'name' => get_lang('GroupSpace').' '.$group_properties['name']
  170. );
  171. // Allowed to upload?
  172. if ($isAllowedToEdit ||
  173. GroupManager::is_subscribed($userId, $group_properties) ||
  174. GroupManager::is_tutor_of_group($userId, $group_properties, $courseId)
  175. ) {
  176. // Only course admin or group members can upload
  177. $group_member_with_upload_rights = true;
  178. }
  179. }
  180. Session::write('group_member_with_upload_rights', $group_member_with_upload_rights);
  181. } else {
  182. Session::write('group_member_with_upload_rights', false);
  183. }
  184. // Actions.
  185. $document_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
  186. $currentUrl = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
  187. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  188. switch ($action) {
  189. case 'delete_item':
  190. if ($isAllowedToEdit ||
  191. $group_member_with_upload_rights ||
  192. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) ||
  193. DocumentManager::is_my_shared_folder(api_get_user_id(), $moveTo, $sessionId)
  194. ) {
  195. if (isset($_GET['deleteid'])) {
  196. if (!$isAllowedToEdit) {
  197. if (api_is_coach()) {
  198. if (!DocumentManager::is_visible_by_id(
  199. $_GET['deleteid'],
  200. $courseInfo,
  201. $sessionId,
  202. api_get_user_id()
  203. )
  204. ) {
  205. api_not_allowed();
  206. }
  207. }
  208. if (DocumentManager::check_readonly($courseInfo, api_get_user_id(), '', $_GET['deleteid'], true)) {
  209. api_not_allowed();
  210. }
  211. }
  212. $documentInfo = DocumentManager::get_document_data_by_id(
  213. $_GET['deleteid'],
  214. $courseInfo['code'],
  215. false,
  216. $sessionId
  217. );
  218. // Check whether the document is in the database.
  219. if (!empty($documentInfo)) {
  220. $deleteDocument = DocumentManager::delete_document(
  221. $courseInfo,
  222. null,
  223. $base_work_dir,
  224. $sessionId,
  225. $_GET['deleteid'],
  226. $groupIid
  227. );
  228. if ($deleteDocument) {
  229. $certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
  230. DocumentManager::remove_attach_certificate(
  231. api_get_course_id(),
  232. $certificateId
  233. );
  234. Display::addFlash(Display::return_message(
  235. get_lang('DocDeleted').': '.$documentInfo['title'],
  236. 'success'
  237. ));
  238. } else {
  239. Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
  240. }
  241. } else {
  242. Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
  243. }
  244. header("Location: $currentUrl");
  245. exit;
  246. }
  247. }
  248. break;
  249. case 'download':
  250. // Get the document data from the ID
  251. $document_data = DocumentManager::get_document_data_by_id(
  252. $document_id,
  253. api_get_course_id(),
  254. false,
  255. $sessionId
  256. );
  257. if ($sessionId != 0 && !$document_data) {
  258. // If there is a session defined and asking for the document *from
  259. // the session* didn't work, try it from the course (out of a
  260. // session context)
  261. $document_data = DocumentManager::get_document_data_by_id(
  262. $document_id,
  263. api_get_course_id(),
  264. false,
  265. 0
  266. );
  267. }
  268. // Check whether the document is in the database
  269. if (empty($document_data)) {
  270. api_not_allowed();
  271. }
  272. // Launch event
  273. Event::event_download($document_data['url']);
  274. // Check visibility of document and paths
  275. if (!($isAllowedToEdit || $group_member_with_upload_rights) &&
  276. !DocumentManager::is_visible_by_id($document_id, $courseInfo, $sessionId, api_get_user_id())
  277. ) {
  278. api_not_allowed(true);
  279. }
  280. $full_file_name = $base_work_dir.$document_data['path'];
  281. if (Security::check_abs_path($full_file_name, $base_work_dir.'/')) {
  282. $result = DocumentManager::file_send_for_download($full_file_name, true);
  283. if ($result === false) {
  284. api_not_allowed(true);
  285. }
  286. }
  287. exit;
  288. break;
  289. case 'downloadfolder':
  290. if (api_get_setting('students_download_folders') == 'true'
  291. || $isAllowedToEdit
  292. || api_is_platform_admin()
  293. ) {
  294. // Get the document data from the ID
  295. $document_data = DocumentManager::get_document_data_by_id(
  296. $document_id,
  297. api_get_course_id(),
  298. false,
  299. $sessionId
  300. );
  301. if ($sessionId != 0 && !$document_data) {
  302. // If there is a session defined and asking for the
  303. // document * from the session* didn't work, try it from the
  304. // course (out of a session context)
  305. $document_data = DocumentManager::get_document_data_by_id(
  306. $document_id,
  307. api_get_course_id(),
  308. false,
  309. 0
  310. );
  311. }
  312. //filter when I am into shared folder, I can download only my shared folder
  313. if (DocumentManager::is_any_user_shared_folder($document_data['path'], $sessionId)) {
  314. if (DocumentManager::is_my_shared_folder(api_get_user_id(), $document_data['path'], $sessionId) ||
  315. $isAllowedToEdit || api_is_platform_admin()
  316. ) {
  317. require 'downloadfolder.inc.php';
  318. }
  319. } else {
  320. require 'downloadfolder.inc.php';
  321. }
  322. // Launch event
  323. Event::event_download($document_data['url']);
  324. exit;
  325. }
  326. break;
  327. case 'export_to_pdf':
  328. if (api_get_setting('students_export2pdf') == 'true' ||
  329. $isAllowedToEdit || api_is_platform_admin()
  330. ) {
  331. $documentOrientation = api_get_configuration_value('document_pdf_orientation');
  332. $orientation = in_array($documentOrientation, ['landscape', 'portrait'])
  333. ? $documentOrientation
  334. : 'landscape';
  335. $showHeaderAndFooter = true;
  336. if ($is_certificate_mode) {
  337. $certificateOrientation = api_get_configuration_value('certificate_pdf_orientation');
  338. $orientation = in_array($certificateOrientation, ['landscape', 'portrait'])
  339. ? $certificateOrientation
  340. : 'landscape';
  341. $showHeaderAndFooter = !api_get_configuration_value('hide_header_footer_in_certificate');
  342. }
  343. DocumentManager::export_to_pdf($document_id, $course_code, $orientation, $showHeaderAndFooter);
  344. }
  345. break;
  346. case 'copytomyfiles':
  347. // Copy a file to general my files user's
  348. if (api_get_setting('allow_my_files') == 'true' &&
  349. api_get_setting('users_copy_files') == 'true' &&
  350. api_get_user_id() != 0 &&
  351. !api_is_anonymous()
  352. ) {
  353. // Get the document data from the ID
  354. $document_info = DocumentManager::get_document_data_by_id(
  355. $document_id,
  356. api_get_course_id(),
  357. true,
  358. $sessionId
  359. );
  360. if ($sessionId != 0 && !$document_info) {
  361. /* If there is a session defined and asking for the document
  362. from the session didn't work, try it from the course
  363. (out of a session context)*/
  364. $document_info = DocumentManager::get_document_data_by_id(
  365. $document_id,
  366. api_get_course_id(),
  367. 0
  368. );
  369. }
  370. $parent_id = $document_info['parent_id'];
  371. $my_path = UserManager::getUserPathById(api_get_user_id(), 'system');
  372. $user_folder = $my_path.'my_files/';
  373. $my_path = null;
  374. if (!file_exists($user_folder)) {
  375. $perm = api_get_permissions_for_new_directories();
  376. @mkdir($user_folder, $perm, true);
  377. }
  378. $file = $sys_course_path.$courseInfo['directory'].'/document'.$document_info['path'];
  379. $copyfile = $user_folder.basename($document_info['path']);
  380. $cidReq = Security::remove_XSS($_GET['cidReq']);
  381. $id_session = Security::remove_XSS($_GET['id_session']);
  382. $gidReq = Security::remove_XSS($_GET['gidReq']);
  383. $id = Security::remove_XSS($_GET['id']);
  384. if (empty($parent_id)) {
  385. $parent_id = 0;
  386. }
  387. $file_link = Display::url(
  388. get_lang('SeeFile'),
  389. api_get_path(WEB_CODE_PATH).'social/myfiles.php?'
  390. .api_get_cidreq_params($cidReq, $id_session, $gidReq).
  391. '&parent_id='.$parent_id
  392. );
  393. if (api_get_setting('allow_my_files') === 'false') {
  394. $file_link = '';
  395. }
  396. if (file_exists($copyfile)) {
  397. $message = get_lang('CopyAlreadyDone').'</p><p>';
  398. $message .= '<a class = "btn btn-default" '
  399. .'href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='
  400. .$parent_id.'">'
  401. .get_lang("No")
  402. .'</a>'
  403. .'&nbsp;&nbsp;|&nbsp;&nbsp;'
  404. .'<a class = "btn btn-default" href="'.api_get_self().'?'
  405. .api_get_cidreq().'&amp;action=copytomyfiles&amp;id='
  406. .$document_info['id']
  407. .'&amp;copy=yes">'
  408. .get_lang('Yes')
  409. .'</a></p>';
  410. if (!isset($_GET['copy'])) {
  411. Display::addFlash(Display::return_message($message, 'warning', false));
  412. }
  413. if ($_GET['copy'] === 'yes') {
  414. if (!copy($file, $copyfile)) {
  415. Display::addFlash(Display::return_message(get_lang('CopyFailed'), 'error'));
  416. } else {
  417. Display::addFlash(Display::return_message(
  418. get_lang('OverwritenFile').' '.$file_link,
  419. 'confirmation',
  420. false
  421. ));
  422. }
  423. }
  424. } else {
  425. if (!copy($file, $copyfile)) {
  426. Display::addFlash(Display::return_message(get_lang('CopyFailed'), 'error'));
  427. } else {
  428. Display::addFlash(
  429. Display::return_message(get_lang('CopyMade').' '.$file_link, 'confirmation', false)
  430. );
  431. }
  432. }
  433. }
  434. break;
  435. case 'convertToPdf':
  436. // PDF format as target by default
  437. $formatTarget = $_REQUEST['formatTarget']
  438. ? strtolower(Security::remove_XSS($_REQUEST['formatTarget']))
  439. : 'pdf';
  440. $formatType = $_REQUEST['formatType']
  441. ? strtolower(Security::remove_XSS($_REQUEST['formatType']))
  442. : 'text';
  443. // Get the document data from the ID
  444. $document_info = DocumentManager::get_document_data_by_id(
  445. $document_id,
  446. api_get_course_id(),
  447. true,
  448. $session_id
  449. );
  450. $file = $sys_course_path.$courseInfo['directory'].'/document'.$document_info['path'];
  451. $fileInfo = pathinfo($file);
  452. if ($fileInfo['extension'] == $formatTarget) {
  453. Display::addFlash(Display::return_message(
  454. get_lang('ConversionToSameFileFormat'),
  455. 'warning'
  456. ));
  457. } elseif (!(in_array($fileInfo['extension'], DocumentManager::getJodconverterExtensionList('from', $formatType))) ||
  458. !(in_array($formatTarget, DocumentManager::getJodconverterExtensionList('to', $formatType)))
  459. ) {
  460. Display::addFlash(Display::return_message(
  461. get_lang('FileFormatNotSupported'),
  462. 'warning'
  463. ));
  464. } else {
  465. $convertedFile = $fileInfo['dirname'].DIRECTORY_SEPARATOR
  466. .$fileInfo['filename'].'_from_'.$fileInfo['extension']
  467. .'.'.$formatTarget;
  468. $convertedTitle = $document_info['title'];
  469. $obj = new OpenofficePresentation(true);
  470. if (file_exists($convertedFile)) {
  471. Display::addFlash(Display::return_message(
  472. get_lang('FileExists'),
  473. 'error'
  474. ));
  475. } else {
  476. $result = $obj->convertCopyDocument(
  477. $file,
  478. $convertedFile,
  479. $convertedTitle
  480. );
  481. if (empty($result)) {
  482. Display::addFlash(Display::return_message(
  483. get_lang('CopyFailed'),
  484. 'error'
  485. ));
  486. } else {
  487. $cidReq = Security::remove_XSS($_GET['cidReq']);
  488. $id_session = api_get_session_id();
  489. $gidReq = Security::remove_XSS($_GET['gidReq']);
  490. $file_link = Display::url(
  491. get_lang('SeeFile'),
  492. api_get_path(WEB_CODE_PATH)
  493. .'document/showinframes.php?'
  494. .api_get_cidreq_params($cidReq, $id_session, $gidReq)
  495. .'&id='.current($result)
  496. );
  497. Display::addFlash(Display::return_message(
  498. get_lang('CopyMade').' '.$file_link,
  499. 'confirmation',
  500. false
  501. ));
  502. }
  503. }
  504. }
  505. break;
  506. }
  507. // If no actions we proceed to show the document (Hack in order to use document.php?id=X)
  508. if (isset($document_id) && empty($action)) {
  509. // Get the document data from the ID
  510. $document_data = DocumentManager::get_document_data_by_id(
  511. $document_id,
  512. api_get_course_id(),
  513. true,
  514. $sessionId
  515. );
  516. if ($sessionId != 0 && !$document_data) {
  517. // If there is a session defined and asking for the
  518. // document * from the session* didn't work, try it from the course
  519. // (out of a session context)
  520. $document_data = DocumentManager::get_document_data_by_id(
  521. $document_id,
  522. api_get_course_id(),
  523. true,
  524. 0
  525. );
  526. }
  527. // If the document is not a folder we show the document.
  528. if ($document_data) {
  529. $parent_id = $document_data['parent_id'];
  530. $visibility = DocumentManager::check_visibility_tree(
  531. $document_id,
  532. api_get_course_id(),
  533. $sessionId,
  534. api_get_user_id(),
  535. $groupIid
  536. );
  537. if (!empty($document_data['filetype']) && $document_data['filetype'] == 'file') {
  538. if ($visibility && api_is_allowed_to_session_edit()) {
  539. $url = api_get_path(WEB_COURSE_PATH).
  540. $courseInfo['path'].'/document'.$document_data['path'].'?'
  541. .api_get_cidreq();
  542. header("Location: $url");
  543. }
  544. exit;
  545. } else {
  546. if (!$visibility && !$isAllowedToEdit) {
  547. api_not_allowed();
  548. }
  549. }
  550. $_GET['curdirpath'] = $document_data['path'];
  551. }
  552. // What's the current path?
  553. // We will verify this a bit further down
  554. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  555. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  556. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  557. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  558. } else {
  559. $curdirpath = '/';
  560. }
  561. $curdirpathurl = urlencode($curdirpath);
  562. } else {
  563. // What's the current path?
  564. // We will verify this a bit further down
  565. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  566. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  567. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  568. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  569. } else {
  570. $curdirpath = '/';
  571. }
  572. $curdirpathurl = urlencode($curdirpath);
  573. // Check the path
  574. // If the path is not found (no document id), set the path to /
  575. $document_id = DocumentManager::get_document_id($courseInfo, $curdirpath);
  576. if (!$document_id) {
  577. $document_id = DocumentManager::get_document_id($courseInfo, $curdirpath, 0);
  578. }
  579. $document_data = DocumentManager::get_document_data_by_id(
  580. $document_id,
  581. api_get_course_id(),
  582. true
  583. );
  584. $parent_id = $document_data['parent_id'];
  585. }
  586. if (isset($document_data) && $document_data['path'] == '/certificates') {
  587. $is_certificate_mode = true;
  588. }
  589. if (!$parent_id) {
  590. $testParentId = 0;
  591. // Get parent id from current path
  592. if (!empty($document_data['path'])) {
  593. $testParentId = DocumentManager::get_document_id(
  594. api_get_course_info(),
  595. dirname($document_data['path']),
  596. 0
  597. );
  598. }
  599. $parent_id = 0;
  600. if (!empty($testParentId)) {
  601. $parent_id = $testParentId;
  602. }
  603. }
  604. $current_folder_id = $document_id;
  605. // Show preview
  606. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' &&
  607. isset($_GET['set_preview']) &&
  608. $_GET['set_preview'] == strval(intval($_GET['set_preview']))
  609. ) {
  610. if (isset($_GET['set_preview'])) {
  611. // Generate document HTML
  612. $content_html = DocumentManager::replace_user_info_into_html(
  613. api_get_user_id(),
  614. api_get_course_id(),
  615. api_get_session_id(),
  616. true
  617. );
  618. $filename = 'certificate_preview/'.api_get_unique_id().'.png';
  619. $qr_code_filename = api_get_path(SYS_ARCHIVE_PATH).$filename;
  620. $temp_folder = api_get_path(SYS_ARCHIVE_PATH).'certificate_preview';
  621. if (!is_dir($temp_folder)) {
  622. mkdir($temp_folder, api_get_permissions_for_new_directories());
  623. }
  624. $qr_code_web_filename = api_get_path(WEB_ARCHIVE_PATH).$filename;
  625. $certificate = new Certificate();
  626. $text = $certificate->parseCertificateVariables($content_html['variables']);
  627. $result = $certificate->generateQRImage($text, $qr_code_filename);
  628. $new_content_html = $content_html['content'];
  629. $path_image = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/gallery';
  630. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  631. $path_image_in_default_course = api_get_path(WEB_CODE_PATH).'default_course_document';
  632. $new_content_html = str_replace(
  633. '/main/default_course_document',
  634. $path_image_in_default_course,
  635. $new_content_html
  636. );
  637. $new_content_html = str_replace(
  638. SYS_CODE_PATH.'img/',
  639. api_get_path(WEB_IMG_PATH),
  640. $new_content_html
  641. );
  642. Display::display_reduced_header();
  643. echo '<style>body {background:none;}</style>
  644. <style media="print" type="text/css"> #print_div { visibility:hidden; } </style>';
  645. echo '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div">';
  646. echo Display::return_icon('printmgr.gif', get_lang('Print'));
  647. echo '</a>';
  648. if (is_file($qr_code_filename) && is_readable($qr_code_filename)) {
  649. $new_content_html = str_replace(
  650. '((certificate_barcode))',
  651. Display::img($qr_code_web_filename),
  652. $new_content_html
  653. );
  654. }
  655. print_r($new_content_html);
  656. exit;
  657. }
  658. }
  659. // Is the document tool visible?
  660. // Check whether the tool is actually visible
  661. /*$table_course_tool = Database::get_course_table(TABLE_TOOL_LIST);
  662. $course_id = api_get_course_int_id();
  663. $tool_sql = 'SELECT visibility FROM '.$table_course_tool.'
  664. WHERE c_id = '.$course_id.' AND name = "'.TOOL_DOCUMENT.'"
  665. LIMIT 1';
  666. $tool_result = Database::query($tool_sql);
  667. $tool_row = Database::fetch_array($tool_result);
  668. $tool_visibility = $tool_row['visibility'];*/
  669. $htmlHeadXtra[] = '<script>
  670. function confirmation (name) {
  671. if (confirm(" '.get_lang('AreYouSureToDeleteJS').' "+ name + " ?")) {
  672. return true;
  673. } else {
  674. return false;
  675. }
  676. }
  677. $(document).ready(function() {
  678. $(".convertAction").click(function() {
  679. var id = $(this).attr("data-documentId");
  680. var format = $(this).attr("data-formatType");
  681. convertModal(id, format);
  682. });
  683. });
  684. function convertModal (id, format) {
  685. $("#convertModal").modal("show");
  686. $("." + format + "FormatType").show();
  687. $("#convertSelect").change(function() {
  688. var formatTarget = $(this).val();
  689. window.location.href = "'
  690. .api_get_self().'?'.api_get_cidreq()
  691. .'&curdirpath='.$curdirpath
  692. .'&action=convertToPdf&formatTarget='
  693. .'" + formatTarget + "&id=" + id + "&'
  694. .api_get_cidreq().'&formatType=" + format;
  695. });
  696. $("#convertModal").on("hidden", function(){
  697. $("." + format + "FormatType").hide();
  698. });
  699. }
  700. </script>';
  701. // If they are looking at group documents they can't see the root
  702. if ($groupId != 0 && $curdirpath == '/') {
  703. $curdirpath = $group_properties['directory'];
  704. $curdirpathurl = urlencode($group_properties['directory']);
  705. }
  706. // Check visibility of the current dir path. Don't show anything if not allowed
  707. //@todo check this validation for coaches
  708. //if (!$isAllowedToEdit || api_is_coach()) { before
  709. if (!$isAllowedToEdit && api_is_coach()) {
  710. if ($curdirpath != '/' &&
  711. !(DocumentManager::is_visible($curdirpath, $courseInfo, $sessionId, 'folder'))
  712. ) {
  713. api_not_allowed(true);
  714. }
  715. }
  716. /* Create shared folders */
  717. if ($sessionId == 0) {
  718. //Create shared folder. Necessary for recycled courses.
  719. // session_id should always be zero and should always be created from a
  720. // base course, never from a session.
  721. if (!file_exists($base_work_dir.'/shared_folder')) {
  722. $usf_dir_title = get_lang('UserFolders');
  723. $usf_dir_name = '/shared_folder';
  724. //$groupId = 0;
  725. $visibility = 0;
  726. create_unexisting_directory(
  727. $courseInfo,
  728. api_get_user_id(),
  729. $sessionId,
  730. 0,
  731. $to_user_id,
  732. $base_work_dir,
  733. $usf_dir_name,
  734. $usf_dir_title,
  735. $visibility
  736. );
  737. }
  738. // Create dynamic user shared folder
  739. if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.$userId)) {
  740. $usf_dir_title = $userInfo['complete_name'];
  741. $usf_dir_name = '/shared_folder/sf_user_'.$userId;
  742. //$groupId = 0;
  743. $visibility = 1;
  744. create_unexisting_directory(
  745. $courseInfo,
  746. api_get_user_id(),
  747. $sessionId,
  748. 0,
  749. $to_user_id,
  750. $base_work_dir,
  751. $usf_dir_name,
  752. $usf_dir_title,
  753. $visibility
  754. );
  755. }
  756. } else {
  757. // Create shared folder session.
  758. if (!file_exists($base_work_dir.'/shared_folder_session_'.$sessionId)) {
  759. $usf_dir_title = get_lang('UserFolders').' ('.api_get_session_name($sessionId).')';
  760. $usf_dir_name = '/shared_folder_session_'.$sessionId;
  761. //$groupId = 0;
  762. $visibility = 0;
  763. create_unexisting_directory(
  764. $courseInfo,
  765. api_get_user_id(),
  766. $sessionId,
  767. 0,
  768. $to_user_id,
  769. $base_work_dir,
  770. $usf_dir_name,
  771. $usf_dir_title,
  772. $visibility
  773. );
  774. }
  775. //Create dynamic user shared folder into a shared folder session
  776. if (!file_exists($base_work_dir.'/shared_folder_session_'.$sessionId.'/sf_user_'.$userId)) {
  777. $usf_dir_title = $userInfo['complete_name'].'('.api_get_session_name($sessionId).')';
  778. $usf_dir_name = '/shared_folder_session_'.$sessionId.'/sf_user_'.$userId;
  779. //$groupId = 0;
  780. $visibility = 1;
  781. create_unexisting_directory(
  782. $courseInfo,
  783. $userId,
  784. $sessionId,
  785. 0,
  786. $to_user_id,
  787. $base_work_dir,
  788. $usf_dir_name,
  789. $usf_dir_title,
  790. $visibility
  791. );
  792. }
  793. }
  794. /* MAIN SECTION */
  795. Session::write('image_files_only', '');
  796. $image_files_only = '';
  797. if ($is_certificate_mode) {
  798. $interbreadcrumb[] = array(
  799. 'url' => '../gradebook/index.php?'.api_get_cidreq(),
  800. 'name' => get_lang('Gradebook')
  801. );
  802. } else {
  803. if ((isset($_GET['id']) && $_GET['id'] != 0) || isset($_GET['curdirpath']) || isset($_GET['createdir'])) {
  804. $interbreadcrumb[] = array(
  805. 'url' => 'document.php?'.api_get_cidreq(),
  806. 'name' => get_lang('Documents')
  807. );
  808. } else {
  809. $interbreadcrumb[] = array(
  810. 'url' => '#',
  811. 'name' => get_lang('Documents')
  812. );
  813. }
  814. }
  815. // Interbreadcrumb for the current directory root path
  816. if (empty($document_data['parents'])) {
  817. if (isset($_GET['createdir'])) {
  818. $interbreadcrumb[] = array(
  819. 'url' => $document_data['document_url'],
  820. 'name' => $document_data['title'],
  821. );
  822. } else {
  823. $interbreadcrumb[] = array(
  824. 'url' => '#',
  825. 'name' => $document_data['title']
  826. );
  827. }
  828. } else {
  829. $counter = 0;
  830. foreach ($document_data['parents'] as $document_sub_data) {
  831. //fixing double group folder in breadcrumb
  832. if ($groupId) {
  833. if ($counter == 0) {
  834. $counter++;
  835. continue;
  836. }
  837. }
  838. if (!isset($_GET['createdir']) && $document_sub_data['id'] == $document_data['id']) {
  839. $document_sub_data['document_url'] = '#';
  840. }
  841. $interbreadcrumb[] = array(
  842. 'url' => $document_sub_data['document_url'],
  843. 'name' => $document_sub_data['title'],
  844. );
  845. $counter++;
  846. }
  847. }
  848. if (isset($_GET['createdir'])) {
  849. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('CreateDir'));
  850. }
  851. $htmlHeadXtra[] = api_get_css('js/jquery-jplayer/skin/chamilo/jplayer.blue.monday.css');
  852. $htmlHeadXtra[] = api_get_js('js/jquery-jplayer/jplayer/jquery.jplayer.min.js');
  853. $documentAndFolders = DocumentManager::get_all_document_data(
  854. $courseInfo,
  855. $curdirpath,
  856. $groupIid,
  857. null,
  858. $isAllowedToEdit || $group_member_with_upload_rights,
  859. false
  860. );
  861. $count = 1;
  862. $jquery = null;
  863. if (!empty($documentAndFolders)) {
  864. foreach ($documentAndFolders as $file) {
  865. if ($file['filetype'] == 'file') {
  866. $path_info = pathinfo($file['path']);
  867. $extension = '';
  868. if (!empty($path_info['extension'])) {
  869. $extension = strtolower($path_info['extension']);
  870. }
  871. //@todo use a js loop to auto generate this code
  872. if (in_array($extension, array('ogg', 'mp3', 'wav'))) {
  873. // Get the document data from the ID
  874. $document_data = DocumentManager::get_document_data_by_id(
  875. $file['id'],
  876. api_get_course_id(),
  877. false,
  878. $sessionId
  879. );
  880. if ($sessionId != 0 && !$document_data) {
  881. /* If there is a session defined and asking for the document
  882. * from the session* didn't work, try it from the
  883. course (out of a session context) */
  884. $document_data = DocumentManager::get_document_data_by_id(
  885. $file['id'],
  886. api_get_course_id(),
  887. false,
  888. 0
  889. );
  890. }
  891. if ($extension == 'ogg') {
  892. $extension = 'oga';
  893. }
  894. $params = array(
  895. 'url' => $document_data['direct_url'],
  896. 'extension' => $extension,
  897. 'count' => $count
  898. );
  899. $jquery .= DocumentManager::generate_jplayer_jquery($params);
  900. $count++;
  901. }
  902. }
  903. }
  904. }
  905. $htmlHeadXtra[] = '<script>
  906. $(document).ready( function() {
  907. //Experimental changes to preview mp3, ogg files
  908. '.$jquery.'
  909. });
  910. </script>';
  911. // Lib for event log, stats & tracking & record of the access
  912. Event::event_access_tool(TOOL_DOCUMENT);
  913. /* DISPLAY */
  914. if ($groupId != 0) { // Add group name after for group documents
  915. $add_group_to_title = ' ('.$group_properties['name'].')';
  916. }
  917. $moveForm = '';
  918. /* MOVE FILE OR DIRECTORY */
  919. //Only teacher and all users into their group and each user into his/her shared folder
  920. if ($isAllowedToEdit ||
  921. $group_member_with_upload_rights ||
  922. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) ||
  923. DocumentManager::is_my_shared_folder(api_get_user_id(), $moveTo, $sessionId)
  924. ) {
  925. if (isset($_GET['move']) && $_GET['move'] != '') {
  926. $my_get_move = intval($_REQUEST['move']);
  927. if (api_is_coach()) {
  928. if (!DocumentManager::is_visible_by_id($my_get_move, $courseInfo, $sessionId, api_get_user_id())) {
  929. api_not_allowed(true);
  930. }
  931. }
  932. if (!$isAllowedToEdit) {
  933. if (DocumentManager::check_readonly($courseInfo, api_get_user_id(), $my_get_move)) {
  934. api_not_allowed(true);
  935. }
  936. }
  937. // Get the document data from the ID
  938. $document_to_move = DocumentManager::get_document_data_by_id(
  939. $my_get_move,
  940. api_get_course_id(),
  941. false,
  942. $sessionId
  943. );
  944. $move_path = $document_to_move['path'];
  945. if (!empty($document_to_move)) {
  946. $folders = DocumentManager::get_all_document_folders(
  947. $courseInfo,
  948. $groupIid,
  949. $isAllowedToEdit || $group_member_with_upload_rights,
  950. false,
  951. $curdirpath
  952. );
  953. // filter if is my shared folder. TODO: move this code to build_move_to_selector function
  954. if (DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) &&
  955. !$isAllowedToEdit
  956. ) {
  957. //only main user shared folder
  958. $main_user_shared_folder_main = '/shared_folder/sf_user_'.api_get_user_id();
  959. $main_user_shared_folder_sub = '/shared_folder\/sf_user_'.api_get_user_id().'\//'; //all subfolders
  960. $user_shared_folders = array();
  961. foreach ($folders as $fold) {
  962. if ($main_user_shared_folder_main == $fold ||
  963. preg_match($main_user_shared_folder_sub, $fold)
  964. ) {
  965. $user_shared_folders[] = $fold;
  966. }
  967. }
  968. $moveForm .= '<legend>'.get_lang('Move').'</legend>';
  969. $moveForm .= DocumentManager::build_move_to_selector(
  970. $user_shared_folders,
  971. $move_path,
  972. $my_get_move,
  973. $group_properties['directory']
  974. );
  975. } else {
  976. $moveForm .= '<legend>'.get_lang('Move').'</legend>';
  977. $moveForm .= DocumentManager::build_move_to_selector(
  978. $folders,
  979. $move_path,
  980. $my_get_move,
  981. $group_properties['directory']
  982. );
  983. }
  984. }
  985. }
  986. if (!empty($moveTo) && isset($_POST['move_file'])) {
  987. if (!$isAllowedToEdit) {
  988. if (DocumentManager::check_readonly($courseInfo, api_get_user_id(), $_POST['move_file'])) {
  989. api_not_allowed(true);
  990. }
  991. }
  992. if (api_is_coach()) {
  993. if (!DocumentManager::is_visible_by_id($_POST['move_file'], $courseInfo, $sessionId, api_get_user_id())) {
  994. api_not_allowed(true);
  995. }
  996. }
  997. // Get the document data from the ID
  998. $document_to_move = DocumentManager::get_document_data_by_id(
  999. $_POST['move_file'],
  1000. api_get_course_id(),
  1001. false,
  1002. $sessionId
  1003. );
  1004. // Security fix: make sure they can't move files that are not in the document table
  1005. if (!empty($document_to_move)) {
  1006. $real_path_target = $base_work_dir.$moveTo.'/'.basename($document_to_move['path']);
  1007. $fileExist = false;
  1008. if (file_exists($real_path_target)) {
  1009. $fileExist = true;
  1010. }
  1011. if (move($base_work_dir.$document_to_move['path'], $base_work_dir.$moveTo)) {
  1012. DocumentManager::updateDbInfo(
  1013. 'update',
  1014. $document_to_move['path'],
  1015. $moveTo.'/'.basename($document_to_move['path'])
  1016. );
  1017. //update database item property
  1018. $doc_id = $_POST['move_file'];
  1019. if (is_dir($real_path_target)) {
  1020. api_item_property_update(
  1021. $courseInfo,
  1022. TOOL_DOCUMENT,
  1023. $doc_id,
  1024. 'FolderMoved',
  1025. api_get_user_id(),
  1026. $group_properties,
  1027. null,
  1028. null,
  1029. null,
  1030. $sessionId
  1031. );
  1032. Display::addFlash(Display::return_message(get_lang('DirMv'), 'confirmation'));
  1033. } elseif (is_file($real_path_target)) {
  1034. api_item_property_update(
  1035. $courseInfo,
  1036. TOOL_DOCUMENT,
  1037. $doc_id,
  1038. 'DocumentMoved',
  1039. api_get_user_id(),
  1040. $group_properties,
  1041. null,
  1042. null,
  1043. null,
  1044. $sessionId
  1045. );
  1046. Display::addFlash(
  1047. Display::return_message(
  1048. get_lang('DocMv'),
  1049. 'confirmation'
  1050. )
  1051. );
  1052. }
  1053. // Set the current path
  1054. $curdirpath = $_POST['move_to'];
  1055. $curdirpathurl = urlencode($_POST['move_to']);
  1056. } else {
  1057. if ($fileExist) {
  1058. if (is_dir($real_path_target)) {
  1059. $message = Display::return_message(get_lang('DirExists'), 'error');
  1060. } elseif (is_file($real_path_target)) {
  1061. $message = Display::return_message(get_lang('FileExists'), 'v');
  1062. }
  1063. Display::addFlash($message);
  1064. } else {
  1065. Display::addFlash(Display::return_message(get_lang('Impossible'), 'error'));
  1066. }
  1067. }
  1068. } else {
  1069. Display::addFlash(Display::return_message(get_lang('Impossible'), 'error'));
  1070. }
  1071. }
  1072. }
  1073. /* DELETE FILE OR DIRECTORY */
  1074. //Only teacher and all users into their group
  1075. if ($isAllowedToEdit ||
  1076. $group_member_with_upload_rights ||
  1077. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1078. ) {
  1079. if (isset($_POST['action']) && isset($_POST['ids'])) {
  1080. $files = $_POST['ids'];
  1081. $readonlyAlreadyChecked = false;
  1082. $messages = '';
  1083. $items = array(
  1084. '/audio',
  1085. '/flash',
  1086. '/images',
  1087. '/shared_folder',
  1088. '/video',
  1089. '/chat_files',
  1090. '/certificates'
  1091. );
  1092. foreach ($files as $documentId) {
  1093. $data = DocumentManager::get_document_data_by_id($documentId, $courseInfo['code']);
  1094. if (in_array($data['path'], $items)) {
  1095. // exclude system directories (do not allow deletion)
  1096. continue;
  1097. } else {
  1098. switch ($_POST['action']) {
  1099. case 'set_invisible':
  1100. $visibilityCommand = 'invisible';
  1101. if (api_item_property_update(
  1102. $courseInfo,
  1103. TOOL_DOCUMENT,
  1104. $documentId,
  1105. $visibilityCommand,
  1106. api_get_user_id(),
  1107. null,
  1108. null,
  1109. null,
  1110. null,
  1111. $sessionId
  1112. )) {
  1113. $messages .= Display::return_message(
  1114. get_lang('VisibilityChanged').': '.$data['title'],
  1115. 'confirmation'
  1116. );
  1117. } else {
  1118. $messages .= Display::return_message(get_lang('ViModProb'), 'error');
  1119. }
  1120. break;
  1121. case 'set_visible':
  1122. $visibilityCommand = 'visible';
  1123. if (api_item_property_update(
  1124. $courseInfo,
  1125. TOOL_DOCUMENT,
  1126. $documentId,
  1127. $visibilityCommand,
  1128. api_get_user_id(),
  1129. null,
  1130. null,
  1131. null,
  1132. null,
  1133. $sessionId
  1134. )) {
  1135. $messages .= Display::return_message(
  1136. get_lang('VisibilityChanged').': '.$data['title'],
  1137. 'confirmation'
  1138. );
  1139. } else {
  1140. $messages .= Display::return_message(get_lang('ViModProb'), 'error');
  1141. }
  1142. break;
  1143. case 'delete':
  1144. // Check all documents scheduled for deletion
  1145. // If one of them is read-only, abandon deletion
  1146. // Note: this is only executed once
  1147. if (!$readonlyAlreadyChecked) {
  1148. foreach ($files as $id) {
  1149. if (!$isAllowedToEdit) {
  1150. if (DocumentManager::check_readonly(
  1151. $courseInfo,
  1152. api_get_user_id(),
  1153. null,
  1154. $id,
  1155. false,
  1156. $sessionId
  1157. )) {
  1158. $messages .= Display::return_message(
  1159. get_lang('CantDeleteReadonlyFiles'),
  1160. 'error'
  1161. );
  1162. break 2;
  1163. }
  1164. }
  1165. }
  1166. $readonlyAlreadyChecked = true;
  1167. }
  1168. $deleteDocument = DocumentManager::delete_document(
  1169. $courseInfo,
  1170. null,
  1171. $base_work_dir,
  1172. $sessionId,
  1173. $documentId,
  1174. $groupIid
  1175. );
  1176. if (!empty($deleteDocument)) {
  1177. $messages .= Display::return_message(
  1178. get_lang('DocDeleted').': '.$data['title'],
  1179. 'confirmation'
  1180. );
  1181. }
  1182. break;
  1183. }
  1184. }
  1185. } // endforeach
  1186. Display::addFlash($messages);
  1187. header('Location: '.$currentUrl);
  1188. exit;
  1189. }
  1190. }
  1191. $dirForm = '';
  1192. /* CREATE DIRECTORY */
  1193. //Only teacher and all users into their group and any user into his/her shared folder
  1194. if ($isAllowedToEdit ||
  1195. $group_member_with_upload_rights ||
  1196. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1197. ) {
  1198. // Create directory with $_POST data
  1199. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  1200. // Needed for directory creation
  1201. $post_dir_name = $_POST['dirname'];
  1202. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  1203. $message = Display::return_message(get_lang('CannotCreateDir'), 'error');
  1204. } else {
  1205. // dir_id is the parent folder id.
  1206. if (!empty($_POST['dir_id'])) {
  1207. // Get the document data from the ID
  1208. $document_data = DocumentManager::get_document_data_by_id(
  1209. $_POST['dir_id'],
  1210. api_get_course_id(),
  1211. false,
  1212. $sessionId
  1213. );
  1214. if ($sessionId != 0 && !$document_data) {
  1215. // If there is a session defined and asking for the
  1216. // document * from the session* didn't work, try it from
  1217. // the course (out of a session context)
  1218. $document_data = DocumentManager::get_document_data_by_id(
  1219. $_POST['dir_id'],
  1220. api_get_course_id(),
  1221. false,
  1222. 0
  1223. );
  1224. }
  1225. $curdirpath = $document_data['path'];
  1226. }
  1227. $added_slash = ($curdirpath == '/') ? '' : '/';
  1228. $dir_name = $curdirpath.$added_slash.api_replace_dangerous_char($post_dir_name);
  1229. $dir_name = disable_dangerous_file($dir_name);
  1230. $dir_check = $base_work_dir.$dir_name;
  1231. $visibility = empty($groupId) ? null : 1;
  1232. $newFolderData = create_unexisting_directory(
  1233. $courseInfo,
  1234. api_get_user_id(),
  1235. $sessionId,
  1236. api_get_group_id(),
  1237. $to_user_id,
  1238. $base_work_dir,
  1239. $dir_name,
  1240. $post_dir_name,
  1241. $visibility
  1242. );
  1243. if (!empty($newFolderData)) {
  1244. $message = Display::return_message(
  1245. get_lang('DirCr').' '.$newFolderData['title'],
  1246. 'confirmation'
  1247. );
  1248. } else {
  1249. $message = Display::return_message(
  1250. get_lang('CannotCreateDir'),
  1251. 'error'
  1252. );
  1253. }
  1254. }
  1255. Display::addFlash($message);
  1256. }
  1257. // Show them the form for the directory name
  1258. if (isset($_GET['createdir'])) {
  1259. $dirForm = DocumentManager::create_dir_form($document_id);
  1260. }
  1261. }
  1262. /* VISIBILITY COMMANDS */
  1263. if ($isAllowedToEdit) {
  1264. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) ||
  1265. (isset($_GET['set_visible']) && !empty($_GET['set_visible']))
  1266. ) {
  1267. // Make visible or invisible?
  1268. if (isset($_GET['set_visible'])) {
  1269. $update_id = intval($_GET['set_visible']);
  1270. $visibility_command = 'visible';
  1271. } else {
  1272. $update_id = intval($_GET['set_invisible']);
  1273. $visibility_command = 'invisible';
  1274. }
  1275. if (!$isAllowedToEdit) {
  1276. if (api_is_coach()) {
  1277. if (!DocumentManager::is_visible_by_id($update_id, $courseInfo, $sessionId, api_get_user_id())) {
  1278. api_not_allowed(true);
  1279. }
  1280. }
  1281. if (DocumentManager::check_readonly($courseInfo, api_get_user_id(), '', $update_id)) {
  1282. api_not_allowed(true);
  1283. }
  1284. }
  1285. // Update item_property to change visibility
  1286. if (api_item_property_update(
  1287. $courseInfo,
  1288. TOOL_DOCUMENT,
  1289. $update_id,
  1290. $visibility_command,
  1291. api_get_user_id(),
  1292. null,
  1293. null,
  1294. null,
  1295. null,
  1296. $sessionId
  1297. )
  1298. ) {
  1299. Display::addFlash(
  1300. Display::return_message(get_lang('VisibilityChanged'), 'confirmation')
  1301. );
  1302. } else {
  1303. Display::addFlash(
  1304. Display::return_message(get_lang('ViModProb'), 'error')
  1305. );
  1306. }
  1307. header('Location: '.$currentUrl);
  1308. exit;
  1309. }
  1310. }
  1311. $templateForm = '';
  1312. /* TEMPLATE ACTION */
  1313. //Only teacher and all users into their group
  1314. if ($isAllowedToEdit ||
  1315. $group_member_with_upload_rights ||
  1316. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1317. ) {
  1318. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  1319. $document_id_for_template = intval($_GET['add_as_template']);
  1320. // Create the form that asks for the directory name
  1321. $templateForm .= '
  1322. <form name="set_document_as_new_template" class="form-horizontal" enctype="multipart/form-data"
  1323. action="'.api_get_self().'?add_as_template='.$document_id_for_template.'" method="post">
  1324. <fieldset>
  1325. <legend>'.get_lang('AddAsTemplate').'</legend>
  1326. <div class="form-group">
  1327. <label for="template_title" class="col-sm-2 control-label">'.get_lang('TemplateName').'</label>
  1328. <div class="col-sm-10">
  1329. <input type="text" class="form-control" id="template_title" name="template_title">
  1330. </div>
  1331. </div>
  1332. <div class="form-group">
  1333. <label for="template_image" class="col-sm-2 control-label">'.get_lang('TemplateImage').'</label>
  1334. <div class="col-sm-10">
  1335. <input type="file" name="template_image" id="template_image">
  1336. </div>
  1337. </div>
  1338. <div class="form-group">
  1339. <div class="col-sm-offset-2 col-sm-10">
  1340. <button type="submit" name="create_template" class="btn btn-primary">'
  1341. .get_lang('CreateTemplate').'
  1342. </button>
  1343. </div>
  1344. </div>
  1345. <input type="hidden" name="curdirpath" value="'.$curdirpath.'" />
  1346. </fieldset>
  1347. </form>
  1348. <hr>
  1349. ';
  1350. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  1351. $document_id_for_template = intval($_GET['add_as_template']);
  1352. $title = Security::remove_XSS($_POST['template_title']);
  1353. $user_id = api_get_user_id();
  1354. // Create the template_thumbnails folder in the upload folder (if needed)
  1355. if (!is_dir(api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/upload/template_thumbnails/')) {
  1356. @mkdir(
  1357. api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/upload/template_thumbnails/',
  1358. api_get_permissions_for_new_directories()
  1359. );
  1360. }
  1361. // Upload the file
  1362. if (!empty($_FILES['template_image']['name'])) {
  1363. $upload_ok = process_uploaded_file($_FILES['template_image']);
  1364. if ($upload_ok) {
  1365. // Try to add an extension to the file if it hasn't one
  1366. $new_file_name = $courseInfo['code'].'-'
  1367. .add_ext_on_mime(
  1368. stripslashes($_FILES['template_image']['name']),
  1369. $_FILES['template_image']['type']
  1370. );
  1371. // Upload dir
  1372. $upload_dir = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/upload/template_thumbnails/';
  1373. // Resize image to max default and end upload
  1374. $temp = new Image($_FILES['template_image']['tmp_name']);
  1375. $picture_info = $temp->get_image_info();
  1376. $max_width_for_picture = 100;
  1377. if ($picture_info['width'] > $max_width_for_picture) {
  1378. $temp->resize($max_width_for_picture);
  1379. }
  1380. $temp->send_image($upload_dir.$new_file_name);
  1381. }
  1382. }
  1383. DocumentManager::set_document_as_template(
  1384. $title,
  1385. '',
  1386. $document_id_for_template,
  1387. $course_code,
  1388. $user_id,
  1389. $new_file_name
  1390. );
  1391. Display::addFlash(
  1392. Display::return_message(get_lang('DocumentSetAsTemplate'), 'confirmation')
  1393. );
  1394. }
  1395. if (isset($_GET['remove_as_template'])) {
  1396. $document_id_for_template = intval($_GET['remove_as_template']);
  1397. $user_id = api_get_user_id();
  1398. DocumentManager::unset_document_as_template(
  1399. $document_id_for_template,
  1400. $course_code,
  1401. $user_id
  1402. );
  1403. Display::addFlash(
  1404. Display::return_message(get_lang('DocumentUnsetAsTemplate'), 'confirmation')
  1405. );
  1406. }
  1407. }
  1408. // END ACTION MENU
  1409. // Attach certificate in the gradebook
  1410. if (isset($_GET['curdirpath']) &&
  1411. $_GET['curdirpath'] == '/certificates' &&
  1412. isset($_GET['set_certificate']) &&
  1413. $_GET['set_certificate'] == strval(intval($_GET['set_certificate']))
  1414. ) {
  1415. if (isset($_GET['cidReq'])) {
  1416. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  1417. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  1418. DocumentManager::attach_gradebook_certificate($course_id, $document_id);
  1419. $message = Display::return_message(get_lang('IsDefaultCertificate'), 'normal');
  1420. Display::addFlash(
  1421. $message
  1422. );
  1423. }
  1424. }
  1425. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  1426. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  1427. $documentAndFolders = DocumentManager::get_all_document_data(
  1428. $courseInfo,
  1429. $curdirpath,
  1430. $groupIid,
  1431. null,
  1432. $isAllowedToEdit || $group_member_with_upload_rights,
  1433. true
  1434. );
  1435. } else {
  1436. $documentAndFolders = DocumentManager::get_all_document_data(
  1437. $courseInfo,
  1438. $curdirpath,
  1439. $groupIid,
  1440. null,
  1441. $isAllowedToEdit || $group_member_with_upload_rights,
  1442. false
  1443. );
  1444. }
  1445. if ($groupId != 0) {
  1446. $userAccess = GroupManager::user_has_access(
  1447. api_get_user_id(),
  1448. $groupIid,
  1449. GroupManager::GROUP_TOOL_DOCUMENTS
  1450. );
  1451. if ($userAccess) {
  1452. $folders = DocumentManager::get_all_document_folders(
  1453. $courseInfo,
  1454. $groupIid,
  1455. $isAllowedToEdit || $group_member_with_upload_rights,
  1456. false,
  1457. $curdirpath
  1458. );
  1459. }
  1460. } else {
  1461. $folders = DocumentManager::get_all_document_folders(
  1462. $courseInfo,
  1463. 0,
  1464. $isAllowedToEdit || $group_member_with_upload_rights,
  1465. false,
  1466. $curdirpath
  1467. );
  1468. }
  1469. if (!isset($folders) || $folders === false) {
  1470. $folders = array();
  1471. }
  1472. $btngroup = array('class' => 'btn btn-default');
  1473. /* GO TO PARENT DIRECTORY */
  1474. $actionsLeft = '';
  1475. if ($curdirpath != '/' &&
  1476. $curdirpath != $group_properties['directory'] &&
  1477. !$is_certificate_mode
  1478. ) {
  1479. $actionsLeft = '<a href="'.api_get_self().'?'.api_get_cidreq().'&id='.$parent_id.'">';
  1480. $actionsLeft .= Display::return_icon('folder_up.png', get_lang('Up'), '', ICON_SIZE_MEDIUM);
  1481. $actionsLeft .= '</a>';
  1482. }
  1483. if ($is_certificate_mode && $curdirpath != '/certificates') {
  1484. $actionsLeft .= Display::url(
  1485. Display::return_icon('folder_up.png', get_lang('Up'), '', ICON_SIZE_MEDIUM),
  1486. api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath
  1487. );
  1488. }
  1489. $column_show = array();
  1490. if ($isAllowedToEdit ||
  1491. $group_member_with_upload_rights ||
  1492. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1493. ) {
  1494. // TODO:check enable more options for shared folders
  1495. /* CREATE NEW DOCUMENT OR NEW DIRECTORY / GO TO UPLOAD / DOWNLOAD ZIPPED FOLDER */
  1496. // Create new document
  1497. if (!$is_certificate_mode) {
  1498. $actionsLeft .= Display::url(
  1499. Display::return_icon('new_document.png', get_lang('CreateDoc'), '',
  1500. ICON_SIZE_MEDIUM),
  1501. api_get_path(WEB_CODE_PATH).'document/create_document.php?'
  1502. .api_get_cidreq().'&id='.$document_id
  1503. );
  1504. // Create new draw
  1505. if (api_get_setting('enabled_support_svg') == 'true') {
  1506. if (api_browser_support('svg')) {
  1507. $actionsLeft .= Display::url(
  1508. Display::return_icon('new_draw.png', get_lang('Draw'), '', ICON_SIZE_MEDIUM),
  1509. api_get_path(WEB_CODE_PATH).'document/create_draw.php?'.api_get_cidreq().'&id='.$document_id
  1510. );
  1511. } else {
  1512. $actionsLeft .= Display::return_icon(
  1513. 'new_draw_na.png',
  1514. get_lang('BrowserDontSupportsSVG'),
  1515. '',
  1516. ICON_SIZE_MEDIUM
  1517. );
  1518. }
  1519. }
  1520. // Create new paint
  1521. if (api_get_setting('enabled_support_pixlr') == 'true') {
  1522. $actionsLeft .= Display::url(
  1523. Display::return_icon('new_paint.png',
  1524. get_lang('PhotoRetouching'), '', ICON_SIZE_MEDIUM),
  1525. api_get_path(WEB_CODE_PATH).'document/create_paint.php?'
  1526. .api_get_cidreq().'&id='.$document_id
  1527. );
  1528. }
  1529. // Record an image clip from my webcam
  1530. if (api_get_setting('enable_webcam_clip') == 'true') {
  1531. $actionsLeft .= Display::url(
  1532. Display::return_icon('webcam.png', get_lang('WebCamClip'), '', ICON_SIZE_MEDIUM),
  1533. api_get_path(WEB_CODE_PATH).'document/webcam_clip.php?'.api_get_cidreq().'&id='.$document_id
  1534. );
  1535. }
  1536. // Record audio (nanogong)
  1537. if (api_get_setting('enable_record_audio') === 'true') {
  1538. $actionsLeft .= Display::url(
  1539. Display::return_icon('new_recording.png', get_lang('RecordMyVoice'), '', ICON_SIZE_MEDIUM),
  1540. api_get_path(WEB_CODE_PATH).'document/record_audio.php?'.api_get_cidreq().'&id='.$document_id
  1541. );
  1542. }
  1543. // Create new audio from text
  1544. if (api_get_setting('enabled_text2audio') == 'true') {
  1545. $actionsLeft .= Display::url(
  1546. Display::return_icon('new_sound.png', get_lang('CreateAudio'), '', ICON_SIZE_MEDIUM),
  1547. api_get_path(WEB_CODE_PATH).'document/create_audio.php?'.api_get_cidreq().'&id='.$document_id
  1548. );
  1549. }
  1550. }
  1551. // Create new certificate
  1552. if ($is_certificate_mode) {
  1553. $actionsLeft .= Display::url(
  1554. Display::return_icon('new_certificate.png',
  1555. get_lang('CreateCertificate'),
  1556. '',
  1557. ICON_SIZE_MEDIUM
  1558. ),
  1559. api_get_path(WEB_CODE_PATH).'document/create_document.php?'
  1560. .api_get_cidreq().'&id='.$document_id.'&certificate=true&selectcat='
  1561. .$selectcat
  1562. );
  1563. }
  1564. // File upload link
  1565. if ($is_certificate_mode) {
  1566. $actionsLeft .= Display::url(
  1567. Display::return_icon('upload_certificate.png', get_lang('UploadCertificate'), '', ICON_SIZE_MEDIUM),
  1568. api_get_path(WEB_CODE_PATH).'document/upload.php?'.api_get_cidreq()
  1569. .'&id='.$current_folder_id.'&certificate=true'
  1570. );
  1571. } else {
  1572. $actionsLeft .= Display::url(
  1573. Display::return_icon('upload_file.png', get_lang('UplUploadDocument'), '', ICON_SIZE_MEDIUM),
  1574. api_get_path(WEB_CODE_PATH).'document/upload.php?'.api_get_cidreq().'&id='.$current_folder_id
  1575. );
  1576. }
  1577. /*echo '<a href="#" id="jcapture">';
  1578. echo Display::display_icon('capture.png', get_lang('CatchScreenCasts'), '', ICON_SIZE_MEDIUM).'</a>';*/
  1579. if ($capturePluginInstalled) {
  1580. $actionsLeft .= '<span id="appletplace"></span>';
  1581. $actionsLeft .= Display::url(
  1582. Display::return_icon('capture.png', get_lang('CatchScreenCasts'), '', ICON_SIZE_MEDIUM),
  1583. '#',
  1584. array('id' => 'jcapture')
  1585. );
  1586. }
  1587. // Create directory
  1588. if (!$is_certificate_mode) {
  1589. $actionsLeft .= Display::url(
  1590. Display::return_icon('new_folder.png', get_lang('CreateDir'), '', ICON_SIZE_MEDIUM),
  1591. api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id='.$document_id.'&createdir=1'
  1592. );
  1593. }
  1594. }
  1595. require 'document_slideshow.inc.php';
  1596. if (!isset($_GET['keyword'])) {
  1597. $actionsLeft .= Display::url(
  1598. Display::return_icon('slideshow.png', get_lang('ViewSlideshow'), '', ICON_SIZE_MEDIUM),
  1599. api_get_path(WEB_CODE_PATH).'document/slideshow.php?'.api_get_cidreq().'&curdirpath='.$curdirpathurl
  1600. );
  1601. }
  1602. if ($isAllowedToEdit) {
  1603. $actionsLeft .= Display::url(
  1604. Display::return_icon('percentage.png', get_lang('DocumentQuota'), '', ICON_SIZE_MEDIUM),
  1605. api_get_path(WEB_CODE_PATH).'document/document_quota.php?'.api_get_cidreq()
  1606. );
  1607. }
  1608. if (!$is_certificate_mode) {
  1609. /* BUILD SEARCH FORM */
  1610. $form = new FormValidator(
  1611. 'search_document',
  1612. 'get',
  1613. api_get_self().'?'.api_get_cidreq(),
  1614. '',
  1615. array(),
  1616. FormValidator::LAYOUT_INLINE
  1617. );
  1618. $form->addText('keyword', '', false, array('class' => 'col-md-2'));
  1619. $form->addElement('hidden', 'cidReq', api_get_course_id());
  1620. $form->addElement('hidden', 'id_session', api_get_session_id());
  1621. $form->addElement('hidden', 'gidReq', $groupId);
  1622. $form->addButtonSearch(get_lang('Search'));
  1623. $actionsRight = $form->returnForm();
  1624. }
  1625. $table_footer = '';
  1626. $total_size = 0;
  1627. $sortable_data = array();
  1628. $row = array();
  1629. $userIsSubscribed = CourseManager::is_user_subscribed_in_course(
  1630. api_get_user_id(),
  1631. $courseInfo['code']
  1632. );
  1633. $getSizeURL = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=get_dir_size&'.api_get_cidreq();
  1634. if (isset($documentAndFolders) && is_array($documentAndFolders)) {
  1635. if ($groupId == 0 || $userAccess) {
  1636. $count = 1;
  1637. $countedPaths = array();
  1638. foreach ($documentAndFolders as $key => $document_data) {
  1639. $row = array();
  1640. $row['id'] = $document_data['id'];
  1641. $row['type'] = $document_data['filetype'];
  1642. // If the item is invisible, wrap it in a span with class invisible.
  1643. $is_visible = DocumentManager::is_visible_by_id(
  1644. $document_data['id'],
  1645. $courseInfo,
  1646. $sessionId,
  1647. api_get_user_id(),
  1648. false,
  1649. $userIsSubscribed
  1650. );
  1651. $invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : '';
  1652. $invisibility_span_close = ($is_visible == 0) ? '</span>' : '';
  1653. $size = 1;
  1654. // Get the title or the basename depending on what we're using
  1655. if ($document_data['title'] != '') {
  1656. $document_name = $document_data['title'];
  1657. } else {
  1658. $document_name = basename($document_data['path']);
  1659. }
  1660. $row['name'] = $document_name;
  1661. // Data for checkbox
  1662. if (($isAllowedToEdit || $group_member_with_upload_rights) && count($documentAndFolders) > 1) {
  1663. $row[] = $document_data['id'];
  1664. }
  1665. if (DocumentManager::is_folder_to_avoid($document_data['path'], $is_certificate_mode)) {
  1666. continue;
  1667. }
  1668. // Show the owner of the file only in groups
  1669. $user_link = '';
  1670. if (!empty($groupId)) {
  1671. if (!empty($document_data['insert_user_id'])) {
  1672. $userInfo = api_get_user_info(
  1673. $document_data['insert_user_id'],
  1674. false,
  1675. false,
  1676. false,
  1677. false,
  1678. false
  1679. );
  1680. $user_link = '<div class="document_owner">'
  1681. .get_lang('Owner').': '.UserManager::getUserProfileLink($userInfo)
  1682. .'</div>';
  1683. }
  1684. }
  1685. // Icons (clickable)
  1686. $row[] = DocumentManager::create_document_link(
  1687. $document_data,
  1688. $courseInfo,
  1689. true,
  1690. $count,
  1691. $is_visible,
  1692. $size,
  1693. $isAllowedToEdit
  1694. );
  1695. $path_info = pathinfo($document_data['path']);
  1696. if (isset($path_info['extension']) &&
  1697. in_array($path_info['extension'], array('ogg', 'mp3', 'wav'))
  1698. ) {
  1699. $count++;
  1700. }
  1701. // Validation when belongs to a session
  1702. $session_img = api_get_session_image($document_data['session_id'], $_user['status']);
  1703. $link = DocumentManager::create_document_link(
  1704. $document_data,
  1705. $courseInfo,
  1706. false,
  1707. null,
  1708. $is_visible,
  1709. $size,
  1710. $isAllowedToEdit
  1711. );
  1712. // Document title with link
  1713. $row[] = $link.$session_img.'<br />'.$invisibility_span_open.'<i>'
  1714. .nl2br(htmlspecialchars($document_data['comment'], ENT_QUOTES, $charset))
  1715. .'</i>'.$invisibility_span_close.$user_link;
  1716. if ($document_data['filetype'] == 'folder') {
  1717. $displaySize = '<span id="document_size_'.$document_data['id']
  1718. .'" data-path= "'.$document_data['path']
  1719. .'" class="document_size"></span>';
  1720. } else {
  1721. $displaySize = format_file_size($document_data['size']);
  1722. }
  1723. $row[] = '<span style="display:none;">'.$size.'</span>'.
  1724. $invisibility_span_open.
  1725. $displaySize.
  1726. $invisibility_span_close;
  1727. // Last edit date
  1728. $last_edit_date = api_get_local_time($document_data['lastedit_date']);
  1729. $display_date = date_to_str_ago($document_data['lastedit_date']).
  1730. ' <div class="muted"><small>'.$last_edit_date."</small></div>";
  1731. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  1732. // Admins get an edit column
  1733. if ($isAllowedToEdit ||
  1734. $groupMemberWithEditRights ||
  1735. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) ||
  1736. $document_data['insert_user_id'] == api_get_user_id()
  1737. ) {
  1738. $is_template = isset($document_data['is_template']) ? $document_data['is_template'] : false;
  1739. // If readonly, check if it the owner of the file or if the user is an admin
  1740. if ($document_data['insert_user_id'] == api_get_user_id() || api_is_platform_admin()) {
  1741. $edit_icons = DocumentManager::build_edit_icons(
  1742. $document_data,
  1743. $key,
  1744. $is_template,
  1745. 0,
  1746. $is_visible
  1747. );
  1748. } else {
  1749. $edit_icons = DocumentManager::build_edit_icons(
  1750. $document_data,
  1751. $key,
  1752. $is_template,
  1753. $document_data['readonly'],
  1754. $is_visible
  1755. );
  1756. }
  1757. $row[] = $edit_icons;
  1758. } else {
  1759. $row[] = '';
  1760. }
  1761. $row[] = $last_edit_date;
  1762. $row[] = $size;
  1763. $row[] = $document_name;
  1764. $total_size = $total_size + $size;
  1765. if (!isset($countedPaths[$document_data['path']])) {
  1766. $total_size = $total_size + $size;
  1767. $countedPaths[$document_data['path']] = true;
  1768. }
  1769. if ((isset($_GET['keyword']) && DocumentManager::search_keyword($document_name, $_GET['keyword'])) ||
  1770. !isset($_GET['keyword']) ||
  1771. empty($_GET['keyword'])
  1772. ) {
  1773. $sortable_data[] = $row;
  1774. }
  1775. }
  1776. }
  1777. } else {
  1778. $sortable_data = '';
  1779. $table_footer = get_lang('NoDocsInFolder');
  1780. }
  1781. if (!is_null($documentAndFolders)) {
  1782. // Show download zipped folder icon
  1783. if (!$is_certificate_mode && $total_size != 0
  1784. && (
  1785. api_get_setting('students_download_folders') == 'true' ||
  1786. $isAllowedToEdit ||
  1787. api_is_platform_admin()
  1788. )
  1789. ) {
  1790. //for student does not show icon into other shared folder, and does not show into main path (root)
  1791. if (DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) &&
  1792. $curdirpath != '/' ||
  1793. $isAllowedToEdit ||
  1794. api_is_platform_admin()
  1795. ) {
  1796. $actionsLeft .= Display::url(
  1797. Display::return_icon(
  1798. 'save_pack.png',
  1799. get_lang('Save').' (ZIP)',
  1800. '',
  1801. ICON_SIZE_MEDIUM
  1802. ),
  1803. api_get_path(WEB_CODE_PATH).'document/document.php?'
  1804. .api_get_cidreq().'&action=downloadfolder&id='.$document_id
  1805. );
  1806. }
  1807. }
  1808. }
  1809. if (api_is_platform_admin()) {
  1810. if (api_get_configuration_value('document_manage_deleted_files')) {
  1811. $actionsLeft .= Display::url(
  1812. get_lang('Recycle'),
  1813. api_get_path(WEB_CODE_PATH).'document/recycle.php?'.api_get_cidreq(),
  1814. array('class' => 'btn btn-default')
  1815. );
  1816. }
  1817. }
  1818. if (!empty($moveTo)) {
  1819. $document_id = DocumentManager::get_document_id($courseInfo, $moveTo);
  1820. }
  1821. if (isset($_GET['createdir']) && isset($_POST['dirname']) && $_POST['dirname'] != '') {
  1822. $post_dir_name = $_POST['dirname'];
  1823. $document_id = DocumentManager::get_document_id($courseInfo, $_POST['dirname']);
  1824. }
  1825. $selector = '';
  1826. if (!$is_certificate_mode && !isset($_GET['move'])) {
  1827. $selector = DocumentManager::build_directory_selector(
  1828. $folders,
  1829. $document_id,
  1830. (isset($group_properties['directory']) ? $group_properties['directory'] : array())
  1831. );
  1832. }
  1833. if (($isAllowedToEdit || $group_member_with_upload_rights) && count($documentAndFolders) > 1) {
  1834. $column_show[] = 1;
  1835. }
  1836. $column_show[] = 1;
  1837. $column_show[] = 1;
  1838. $column_show[] = 1;
  1839. $column_show[] = 1;
  1840. if ($isAllowedToEdit ||
  1841. $group_member_with_upload_rights ||
  1842. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1843. ) {
  1844. $column_show[] = 1;
  1845. }
  1846. $column_show[] = 0;
  1847. $column_show[] = 0;
  1848. $column_order = array();
  1849. if (count($row) == 12) {
  1850. //teacher
  1851. $column_order[2] = 8; //name
  1852. $column_order[3] = 7;
  1853. $column_order[4] = 6;
  1854. } elseif (count($row) == 10) {
  1855. //student
  1856. $column_order[1] = 6;
  1857. $column_order[2] = 5;
  1858. $column_order[3] = 4;
  1859. }
  1860. $default_column = $isAllowedToEdit ? 2 : 1;
  1861. $tableName = $isAllowedToEdit ? 'teacher_table' : 'student_table';
  1862. $table = new SortableTableFromArrayConfig(
  1863. $sortable_data,
  1864. $default_column,
  1865. 20,
  1866. $tableName,
  1867. $column_show,
  1868. $column_order,
  1869. 'ASC',
  1870. true
  1871. );
  1872. $query_vars = array();
  1873. if (isset($_GET['keyword'])) {
  1874. $query_vars['keyword'] = Security::remove_XSS($_GET['keyword']);
  1875. } else {
  1876. $query_vars['curdirpath'] = $curdirpath;
  1877. }
  1878. if ($groupId) {
  1879. $query_vars['gidReq'] = $groupId;
  1880. }
  1881. $query_vars['cidReq'] = api_get_course_id();
  1882. $table->set_additional_parameters($query_vars);
  1883. $column = 0;
  1884. if (($isAllowedToEdit || $group_member_with_upload_rights) &&
  1885. count($documentAndFolders) > 1
  1886. ) {
  1887. $table->set_header($column++, '', false, array('style' => 'width:12px;'));
  1888. }
  1889. $table->set_header($column++, get_lang('Type'), true, array('style' => 'width:30px;'));
  1890. $table->set_header($column++, get_lang('Name'));
  1891. $table->set_header($column++, get_lang('Size'), true, array('style' => 'width:50px;'));
  1892. $table->set_header($column++, get_lang('Date'), true, array('style' => 'width:150px;'));
  1893. // Admins get an edit column
  1894. if ($isAllowedToEdit ||
  1895. $group_member_with_upload_rights ||
  1896. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId)
  1897. ) {
  1898. $table->set_header($column++, get_lang('Actions'), false, array('class' => 'td_actions'));
  1899. }
  1900. // Actions on multiple selected documents
  1901. // TODO: Currently only delete action -> take only DELETE permission into account
  1902. if (count($documentAndFolders) > 1) {
  1903. if ($isAllowedToEdit || $groupMemberWithEditRights) {
  1904. $form_actions = array();
  1905. $form_action['set_invisible'] = get_lang('SetInvisible');
  1906. $form_action['set_visible'] = get_lang('SetVisible');
  1907. $form_action['delete'] = get_lang('Delete');
  1908. $table->set_form_actions($form_action, 'ids');
  1909. }
  1910. }
  1911. Display::display_header('', 'Doc');
  1912. /* Introduction section (editable by course admins) */
  1913. if (!empty($groupId)) {
  1914. Display::display_introduction_section(TOOL_DOCUMENT.$groupId);
  1915. } else {
  1916. Display::display_introduction_section(TOOL_DOCUMENT);
  1917. }
  1918. $toolbar = Display::toolbarAction(
  1919. 'toolbar-document',
  1920. array($actionsLeft, $actionsRight)
  1921. );
  1922. echo $toolbar;
  1923. echo $templateForm;
  1924. echo $moveForm;
  1925. echo $dirForm;
  1926. echo $selector;
  1927. $table->display();
  1928. $ajaxURL = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=get_document_quota&'.api_get_cidreq();
  1929. if (count($documentAndFolders) > 1) {
  1930. if ($isAllowedToEdit || $group_member_with_upload_rights) {
  1931. echo '<script>
  1932. $(document).ready(function() {
  1933. $.ajax({
  1934. url:"'.$ajaxURL.'",
  1935. success:function(data){
  1936. $("#course_quota").html(data);
  1937. }
  1938. });
  1939. $(".document_size").each(function(i, obj) {
  1940. var path = obj.getAttribute("data-path");
  1941. $.ajax({
  1942. url:"'.$getSizeURL.'&path="+path,
  1943. success:function(data){
  1944. $(obj).html(data);
  1945. }
  1946. });
  1947. });
  1948. });
  1949. </script>';
  1950. echo '<span id="course_quota"></span>';
  1951. }
  1952. }
  1953. if (!empty($table_footer)) {
  1954. echo Display::return_message($table_footer, 'warning');
  1955. }
  1956. echo '
  1957. <div id="convertModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
  1958. <div class="modal-dialog">
  1959. <div class="modal-content">
  1960. <div class="modal-header" style="text-align: center;">
  1961. <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
  1962. <span aria-hidden="true">&times;</span>
  1963. </button>
  1964. <h4 class="modal-title">'.get_lang('Convert').'</h4>
  1965. </div>
  1966. <div class="modal-body">
  1967. <form action="#" class="form-horizontal">
  1968. <div class="form-group">
  1969. <label class="col-sm-4 control-label" for="convertSelect">'.get_lang('ConvertFormats').'</label>
  1970. <div class="col-sm-8">
  1971. <select id="convertSelect">
  1972. <option value="">'.get_lang('Select').'</option>
  1973. <option value="pdf">
  1974. PDF - Portable Document File
  1975. </option>
  1976. <option value="odt" style="display:none;" class="textFormatType">
  1977. ODT - Open Document Text
  1978. </option>
  1979. <option value="odp" style="display:none;" class="presentationFormatType">
  1980. ODP - Open Document Portable
  1981. </option>
  1982. <option value="ods" style="display:none;" class="spreadsheetFormatType">
  1983. ODS - Open Document Spreadsheet
  1984. </option>
  1985. </select>
  1986. </div>
  1987. </div>
  1988. </form>
  1989. </div>
  1990. <div class="modal-footer">
  1991. <button type="button" class="btn btn-default" data-dismiss="modal">'.get_lang('Close').'</button>
  1992. </div>
  1993. </div>
  1994. </div>
  1995. ';
  1996. // Footer
  1997. Display::display_footer();