429818ad640ccf98a51af22f11a39f98c00e0249
[atutor.git] / docs / mods / _standard / file_storage / index.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php'); // for get_human_size()
18 require(AT_INCLUDE_PATH.'../mods/_standard/file_storage/file_storage.inc.php');
19
20 // check folders and files id
21 if (isset($_GET['folders'])){
22         if (is_array($_GET['folders'])){
23                 foreach($_GET['folders'] as $k=>$v){
24                         $_GET['folders'][$k] = abs($_GET['folders'][$k]);
25                 }
26         } else {
27                 $_GET['folders']= abs($_GET['folders']);
28         } 
29 }
30 if (isset($_GET['files'])){
31         if (is_array($_GET['files'])){
32                 foreach($_GET['files'] as $k=>$v){
33                         $_GET['files'][$k] = abs($_GET['files'][$k]);
34                 }
35         } else {
36                 $_GET['files']= abs($_GET['files']);
37         } 
38 }
39
40 if (isset($_GET['submit_workspace'])) {
41         unset($_GET['folder']);
42         unset($assignment_for);
43
44         $owner_type = abs($_GET['ot']);
45
46         if ($owner_type == WORKSPACE_GROUP) {
47
48                 $parts = explode('_', $_GET['ot'], 2);
49                 if (isset($parts[1]) && $parts[1] && isset($_SESSION['groups'][$parts[1]])) {
50                         $owner_id = $parts[1];
51                 } else {
52                         $owner_type = WORKSPACE_COURSE;
53                         unset($owner_id);
54                 }
55         } else if ($owner_type == WORKSPACE_ASSIGNMENT) {
56                 $parts = explode('_', $_GET['ot'], 3);
57
58                 if (isset($parts[1]) && $parts[1]) {
59                         if ($parts[2] == 'my') {
60                                 $assignment_for = 'my'; 
61                         }
62                         $owner_id = $parts[1];
63                 } else {
64                         $owner_type = WORKSPACE_ASSIGNMENT;
65                         unset($owner_id);
66                 }
67         } else {
68                 unset($owner_id);
69         }
70         $_REQUEST['folder'] = 0;
71 } else if (isset($_REQUEST['ot'], $_REQUEST['oid'])) {
72         $owner_type = abs($_REQUEST['ot']);
73         $owner_id   = abs($_REQUEST['oid']);
74 } else if (isset($_SESSION['fs_owner_type'], $_SESSION['fs_owner_id'], $_SESSION['fs_folder_id'])) {
75         $owner_type = abs($_SESSION['fs_owner_type']);
76         $owner_id   = abs($_SESSION['fs_owner_id']);
77 } else {
78         $owner_type = WORKSPACE_COURSE;
79 }
80
81 if (isset($_REQUEST['folder'])) {
82         $folder_id = abs($_REQUEST['folder']);
83 } else if (isset($_SESSION['fs_folder_id'])) {
84         $folder_id = abs($_SESSION['fs_folder_id']);
85 } else {
86         $folder_id = 0;
87 }
88
89 // init the owner_id if not currently set
90 if (!isset($owner_id)) {
91         if ($owner_type == WORKSPACE_COURSE) {
92                 $owner_id = $_SESSION['course_id'];
93         } else if ($owner_type == WORKSPACE_PERSONAL) {
94                 $owner_id = $_SESSION['member_id'];
95         } else if ($owner_type == WORKSPACE_GROUP) {
96                 $owner_id = $group_id;
97         }
98 }
99
100 $owner_arg_prefix = '?ot='.$owner_type.SEP.'oid='.$owner_id. SEP;
101
102 if ($assignment_for == 'my') {
103         $owner_arg_prefix .= 'folder='.$_SESSION['member_id'];  
104 }
105 if (!($owner_status = fs_authenticate($owner_type, $owner_id))) {
106         $msg->addError('ACCESS_DENIED');
107         header('Location: '.url_rewrite('mods/_standard/file_storage/index.php', AT_PRETTY_URL_IS_HEADER));
108         exit;
109 }
110 $_SESSION['fs_owner_type'] = $owner_type;
111 $_SESSION['fs_owner_id']   = $owner_id;
112 $_SESSION['fs_folder_id']  = $folder_id;
113
114 if (isset($_GET['submit_workspace'])) {
115         header('Location: '.url_rewrite('mods/_standard/file_storage/index.php'.$owner_arg_prefix, AT_PRETTY_URL_IS_HEADER));
116         exit;
117 }
118
119 // action - Submit Assignment
120 if (isset($_GET['assignment']) && (isset($_GET['files']) || isset($_GET['folders']))) {
121         if (isset($_GET['folders'])) {
122                 $msg->addError('HAND_IN_FOLDER');
123         } else if (!isset($_GET['files'])) {
124                 $msg->addError('NO_ITEM_SELECTED');
125         } else {
126                 header('Location: '.AT_BASE_HREF.'mods/_standard/file_storage/assignment.php?'.$_SERVER['QUERY_STRING']);
127                 exit;
128         }
129 }
130 // action - View Revisions
131 else if (isset($_GET['revisions'], $_GET['files'])) {
132         if (is_array($_GET['files']) && (count($_GET['files']) == 1) && empty($_GET['folders'])) {
133                 $file_id = current($_GET['files']);
134                 header('Location: '.url_rewrite('mods/_standard/file_storage/revisions.php'.$owner_arg_prefix.'id='.$file_id, AT_PRETTY_URL_IS_HEADER));
135                 exit;
136         }
137 }
138 // action - View Comments
139 else if (isset($_GET['comments'], $_GET['files'])) {
140         if (is_array($_GET['files']) && (count($_GET['files']) == 1) && empty($_GET['folders'])) {
141                 $file_id = current($_GET['files']);
142                 header('Location: '.url_rewrite('comments.php'.$owner_arg_prefix.'id='.$file_id, AT_PRETTY_URL_IS_HEADER));
143                 exit;
144         }
145 }
146 // action - Edit File/Folder
147 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_GET['edit']) && (isset($_GET['folders']) || isset($_GET['files']))) {
148         if (is_array($_GET['files']) && (count($_GET['files']) == 1) && empty($_GET['folders'])) {
149                 $file_id = current($_GET['files']);
150                 header('Location: '.AT_BASE_HREF.'mods/_standard/file_storage/edit.php'.$owner_arg_prefix.'id='.$file_id);
151                 exit;
152         } else if (is_array($_GET['folders']) && (count($_GET['folders']) == 1) && empty($_GET['files'])) {
153                 $folder_id = current($_GET['folders']);
154                 header('Location: '.AT_BASE_HREF.'mods/_standard/file_storage/edit_folder.php'.$owner_arg_prefix.'id='.$folder_id);
155                 exit;
156         }
157 }
158 // action - Move Files/Folders
159 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_GET['move']) && (isset($_GET['folders']) || isset($_GET['files']))) {
160         header('Location: '.AT_BASE_HREF.'mods/_standard/file_storage/move.php'.$owner_arg_prefix.$_SERVER['QUERY_STRING']);
161         exit;
162 }
163 // action - Download Files/Folders
164 else if (isset($_GET['download']) && (isset($_GET['folders']) || isset($_GET['files']))) {
165         if (is_array($_GET['files']) && (count($_GET['files']) == 1) && empty($_GET['folders'])) {
166                 $file_id = current($_GET['files']);
167                 $sql = "SELECT file_name, file_size FROM ".TABLE_PREFIX."files WHERE file_id=$file_id AND owner_type=$owner_type AND owner_id=$owner_id";
168                 $result = mysql_query($sql, $db);
169                 if ($row = mysql_fetch_assoc($result)) {
170                         $ext = fs_get_file_extension($row['file_name']);
171
172                         if (isset($mime[$ext]) && $mime[$ext][0]) {
173                                 $file_mime = $mime[$ext][0];
174                         } else {
175                                 $file_mime = 'application/octet-stream';
176                         }
177                         $file_path = fs_get_file_path($file_id) . $file_id;
178
179                         ob_end_clean();
180                         header("Content-Encoding: none");
181                         header('Content-Type: ' . $file_mime);
182                         header('Content-transfer-encoding: binary'); 
183                         header('Content-Disposition: attachment; filename="'.htmlspecialchars($row['file_name']).'"');
184                         header('Expires: 0');
185                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
186                         header('Pragma: public');
187                         header('Content-Length: '.$row['file_size']);
188
189                         // see the note in get.php about the use of x-Sendfile
190                         header('x-Sendfile: '.$file_path);
191                         header('x-Sendfile: ', TRUE); // if we get here then it didn't work
192
193                         @readfile($file_path);
194                         exit;
195                 }
196         } else {
197                 // zip multiple files and folders
198                 require(AT_INCLUDE_PATH . 'classes/zipfile.class.php');
199                 $zipfile = new zipfile();
200
201                 $zip_file_name = fs_get_workspace($owner_type, $owner_id); // want the name of the workspace
202                 $zip_file_name = str_replace(" ","_",$zip_file_name );
203
204                 if (is_array($_GET['files'])) {
205                         foreach ($_GET['files'] as $file_id) {
206                                 $file_path = fs_get_file_path($file_id) . $file_id;
207                                 
208
209                                 $sql = "SELECT file_name, UNIX_TIMESTAMP(date) AS date FROM ".TABLE_PREFIX."files WHERE file_id=$file_id AND owner_type=$owner_type AND owner_id=$owner_id";
210                                 $result = mysql_query($sql, $db);
211                                 if (($row = mysql_fetch_assoc($result)) && file_exists($file_path)) {
212                                         $zipfile->add_file(file_get_contents($file_path), $row['file_name'], $row['date']);
213                                 }
214                         }
215                 }
216                 if (is_array($_GET['folders'])) {
217                         foreach($_GET['folders'] as $folder_id) {
218                                 fs_download_folder($folder_id, $zipfile, $owner_type, $owner_id);
219                                 $row['title'] = str_replace(" ","_",$row['title']  );
220                                 $zipfile->create_dir($row['title']);
221                         }
222
223                         if (count($_GET['folders']) == 1) {
224                                 // zip just one folder, use that folder's title as the zip file name
225                                 $row = fs_get_folder_by_id($_GET['folders'][0], $owner_type, $owner_id);
226                                 if ($row) {
227                                         $zip_file_name = $row['title'];
228                                         $zip_file_name = str_replace(" ","_",$zip_file_name );
229                                 }
230                         }
231                 }
232                 $zipfile->close();
233                 $zipfile->send_file($zip_file_name);
234         }
235         exit;
236 }
237 // action - Delete Files/Folders (pre-confirmation)
238 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_GET['delete']) && (isset($_GET['folders']) || isset($_GET['files']))) {
239         $hidden_vars = array();
240         $hidden_vars['folder'] = $folder_id;
241         $hidden_vars['ot']     = $owner_type;
242         $hidden_vars['oid']     = $owner_id;
243         if (isset($_GET['files'])) {
244                 $file_list_to_print = '';
245                 $files = implode(',', $_GET['files']);
246                 $hidden_vars['files'] = $files;
247                 $sql = "SELECT file_name FROM ".TABLE_PREFIX."files WHERE file_id IN ($files) AND owner_type=$owner_type AND owner_id=$owner_id ORDER BY file_name";
248                 $result = mysql_query($sql, $db);
249                 while ($row = mysql_fetch_assoc($result)) {
250                         $file_list_to_print .= '<li style="list-style: none; margin: 0px; padding: 0px 10px;"><img src="images/file_types/'.fs_get_file_type_icon($row['file_name']).'.gif" height="16" width="16" alt="" title="" /> '.htmlspecialchars($row['file_name']).'</li>';
251                 }
252                 $msg->addConfirm(array('FILE_DELETE', $file_list_to_print), $hidden_vars);
253         }
254                 
255         if (isset($_GET['folders'])) {
256                 $dir_list_to_print = '';
257                 $folders = implode(',', $_GET['folders']);
258                 $hidden_vars['folders'] = $folders;
259                 $rows = fs_get_folder_by_id($_GET['folders'], $owner_type, $owner_id);
260                 foreach ($rows as $row) {
261                         $dir_list_to_print .= '<li style="list-style: none; margin: 0px; padding: 0px 10px;"><img src="images/folder.gif" height="18" width="20" alt="" title="" /> '.AT_print($row['title'], 'input.text').'</li>';
262                 }
263                 $msg->addConfirm(array('DIR_DELETE', $dir_list_to_print), $hidden_vars);
264         }
265
266         require(AT_INCLUDE_PATH.'header.inc.php');
267         $msg->printConfirm();
268         require(AT_INCLUDE_PATH.'footer.inc.php');
269         exit;
270
271 }
272 // action - Confirm Delete Files/Folders
273 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_POST['submit_yes'])) {
274
275         // handle the delete
276         if (isset($_POST['files'])) {
277                 $files = explode(',', $_POST['files']);
278         }
279         if (isset($_POST['folders'])) {
280                 $folders = explode(',', $_POST['folders']);
281         }
282         if (isset($files)) {
283                 foreach ($files as $file) {
284                         fs_delete_file($file, $owner_type, $owner_id);
285                 }
286                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
287         }
288
289         if (isset($folders)) {
290                 foreach ($folders as $folder) {
291                         fs_delete_folder($folder, $owner_type, $owner_id);
292                 }
293                 $msg->addFeedback('DIR_DELETED');
294         }
295
296         header('Location: '.url_rewrite('mods/_standard/file_storage/index.php'.$owner_arg_prefix.'folder='.abs($_POST['folder']), AT_PRETTY_URL_IS_HEADER));
297         exit;
298 }
299 // action - Cancel Delete
300 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_POST['submit_no'])) {
301         $msg->addFeedback('CANCELLED');
302         header('Location: '.url_rewrite('mods/_standard/file_storage/index.php'.$owner_arg_prefix.'folder='.abs($_POST['folder']), AT_PRETTY_URL_IS_HEADER));
303         exit;
304
305 // action - Create Folder
306 } else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_POST['create_folder'])) {
307         // create a new folder
308         $_POST['new_folder_name'] = trim($_POST['new_folder_name']);
309
310         if (!$_POST['new_folder_name']) {
311                 $msg->addError(array('EMPTY_FIELDS', _AT('name')));
312         }
313
314         if (!$msg->containsErrors()) {
315                 $_POST['new_folder_name'] = $addslashes($_POST['new_folder_name']);
316
317                 $parent_folder_id = abs($_POST['folder']);
318
319                 $sql = "INSERT INTO ".TABLE_PREFIX."folders VALUES (NULL, $parent_folder_id, $owner_type, $owner_id, '$_POST[new_folder_name]')";
320                 $result = mysql_query($sql, $db);
321                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
322                 header('Location: '.url_rewrite('mods/_standard/file_storage/index.php'.$owner_arg_prefix.'folder='.$parent_folder_id, AT_PRETTY_URL_IS_HEADER));
323                 exit;
324         }
325 }
326 // action - Upload
327 else if (query_bit($owner_status, WORKSPACE_AUTH_WRITE) && isset($_POST['upload'])) {
328         // handle the file upload
329         $_POST['comments'] = trim($_POST['comments']);
330
331         $parent_folder_id = abs($_POST['folder']);
332         
333         if ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE) {
334                 $msg->addError(array('FILE_TOO_BIG', get_human_size(megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1)))));
335
336         } else if (!isset($_FILES['file']['name']) || ($_FILES['file']['error'] == UPLOAD_ERR_NO_FILE) || ($_FILES['file']['size'] == 0)) {
337                 $msg->addError('FILE_NOT_SELECTED');
338
339         } else if ($_FILES['file']['error'] || !is_uploaded_file($_FILES['file']['tmp_name'])) {
340                 $msg->addError('FILE_NOT_SAVED');
341         }
342
343         // check that we own this folder
344         if ($parent_folder_id) {
345                 $sql = "SELECT folder_id FROM ".TABLE_PREFIX."folders WHERE folder_id=$parent_folder_id AND owner_type=$owner_type AND owner_id=$owner_id";
346                 $result = mysql_query($sql, $db);
347                 if (!$row = mysql_fetch_assoc($result)) {
348                         $msg->addError('ACCESS_DENIED');
349                         header('Location: '.AT_BASE_HREF.'mods/_standard/file_storage/index.php');
350                         exit;
351                 }
352         }
353
354         if (!$msg->containsErrors()) {
355                 $_POST['description'] = $addslashes(trim($_POST['description']));
356                 $_FILES['file']['name'] = addslashes($_FILES['file']['name']);
357
358                 if ($_POST['comments']) {
359                         $num_comments = 1;
360                 } else {
361                         $num_comments = 0;
362                 }
363
364                 $sql = "INSERT INTO ".TABLE_PREFIX."files VALUES (NULL, $owner_type, $owner_id, $_SESSION[member_id], $parent_folder_id, 0, NOW(), $num_comments, 0, '{$_FILES['file']['name']}', {$_FILES['file']['size']}, '$_POST[description]')";
365                 $result = mysql_query($sql, $db);
366
367                 if ($result && ($file_id = mysql_insert_id($db))) {
368                         $path = fs_get_file_path($file_id);
369                         move_uploaded_file($_FILES['file']['tmp_name'], $path . $file_id);
370
371                         // check if this file name already exists
372                         $sql = "SELECT file_id, num_revisions FROM ".TABLE_PREFIX."files WHERE owner_type=$owner_type AND owner_id=$owner_id AND folder_id=$parent_folder_id AND file_id<>$file_id AND file_name='{$_FILES['file']['name']}' AND parent_file_id=0 ORDER BY file_id DESC LIMIT 1";
373                         $result = mysql_query($sql, $db);
374                         if ($row = mysql_fetch_assoc($result)) {
375                                 if ($_config['fs_versioning']) {
376                                         $sql = "UPDATE ".TABLE_PREFIX."files SET parent_file_id=$file_id, date=date WHERE file_id=$row[file_id]";
377                                         $result = mysql_query($sql, $db);
378
379                                         $sql = "UPDATE ".TABLE_PREFIX."files SET num_revisions=$row[num_revisions]+1, date=date WHERE file_id=$file_id";
380                                         $result = mysql_query($sql, $db);
381                                 } else {
382                                         fs_delete_file($row['file_id'], $owner_type, $owner_id);
383                                 }
384                         }
385
386                         $msg->addFeedback('FILE_UPLOADED');
387                 } else {
388                         $msg->addError('FILE_NOT_SAVED');
389                 }
390         }
391         header('Location: '.url_rewrite('mods/_standard/file_storage/index.php'.$owner_arg_prefix.'folder='.$parent_folder_id, AT_PRETTY_URL_IS_HEADER));
392         exit;
393 } else if ((isset($_GET['delete']) || isset($_GET['download']) || isset($_GET['move']) || isset($_GET['edit']) || isset($_GET['assignment'])) && !isset($_GET['files']) && !isset($_GET['folders'])) {
394         $msg->addError('NO_ITEM_SELECTED');
395 }
396
397 if (query_bit($owner_status, WORKSPACE_AUTH_WRITE)) {
398         $onload = 'hideform(\'upload\'); hideform(\'c_folder\');';
399 }
400
401 require(AT_INCLUDE_PATH.'header.inc.php');
402
403 $orders = array('asc' => 'desc', 'desc' => 'asc');
404 $cols   = array('file_name' => 1, 'file_size' => 1, 'date' => 1);
405
406 if (isset($_GET['asc'])) {
407         $order = 'asc';
408         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'file_name';
409 } else if (isset($_GET['desc'])) {
410         $order = 'desc';
411         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'file_name';
412 } else {
413         // no order set
414         $order = 'asc';
415         $col   = 'file_name';
416 }
417
418 $folder_path = fs_get_folder_path($folder_id, $owner_type, $owner_id);
419
420 $folders = fs_get_folder_by_pid($folder_id, $owner_type, $owner_id);
421
422 $files = array();
423 $sql = "SELECT * FROM ".TABLE_PREFIX."files WHERE folder_id=$folder_id AND owner_type=$owner_type AND owner_id=$owner_id AND parent_file_id=0 ORDER BY $col $order";
424 $result = mysql_query($sql, $db);
425
426 while ($row = mysql_fetch_assoc($result)) {
427         $files[] = $row;
428 }
429
430 ?>
431
432 <?php if (query_bit($owner_status, WORKSPACE_AUTH_WRITE)): ?>
433         <form method="post" action="<?php echo 'mods/_standard/file_storage/index.php'.$owner_arg_prefix; ?>" enctype="multipart/form-data" name="form0">
434         <input type="hidden" name="folder" value="<?php echo $folder_id; ?>" />
435         <div style="margin-left:auto; margin-right:auto;width: 95%;">
436                         <div class="input-form" style="width: 48%; float: right;" >
437                                 <div class="row">
438                                         <h3><a href="mods/_standard/file_storage/index.php" onclick="javascript:toggleform('c_folder'); return false;" style="font-family: Helevetica, Arial, sans-serif;" onmouseover="this.style.cursor='pointer'" onfocus="this.style.cursor='pointer'"><?php echo _AT('create_folder'); ?></a></h3>
439                                 </div>
440                                 <div  id="c_folder">
441                                         <div class="row">
442                                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="fname"><?php echo _AT('name'); ?></label><br />
443                                                 <input type="text" id="fname" name="new_folder_name" size="20" />
444                                         </div>
445                                         <div class="row buttons">
446                                                 <input type="submit" name="create_folder" value="<?php echo _AT('create'); ?>" class="button" />
447                                         </div>
448                                 </div>
449                         </div>
450         
451         
452                         <div class="input-form" style="float: left; width: 48%;">
453                                 <div class="row">
454                                         <h3><a href="mods/_standard/file_storage/index.php" onclick="javascript:toggleform('upload'); return false;" style="font-family: Helevetica, Arial, sans-serif;" onmouseover="this.style.cursor='pointer'" onfocus="this.style.cursor='pointer'"><?php echo _AT('new_file'); ?></a></h3>
455                                 </div>
456                                 <div id="upload">
457                                         <div class="row">
458                                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="file"><?php echo _AT('upload_file'); ?></label><br />
459                                                 <input type="file" name="file" id="file" />
460                                                 <br /><?php echo _AT('or'); ?> <a href="mods/_standard/file_storage/new.php<?php echo $owner_arg_prefix; ?>folder=<?php echo $folder_id; ?>"><?php echo _AT('file_manager_new'); ?></a>
461                                         </div>
462                                         <div class="row">
463                                                 <label for="description"><?php echo _AT('description'); ?></label><br />
464                                                 <textarea name="description" id="description" rows="1" cols="20"></textarea>
465                                         </div>
466                                         <div class="row buttons">
467                                                 <input type="submit" name="upload" value="<?php echo _AT('upload'); ?>"  class="button"/>
468                                         </div>
469                                 </div>
470                         </div>
471
472         </div>
473         </form>
474
475 <?php endif; ?>
476
477 <?php
478 if ($_SESSION['groups']) {
479         $file_storage_groups = array();
480         $groups_list = implode(',',$_SESSION['groups']);
481         $sql = "SELECT G.type_id, G.title, G.group_id FROM ".TABLE_PREFIX."file_storage_groups FS INNER JOIN ".TABLE_PREFIX."groups G USING (group_id) WHERE FS.group_id IN ($groups_list) ORDER BY G.type_id, G.title";
482         $result = mysql_query($sql, $db);
483         while ($row = mysql_fetch_assoc($result)) {
484                 $file_storage_groups[] = $row;
485         }
486 }
487
488 if (authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN)) {
489         $file_storage_assignments = array();
490         $sql = "SELECT * FROM ".TABLE_PREFIX."assignments WHERE course_id=$_SESSION[course_id] ORDER BY title";
491         $result = mysql_query($sql, $db);
492         while ($row = mysql_fetch_assoc($result)) {
493                 $file_storage_assignments[] = $row;
494         }
495 }
496
497 if ($_SESSION['member_id'] && $_SESSION['enroll']){
498         $my_assignments = array();
499         $sql = "SELECT distinct a.title, a.assignment_id FROM ".TABLE_PREFIX."assignments a, ".TABLE_PREFIX."files f
500                  WHERE a.course_id = ".$_SESSION[course_id]."
501                    AND a.assignment_id = f.owner_id
502                    AND f.owner_type= ".WORKSPACE_ASSIGNMENT."
503                    AND f.member_id = ".$_SESSION['member_id']."
504                  ORDER BY a.title";
505         $result = mysql_query($sql, $db);
506         while ($row = mysql_fetch_assoc($result)) {
507                 $my_assignments[] = $row;
508         }
509 }
510 ?>
511 <div style="margin-left:auto;margin-right:auto; clear:both;width:95%;">
512 <form method="get" action="<?php echo url_rewrite('mods/_standard/file_storage/index.php', AT_PRETTY_URL_IS_HEADER);?>" name="form">
513 <input type="hidden" name="folder" value="<?php echo $folder_id; ?>" />
514 <input type="hidden" name="oid" value="<?php echo $owner_id; ?>" />
515 <table class="data">
516 <colgroup>
517         <?php if ($col == 'file_name'): ?>
518                 <col />
519                 <col class="sort" />
520                 <col span="5" />
521         <?php elseif($col == 'file_size'): ?>
522                 <col span="5" />
523                 <col class="sort" />
524                 <col />
525         <?php elseif($col == 'date'): ?>
526                 <col span="6" />
527                 <col class="sort" />
528         <?php endif; ?>
529 </colgroup>
530 <thead>
531 <tr>
532         <td colspan="7">
533                 <label for="ot"><?php echo _AT('workspace'); ?> </label>
534                 <select name="ot" id="ot">
535                         <option value="1" <?php if ($owner_type == WORKSPACE_COURSE) { echo 'selected="selected"'; } ?>><?php echo _AT('course_files'); ?></option>
536                         <?php if ($_SESSION['member_id'] && $_SESSION['enroll']): ?>
537                                 <option value="2" <?php if ($owner_type == WORKSPACE_PERSONAL) { echo 'selected="selected"'; } ?>><?php echo _AT('my_files'); ?></option>
538                         <?php endif; ?>
539                         <?php if ($file_storage_groups): ?>
540                                 <optgroup label="<?php echo _AT('groups'); ?>">
541                                         <?php foreach ($file_storage_groups as $group): ?>
542                                                 <option value="<?php echo WORKSPACE_GROUP; ?>_<?php echo $group['group_id']; ?>" <?php if ($owner_type == WORKSPACE_GROUP && $owner_id == $group['group_id']) { echo 'selected="selected"'; } ?>><?php echo AT_print($group['title'], 'input.text'); ?></option>
543                                         <?php endforeach; ?>
544                                 </optgroup>
545                         <?php endif; ?>
546                         <?php if (count($my_assignments) != 0) : ?>
547                                 <optgroup label="<?php echo _AT('assignments'); ?>">
548                                         <?php foreach ($my_assignments as $my_assignment): ?>
549                                                 <option value="<?php echo WORKSPACE_ASSIGNMENT; ?>_<?php echo $my_assignment['assignment_id']; ?>_my" <?php if ($owner_type == WORKSPACE_ASSIGNMENT && $owner_id == $my_assignment['assignment_id']) { echo 'selected="selected"'; } ?>><?php echo AT_print($my_assignment['title'], 'input.text'); ?></option>
550                                         <?php endforeach; ?>
551                                 </optgroup>
552                         <?php endif; ?>
553                         <?php if (authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN) && count($file_storage_assignments) != 0) : ?>
554                                 <optgroup label="<?php echo _AT('assignments'); ?>">
555                                         <?php foreach ($file_storage_assignments as $assignment): ?>
556                                                 <option value="<?php echo WORKSPACE_ASSIGNMENT; ?>_<?php echo $assignment['assignment_id']; ?>" <?php if ($owner_type == WORKSPACE_ASSIGNMENT && $owner_id == $assignment['assignment_id']) { echo 'selected="selected"'; } ?>><?php echo AT_print($assignment['title'], 'input.text'); ?></option>
557                                         <?php endforeach; ?>
558                                 </optgroup>
559                         <?php endif; ?>
560                 </select>
561                 <input type="submit" name="submit_workspace" value="<?php echo _AT('go'); ?>" class="button" />
562
563                 <br />
564                 <?php echo _AT('current_path'); ?>
565                         <a href="<?php 
566                         if ($owner_type == WORKSPACE_ASSIGNMENT && !authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN))
567                         { // student assignment's folder; if it's instrutor who has priviledge to view all students' assignments, folder is 0
568                                 $folder = $_SESSION['member_id'];
569                         }
570                         else
571                         {
572                                 $folder = 0;
573                         }
574                                 
575                         echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.$folder); ?>"><?php echo _AT('home'); ?></a>
576                 <?php foreach ($folder_path as $folder_info): ?>
577                         <?php if ($folder_info['folder_id'] == $folder_id): ?>
578                                 » <?php echo AT_print($folder_info['title'], 'input.text'); ?>
579                                 <?php $parent_folder_id = $folder_info['parent_folder_id']; ?>
580                         <?php else: ?>
581                                 » <a href="<?php echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.$folder_info['folder_id']); ?>"><?php echo AT_print($folder_info['title'], 'input.text'); ?></a>
582                         <?php endif; ?>
583                 <?php endforeach; ?>
584         </td>
585 </tr>
586 <tr>
587         <th align="left" width="10"><input type="checkbox" value="<?php echo _AT('select_all'); ?>" id="all" title="<?php echo _AT('select_all'); ?>" name="selectall" onclick="CheckAll();" /></th>
588         <th scope="col"><a href="<?php echo url_rewrite($_SERVER['PHP_SELF'] . $owner_arg_prefix . 'folder='.$folder_id.SEP.$orders[$order].'=file_name'); ?>"><?php echo _AT('file');      ?></a></th>
589         <th scope="col"><?php echo _AT('author');    ?></th>
590         <th scope="col"><?php if ($_config['fs_versioning']): ?><?php echo _AT('revisions'); ?><?php endif; ?></th>
591         <th scope="col"><?php echo _AT('comments');  ?></th>
592         <th scope="col"><a href="<?php echo url_rewrite($_SERVER['PHP_SELF'] . $owner_arg_prefix . 'folder='.$folder_id.SEP.$orders[$order].'=file_size'); ?>"><?php echo _AT('size'); ?></a></th>
593         <th scope="col"><a href="<?php echo url_rewrite($_SERVER['PHP_SELF'] . $owner_arg_prefix . 'folder='.$folder_id.SEP.$orders[$order].'=date'); ?>"><?php echo _AT('date'); ?></a></th>
594 </tr>
595
596 </thead>
597 <tfoot>
598 <tr>
599         <td colspan="7">
600                 <input type="submit" name="download" value="<?php echo _AT('download'); ?>"  class="button"/>
601                 <?php if (query_bit($owner_status, WORKSPACE_AUTH_WRITE)): ?>
602                         <?php if (($owner_type != WORKSPACE_COURSE) && !(($owner_type == WORKSPACE_PERSONAL) && ($_SESSION['is_admin'] || authenticate(AT_PRIV_GROUPS,true))) ): ?>
603                                 <input type="submit" name="assignment" value="<?php echo _AT('hand_in'); ?>"  class="button"/>
604                         <?php endif; ?>
605                         <input type="submit" name="edit" value="<?php echo _AT('edit'); ?>"  class="button"/>
606                         <input type="submit" name="move" value="<?php echo _AT('move'); ?>"  class="button"/>
607                         <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>"  class="button"/>
608                 <?php endif; ?>
609         </td>
610 </tr>
611 </tfoot>
612 <tbody>
613 <?php if ($folder_id): ?>
614         <tr>
615                 <td colspan="7"><a href="<?php echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.intval($folder_path[count($folder_path)-1]['parent_folder_id'])); ?>"><img src="images/arrowicon.gif" border="0" height="" width="" alt="" /> <?php echo _AT('back'); ?></a></td>
616         </tr>
617 <?php endif; ?>
618 <?php if ($folders || $files): ?>
619         <?php foreach ($folders as $folder_info): ?>
620                 <tr onmousedown="document.form['f<?php echo $folder_info['folder_id']; ?>'].checked = !document.form['f<?php echo $folder_info['folder_id']; ?>'].checked; rowselectbox(this, document.form['f<?php echo $folder_info['folder_id']; ?>'].checked, 'checkbuttons(false)');" id="r_<?php echo $folder_info['folder_id']; ?>_1">
621                         <td width="10"><input type="checkbox" name="folders[]" value="<?php echo $folder_info['folder_id']; ?>" id="f<?php echo $folder_info['folder_id']; ?>" onmouseup="this.checked=!this.checked" /></td>
622                         <td><img src="images/folder.gif" height="18" width="20" alt="" /> <label for="f<?php echo $folder_info['folder_id']; ?>"><a href="<?php echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.
623                         $folder_info['folder_id']); ?>"><?php echo AT_print($folder_info['title'], 'input.text'); ?></a></label></td>
624                         <td>&nbsp;</td>
625                         <td>&nbsp;</td>
626                         <td>&nbsp;</td>
627                         <td>&nbsp;</td>
628                         <td>&nbsp;</td>
629                 </tr>
630         <?php endforeach; ?>
631         <?php foreach ($files as $file_info): ?>
632                 <tr onmousedown="document.form['r<?php echo $file_info['file_id']; ?>'].checked = !document.form['r<?php echo $file_info['file_id']; ?>'].checked; rowselectbox(this, document.form['r<?php echo $file_info['file_id']; ?>'].checked, 'checkbuttons(false)');" id="r_<?php echo $file_info['file_id']; ?>_0">
633                         <td valign="top" width="10"><input type="checkbox" name="files[]" value="<?php echo $file_info['file_id']; ?>" id="r<?php echo $file_info['file_id']; ?>" onmouseup="this.checked=!this.checked" /></td>
634                         <td valign="top">
635                                 <img src="images/file_types/<?php echo fs_get_file_type_icon($file_info['file_name']); ?>.gif" height="16" width="16" alt="" title="" /> <label for="r<?php echo $file_info['file_id']; ?>" onmousedown="document.form['r<?php echo $file_info['file_id']; ?>'].checked = !document.form['r<?php echo $file_info['file_id']; ?>'].checked; rowselectbox(this, document.form['r<?php echo $file_info['file_id']; ?>'].checked, 'checkbuttons(false)');"><?php echo htmlspecialchars($file_info['file_name']); ?></label>
636                                 <?php if ($file_info['description']): ?>
637                                         <p class="fm-desc"><?php echo htmlspecialchars($file_info['description']); ?></p>
638                                 <?php endif; ?>
639                         </td>
640                         <td valign="top"><?php echo get_display_name($file_info['member_id']); ?></td>
641                         <td valign="top">
642                                 <?php if ($_config['fs_versioning']): ?>
643                                         <?php if ($file_info['num_revisions']): 
644                                                 if ($file_info['num_revisions'] == 1) {
645                                                         $lang_var = 'fs_revision';
646                                                 } else {
647                                                         $lang_var = 'fs_revisions';
648                                                 }
649                                                 ?>
650                                                 
651                                                 <a href="<?php echo url_rewrite('mods/_standard/file_storage/revisions.php'.$owner_arg_prefix.'id='.$file_info['file_id']); ?>"><?php echo _AT($lang_var, $file_info['num_revisions']); ?></a>
652                                         <?php else: ?>
653                                                 -
654                                         <?php endif; ?>
655                                 <?php endif; ?>
656                         </td>
657                         <td valign="top">
658                         <?php 
659                         if ($file_info['num_comments'] == 1) {
660                                 $lang_var = 'fs_comment';
661                         } else {
662                                 $lang_var = 'fs_comments';
663                         }
664                         ?>
665                         <a href="<?php echo url_rewrite('mods/_standard/file_storage/comments.php'.$owner_arg_prefix.'id='.$file_info['file_id']); ?>"><?php echo _AT($lang_var, $file_info['num_comments']); ?></a></td>
666                         <td align="right" valign="top"><?php echo get_human_size($file_info['file_size']); ?></td>
667                         <td align="right" valign="top"><?php echo AT_date(_AT('filemanager_date_format'), $file_info['date'], AT_DATE_MYSQL_DATETIME); ?></td>
668                 </tr>
669         <?php endforeach; ?>
670 <?php else: ?>
671         <tr>
672                 <td colspan="7"><?php echo _AT('none_found'); ?></td>
673         </tr>
674 <?php endif; ?>
675 </tbody>
676 </table>
677 </form>
678 </div>
679 <script type="text/javascript">
680 // <!--
681 function checkbuttons(state) {
682         document.form.selectall.checked = state;
683
684         var num_files_checked = 0;
685         var num_folders_checked = 0;
686         for (var i=0;i<document.form.elements.length;i++) {
687                 var e = document.form.elements[i];
688                 if ((e.name == 'folders[]') && (e.type=='checkbox') && e.checked) {
689                         num_folders_checked++;
690                 } else if ((e.name == 'files[]') && (e.type=='checkbox') && e.checked) {
691                         num_files_checked++;
692                 }
693         }
694         if (num_files_checked + num_folders_checked > 1) {
695                 if (document.form.edit)
696                         document.form.edit.disabled = true;
697         } else {
698                 if (document.form.edit)
699                         document.form.edit.disabled = false;
700         }
701 }
702 function CheckAll() {
703         var state = document.form.selectall.checked;
704         for (var i=0;i<document.form.elements.length;i++)       {
705                 var e = document.form.elements[i];
706                 if ((e.name == 'folders[]') && (e.type=='checkbox')) {
707                         e.checked = state;
708                         rowselectbox(document.getElementById('r_' + e.value + '_1'), state, 'checkbuttons(' + state + ')');
709                 } else if ((e.name == 'files[]') && (e.type=='checkbox')) {
710                         e.checked = state;
711                         rowselectbox(document.getElementById('r_' + e.value + '_0'), state, 'checkbuttons(' + state + ')');
712                 }
713         }
714 }
715
716 function hideform(id) {
717         document.getElementById(id).style.display='none';
718 }
719
720 function toggleform(id) {
721         if (document.getElementById(id).style.display == "none") {
722                 //show
723                 document.getElementById(id).style.display='';   
724
725                 if (id == "c_folder") {
726                         document.form0.new_folder_name.focus();
727                 } else if (id == "upload") {
728                         document.form0.file.focus();
729                 }
730
731         } else {
732                 //hide
733                 document.getElementById(id).style.display='none';
734         }
735 }
736
737 // -->
738 </script>
739
740 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>