示例#1
0
文件: lib.php 项目: isuruAb/moodle
 /**
  * Builds a tree of files This function is then called recursively.
  *
  * @static
  * @todo take $search into account, and respect a threshold for dynamic loading
  * @param file_info $fileinfo an object returned by file_browser::get_file_info()
  * @param string $search searched string
  * @param bool $dynamicmode no recursive call is done when in dynamic mode
  * @param array $list the array containing the files under the passed $fileinfo
  * @return int the number of files found
  */
 public static function build_tree($fileinfo, $search, $dynamicmode, &$list)
 {
     global $CFG, $OUTPUT;
     $filecount = 0;
     $children = $fileinfo->get_children();
     foreach ($children as $child) {
         $filename = $child->get_visible_name();
         $filesize = $child->get_filesize();
         $filesize = $filesize ? display_size($filesize) : '';
         $filedate = $child->get_timemodified();
         $filedate = $filedate ? userdate($filedate) : '';
         $filetype = $child->get_mimetype();
         if ($child->is_directory()) {
             $path = array();
             $level = $child->get_parent();
             while ($level) {
                 $params = $level->get_params();
                 $path[] = array($params['filepath'], $level->get_visible_name());
                 $level = $level->get_parent();
             }
             $tmp = array('title' => $child->get_visible_name(), 'size' => 0, 'date' => $filedate, 'path' => array_reverse($path), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false));
             //if ($dynamicmode && $child->is_writable()) {
             //    $tmp['children'] = array();
             //} else {
             // if folder name matches search, we send back all files contained.
             $_search = $search;
             if ($search && stristr($tmp['title'], $search) !== false) {
                 $_search = false;
             }
             $tmp['children'] = array();
             $_filecount = repository::build_tree($child, $_search, $dynamicmode, $tmp['children']);
             if ($search && $_filecount) {
                 $tmp['expanded'] = 1;
             }
             //}
             if (!$search || $_filecount || stristr($tmp['title'], $search) !== false) {
                 $filecount += $_filecount;
                 $list[] = $tmp;
             }
         } else {
             // not a directory
             // skip the file, if we're in search mode and it's not a match
             if ($search && stristr($filename, $search) === false) {
                 continue;
             }
             $params = $child->get_params();
             $source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']));
             $list[] = array('title' => $filename, 'size' => $filesize, 'date' => $filedate, 'source' => base64_encode($source), 'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false), 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false));
             $filecount++;
         }
     }
     return $filecount;
 }
示例#2
0
 /**
  * Constructor of moodle_file_tree_viewer class
  * @param file_info $file_info
  * @param array $options
  */
 public function __construct(file_info $file_info, array $options = null)
 {
     global $CFG;
     //note: this MUST NOT use get_file_storage() !!!!!!!!!!!!!!!!!!!!!!!!!!!!
     $this->options = (array) $options;
     $this->context = $options['context'];
     $this->tree = array();
     $children = $file_info->get_children();
     $current_file_params = $file_info->get_params();
     $parent_info = $file_info->get_parent();
     $level = $parent_info;
     $this->path = array();
     while ($level) {
         $params = $level->get_params();
         $context = context::instance_by_id($params['contextid']);
         // $this->context is current context
         if ($context->id != $this->context->id or empty($params['filearea'])) {
             break;
         }
         // unset unused parameters
         unset($params['component']);
         unset($params['filearea']);
         unset($params['filename']);
         unset($params['itemid']);
         $url = new moodle_url('/files/index.php', $params);
         $this->path[] = html_writer::link($url, $level->get_visible_name());
         $level = $level->get_parent();
     }
     $this->path = array_reverse($this->path);
     if ($current_file_params['filepath'] != '/') {
         $this->path[] = $file_info->get_visible_name();
     }
     foreach ($children as $child) {
         $filedate = $child->get_timemodified();
         $filesize = $child->get_filesize();
         $mimetype = $child->get_mimetype();
         $params = $child->get_params();
         unset($params['component']);
         unset($params['filearea']);
         unset($params['filename']);
         unset($params['itemid']);
         $fileitem = array('params' => $params, 'filename' => $child->get_visible_name(), 'mimetype' => $child->get_mimetype(), 'filedate' => $filedate ? $filedate : '', 'filesize' => $filesize ? $filesize : '');
         $url = new moodle_url('/files/index.php', $params);
         if ($child->is_directory()) {
             $fileitem['isdir'] = true;
             $fileitem['url'] = $url->out(false);
         } else {
             $fileitem['url'] = $child->get_url();
         }
         $this->tree[] = $fileitem;
     }
 }