Exemplo n.º 1
0
    /**
     * Test times for get_parent using recursive get_parent alfresco calls
     * @uses $CFG, $DB
     */
    public function test_get_parent_path_tree() {
        global $CFG, $DB;

        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $options = array(
            'ajax' => false,
            'name' => 'elis files phpunit test',
            'type' => 'elisfiles'
        );

        $repo = new repository_elisfiles('elisfiles', context_system::instance(), $options);

        // Make sure we connected to the repository successfully.
        if (empty($repo->elis_files)) {
            $this->markTestSkipped('Repository not configured or enabled');
        }

        // set up the storage for the full path of the path's UUIDs to validate against
        $expectedpath = array();

        // create folder, get uuid, get path via get_parent_path and elis_files_folder structure
        // for first folder, create under moodle, then create under the previous folder...
        $parentfolderuuid = $repo->elis_files->get_root()->uuid;
        $times = array();
        for ($i = 1; $i <= 20; $i++) {
            $currentfolder = FOLDER_NAME_PREFIX.$i;

            $currentfolderuuid = $repo->elis_files->create_dir($currentfolder, $parentfolderuuid, '', true);

            // add the parent folder to our expected sequence of UUIDs
            $expectedpath[] = repository_elisfiles::build_encodedpath($parentfolderuuid);

            // elis_files_folder_structure get_parent_path test
            $starttime = microtime();
            $folders = elis_files_folder_structure();
            $altrecursivepath = array();
            $repo->get_parent_path($currentfolderuuid, $altrecursivepath, 0, 0, 0, 0, 'tree');
            $endtime = time();
            $structuretime = microtime_diff($starttime, microtime());

            // validate the count
            $this->assertEquals($i, count($altrecursivepath));
            // validate the encoded folder UUIDs

            // look over the expected path parts
            foreach ($expectedpath as $pathindex => $expectedpart) {
                // obtain the matching part from the actual return value
                $resultpart = $altrecursivepath[$pathindex];
                $this->assertEquals($expectedpart, $resultpart['path']);
            }

            // NOTE: add this back in if we are testing performance
            $times[] = $times[] = "Folder: $currentfolder and time: $structuretime";

            // or nested folders
            $parentfolderuuid = $currentfolderuuid;
        }
    }
Exemplo n.º 2
0
    /**
     * Prepares list of files before passing it to AJAX, makes sure data is in the correct
     * format and stores formatted values.
     *
     * @param array|stdClass $listing result of get_listing() or search() or file_get_drafarea_files()
     * @param string         $parent  encoded path of parent folder
     * @uses   $CFG
     * @uses   $OUTPUT
     * @return list((array)listing, count)
     */
    public function prepare_fm_listing($listing, $parent = '') {
        global $CFG, $OUTPUT;

        $locations = null;
        if (is_array($listing) && !empty($listing['locations'])) {
            $locations = &$listing['locations'];
        } else if (is_object($listing) && !empty($listing->locations)) {
            $locations = &$listing->locations;
        }

        if (is_array($listing) && !empty($listing['parent']) && !empty($listing['parent']->uuid)) {
            $parent = repository_elisfiles::build_encodedpath($listing['parent']->uuid);
        } else if (is_object($listing) && !empty($listing->parent) && !empty($listing->parent->uuid)) {
            $parent = repository_elisfiles::build_encodedpath($listing->parent->uuid);
        }

        $defaultfoldericon = $OUTPUT->pix_url(file_folder_icon(64))->out(false);
        // prepare $listing['path'] or $listing->path
        if (is_array($listing) && isset($listing['path']) && is_array($listing['path'])) {
            $path = &$listing['path'];
        } else if (is_object($listing) && isset($listing->path) && is_array($listing->path)) {
            $path = &$listing->path;
        }
        $elisfilesfolder = array();
        $companyhomefolder = array();
        $textpath = '';
        $lastpathvalue = 0;
        if (isset($path)) {
            $len = count($path);
            for ($i = 0; $i < $len; $i++) {
                $pathname = false;
                $pathvalue = false;
                if (is_array($path[$i])) {
                    if (!isset($path[$i]['icon'])) {
                        $path[$i]['icon'] = $defaultfoldericon;
                    }
                    if (!empty($path[$i]['name'])) {
                        $pathname = $path[$i]['name'];
                    }
                    if (!empty($path[$i]['path'])) {
                        $pathvalue = $path[$i]['path'];
                    }
                } else if (is_object($path[$i])) {
                    if (!isset($path[$i]->icon)) {
                        $path[$i]->icon = $defaultfoldericon;
                    }
                    if (!empty($path[$i]->name)) {
                        $pathname = $path[$i]->name;
                    }
                    if (!empty($path[$i]->path)) {
                        $pathvalue = $path[$i]->path;
                    }
                }
                if ($pathname == $this->elis_files->get_root()->title) {
                    $pathname = get_string('repository', 'repository_elisfiles');
                    if (is_array($path[$i])) {
                        $path[$i]['name'] = $pathname;
                    } else {
                        $path[$i]->name = $pathname;
                    }
                    $companyhomefolder['filepath'] = $pathvalue;
                    $companyhomefolder['textpath'] = $pathname;
                    $companyhomefolder['fullname'] = $pathname;
                    $companyhomefolder['id'] = $pathvalue; // TBD
                    $companyhomefolder['sortorder'] = 0; // TBD
                    $companyhomefolder['children'] = array();
                }
                $textpath .= (!empty($textpath) ? '/' : '') . $pathname;
            }
        }

        // prepare $listing['list'] or $listing->list
        if (is_array($listing) && isset($listing['list']) && is_array($listing['list'])) {
            $listing['list'] = array_values($listing['list']); // convert to array
            $files = &$listing['list'];
        } else if (is_object($listing) && isset($listing->list) && is_array($listing->list)) {
            $listing->list = array_values($listing->list); // convert to array
            $files = &$listing->list;
        } else {
            return array($listing, 0); // TBD
        }
        $len = count($files);
        for ($i = 0; $i < $len; $i++) {
            if (is_object($files[$i])) {
                $file = (array)$files[$i];
                $converttoobject = true;
            } else {
                $file = & $files[$i];
                $converttoobject = false;
            }
            if (!empty($parent)) {
                $file['parent'] = $parent;
            }
            if (isset($file['size'])) { // TBD
                $file['size'] = (int)$file['size'];
                $file['size_f'] = display_size($file['size']);
            }
            if (isset($file['license']) &&
                    get_string_manager()->string_exists($file['license'], 'license')) {
                $file['license_f'] = get_string($file['license'], 'license');
            }
            if (isset($file['image_width']) && isset($file['image_height'])) {
                $a = array('width' => $file['image_width'], 'height' => $file['image_height']);
                $file['dimensions'] = get_string('imagesize', 'repository', (object)$a);
            }
            // Map date fields
            foreach (array('datemodified',
                           'datecreated'
                     ) as $key) {
                if (!isset($file[$key]) && isset($file['date'])) {
                    $file[$key] = userdate($file['date']);
                }
                if (isset($file[$key])) {
                    $file[$key.'_f'] = userdate($file[$key], get_string('strftimedatetime', 'langconfig'));
                    $file[$key.'_f_s'] = userdate($file[$key], get_string('strftimedatetimeshort', 'langconfig'));
                }
            }
            // Map other fields
            foreach (array('author' => 'author',
                           'path'  => 'filepath' // TBD
                     ) as $key => $value) {
                if (isset($file[$key])) {
                    $file[$value] = $file[$key];
                } else {
                    $file[$value] = ''; // TBD
                }
            }
            $isfolder = (array_key_exists('children', $file) || (isset($file['type']) && $file['type'] == 'folder'));
            if (!isset($file['type'])) {
                $file['type'] = $isfolder ? 'folder' : 'file';
            }
            $filename = null;
            if (isset($file['title'])) {
                $filename = $file['title'];
            }
            else if (isset($file['fullname'])) {
                $filename = $file['fullname'];
            }
            if ($filename) {
                $file['filename'] = $filename;
                $file['fullname'] = $filename;
            }
            if (isset($file['source'])) {
                $file['url'] = $this->get_link($file['source']);
            }
            //error_log("/repository/elisfiles/lib.php::prepare_listing(): filepath = {$textpath}");
            $file['textpath'] = $textpath; // TBD
            if (!isset($file['mimetype']) && !$isfolder && $filename) {
                $file['mimetype'] = get_mimetype_description(array('filename' => $filename));
            }
            if (!isset($file['icon'])) {
                if ($isfolder) {
                    $file['icon'] = $defaultfoldericon;
                } else if ($filename) {
                    $file['icon'] = $OUTPUT->pix_url(file_extension_icon($filename, 90))->out(false);
                }
            }
            if ($converttoobject) {
                $files[$i] = (object)$file;
            }
        }

        // Now build the entire folder tree, respecting "create" permissions ...
        $folders = elis_files_folder_structure(true);
        $foldertree = array();
        $this->folder_tree_to_fm($foldertree, $folders, $companyhomefolder['textpath']);

        // Must add missing 'ELIS Files' & 'Company Home' locations to tree
        // if permissions allow

        // For this, we care about the "root" node
        $root_uuid = $this->elis_files->get_root()->uuid;
        // Validate whether the current user has proper "site-level create" permissions
        $access_permitted = $this->check_editing_permissions(SITEID, 0, 0, $root_uuid, 0);

        if (!empty($companyhomefolder) && $access_permitted) {
            // We have access to 'Company Home'
            $companyhomefolder['children'] = $foldertree;
            $foldertree = array($companyhomefolder);
        }

        if (ELIS_FILES_DEBUG_TRACE) {
            ob_start();
            var_dump($folders);
            $tmp = ob_get_contents();
            ob_end_clean();
            error_log("/repository/elisfiles/lib.php::prepare_fm_listing(): folders = {$tmp}");
            ob_start();
            var_dump($foldertree);
            $tmp = ob_get_contents();
            ob_end_clean();
            error_log("/repository/elisfiles/lib.php::prepare_fm_listing(): foldertree = {$tmp}");
        }

        if (is_array($listing)) {
            $listing['tree'] = array('children' => $foldertree);
        } else {
            $treeelm = new stdClass;
            $treeelm->children = $foldertree;
            $listing->tree = $treeelm;
        }

        return array($listing, $len);
    }
Exemplo n.º 3
0
/**
 * Obtain the current (i.e. "default") path for the provided course, depending
 * on the configured default browse location
 *
 * @param int $courseid The id of the course whose file picker view we are currently
 *                      on, or SITEID if we are viewing private files
 * @param bool $default True if desire default browsing location
 * @return string The encoded path corresponding to the current location
 */
function elis_files_get_current_path_for_course($courseid, $default = false) {
    global $CFG, $DB, $USER;
    require_once($CFG->dirroot.'/repository/elisfiles/lib.php');

    // Default to the Moodle area
    $currentpath = '/';

    // Determine if the ELIS Files repository is enabled
    $sql = 'SELECT i.name, i.typeid, r.type
            FROM {repository} r
            JOIN {repository_instances} i
            WHERE r.type = ?
            AND i.typeid = r.id';

    $repository = $DB->get_record_sql($sql, array('elisfiles'), IGNORE_MISSING);

    if ($repository) {
        // Initialize repository plugin
        try {
            $ctx = context_user::instance($USER->id);
            $options = array(
                'ajax' => false,
                'name' => $repository->name,
                'type' => 'elisfiles'
            );
            $repo = new repository_elisfiles('elisfiles', $ctx, $options);

            if (!empty($repo->elis_files)) {
                // TBD: Is the following required???
                //$listing = (object)$repo->get_listing(true);
                $uuid = ($courseid == SITEID)
                         ? false // No Site course folder, use default location
                         : $repo->elis_files->get_course_store($courseid);

                // Determine the default browsing location
                $cid    = $courseid;
                $uid    = 0;
                $shared = false;
                $oid    = 0;
                if ($default || empty($uuid)) {
                    $uuid = $repo->elis_files->get_default_browsing_location($cid, $uid, $shared, $oid);
                }

                if ($uuid != false) {
                    // Encode the UUID
                    $currentpath = repository_elisfiles::build_encodedpath($uuid, $uid, $cid, $oid, $shared);
                }
            }
        } catch (Exception $e) {
            // The parent "repository" class may throw exceptions
            // error_log("/repository/elisfiles/lib/lib.php::elis_files_get_current_path_for_course({$courseid}): Exception: ". $e->getMessage());
        }
    }

    return $currentpath;
}