remove old readme
[atutor.git] / docs / mods / _core / file_manager / filemanager_display.inc.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 if (!defined('AT_INCLUDE_PATH')) { exit; }
16
17 function get_file_extension($file_name) {
18         $ext = pathinfo($file_name);
19         return $ext['extension'];
20 }
21
22 function get_file_type_icon($file_name) {
23         static $mime;
24
25         $ext = get_file_extension($file_name);
26
27         if (!isset($mime)) {
28                 require(AT_INCLUDE_PATH .'lib/mime.inc.php');
29         }
30
31         if (isset($mime[$ext]) && $mime[$ext][1]) {
32                 return $mime[$ext][1];
33         }
34         return 'generic';
35 }
36
37 function get_relative_path($src, $dest) {
38         if ($src == '') {
39                 $path = $dest;
40         } else if (substr($dest, 0, strlen($src)) == $src) {
41                 $path = substr($dest, strlen($src) + 1);
42         } else {
43                 $depth = substr_count($src, '/');
44                 for ($i = 0; $i < $depth + 1; $i++)  // $depth+1 because the last '/' is not recorded in content.content_path
45                         $path .= '../';
46                 $path .= $dest;
47         }
48
49         return $path;
50 }
51
52 // get the course total in Bytes 
53 $course_total = dirsize($current_path);
54
55 $framed = intval($_GET['framed']);
56 $popup = intval($_GET['popup']);
57 $cp = $_GET['cp'];
58 $cid = intval($_GET['cid']);        // content id, used at "adapted content" page, => add/edit alternatives
59 $pid = intval($_GET['pid']);        // primary resource id, used at "adapted content" page, => add/edit alternatives
60 $a_type = intval($_GET['a_type']);  // alternative_type, used at "adapted content" page, => add/edit alternatives
61
62 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
63         $get_file = 'get.php/';
64 } else {
65         $get_file = 'content/' . $_SESSION['course_id'] . '/';
66 }
67
68 function fm_path(){
69         global $pathext, $framed, $popup, $cp, $cid, $pid, $a_type;
70 echo '<p>'._AT('current_path').' ';
71
72 if (isset($pathext) && $pathext != '') {
73         echo '<a href="'.$_SERVER['PHP_SELF'].'?popup=' . $popup . SEP . 'framed=' . $framed.SEP . 'cp=' . $cp.SEP . 'cid=' . $cid.SEP . 'pid=' . $pid.SEP . 'a_type=' . $a_type.'">'._AT('home').'</a> ';
74 }
75 else {
76         $pathext = '';
77         echo _AT('home');
78 }
79
80
81 if ($pathext == '' && isset($_POST['pathext'])) {
82
83         $pathext = urlencode($_POST['pathext']);
84 }
85
86 if ($pathext != '') {
87         $bits = explode('/', $pathext);
88
89         foreach ($bits as $bit) {
90                 if ($bit != '') {
91                         $bit_path .= $bit . '/';
92                         echo ' / ';
93
94                         if ($bit_path == $pathext) {
95                                 echo $bit;
96                         } else {
97                                 echo '<a href="'.$_SERVER['PHP_SELF'].'?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">' . $bit . '</a>';
98                         }
99                 }
100         }
101         $bit_path = "";
102         $bit = "";
103 }
104 echo '</p>';
105
106 }
107
108 if ($popup == TRUE) {
109         $totalcol = 6;
110 } else {
111         $totalcol = 5;
112 }
113 $labelcol = 3;
114
115 if (TRUE || $framed != TRUE) {
116
117         if ($_GET['overwrite'] != '') {
118                 // get file name, out of the full path
119                 $path_parts = pathinfo($current_path.$_GET['overwrite']);
120
121                 if (!file_exists($path_parts['dirname'].'/'.$pathext.$path_parts['basename'])
122                         || !file_exists($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5))) {
123                         /* source and/or destination does not exist */
124                         $msg->addErrors('CANNOT_OVERWRITE_FILE');
125                 } else {
126                         @unlink($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
127                         $result = @rename($path_parts['dirname'].'/'.$pathext.$path_parts['basename'], $path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
128
129                         if ($result) {
130                                 $msg->addFeedback('FILE_OVERWRITE');
131                         } else {
132                                 $msg->addErrors('CANNOT_OVERWRITE_FILE');
133                         }
134                 }
135         }
136         
137         // filemanager listing table
138         // make new directory 
139         echo '<div class="input-form"><fieldset class="group_form"><legend class="group_form">'._AT('add_file_folder').'</legend>'."\n";
140         echo '  <div class="row">'."\n";
141         echo '          <form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext != '') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup='.$popup.SEP.'cp='.SEP.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">'."\n";
142         if( $MakeDirOn ) {
143                 if ($depth < $MaxDirDepth) {
144                         echo '          <label for="dirname">'._AT('create_folder_here').'</label><br />'."\n";
145                         echo '          &nbsp;<small class="spacer">'._AT('keep_it_short').'</small><br />'."\n";
146                         echo '          <input type="text" name="dirname" id="dirname" size="20" /> '."\n";
147                         echo '          <input type="hidden" name="mkdir_value" value="true" /> '."\n";
148                         echo '          <input type="submit" name="mkdir" value="'._AT('create_folder').'" class="button" />'."\n";
149                 } else {
150                         echo _AT('depth_reached')."\n";
151                 }
152         }
153         echo '          <input type="hidden" name="pathext" value="'.$pathext.'" />'."\n";
154         echo '          </form>'."\n";
155         echo '  </div>'."\n";
156
157         echo '  <div class="row"><hr /></div>'."\n";
158
159     // If flash is available, provide the option of using Fluid's uploader or the basic uploader
160         if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
161                 echo '<div class="row">'."\n";
162                 if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
163                         $fluid_on = 'checked="checked"';
164                 echo '(<input type="checkbox" id="fluid_on" name="fluid_on" value="yes" '.$fluid_on.' /> '."\n";
165                 echo '<label for="fluid_on" >'._AT('enable_uploader').'</label>)'."\n";
166                 echo '</div>'."\n";
167         }
168         // Create a new file
169         echo '  <div class="row" style="float: left;"><input type="button" class="button" name="new_file" value="' . _AT('file_manager_new') . '" onclick="window.location.href=\''.AT_BASE_HREF.'mods/_core/file_manager/new.php?pathext=' . urlencode($pathext) . SEP . 'framed=' . $framed . SEP . 'popup=' . $popup . '\'"/></div>'."\n";
170
171         $my_MaxCourseSize = $system_courses[$_SESSION['course_id']]['max_quota'];
172
173         // upload file 
174         if (($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED) 
175                 || (($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) && ($course_total < $MaxCourseSize))
176                 || ($my_MaxCourseSize-$course_total > 0)) {
177                 echo '  <div class="row" style="float: left;">'._AT('OR').'</div>'."\n".'       <div class="row" style="float: left;">'."\n";
178                 if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
179                 ?>
180                 <div id="uploader-error-container"></div>
181                         <div id="fluid-container">
182                                 <div id="uploader">
183                                 <!-- Basic upload controls, used when JavaScript is unavailable -->
184         <form method="post" enctype="multipart/form-data" class="fl-progEnhance-basic">
185             <p>Use the Browse button to add a file, and the Save button to upload it.</p>
186             <input name="fileData" type="file" />
187             <input class="fl-uploader-basic-save" type="submit" value="Save"/>
188         </form>
189         
190         <!-- Uploader container -->
191         <form class="flc-uploader fl-uploader fl-progEnhance-enhanced" method="get" enctype="multipart/form-data">
192             
193             <!-- File Queue, which is split up into two separate tables: one for the header and body -->
194             <table class="fl-uploader-header">
195                         <tr>
196                                         <th class="fl-uploader-file-name">File Name</th>
197                                         <th class="fl-uploader-file-size">Size</th>
198                                         <th class="fl-uploader-file-actions"></th>
199                                 </tr>u
200             </table>
201             
202             <!-- File Queue body, which is the default container for the FileQueueView component -->
203             <table summary="The list of files" class="flc-uploader-queue fl-uploader-queue">
204                                 <caption>File Upload Queue:</caption>
205                                 <tbody>
206                                         <!-- Template for file row -->
207                                         <tr class="flc-uploader-file-tmplt flc-uploader-file">
208                                                 <td class="flc-uploader-file-name fl-uploader-file-name">File Name Placeholder</td>
209                                                 <td class="flc-uploader-file-size fl-uploader-file-size">0 KB</td>
210                                                 <td class="fl-uploader-file-actions">
211                                                         <button type="button" class="flc-uploader-file-action" tabindex="-1"></button>
212                                                 </td>
213                                         </tr>
214                                         
215                                         <!-- Template for error info row -->
216                                         <tr class="flc-uploader-file-error-tmplt fl-uploader-file-error">
217                                                 <td colspan="3" class="flc-uploader-file-error"></td>
218                                         </tr>
219                                 </tbody>
220                         </table>
221             
222             <!-- File progress bar template, used to generate progress bars for each file in the queue -->
223             <div class="flc-uploader-file-progressor-tmplt fl-uploader-file-progress"></div>            
224
225             <!-- Initial instructions -->
226             <div class="flc-uploader-browse-instructions fl-uploader-browse-instructions">
227                 Choose <em>Browse files</em> to add files to the queue. 
228             </div>            
229
230             <!-- Status footer -->
231             <div class="flc-uploader-queue-footer fl-uploader-queue-footer fl-fix">
232                 <div class="flc-uploader-total-progress-text fl-uploader-total-progress-text fl-force-left">
233                     Total: 0 files (0 KB)
234                 </div>
235                 <div class="fl-text-align-right fl-force-right">
236                     <span class="flc-uploader-button-browse fl-uploader-browse">
237                         <span class="flc-uploader-button-browse-text">Browse files</span>
238                     </span>
239                 </div>
240                 <!-- Total progress bar -->
241                 <div class="flc-uploader-total-progress fl-uploader-total-progress-okay"></div>
242                 <div class="flc-uploader-errorsPanel fl-uploader-errorsPanel">
243                      <div class="fl-uploader-errorsPanel-header"><span class="flc-uploader-errorPanel-header">Warnings:</span></div>
244     
245                      <!-- The markup for each error section will be rendered into these containers. -->
246                      <div class="flc-uploader-errorPanel-section-fileSize"></div>
247                      <div class="flc-uploader-errorPanel-section-numFiles"></div>
248                      
249                      <!-- Error section template.-->
250                      <div class="flc-uploader-errorPanel-section-tmplt fl-uploader-hidden-templates">
251                          <div class="flc-uploader-errorPanel-section-title fl-uploader-errorPanel-section-title">
252                              x files were too y and were not added to the queue.
253                          </div>
254                          
255                          <div class="flc-uploader-errorPanel-section-details fl-uploader-errorPanel-section-details">
256                              <p>The following files were not added:</p>
257                              <p class="flc-uploader-errorPanel-section-files">file_1, file_2, file_3, file_4, file_5 </p>
258                          </div>
259                          
260                          <button type="button" class="flc-uploader-errorPanel-section-toggleDetails fl-uploader-errorPanel-section-toggleDetails">Hide this list</button>
261                          <button type="button" class="flc-uploader-errorPanel-section-removeButton fl-uploader-errorPanel-section-removeButton">
262                              <span class="flc-uploader-erroredButton-text fl-uploader-hidden">Remove error</span>
263                          </button>
264                      </div>
265                  </div>                
266             </div>
267             
268             <!-- Upload buttons -->
269             <div class="fl-uploader-buttons">
270                 <button type="button" class="flc-uploader-button-pause fl-uploader-button-stop fl-uploader-hidden">Stop Upload</button>
271                 <button type="button" class="flc-uploader-button-upload fl-uploader-button-upload fl-uploader-dim">Upload</button>
272             </div>
273             
274             <div class="flc-uploader-status-region fl-offScreen-hidden"></div>
275         </form>        
276             
277         <script type="text/javascript">
278             var myUploader = fluid.uploader(".flc-uploader", {
279                 queueSettings: {
280                     uploadURL: '<?php echo AT_BASE_HREF; ?>mods/_core/file_manager/upload.php',
281                     fileUploadLimit: 5,
282                     fileQueueLimit: 2,
283                     postParams: {pathext: '<?php echo $pathext; ?>', type: 'ajax', submit: 'submit'},
284                     fileSizeLimit: <?php echo $my_MaxFileSize/1024; ?>
285                 },
286                 events: {
287                     onSuccess: {
288                         event: "onFileSuccess",
289                         args: [
290                             {
291                                 fileName: "{arguments}.0.name",
292                                 responseText: "{arguments}.1"
293                             }
294                         ]
295                     },
296                     onError: {
297                         event: "onFileError",
298                         args: [
299                             {
300                                 fileName: "{arguments}.0.name",
301                                 statusCode: "{arguments}.2",
302                                 responseText: "{arguments}.3.responseText"
303                             }
304                         ]
305                     }
306                 },
307                 listeners: {
308                         onSuccess: function (response){
309                                 // example assumes that the server code passes the new image URL in the serverData
310                                 console.log("Success triggered", response);
311                                 jQuery('#uploader-error-container').html(response.responseText);
312                         }, 
313                         onError: function(response) {
314                         console.log("Error triggered", response);
315                         jQuery('#uploader-error-container').html(response.responseText);
316                     },
317                     onUploadStart: function() {
318                         jQuery('#uploader-error-container').html("");
319                     },
320                     afterUploadComplete: function () {
321                         window.location = "<?php echo AT_BASE_HREF; ?>mods/_core/file_manager/index.php?pathext=<?php echo $pathext; ?>";
322                     }
323                     },
324                     components: {
325                     strategy: {
326                         options: {
327                             flashMovieSettings: {
328                                 flashURL: "<?php echo AT_BASE_HREF; ?>jscripts/infusion/lib/swfupload/flash/swfupload.swf",
329                                 flashButtonImageURL: "<?php echo AT_BASE_HREF; ?>jscripts/infusion/components/uploader/images/browse.png"
330                             }
331                         }
332                     }
333                 }
334             });
335             
336             //bind fluid checkbox
337             jQuery('#fluid_on').bind("click", function() {
338                 toggleform('simple-container', 'fluid-container'); 
339                 setCheckboxCookie(this, 'fluid_on=yes', 'fluid_on=no','December 31, 2099');
340                 console.log('hey');
341             });
342             
343             //hide multifile uploader if it's not checked 
344             if (!jQuery('#fluid_on').attr('checked')) {
345                 jQuery('#fluid-container').hide();
346             }
347         </script>
348                                 </div>
349                         </div>
350                 <?php
351                 if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
352                                 echo '<div id="simple-container" style="display: none;">';
353                         else
354                                 echo '<div id="simple-container">';
355                 } else {
356                         // Display as regular if there's no Flash detected
357                         echo '<div id="simple-container">'."\n";
358                 }
359
360                 // Simple single file uploader
361                 echo '<form onsubmit="openWindow(\''.AT_BASE_HREF.'tools/prog.php\');" name="form1" method="post" action="mods/_core/file_manager/upload.php?popup='.$popup.SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'" enctype="multipart/form-data">';
362                 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$my_MaxFileSize.'" />';
363                 echo '<label for="uploadedfile">'._AT('upload_files').'</label><br />'."\n";
364                 echo '<input type="file" name="uploadedfile" id="uploadedfile" class="formfield" size="20" /> ';
365                 echo '<input type="submit" name="submit" value="'._AT('upload').'" class="button" />';
366                 echo '<input type="hidden" name="pathext" value="'.$pathext.'" />  ';
367
368                 if ($popup == TRUE) {
369                         echo '<input type="hidden" name="popup" value="1" />';
370                 }
371                 echo '</form>';
372                 echo '</div>';
373                 echo '          </div>'."\n".'  </fieldset></div>';
374         } else {
375                 echo '  </fieldset></div>'."\n";
376                 $msg->printInfos('OVER_QUOTA');
377         }
378 }
379
380 // Directory and File listing 
381 echo '<form name="checkform" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : '').'popup='.$popup .SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'" method="post">';
382 echo '<input type="hidden" name="pathext" value ="'.$pathext.'" />';
383
384 // display the section to use a remote URL as an alternative
385 if ($a_type > 0) {
386 ?>
387 <div class="input-form" style="min-height:10px">
388 <fieldset class="group_form" style="min-height: 0px;"><legend class="group_form"><?php echo _AT('use_url_as_alternative'); ?></legend>
389         <div class="row">
390           <input name="remote_alternative" id="remote_alternative" value="http://" size="60" />
391           <input class="button" type="button" name="alternative" value="<?php echo _AT('use_as_alternative'); ?>" onclick="javascript: setURLAlternative();" />
392         </div>
393 </fieldset>
394 </div>
395 <?php }?>
396
397 <table class="data static" summary="" border="0" rules="groups" style="width: 90%">
398 <thead>
399 <tr>
400 <td colspan="5">
401 <?php fm_path(); ?>
402 </td>
403 </tr>
404 <tr>
405         <th scope="col"><input type="checkbox" name="checkall" onclick="Checkall(checkform);" id="selectall" title="<?php echo _AT('select_all'); ?>" /></th>
406         <th>&nbsp;</th>
407         <th scope="col"><?php echo _AT('name');   ?></th>
408         <th scope="col"><?php echo _AT('date');   ?></th>
409         <th scope="col"><?php echo _AT('size');   ?></th>
410 </tr>
411 </thead>
412 <tfoot>
413 <tr>
414         <td colspan="5"><input type="submit" name="rename" value="<?php echo _AT('rename'); ?>" /> 
415                 <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>" /> 
416                 <input type="submit" name="move"   value="<?php echo _AT('move'); ?>" /></td>
417 </tr>
418
419 <tr>
420         <td colspan="4" align="right"><strong><?php echo _AT('directory_total'); ?>:</strong></td>
421         <td align="right">&nbsp;<strong><?php echo get_human_size(dirsize($current_path.$pathext.$file.'/')); ?></strong>&nbsp;</td>
422 </tr>
423
424 <tr>
425         <td colspan="4" align="right"><strong><?php echo _AT('course_total'); ?>:</strong></td>
426         <td align="right">&nbsp;<strong><?php echo get_human_size($course_total); ?></strong>&nbsp;</td>
427 </tr>
428 <tr>
429         <td colspan="4" align="right"><strong><?php echo _AT('course_available'); ?>:</strong></td>
430         <td align="right"><strong><?php
431                 if ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED) {
432                         echo _AT('unlimited');
433                 } else if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
434                         echo get_human_size($MaxCourseSize-$course_total);
435                 } else {
436                         echo get_human_size($my_MaxCourseSize-$course_total);
437                 } ?></strong>&nbsp;</td>
438 </tr>
439 </tfoot>
440 <?php
441
442
443 if($pathext) : ?>
444         <tr>
445                 <td colspan="5"><a href="<?php echo $_SERVER['PHP_SELF'].'?back=1'.SEP.'pathext='.$pathext.SEP. 'popup=' . $popup .SEP. 'framed=' . $framed .SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type; ?>"><img src="images/arrowicon.gif" border="0" height="11" width="10" alt="" /> <?php echo _AT('back'); ?></a></td>
446         </tr>
447 <?php endif; ?>
448 <?php
449 $totalBytes = 0;
450
451 if ($dir == '')
452         $dir=opendir($current_path);
453         
454 // loop through folder to get files and directory listing
455 while (false !== ($file = readdir($dir)) ) {
456
457         // if the name is not a directory 
458         if( ($file == '.') || ($file == '..') ) {
459                 continue;
460         }
461
462         // get some info about the file
463         $filedata = stat($current_path.$pathext.$file);
464         $path_parts = pathinfo($file);
465         $ext = strtolower($path_parts['extension']);
466
467         $is_dir = false;
468
469         // if it is a directory change the file name to a directory link 
470         if(is_dir($current_path.$pathext.$file)) {
471                 $size = dirsize($current_path.$pathext.$file.'/');
472                 $totalBytes += $size;
473                 $filename = '<a href="'.$_SERVER['PHP_SELF'].'?pathext='.urlencode($pathext.$file.'/'). SEP . 'popup=' . $popup . SEP . 'framed='. $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">'.$file.'</a>';
474                 $fileicon = '&nbsp;';
475                 $fileicon .= '<img src="images/folder.gif" alt="'._AT('folder').':'.$file.'" height="18" width="20" class="img-size-fm1" />';
476                 $fileicon .= '&nbsp;';
477                 if(!$MakeDirOn) {
478                         $deletelink = '';
479                 }
480
481                 $is_dir = true;
482         } else if ($ext == 'zip') {
483
484                 $totalBytes += $filedata[7];
485                 $filename = $file;
486                 $fileicon = '&nbsp;<img src="images/icon-zip.gif" alt="'._AT('zip_archive').':'.$file.'" height="16" width="16" border="0" class="img-size-fm2" />&nbsp;';
487
488         } else {
489                 $totalBytes += $filedata[7];
490                 $filename = $file;
491                 $fileicon = '&nbsp;<img src="images/file_types/'.get_file_type_icon($filename).'.gif" height="16" width="16" alt="" title="" class="img-size-fm2" />&nbsp;';
492         } 
493         $file1 = strtolower($file);
494         // create listing for dirctor or file
495         if ($is_dir) {
496                 
497                 $dirs[$file1] .= '<tr><td  align="center" width="0%">';
498                 $dirs[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/></td>';
499                 $dirs[$file1] .= '<td  align="center"><label for="'.$file.'" >'.$fileicon.'</label></td>';
500                 $dirs[$file1] .= '<td >&nbsp;';
501                 $dirs[$file1] .= $filename.'</td>';
502                 $dirs[$file1] .= '<td  align="right">&nbsp;';
503                 $dirs[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], AT_DATE_UNIX_TIMESTAMP);
504                 $dirs[$file1] .= '&nbsp;</td>';
505                 $dirs[$file1] .= '<td  align="right">';
506                 $dirs[$file1] .= get_human_size($size).'</td></tr>';
507
508                 
509         } else {
510                 $files[$file1] .= '<tr> <td  align="center">';
511                 $files[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/> </td>';
512                 $files[$file1] .= '<td  align="center"><label for="'.$file.'">'.$fileicon.'</label></td>';
513                 $files[$file1] .= '<td >&nbsp;';
514
515                 if ($framed) {
516                         $files[$file1] .= '<a href="'.$get_file.$pathext.urlencode($filename).'">'.$filename.'</a>';
517                 } else {
518                         $files[$file1] .= '<a href="mods/_core/file_manager/preview.php?file='.$pathext.$filename.SEP.'pathext='.urlencode($pathext).SEP.'popup='.$popup.'">'.$filename.'</a>';
519                 }
520
521                 if ($ext == 'zip') {
522                         $files[$file1] .= ' <a href="mods/_core/file_manager/zip.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'file=' . urlencode($file) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed .'">';
523                         $files[$file1] .= '<img src="images/archive.gif" border="0" alt="'._AT('extract_archive').'" title="'._AT('extract_archive').'"height="16" width="11" class="img-size-fm3" />';
524                         $files[$file1] .= '</a>';
525                 }
526
527                 if (in_array($ext, $editable_file_types)) {
528                         $files[$file1] .= ' <a href="mods/_core/file_manager/edit.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup=' . $popup . SEP . 'framed=' . $framed . SEP . 'file=' . $file . '">';
529                         $files[$file1] .= '<img src="images/edit.gif" border="0" alt="'._AT('extract_archive').'" title="'._AT('edit').'" height="15" width="18" class="img-size-fm4" />';
530                         $files[$file1] .= '</a>';
531                 }
532
533                 $files[$file1] .= '&nbsp;</td>';
534
535                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
536
537                 if ($popup == TRUE) {
538                         if ($a_type > 0)  // define content alternative
539                         {
540                                 $files[$file1] .= '<input class="button" type="button" name="alternative" value="' ._AT('use_as_alternative') . '" onclick="javascript: setAlternative(\''.get_relative_path($_GET['cp'], $pathext).$file.'\', \''.AT_BASE_HREF.$get_file.$pathext.urlencode($file).'\', \''.$cid.'\', \''.$pid.'\', \''.$a_type.'\');" />&nbsp;';
541                         }
542                         else
543                                 $files[$file1] .= '<input class="button" type="button" name="insert" value="' ._AT('insert') . '" onclick="javascript:insertFile(\'' . $file . '\', \'' . get_relative_path($_GET['cp'], $pathext) . '\', \'' . $ext . '\', \'' .$_SESSION['prefs']['PREF_CONTENT_EDITOR']. '\');" />&nbsp;';
544                 }
545
546                 $files[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], AT_DATE_UNIX_TIMESTAMP);
547                 $files[$file1] .= '&nbsp;</td>';
548                 
549                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
550                 $files[$file1] .= get_human_size($filedata[7]).'</td></tr>';
551         }
552 } // end while
553
554 // sort listing and output directories
555 if (is_array($dirs)) {
556         ksort($dirs, SORT_STRING);
557         foreach($dirs as $x => $y) {
558                 echo $y;
559         }
560 }
561
562 //sort listing and output files
563 if (is_array($files)) {
564         ksort($files, SORT_STRING);
565         foreach($files as $x => $y) {
566                 echo $y;
567         }
568 }
569 echo '</table></form>';
570 ?>
571
572 <script type="text/javascript">
573 //<!--
574 function insertFile(fileName, pathTo, ext, ed_pref) { 
575         // pathTo + fileName should be relative to current path (specified by the Content Package Path)
576
577         if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "png") {
578                 var info = "<?php echo _AT('alternate_text'); ?>";
579                 var html = '<img src="' + pathTo+fileName + '" border="0" alt="' + info + '" />';
580
581                 insertLink(html, ed_pref);
582         } else if (ext == "mpg" || ext == "avi" || ext == "wmv" || ext == "mov" || ext == "swf" || ext == "mp3" || ext == "wav" || ext == "ogg" || ext == "mid" ||ext == "flv"|| ext == "mp4") {
583                 var html = '[media]'+ pathTo + fileName + '[/media]';
584
585                 insertLink(html, ed_pref);
586         } else {
587                 var info = "<?php echo _AT('put_link'); ?>";
588                 var html = '<a href="' + pathTo+fileName + '">' + info + '</a>';
589                 
590                 insertLink(html, ed_pref);
591         }
592 }
593
594 function insertLink(html, ed_pref)
595 {
596         var isVisual = false;
597         
598         if (window.opener) {
599                 if (typeof window.opener.jQuery("#html_visual_editor:checked").val() !== "undefined") {
600                         isVisual = true;
601                 }
602         }
603
604         if (!window.opener || isVisual) {
605                 if (!window.opener && window.parent.tinyMCE)
606                         window.parent.tinyMCE.execCommand('mceInsertContent', false, html);
607                 else
608                         if (window.opener && window.opener.tinyMCE)
609                                 window.opener.tinyMCE.execCommand('mceInsertContent', false, html);
610         } else {
611                 insertAtCursor(window.opener.document.form.body_text, html);
612         }
613 }
614
615 function insertAtCursor(myField, myValue) {
616         //IE support
617         if (window.opener.document.selection) {
618                 myField.focus();
619                 sel = window.opener.document.selection.createRange();
620                 sel.text = myValue;
621         }
622         //MOZILLA/NETSCAPE support
623         else if (myField.selectionStart || myField.selectionStart == '0') {
624                 var startPos = myField.selectionStart;
625                 var endPos = myField.selectionEnd;
626                 myField.value = myField.value.substring(0, startPos)
627                 + myValue
628                 + myField.value.substring(endPos, myField.value.length);
629                 myField.focus();
630         } else {
631                 myField.value += myValue;
632                 myField.focus();
633         }
634 }
635
636 // This function does:
637 // 1. save into db via ajax
638 // 2. set the according field in opener window to the selected file
639 // 3. close file manager itself
640 function setAlternative(file, file_preview_link, cid, pid, a_type) {
641         // HTML encode the name of the secondary resource
642         file = jQuery('<div/>').text(file).html();
643
644         // save the selected secondary resource into db
645         jQuery.post("<?php echo AT_BASE_HREF; ?>mods/_core/editor/save_alternative.php", 
646                         {"pid":pid, "a_type":a_type, "alternative":file}, 
647                         function(data) {});
648
649         link_html = '\
650       <a href="'+file_preview_link+'" title="<?php echo _AT('new_window'); ?>" target="_new">'+file+'</a><br /> \
651       <a href="#" onclick="ATutor.poptastic(\\\'<?php echo AT_BASE_HREF; ?>mods/_core/file_manager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>cp=<?php echo $cp.SEP; ?>cid='+cid+'<?php echo SEP; ?>pid='+pid+'<?php echo SEP; ?>a_type='+a_type+'\\\');return false;" title="<?php echo _AT('new_window'); ?>"> \
652         <img src="<?php echo AT_BASE_HREF; ?>images/home-tests_sm.png" border="0" title="<?php echo _AT('alter'); ?>" alt="<?php echo _AT('alter'); ?>" /> \
653       </a> \
654       <a href="#" onclick="removeAlternative(\\\'<?php echo $cp; ?>\\\', '+cid+','+pid+','+a_type+');return false;"> \
655         <img src="<?php echo AT_BASE_HREF; ?>images/icon_delete.gif" border="0" title="<?php echo _AT('remove'); ?>" alt="<?php echo _AT('remove'); ?>" /> \
656       </a> \
657     </div> \
658 ';
659         eval("window.opener.document.getElementById(\""+pid+"_"+a_type+"\").innerHTML = '"+link_html+"'");
660         
661         window.close();
662 }
663
664 // This function validates the url then call setAlternative()
665 function setURLAlternative() {
666         remote_url = jQuery('#remote_alternative').val();
667         if (remote_url == '' || remote_url == 'http://') {
668                 alert("<?php echo _AT('empty_url'); ?>");
669                 return false;
670         }
671         setAlternative(remote_url, remote_url, '<?php echo $cid; ?>.', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
672 }
673
674 <?php  if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes"): ?>
675 // toggle the view between div object and button
676 function toggleform(id, link) {
677         var obj = document.getElementById(id);
678         var btn = document.getElementById(link);
679
680         if (obj.style.display == "none") {
681                 //show
682                 obj.style.display='';
683                 obj.focus();
684
685                 btn.style.display = 'none';
686
687
688         } else {
689                 //hide
690                 obj.style.display='none';
691                 btn.style.display = '';
692         }
693 }
694
695 // set a cookie
696 function setCheckboxCookie(obj, value1, value2, date)
697 {
698         var today = new Date();
699         var the_date = new Date(date);
700         var the_cookie_date = the_date.toGMTString();
701         if (obj.checked==true)
702                 var the_cookie = value1 + ";expires=" + the_cookie_date;
703         else
704                 var the_cookie = value2 + ";expires=" + the_cookie_date;
705         document.cookie = the_cookie;
706 }
707 <?php endif; ?>
708
709 <?php 
710 // When uploading a file as an alternative content, set the alternative field in the opener window 
711 // and close "file manager" once the upload is successful
712 if ($a_type > 0 && isset($_GET['uploadfile']) && $_GET['uploadfile'] <> '') { ?>
713 function setAlternativeAndClose() {
714         setAlternative('<?php echo get_relative_path($_GET['cp'], $pathext).$_GET['uploadfile']; ?>', '<?php echo AT_BASE_HREF.$get_file.$pathext.urlencode($_GET['uploadfile']); ?>', '<?php echo $cid; ?>', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
715         window.close();
716 }
717
718 window.onload=setAlternativeAndClose;
719 <?php } ?>
720
721 //-->
722 </script>