document.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Homepage script for the documents tool.
  6. *
  7. * This script allows the user to manage files and directories on a remote http
  8. * server.
  9. * The user can : - navigate through files and directories.
  10. * - upload a file
  11. * - delete, copy a file or a directory
  12. * - edit properties & content (name, comments, html content)
  13. * The script is organised in four sections.
  14. *
  15. * 1) Execute the command called by the user
  16. * Note: somme commands of this section are organised in two steps.
  17. * The script always begins with the second step,
  18. * so it allows to return more easily to the first step.
  19. *
  20. * Note (March 2004) some editing functions (renaming, commenting)
  21. * are moved to a separate page, edit_document.php. This is also
  22. * where xml and other stuff should be added.
  23. * 2) Define the directory to display
  24. * 3) Read files and directories from the directory defined in part 2
  25. * 4) Display all of that on an HTML page
  26. *
  27. * @package chamilo.document
  28. */
  29. require_once __DIR__.'/../inc/global.inc.php';
  30. $allowDownloadDocumentsByApiKey = api_get_setting('allow_download_documents_by_api_key') === 'true';
  31. $current_course_tool = TOOL_DOCUMENT;
  32. $this_section = SECTION_COURSES;
  33. $to_user_id = null;
  34. $parent_id = null;
  35. $lib_path = api_get_path(LIBRARY_PATH);
  36. $actionsRight = '';
  37. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  38. $allowUseTool = false;
  39. if ($allowDownloadDocumentsByApiKey) {
  40. try {
  41. if ($action != 'download') {
  42. throw new Exception(get_lang('SelectAnAction'));
  43. }
  44. $username = isset($_GET['username']) ? Security::remove_XSS($_GET['username']) : null;
  45. $apiKey = isset($_GET['api_key']) ? Security::remove_XSS($_GET['api_key']) : null;
  46. $restApi = Rest::validate($username, $apiKey);
  47. $allowUseTool = $restApi ? true : false;
  48. } catch (Exception $e) {
  49. $allowUseTool = false;
  50. }
  51. }
  52. if (!$allowUseTool) {
  53. api_protect_course_script(true);
  54. api_protect_course_group(GroupManager::GROUP_TOOL_DOCUMENTS);
  55. }
  56. DocumentManager::removeGeneratedAudioTempFile();
  57. $tempRealPath = Session::read('temp_realpath_image');
  58. if (!empty($tempRealPath) &&
  59. file_exists($tempRealPath)
  60. ) {
  61. unlink($tempRealPath);
  62. }
  63. $_user = api_get_user_info();
  64. $courseInfo = api_get_course_info();
  65. $courseId = $courseInfo['real_id'];
  66. $course_dir = $courseInfo['directory'].'/document';
  67. $usePpt2lp = api_get_setting('service_ppt2lp', 'active') == 'true';
  68. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  69. $base_work_dir = $sys_course_path.$course_dir;
  70. $http_www = api_get_path(WEB_COURSE_PATH).$courseInfo['directory'].'/document';
  71. $document_path = $base_work_dir;
  72. $currentUrl = api_get_self().'?'.api_get_cidreq();
  73. // I'm in the certification module?
  74. $is_certificate_mode = false;
  75. if (isset($_GET['curdirpath'])) {
  76. $is_certificate_mode = DocumentManager::is_certificate_mode($_GET['curdirpath']);
  77. }
  78. if (isset($_REQUEST['certificate']) && $_REQUEST['certificate'] == 'true') {
  79. $is_certificate_mode = true;
  80. }
  81. // Removing sessions
  82. Session::erase('draw_dir');
  83. Session::erase('paint_dir');
  84. Session::erase('temp_audio_nanogong');
  85. $plugin = new AppPlugin();
  86. $pluginList = $plugin->getInstalledPlugins();
  87. $capturePluginInstalled = in_array('jcapture', $pluginList);
  88. if ($capturePluginInstalled) {
  89. $jcapturePath = api_get_path(WEB_PLUGIN_PATH).'jcapture/plugin_applet.php';
  90. $htmlHeadXtra[]
  91. = '<script>
  92. $(function() {
  93. $("#jcapture").click(function(){
  94. $("#appletplace").load("'.$jcapturePath.'");
  95. });
  96. });
  97. </script>
  98. ';
  99. }
  100. if (empty($courseInfo)) {
  101. api_not_allowed(true);
  102. }
  103. // Create directory certificates.
  104. DocumentManager::create_directory_certificate_in_course($courseInfo);
  105. // Used for avoiding double-click.
  106. $dbl_click_id = 0;
  107. $selectcat = isset($_GET['selectcat']) ? Security::remove_XSS($_GET['selectcat']) : null;
  108. $moveTo = isset($_POST['move_to']) ? Security::remove_XSS($_POST['move_to']) : null;
  109. $moveFile = isset($_POST['move_file']) ? (int) $_POST['move_file'] : 0;
  110. $certificateLink = '';
  111. if ($is_certificate_mode) {
  112. $certificateLink = '&certificate=true&selectcat='.$selectcat;
  113. }
  114. /* Constants and variables */
  115. $userId = api_get_user_id();
  116. $userInfo = api_get_user_info();
  117. $sessionId = api_get_session_id();
  118. $course_code = api_get_course_id();
  119. $groupId = api_get_group_id();
  120. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  121. $groupMemberWithUploadRights = false;
  122. // If the group id is set, we show them group documents
  123. $group_properties = [];
  124. $group_properties['directory'] = null;
  125. // For sessions we should check the parameters of visibility
  126. if (api_get_session_id() != 0) {
  127. $groupMemberWithUploadRights = $groupMemberWithUploadRights && api_is_allowed_to_session_edit(false, true);
  128. }
  129. // Get group info
  130. $groupIid = 0;
  131. $groupMemberWithEditRights = false;
  132. // Setting group variables.
  133. if (!empty($groupId)) {
  134. $group_properties = GroupManager::get_group_properties($groupId);
  135. $groupIid = isset($group_properties['iid']) ? $group_properties['iid'] : 0;
  136. $groupMemberWithEditRights = GroupManager::allowUploadEditDocument(
  137. $userId,
  138. $courseId,
  139. $group_properties,
  140. null
  141. );
  142. // Let's assume the user cannot upload files for the group
  143. $groupMemberWithUploadRights = false;
  144. if ($group_properties['doc_state'] == 2) {
  145. // Documents are private
  146. if ($isAllowedToEdit || GroupManager::is_user_in_group($userId, $group_properties)) {
  147. // Only courseadmin or group members (members + tutors) allowed
  148. $interbreadcrumb[] = [
  149. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  150. 'name' => get_lang('Groups'),
  151. ];
  152. $interbreadcrumb[] = [
  153. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  154. 'name' => get_lang('GroupSpace').' '.$group_properties['name'],
  155. ];
  156. //they are allowed to upload
  157. $groupMemberWithUploadRights = true;
  158. } else {
  159. $groupId = 0;
  160. }
  161. } elseif ($group_properties['doc_state'] == 1) {
  162. // Documents are public
  163. $interbreadcrumb[] = [
  164. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  165. 'name' => get_lang('Groups'),
  166. ];
  167. $interbreadcrumb[] = [
  168. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  169. 'name' => get_lang('GroupSpace').' '.$group_properties['name'],
  170. ];
  171. // Allowed to upload?
  172. if ($isAllowedToEdit ||
  173. GroupManager::is_subscribed($userId, $group_properties) ||
  174. GroupManager::is_tutor_of_group($userId, $group_properties, $courseId)
  175. ) {
  176. // Only course admin or group members can upload
  177. $groupMemberWithUploadRights = true;
  178. }
  179. }
  180. // Group mode
  181. if (!GroupManager::allowUploadEditDocument($userId, $courseId, $group_properties)) {
  182. $groupMemberWithUploadRights = false;
  183. }
  184. Session::write('group_member_with_upload_rights', $groupMemberWithUploadRights);
  185. } else {
  186. Session::write('group_member_with_upload_rights', false);
  187. }
  188. // Actions.
  189. $document_id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
  190. $currentUrl = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
  191. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  192. switch ($action) {
  193. case 'delete_item':
  194. if ($isAllowedToEdit ||
  195. $groupMemberWithUploadRights ||
  196. DocumentManager::isBasicCourseFolder($curdirpath, $sessionId) ||
  197. DocumentManager::is_my_shared_folder(api_get_user_id(), $curdirpath, $sessionId) ||
  198. DocumentManager::is_my_shared_folder(api_get_user_id(), $moveTo, $sessionId)
  199. ) {
  200. if (isset($_GET['deleteid'])) {
  201. if (!$isAllowedToEdit) {
  202. if (api_is_coach()) {
  203. if (!DocumentManager::is_visible_by_id(
  204. $_GET['deleteid'],
  205. $courseInfo,
  206. $sessionId,
  207. api_get_user_id()
  208. )
  209. ) {
  210. api_not_allowed();
  211. }
  212. }
  213. if (DocumentManager::check_readonly($courseInfo, api_get_user_id(), '', $_GET['deleteid'], true)) {
  214. api_not_allowed();
  215. }
  216. }
  217. $documentInfo = DocumentManager::get_document_data_by_id(
  218. $_GET['deleteid'],
  219. $courseInfo['code'],
  220. false,
  221. $sessionId
  222. );
  223. GroupManager::allowUploadEditDocument(
  224. $userId,
  225. $courseId,
  226. $group_properties,
  227. $documentInfo,
  228. true
  229. );
  230. // Check whether the document is in the database.
  231. if (!empty($documentInfo)) {
  232. if ($documentInfo['filetype'] != 'link') {
  233. $deleteDocument = DocumentManager::delete_document(
  234. $courseInfo,
  235. null,
  236. $base_work_dir,
  237. $sessionId,
  238. $_GET['deleteid'],
  239. $groupIid
  240. );
  241. if ($deleteDocument) {
  242. $certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
  243. DocumentManager::remove_attach_certificate(
  244. api_get_course_id(),
  245. $certificateId
  246. );
  247. Display::addFlash(
  248. Display::return_message(
  249. get_lang('DocDeleted').': '.$documentInfo['title'],
  250. 'success'
  251. )
  252. );
  253. } else {
  254. Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
  255. }
  256. } else {
  257. // Cloud Links
  258. $deleteDocument = DocumentManager::deleteCloudLink($courseInfo, $_GET['deleteid']);
  259. if ($deleteDocument) {
  260. Display::addFlash(Display::return_message(
  261. get_lang('CloudLinkDeleted').': '.$documentInfo['title'],
  262. 'success'
  263. ));
  264. } else {
  265. Display::addFlash(Display::return_message(
  266. get_lang('CloudLinkDeleteError').': '.$documentInfo['title'],
  267. 'error'
  268. ));
  269. }
  270. }
  271. } else {
  272. Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
  273. }
  274. header("Location: $currentUrl");
  275. exit;
  276. }
  277. }
  278. break;
  279. case 'download':
  280. // Get the document data from the ID
  281. $document_data = DocumentManager::get_document_data_by_id(
  282. $document_id,
  283. api_get_course_id(),
  284. false,
  285. $sessionId
  286. );
  287. if ($sessionId != 0 && !$document_data) {
  288. // If there is a session defined and asking for the document *from
  289. // the session* didn't work, try it from the course (out of a
  290. // session context)
  291. $document_data = DocumentManager::get_document_data_by_id(
  292. $document_id,
  293. api_get_course_id(),
  294. false,
  295. 0
  296. );
  297. }
  298. // Check whether the document is in the database
  299. if (empty($document_data)) {
  300. api_not_allowed();
  301. }
  302. // Launch event
  303. Event::event_download($document_data['url']);
  304. // Check visibility of document and paths
  305. if (!($isAllowedToEdit || $groupMemberWithUploadRights) &&
  306. !DocumentManager::is_visible_by_id($document_id, $courseInfo, $sessionId, api_get_user_id())
  307. ) {
  308. api_not_allowed(true);
  309. }
  310. $full_file_name = $base_work_dir.$document_data['path'];
  311. if (Security::check_abs_path($full_file_name, $base_work_dir.'/')) {
  312. $result = DocumentManager::file_send_for_download($full_file_name, true);
  313. if ($result === false) {
  314. api_not_allowed(true);
  315. }
  316. }
  317. exit;
  318. break;
  319. case 'downloadfolder':
  320. if (api_get_setting('students_download_folders') == 'true' ||
  321. $isAllowedToEdit ||
  322. api_is_platform_admin()
  323. ) {
  324. // Get the document data from the ID
  325. $document_data = DocumentManager::get_document_data_by_id(
  326. $document_id,
  327. api_get_course_id(),
  328. false,
  329. $sessionId
  330. );
  331. if ($sessionId != 0 && !$document_data) {
  332. // If there is a session defined and asking for the
  333. // document * from the session* didn't work, try it from the
  334. // course (out of a session context)
  335. $document_data = DocumentManager::get_document_data_by_id(
  336. $document_id,
  337. api_get_course_id(),
  338. false,
  339. 0
  340. );
  341. }
  342. //filter when I am into shared folder, I can download only my shared folder
  343. if (DocumentManager::is_any_user_shared_folder($document_data['path'], $sessionId)) {
  344. if (DocumentManager::is_my_shared_folder(api_get_user_id(), $document_data['path'], $sessionId) ||
  345. $isAllowedToEdit || api_is_platform_admin()
  346. ) {
  347. require 'downloadfolder.inc.php';
  348. }
  349. } else {
  350. require 'downloadfolder.inc.php';
  351. }
  352. // Launch event
  353. Event::event_download($document_data['url']);
  354. exit;
  355. }
  356. break;
  357. case 'export_to_pdf':
  358. if (api_get_setting('students_export2pdf') == 'true' ||
  359. $isAllowedToEdit || api_is_platform_admin()
  360. ) {
  361. $documentOrientation = api_get_configuration_value('document_pdf_orientation');
  362. $orientation = in_array($documentOrientation, ['landscape', 'portrait'])
  363. ? $documentOrientation
  364. : 'landscape';
  365. $showHeaderAndFooter = true;
  366. if ($is_certificate_mode) {
  367. $certificateOrientation = api_get_configuration_value('certificate_pdf_orientation');
  368. $orientation = in_array($certificateOrientation, ['landscape', 'portrait'])
  369. ? $certificateOrientation
  370. : 'landscape';
  371. $showHeaderAndFooter = !api_get_configuration_value('hide_header_footer_in_certificate');
  372. }
  373. DocumentManager::export_to_pdf($document_id, $course_code, $orientation, $showHeaderAndFooter);
  374. }
  375. break;
  376. case 'copytomyfiles':
  377. // Copy a file to general my files user's
  378. if (api_get_setting('allow_my_files') == 'true' &&
  379. api_get_setting('users_copy_files') == 'true' &&
  380. api_get_user_id() != 0 &&
  381. !api_is_anonymous()
  382. ) {
  383. // Get the document data from the ID
  384. $document_info = DocumentManager::get_document_data_by_id(
  385. $document_id,
  386. api_get_course_id(),
  387. true,
  388. $sessionId
  389. );
  390. if ($sessionId != 0 && !$document_info) {
  391. /* If there is a session defined and asking for the document
  392. from the session didn't work, try it from the course
  393. (out of a session context)*/
  394. $document_info = DocumentManager::get_document_data_by_id(
  395. $document_id,
  396. api_get_course_id(),
  397. 0
  398. );
  399. }
  400. 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. if (count($documentAndFolders) > 1) {
  1988. $ajaxURL = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=get_document_quota&'.api_get_cidreq();
  1989. if ($isAllowedToEdit) {
  1990. echo '<script>
  1991. $(function() {
  1992. $.ajax({
  1993. url:"'.$ajaxURL.'",
  1994. success:function(data){
  1995. $("#course_quota").html(data);
  1996. }
  1997. });
  1998. });
  1999. </script>';
  2000. }
  2001. echo '<script>
  2002. $(function() {
  2003. $(".document_size").each(function(i, obj) {
  2004. var path = obj.getAttribute("data-path");
  2005. $.ajax({
  2006. url:"'.$getSizeURL.'&path="+path,
  2007. success:function(data){
  2008. $(obj).html(data);
  2009. }
  2010. });
  2011. });
  2012. });
  2013. </script>';
  2014. echo '<span id="course_quota"></span>';
  2015. }
  2016. if (!empty($table_footer)) {
  2017. echo Display::return_message($table_footer, 'warning');
  2018. }
  2019. echo '
  2020. <div id="convertModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
  2021. <div class="modal-dialog">
  2022. <div class="modal-content">
  2023. <div class="modal-header" style="text-align: center;">
  2024. <button type="button" class="close" data-dismiss="modal" aria-label="'.get_lang('Close').'">
  2025. <span aria-hidden="true">&times;</span>
  2026. </button>
  2027. <h4 class="modal-title">'.get_lang('Convert').'</h4>
  2028. </div>
  2029. <div class="modal-body">
  2030. <form action="#" class="form-horizontal">
  2031. <div class="form-group">
  2032. <label class="col-sm-4 control-label" for="convertSelect">'.get_lang('ConvertFormats').'</label>
  2033. <div class="col-sm-8">
  2034. <select id="convertSelect">
  2035. <option value="">'.get_lang('Select').'</option>
  2036. <option value="pdf">
  2037. PDF - Portable Document File
  2038. </option>
  2039. <option value="odt" style="display:none;" class="textFormatType">
  2040. ODT - Open Document Text
  2041. </option>
  2042. <option value="odp" style="display:none;" class="presentationFormatType">
  2043. ODP - Open Document Portable
  2044. </option>
  2045. <option value="ods" style="display:none;" class="spreadsheetFormatType">
  2046. ODS - Open Document Spreadsheet
  2047. </option>
  2048. </select>
  2049. </div>
  2050. </div>
  2051. </form>
  2052. </div>
  2053. <div class="modal-footer">
  2054. <button type="button" class="btn btn-default" data-dismiss="modal">'.get_lang('Close').'</button>
  2055. </div>
  2056. </div>
  2057. </div>
  2058. ';
  2059. Session::erase('slideshow_'.api_get_course_id().api_get_session_id());
  2060. Display::display_footer();