document.php 82 KB

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