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/repositories_config.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(\" ". get_lang("AreYouSureToDelete") ." \"+ 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) . 'events.lib.inc.php');
  130. include_once(api_get_path(LIBRARY_PATH) . 'document.lib.php');
  131. include_once(api_get_path(LIBRARY_PATH) . 'tablesort.lib.php');
  132. /*
  133. -----------------------------------------------------------
  134. Constants and variables
  135. -----------------------------------------------------------
  136. */
  137. $course_quota = DocumentManager::get_course_quota();
  138. /*
  139. ==============================================================================
  140. MAIN SECTION
  141. ==============================================================================
  142. */
  143. /*
  144. -----------------------------------------------------------
  145. Header
  146. -----------------------------------------------------------
  147. */
  148. $tool_name = get_lang("Doc"); // title of the page (should come from the language file)
  149. ?>
  150. <style type="text/css" media="screen, projection">
  151. /*<![CDATA[*/
  152. @import "<?php echo api_get_path(WEB_CODE_PATH); ?>css/public_admin/default.css";
  153. /*]]>*/
  154. </style>
  155. <?php
  156. if(api_get_setting('stylesheets')<>'')
  157. {
  158. ?>
  159. <style type="text/css" media="screen, projection">
  160. /*<![CDATA[*/
  161. @import "<?php echo api_get_path(WEB_CODE_PATH); ?>css/<?php echo api_get_setting('stylesheets');?>/default.css";
  162. /*]]>*/
  163. </style>
  164. <?php
  165. }
  166. $is_allowed_to_edit = api_is_allowed_to_edit();
  167. if($is_allowed_to_edit) // TEACHER ONLY
  168. {
  169. /*======================================
  170. DELETE FILE OR DIRECTORY
  171. ======================================*/
  172. if ( isset($_GET['delete']) )
  173. {
  174. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  175. if(DocumentManager::delete_document($_course,$_GET['delete'],$base_work_dir))
  176. {
  177. Display::display_normal_message(get_lang('DocDeleted'));
  178. }
  179. else
  180. {
  181. Display::display_normal_message(get_lang('DocDeleteError'));
  182. }
  183. }
  184. if( isset($_POST['action']))
  185. {
  186. switch($_POST['action'])
  187. {
  188. case 'delete':
  189. foreach($_POST['path'] as $index => $path)
  190. {
  191. DocumentManager::delete_document($_course,$path,$base_work_dir);
  192. }
  193. Display::display_normal_message(get_lang('DocDeleted'));
  194. break;
  195. }
  196. }
  197. }
  198. /*
  199. -----------------------------------------------------------
  200. GET ALL DOCUMENT DATA FOR CURDIRPATH
  201. -----------------------------------------------------------
  202. */
  203. $docs_and_folders = getlist ($base_work_dir.'/');
  204. ?>
  205. <?php
  206. if($docs_and_folders)
  207. {
  208. //echo('<pre>');
  209. //print_r($docs_and_folders);
  210. //echo('</pre>');
  211. //*************************************************************************************************
  212. //do we need the title field for the document name or not?
  213. //we get the setting here, so we only have to do it once
  214. $use_document_title = api_get_setting('use_document_title');
  215. //create a sortable table with our data
  216. $sortable_data = array();
  217. while (list ($key, $id) = each($docs_and_folders))
  218. {
  219. // Skip directories.
  220. if ($id['filetype'] != 'file')
  221. {
  222. continue;
  223. }
  224. $row = array ();
  225. //if the item is invisible, wrap it in a span with class invisible
  226. $invisibility_span_open = ($id['visibility']==0)?'<span class="invisible">':'';
  227. $invisibility_span_close = ($id['visibility']==0)?'</span>':'';
  228. //size (or total size of a directory)
  229. $size = $id['filetype']=='folder' ? get_total_folder_size($id['path'],$is_allowed_to_edit) : $id[size];
  230. //get the title or the basename depending on what we're using
  231. if ($use_document_title=='true' AND $id['title']<>'')
  232. {
  233. $document_name=$id['title'];
  234. }
  235. else
  236. {
  237. $document_name=basename($id['path']);
  238. }
  239. //$row[] = $key; //testing
  240. //data for checkbox
  241. /*
  242. if ($is_allowed_to_edit AND count($docs_and_folders)>1)
  243. {
  244. $row[] = $id['path'];
  245. }
  246. */
  247. // icons with hyperlinks
  248. $row[]= '<a href="#" onclick="javascript:OpenFile(\''.$http_www.'/'.$id['title'].'\', \''.$sType.'\');return false;">'.build_document_icon_tag($id['filetype'],$id['path']).'</a>';
  249. //document title with hyperlink
  250. $row[] = '<a href="#" onclick="javascript:OpenFile(\''.$http_www.'/'.$id['title'].'\', \''.$sType.'\');return false;">'.$id['title'].'</a>';
  251. //comments => display comment under the document name
  252. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  253. $display_size = format_file_size($size);
  254. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  255. //last edit date
  256. $display_date = format_date(strtotime($id['lastedit_date']));
  257. $row[] = '<span style="display:none;">'.$id['lastedit_date'].'</span>'.$invisibility_span_open.$display_date.$invisibility_span_close;
  258. $sortable_data[] = $row;
  259. }
  260. //*******************************************************************************************
  261. }
  262. else
  263. {
  264. $sortable_data=array();
  265. //$table_footer='<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  266. }
  267. $table = new SortableTableFromArray($sortable_data,4,10);
  268. $query_vars['curdirpath'] = $curdirpath;
  269. if(isset($_SESSION['_gid']))
  270. {
  271. $query_vars['gidReq'] = $_SESSION['_gid'];
  272. }
  273. $table->set_additional_parameters($query_vars);
  274. $column = 0;
  275. /*
  276. if ($is_allowed_to_edit AND count($docs_and_folders)>1)
  277. {
  278. $table->set_header($column++,'',false);
  279. }
  280. */
  281. $table->set_header($column++,get_lang('Type'));
  282. $table->set_header($column++,get_lang('Title'));
  283. //$column_header[] = array(get_lang('Comment'),true); => display comment under the document name
  284. $table->set_header($column++,get_lang('Size'));
  285. $table->set_header($column++,get_lang('Date'));
  286. //currently only delete action -> take only DELETE right into account
  287. /*
  288. if (count($docs_and_folders)>1)
  289. {
  290. if ($is_allowed_to_edit)
  291. {
  292. $form_actions = array();
  293. $form_action['delete'] = get_lang('Delete');
  294. $table->set_form_actions($form_action,'path');
  295. }
  296. }
  297. */
  298. $table->display();
  299. echo $table_footer;
  300. ////////// functions ////////////
  301. function getlist ($directory) {
  302. //global $delim, $win;
  303. if ($d = @opendir($directory)) {
  304. while (($filename = @readdir($d)) !== false) {
  305. $path = $directory . $filename;
  306. if ($filename != '.' && $filename != '..' && $filename != '.svn')
  307. {
  308. $file = array(
  309. "lastedit_date" =>date ("Y-m-d H:i:s", filemtime($path)),
  310. "visibility" => 1,
  311. "path" => $path,
  312. "title" => basename($path),
  313. "filetype" => filetype($path),
  314. "size" => filesize ($path)
  315. );
  316. $files[] = $file;
  317. }
  318. }
  319. return $files;
  320. }
  321. else
  322. {
  323. return false;
  324. }
  325. }
  326. function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
  327. {
  328. global $permissions_for_new_directories;
  329. $resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1) . $resource_directory . '/';
  330. if (!is_dir($resource_directory_full_path))
  331. {
  332. if (@mkdir($resource_directory_full_path, $permissions_for_new_directories))
  333. {
  334. // While we are in a course: Registering the newly created folder in the course's database.
  335. if (api_is_in_course())
  336. {
  337. global $_course, $_user;
  338. global $group_properties, $to_group_id;
  339. $group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
  340. $doc_id = add_document($_course, $group_directory.$resource_directory, 'folder', 0, $resource_directory_name);
  341. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id'], $to_group_id);
  342. }
  343. return true;
  344. }
  345. return false;
  346. }
  347. return true;
  348. }
  349. ?>
  350. <script type="text/javascript">
  351. <!--
  352. function OpenFile( fileUrl, type )
  353. {
  354. if(type=="audio")
  355. {
  356. ret = confirm('<?php echo get_lang('AutostartMp3') ?>');
  357. if(ret==true)
  358. {
  359. GetE('autostart').checked = true;
  360. }
  361. else
  362. {
  363. GetE('autostart').checked = false;
  364. }
  365. }
  366. SetUrl( fileUrl ) ;
  367. //window.close() ;
  368. }
  369. //-->
  370. </script>