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