document.php 82 KB

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