document.php 81 KB

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