(no commit message)
[atutor.git] / 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="" /> '.htmlentities_utf8($row['title']).'</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: 75%;">
436                 <div style="" >
437                         <div class="input-form" style="width: 48%; float: right;" >
438                                 <div class="row">
439                                         <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>
440                                 </div>
441                                 <div  id="c_folder">
442                                         <div class="row">
443                                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="fname"><?php echo _AT('name'); ?></label><br />
444                                                 <input type="text" id="fname" name="new_folder_name" size="20" />
445                                         </div>
446                                         <div class="row buttons">
447                                                 <input type="submit" name="create_folder" value="<?php echo _AT('create'); ?>" class="button" />
448                                         </div>
449                                 </div>
450                         </div>
451         
452         
453                         <div class="input-form" style="float: left; width: 45%;">
454                                 <div class="row">
455                                         <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>
456                                 </div>
457                                 <div id="upload">
458                                         <div class="row">
459                                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="file"><?php echo _AT('upload_file'); ?></label><br />
460                                                 <input type="file" name="file" id="file" />
461                                                 <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>
462                                         </div>
463                                         <div class="row">
464                                                 <label for="description"><?php echo _AT('description'); ?></label><br />
465                                                 <textarea name="description" id="description" rows="1" cols="20"></textarea>
466                                         </div>
467                                         <div class="row buttons">
468                                                 <input type="submit" name="upload" value="<?php echo _AT('upload'); ?>"  class="button"/>
469                                         </div>
470                                 </div>
471                         </div>
472         
473                 </div>
474         </div>
475         </form>
476
477 <?php endif; ?>
478
479 <?php
480 if ($_SESSION['groups']) {
481         $file_storage_groups = array();
482         $groups_list = implode(',',$_SESSION['groups']);
483         $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";
484         $result = mysql_query($sql, $db);
485         while ($row = mysql_fetch_assoc($result)) {
486                 $file_storage_groups[] = $row;
487         }
488 }
489
490 if (authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN)) {
491         $file_storage_assignments = array();
492         $sql = "SELECT * FROM ".TABLE_PREFIX."assignments WHERE course_id=$_SESSION[course_id] ORDER BY title";
493         $result = mysql_query($sql, $db);
494         while ($row = mysql_fetch_assoc($result)) {
495                 $file_storage_assignments[] = $row;
496         }
497 }
498
499 if ($_SESSION['member_id'] && $_SESSION['enroll']){
500         $my_assignments = array();
501         $sql = "SELECT distinct a.title, a.assignment_id FROM ".TABLE_PREFIX."assignments a, ".TABLE_PREFIX."files f
502                  WHERE a.course_id = ".$_SESSION[course_id]."
503                    AND a.assignment_id = f.owner_id
504                    AND f.owner_type= ".WORKSPACE_ASSIGNMENT."
505                    AND f.member_id = ".$_SESSION['member_id']."
506                  ORDER BY a.title";
507         $result = mysql_query($sql, $db);
508         while ($row = mysql_fetch_assoc($result)) {
509                 $my_assignments[] = $row;
510         }
511 }
512 ?>
513 <div style="float:left; clear:right; width:95%;">
514 <form method="get" action="<?php echo url_rewrite('mods/_standard/file_storage/index.php', AT_PRETTY_URL_IS_HEADER);?>" name="form">
515 <input type="hidden" name="folder" value="<?php echo $folder_id; ?>" />
516 <input type="hidden" name="oid" value="<?php echo $owner_id; ?>" />
517 <table class="data">
518 <colgroup>
519         <?php if ($col == 'file_name'): ?>
520                 <col />
521                 <col class="sort" />
522                 <col span="5" />
523         <?php elseif($col == 'file_size'): ?>
524                 <col span="5" />
525                 <col class="sort" />
526                 <col />
527         <?php elseif($col == 'date'): ?>
528                 <col span="6" />
529                 <col class="sort" />
530         <?php endif; ?>
531 </colgroup>
532 <thead>
533 <tr>
534         <td colspan="7">
535                 <label for="ot"><?php echo _AT('workspace'); ?> </label>
536                 <select name="ot" id="ot">
537                         <option value="1" <?php if ($owner_type == WORKSPACE_COURSE) { echo 'selected="selected"'; } ?>><?php echo _AT('course_files'); ?></option>
538                         <?php if ($_SESSION['member_id'] && $_SESSION['enroll']): ?>
539                                 <option value="2" <?php if ($owner_type == WORKSPACE_PERSONAL) { echo 'selected="selected"'; } ?>><?php echo _AT('my_files'); ?></option>
540                         <?php endif; ?>
541                         <?php if ($file_storage_groups): ?>
542                                 <optgroup label="<?php echo _AT('groups'); ?>">
543                                         <?php foreach ($file_storage_groups as $group): ?>
544                                                 <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 htmlentities_utf8($group['title']); ?></option>
545                                         <?php endforeach; ?>
546                                 </optgroup>
547                         <?php endif; ?>
548                         <?php if (count($my_assignments) != 0) : ?>
549                                 <optgroup label="<?php echo _AT('assignments'); ?>">
550                                         <?php foreach ($my_assignments as $my_assignment): ?>
551                                                 <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 htmlentities_utf8($my_assignment['title']); ?></option>
552                                         <?php endforeach; ?>
553                                 </optgroup>
554                         <?php endif; ?>
555                         <?php if (authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN) && count($file_storage_assignments) != 0) : ?>
556                                 <optgroup label="<?php echo _AT('assignments'); ?>">
557                                         <?php foreach ($file_storage_assignments as $assignment): ?>
558                                                 <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 htmlentities_utf8($assignment['title']); ?></option>
559                                         <?php endforeach; ?>
560                                 </optgroup>
561                         <?php endif; ?>
562                 </select>
563                 <input type="submit" name="submit_workspace" value="<?php echo _AT('go'); ?>" class="button" />
564
565                 <br />
566                 <?php echo _AT('current_path'); ?>
567                         <a href="<?php 
568                         if ($owner_type == WORKSPACE_ASSIGNMENT && !authenticate(AT_PRIV_ASSIGNMENTS, AT_PRIV_RETURN))
569                         { // student assignment's folder; if it's instrutor who has priviledge to view all students' assignments, folder is 0
570                                 $folder = $_SESSION['member_id'];
571                         }
572                         else
573                         {
574                                 $folder = 0;
575                         }
576                                 
577                         echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.$folder); ?>"><?php echo _AT('home'); ?></a>
578                 <?php foreach ($folder_path as $folder_info): ?>
579                         <?php if ($folder_info['folder_id'] == $folder_id): ?>
580                                 Â» <?php echo htmlentities_utf8($folder_info['title']); ?>
581                                 <?php $parent_folder_id = $folder_info['parent_folder_id']; ?>
582                         <?php else: ?>
583                                 Â» <a href="<?php echo url_rewrite($_SERVER['PHP_SELF'].$owner_arg_prefix.'folder='.$folder_info['folder_id']); ?>"><?php echo htmlentities_utf8($folder_info['title']); ?></a>
584                         <?php endif; ?>
585                 <?php endforeach; ?>
586         </td>
587 </tr>
588 <tr>
589         <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>
590         <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>
591         <th scope="col"><?php echo _AT('author');    ?></th>
592         <th scope="col"><?php if ($_config['fs_versioning']): ?><?php echo _AT('revisions'); ?><?php endif; ?></th>
593         <th scope="col"><?php echo _AT('comments');  ?></th>
594         <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>
595         <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>
596 </tr>
597
598 </thead>
599 <tfoot>
600 <tr>
601         <td colspan="7">
602                 <input type="submit" name="download" value="<?php echo _AT('download'); ?>"  class="button"/>
603                 <?php if (query_bit($owner_status, WORKSPACE_AUTH_WRITE)): ?>
604                         <?php if (($owner_type != WORKSPACE_COURSE) && !(($owner_type == WORKSPACE_PERSONAL) && ($_SESSION['is_admin'] || authenticate(AT_PRIV_GROUPS,true))) ): ?>
605                                 <input type="submit" name="assignment" value="<?php echo _AT('hand_in'); ?>"  class="button"/>
606                         <?php endif; ?>
607                         <input type="submit" name="edit" value="<?php echo _AT('edit'); ?>"  class="button"/>
608                         <input type="submit" name="move" value="<?php echo _AT('move'); ?>"  class="button"/>
609                         <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>"  class="button"/>
610                 <?php endif; ?>
611         </td>
612 </tr>
613 </tfoot>
614 <tbody>
615 <?php if ($folder_id): ?>
616         <tr>
617                 <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>
618         </tr>
619 <?php endif; ?>
620 <?php if ($folders || $files): ?>
621         <?php foreach ($folders as $folder_info): ?>
622                 <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">
623                         <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>
624                         <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='.
625                         $folder_info['folder_id']); ?>"><?php echo htmlentities_utf8($folder_info['title']); ?></a></label></td>
626                         <td>&nbsp;</td>
627                         <td>&nbsp;</td>
628                         <td>&nbsp;</td>
629                         <td>&nbsp;</td>
630                         <td>&nbsp;</td>
631                 </tr>
632         <?php endforeach; ?>
633         <?php foreach ($files as $file_info): ?>
634                 <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">
635                         <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>
636                         <td valign="top">
637                                 <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>
638                                 <?php if ($file_info['description']): ?>
639                                         <p class="fm-desc"><?php echo htmlspecialchars($file_info['description']); ?></p>
640                                 <?php endif; ?>
641                         </td>
642                         <td valign="top"><?php echo get_display_name($file_info['member_id']); ?></td>
643                         <td valign="top">
644                                 <?php if ($_config['fs_versioning']): ?>
645                                         <?php if ($file_info['num_revisions']): 
646                                                 if ($file_info['num_revisions'] == 1) {
647                                                         $lang_var = 'fs_revision';
648                                                 } else {
649                                                         $lang_var = 'fs_revisions';
650                                                 }
651                                                 ?>
652                                                 
653                                                 <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>
654                                         <?php else: ?>
655                                                 -
656                                         <?php endif; ?>
657                                 <?php endif; ?>
658                         </td>
659                         <td valign="top">
660                         <?php 
661                         if ($file_info['num_comments'] == 1) {
662                                 $lang_var = 'fs_comment';
663                         } else {
664                                 $lang_var = 'fs_comments';
665                         }
666                         ?>
667                         <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>
668                         <td align="right" valign="top"><?php echo get_human_size($file_info['file_size']); ?></td>
669                         <td align="right" valign="top"><?php echo AT_date(_AT('filemanager_date_format'), $file_info['date'], AT_DATE_MYSQL_DATETIME); ?></td>
670                 </tr>
671         <?php endforeach; ?>
672 <?php else: ?>
673         <tr>
674                 <td colspan="7"><?php echo _AT('none_found'); ?></td>
675         </tr>
676 <?php endif; ?>
677 </tbody>
678 </table>
679 </form>
680 </div>
681 <script type="text/javascript">
682 // <!--
683 function checkbuttons(state) {
684         document.form.selectall.checked = state;
685
686         var num_files_checked = 0;
687         var num_folders_checked = 0;
688         for (var i=0;i<document.form.elements.length;i++) {
689                 var e = document.form.elements[i];
690                 if ((e.name == 'folders[]') && (e.type=='checkbox') && e.checked) {
691                         num_folders_checked++;
692                 } else if ((e.name == 'files[]') && (e.type=='checkbox') && e.checked) {
693                         num_files_checked++;
694                 }
695         }
696         if (num_files_checked + num_folders_checked > 1) {
697                 if (document.form.edit)
698                         document.form.edit.disabled = true;
699         } else {
700                 if (document.form.edit)
701                         document.form.edit.disabled = false;
702         }
703 }
704 function CheckAll() {
705         var state = document.form.selectall.checked;
706         for (var i=0;i<document.form.elements.length;i++)       {
707                 var e = document.form.elements[i];
708                 if ((e.name == 'folders[]') && (e.type=='checkbox')) {
709                         e.checked = state;
710                         rowselectbox(document.getElementById('r_' + e.value + '_1'), state, 'checkbuttons(' + state + ')');
711                 } else if ((e.name == 'files[]') && (e.type=='checkbox')) {
712                         e.checked = state;
713                         rowselectbox(document.getElementById('r_' + e.value + '_0'), state, 'checkbuttons(' + state + ')');
714                 }
715         }
716 }
717
718 function hideform(id) {
719         document.getElementById(id).style.display='none';
720 }
721
722 function toggleform(id) {
723         if (document.getElementById(id).style.display == "none") {
724                 //show
725                 document.getElementById(id).style.display='';   
726
727                 if (id == "c_folder") {
728                         document.form0.new_folder_name.focus();
729                 } else if (id == "upload") {
730                         document.form0.file.focus();
731                 }
732
733         } else {
734                 //hide
735                 document.getElementById(id).style.display='none';
736         }
737 }
738
739 // -->
740 </script>
741
742 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>