course_document.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. $language_file = array('create_course', 'document');
  3. include_once('global.inc.php');
  4. /*
  5. ==============================================================================
  6. INIT SECTION
  7. ==============================================================================
  8. */
  9. // name of the language file that needs to be included
  10. require_once api_get_path(INCLUDE_PATH).'lib/fckeditor/repository.php';
  11. include(api_get_path(SYS_CODE_PATH).'document/document.inc.php');
  12. //if(!$is_in_admin){
  13. if(!api_is_platform_admin()){
  14. api_protect_course_script();
  15. }
  16. //session
  17. if(isset($_GET['id_session']))
  18. $_SESSION['id_session'] = $_GET['id_session'];
  19. $htmlHeadXtra[] =
  20. "<script type=\"text/javascript\">
  21. function confirmation (name)
  22. {
  23. if (confirm(\" ". api_convert_encoding(get_lang('AreYouSureToDelete'), 'UTF-8', $charset) ." \"+ name + \" ?\"))
  24. {return true;}
  25. else
  26. {return false;}
  27. }
  28. </script>";
  29. /*
  30. -----------------------------------------------------------
  31. Variables
  32. - some need defining before inclusion of libraries
  33. -----------------------------------------------------------
  34. */
  35. $sType = isset($sType) ? $sType : '';
  36. if($sType=="MP3") $sType="audio";
  37. // Resource type
  38. $sType = strtolower($sType);
  39. // Choosing the repository to be used.
  40. if (api_is_in_course())
  41. {
  42. if (!api_is_in_group())
  43. {
  44. // 1. We are inside a course and not in a group.
  45. if (api_is_allowed_to_edit())
  46. {
  47. // 1.1. Teacher
  48. $base_work_dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document/';
  49. $http_www = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/';
  50. }
  51. else
  52. {
  53. // 1.2. Student
  54. $base_work_dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document/shared_folder/'.api_get_user_id().'/';
  55. $http_www = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/shared_folder/'.api_get_user_id().'/';
  56. }
  57. }
  58. else
  59. {
  60. // 2. Inside a course and inside a group.
  61. $base_work_dir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/';
  62. $http_www = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/';
  63. }
  64. }
  65. else
  66. {
  67. if (api_is_platform_admin() && $_SESSION['this_section'] == 'platform_admin')
  68. {
  69. // 3. Platform administration activities.
  70. $base_work_dir = $_configuration['root_sys'].'home/default_platform_document/';
  71. $http_www = $_configuration['root_web'].'home/default_platform_document/';
  72. }
  73. else
  74. {
  75. // 4. The user is outside courses.
  76. $base_work_dir = $_configuration['root_sys'].'main/upload/users/'.api_get_user_id().'/my_files/';
  77. $http_www = $_configuration['root_web'].'main/upload/users/'.api_get_user_id().'/my_files/';
  78. }
  79. }
  80. // Set the upload path according to the resource type.
  81. if ($sType == 'audio')
  82. {
  83. check_and_create_resource_directory($base_work_dir, '/audio', get_lang('Audio'));
  84. $base_work_dir = $base_work_dir.'audio/';
  85. $http_www = $http_www.'audio/';
  86. $path = "/audio/";
  87. }
  88. elseif ($sType == 'flash')
  89. {
  90. check_and_create_resource_directory($base_work_dir, '/flash', get_lang('Flash'));
  91. $base_work_dir = $base_work_dir.'flash/';
  92. $http_www = $http_www.'flash/';
  93. $path = "/flash/";
  94. }
  95. elseif ($sType == 'images')
  96. {
  97. check_and_create_resource_directory($base_work_dir, '/images', get_lang('Images'));
  98. $base_work_dir = $base_work_dir.'images/';
  99. $http_www = $http_www.'images/';
  100. $path = "/images/";
  101. }
  102. elseif ($sType == 'video')
  103. {
  104. check_and_create_resource_directory($base_work_dir, '/video', get_lang('Video'));
  105. $base_work_dir = $base_work_dir.'video/';
  106. $http_www = $http_www.'video/';
  107. $path = "/video/";
  108. }
  109. elseif ($sType == 'video/flv')
  110. {
  111. check_and_create_resource_directory($base_work_dir, '/video', get_lang('Video'));
  112. check_and_create_resource_directory($base_work_dir, '/video/flv', 'flv');
  113. $base_work_dir = $base_work_dir.'video/flv/';
  114. $http_www = $http_www.'video/flv/';
  115. $path = "/video/flv/";
  116. }
  117. $course_dir = $_course['path']."/document/".$sType;
  118. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  119. $dbl_click_id = 0; // used to avoid double-click
  120. $is_allowed_to_edit = api_is_allowed_to_edit();
  121. $req_gid = '';
  122. /*
  123. -----------------------------------------------------------
  124. Libraries
  125. -----------------------------------------------------------
  126. */
  127. //libraries are included by default
  128. include_once(api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php');
  129. include_once(api_get_path(LIBRARY_PATH) . 'document.lib.php');
  130. include_once(api_get_path(LIBRARY_PATH) . 'tablesort.lib.php');
  131. /*
  132. -----------------------------------------------------------
  133. Constants and variables
  134. -----------------------------------------------------------
  135. */
  136. $course_quota = DocumentManager::get_course_quota();
  137. /*
  138. ==============================================================================
  139. MAIN SECTION
  140. ==============================================================================
  141. */
  142. /*
  143. -----------------------------------------------------------
  144. Header
  145. -----------------------------------------------------------
  146. */
  147. $tool_name = get_lang("Doc"); // title of the page (should come from the language file)
  148. ?>
  149. <style type="text/css" media="screen, projection">
  150. /*<![CDATA[*/
  151. @import "<?php echo api_get_path(WEB_CODE_PATH); ?>css/public_admin/default.css";
  152. /*]]>*/
  153. </style>
  154. <?php
  155. if(api_get_setting('stylesheets')<>'')
  156. {
  157. ?>
  158. <style type="text/css" media="screen, projection">
  159. /*<![CDATA[*/
  160. @import "<?php echo api_get_path(WEB_CODE_PATH); ?>css/<?php echo api_get_setting('stylesheets');?>/default.css";
  161. /*]]>*/
  162. </style>
  163. <?php
  164. }
  165. $is_allowed_to_edit = api_is_allowed_to_edit();
  166. if($is_allowed_to_edit) // TEACHER ONLY
  167. {
  168. /*======================================
  169. DELETE FILE OR DIRECTORY
  170. ======================================*/
  171. if ( isset($_GET['delete']) )
  172. {
  173. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  174. if(DocumentManager::delete_document($_course,$_GET['delete'],$base_work_dir))
  175. {
  176. Display::display_normal_message(api_convert_encoding(get_lang('DocDeleted'), 'UTF-8', $charset));
  177. }
  178. else
  179. {
  180. Display::display_normal_message(api_convert_encoding(get_lang('DocDeleteError'), 'UTF-8', $charset));
  181. }
  182. }
  183. if( isset($_POST['action']))
  184. {
  185. switch($_POST['action'])
  186. {
  187. case 'delete':
  188. foreach($_POST['path'] as $index => $path)
  189. {
  190. DocumentManager::delete_document($_course,$path,$base_work_dir);
  191. }
  192. Display::display_normal_message(api_convert_encoding(get_lang('DocDeleted'), 'UTF-8', $charset));
  193. break;
  194. }
  195. }
  196. }
  197. /*
  198. -----------------------------------------------------------
  199. GET ALL DOCUMENT DATA FOR CURDIRPATH
  200. -----------------------------------------------------------
  201. */
  202. $docs_and_folders = getlist ($base_work_dir.'/');
  203. ?>
  204. <?php
  205. if($docs_and_folders)
  206. {
  207. //echo('<pre>');
  208. //print_r($docs_and_folders);
  209. //echo('</pre>');
  210. //*************************************************************************************************
  211. //do we need the title field for the document name or not?
  212. //we get the setting here, so we only have to do it once
  213. $use_document_title = api_get_setting('use_document_title');
  214. //create a sortable table with our data
  215. $sortable_data = array();
  216. while (list ($key, $id) = each($docs_and_folders))
  217. {
  218. // Skip directories.
  219. if ($id['filetype'] != 'file')
  220. {
  221. continue;
  222. }
  223. $row = array ();
  224. //if the item is invisible, wrap it in a span with class invisible
  225. $invisibility_span_open = ($id['visibility']==0)?'<span class="invisible">':'';
  226. $invisibility_span_close = ($id['visibility']==0)?'</span>':'';
  227. //size (or total size of a directory)
  228. $size = $id['filetype']=='folder' ? get_total_folder_size($id['path'],$is_allowed_to_edit) : $id[size];
  229. //get the title or the basename depending on what we're using
  230. if ($use_document_title=='true' AND $id['title']<>'')
  231. {
  232. $document_name=$id['title'];
  233. }
  234. else
  235. {
  236. $document_name=basename($id['path']);
  237. }
  238. //$row[] = $key; //testing
  239. //data for checkbox
  240. /*
  241. if ($is_allowed_to_edit AND count($docs_and_folders)>1)
  242. {
  243. $row[] = $id['path'];
  244. }
  245. */
  246. // icons with hyperlinks
  247. $row[]= '<a href="#" onclick="javascript:OpenFile(\''.$http_www.'/'.$id['title'].'\', \''.$sType.'\');return false;">'.build_document_icon_tag($id['filetype'],$id['path']).'</a>';
  248. //document title with hyperlink
  249. $row[] = '<a href="#" onclick="javascript:OpenFile(\''.$http_www.'/'.$id['title'].'\', \''.$sType.'\');return false;">'.$id['title'].'</a>';
  250. //comments => display comment under the document name
  251. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  252. $display_size = format_file_size($size);
  253. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  254. //last edit date
  255. $display_date = format_date(strtotime($id['lastedit_date']));
  256. $row[] = '<span style="display:none;">'.$id['lastedit_date'].'</span>'.$invisibility_span_open.$display_date.$invisibility_span_close;
  257. $sortable_data[] = $row;
  258. }
  259. //*******************************************************************************************
  260. }
  261. else
  262. {
  263. $sortable_data=array();
  264. //$table_footer='<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  265. }
  266. $table = new SortableTableFromArray($sortable_data,4,10);
  267. $query_vars['curdirpath'] = $curdirpath;
  268. if(isset($_SESSION['_gid']))
  269. {
  270. $query_vars['gidReq'] = $_SESSION['_gid'];
  271. }
  272. $table->set_additional_parameters($query_vars);
  273. $column = 0;
  274. /*
  275. if ($is_allowed_to_edit AND count($docs_and_folders)>1)
  276. {
  277. $table->set_header($column++,'',false);
  278. }
  279. */
  280. $table->set_header($column++, api_htmlentities(get_lang('Type'), ENT_QUOTES, $charset));
  281. $table->set_header($column++, api_htmlentities(get_lang('Title'), ENT_QUOTES, $charset));
  282. //$column_header[] = array(get_lang('Comment'),true); => display comment under the document name
  283. $table->set_header($column++, api_htmlentities(get_lang('Size'), ENT_QUOTES, $charset));
  284. $table->set_header($column++, api_htmlentities(get_lang('Date'), ENT_QUOTES, $charset));
  285. //currently only delete action -> take only DELETE right into account
  286. /*
  287. if (count($docs_and_folders)>1)
  288. {
  289. if ($is_allowed_to_edit)
  290. {
  291. $form_actions = array();
  292. $form_action['delete'] = get_lang('Delete');
  293. $table->set_form_actions($form_action,'path');
  294. }
  295. }
  296. */
  297. echo api_convert_encoding($table->get_table_html(), 'UTF-8', $charset);
  298. echo api_convert_encoding($table_footer, 'UTF-8', $charset);
  299. ////////// functions ////////////
  300. function getlist ($directory) {
  301. //global $delim, $win;
  302. if ($d = @opendir($directory)) {
  303. while (($filename = @readdir($d)) !== false) {
  304. $path = $directory . $filename;
  305. if ($filename != '.' && $filename != '..' && $filename != '.svn')
  306. {
  307. $file = array(
  308. "lastedit_date" =>date ("Y-m-d H:i:s", filemtime($path)),
  309. "visibility" => 1,
  310. "path" => $path,
  311. "title" => basename($path),
  312. "filetype" => filetype($path),
  313. "size" => filesize ($path)
  314. );
  315. $files[] = $file;
  316. }
  317. }
  318. return $files;
  319. }
  320. else
  321. {
  322. return false;
  323. }
  324. }
  325. function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
  326. {
  327. global $permissions_for_new_directories;
  328. $resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1) . $resource_directory . '/';
  329. if (!is_dir($resource_directory_full_path))
  330. {
  331. if (@mkdir($resource_directory_full_path, $permissions_for_new_directories))
  332. {
  333. // While we are in a course: Registering the newly created folder in the course's database.
  334. if (api_is_in_course())
  335. {
  336. global $_course, $_user;
  337. global $group_properties, $to_group_id;
  338. $group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
  339. $doc_id = add_document($_course, $group_directory.$resource_directory, 'folder', 0, $resource_directory_name);
  340. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id'], $to_group_id);
  341. }
  342. return true;
  343. }
  344. return false;
  345. }
  346. return true;
  347. }
  348. ?>
  349. <script type="text/javascript">
  350. <!--
  351. function OpenFile( fileUrl, type )
  352. {
  353. if(type=="audio")
  354. {
  355. ret = confirm('<?php echo api_convert_encoding(get_lang('AutostartMp3'), 'UTF-8', $charset); ?>');
  356. if(ret==true)
  357. {
  358. GetE('autostart').checked = true;
  359. }
  360. else
  361. {
  362. GetE('autostart').checked = false;
  363. }
  364. }
  365. SetUrl( fileUrl ) ;
  366. //window.close() ;
  367. }
  368. //-->
  369. </script>