Beispiel #1
0
 /**
  * Return is the instance is visible
  * (is the type visible ? is the context enable ?)
  *
  * @return bool
  */
 public function is_visible()
 {
     $type = repository::get_type_by_id($this->options['typeid']);
     $instanceoptions = repository::static_function($type->get_typename(), 'get_instance_option_names');
     if ($type->get_visible()) {
         //if the instance is unique so it's visible, otherwise check if the instance has a enabled context
         if (empty($instanceoptions) || $type->get_contextvisibility(context::instance_by_id($this->instance->contextid))) {
             return true;
         }
     }
     return false;
 }
Beispiel #2
0
    }
}
admin_externalpage_setup($pagename, '', null, new moodle_url('/admin/repositoryinstances.php'));
require_capability('moodle/site:config', $context);
$baseurl = new moodle_url("/{$CFG->admin}/repositoryinstance.php", array('sesskey' => sesskey()));
$parenturl = new moodle_url("/{$CFG->admin}/repository.php", array('sesskey' => sesskey(), 'action' => 'edit'));
if ($new) {
    $parenturl->param('repos', $new);
} else {
    $parenturl->param('repos', $type);
}
$return = true;
if (!empty($edit) || !empty($new)) {
    if (!empty($edit)) {
        $instance = repository::get_instance($edit);
        $instancetype = repository::get_type_by_id($instance->options['typeid']);
        $classname = 'repository_' . $instancetype->get_typename();
        $configs = $instance->get_instance_option_names();
        $plugin = $instancetype->get_typename();
        $typeid = $instance->options['typeid'];
    } else {
        $plugin = $new;
        $typeid = null;
        $instance = null;
    }
    // display the edit form for this instance
    $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid, 'instance' => $instance, 'contextid' => $context->id));
    // end setup, begin output
    if ($mform->is_cancelled()) {
        redirect($parenturl);
        exit;
    if (!($course = $DB->get_record('course', array('id' => $usercourseid)))) {
        print_error('invalidcourseid');
    }
    $currenttab = 'repositories';
    include $CFG->dirroot . '/user/tabs.php';
}
print_heading($pagename);
$return = true;
if (!empty($edit) || !empty($new)) {
    if (!empty($edit)) {
        $instance = repository::get_instance($edit);
        //if you try to edit an instance set as readonly, display an error message
        if ($instance->readonly) {
            throw new repository_exception('readonlyinstance', 'repository');
        }
        $instancetype = repository::get_type_by_id($instance->typeid);
        $classname = 'repository_' . $instancetype->get_typename();
        $configs = $instance->get_instance_option_names();
        $plugin = $instancetype->get_typename();
        $typeid = $instance->typeid;
    } else {
        $plugin = $new;
        $typeid = $new;
        $instance = null;
    }
    /// Create edit form for this instance
    $mform = new repository_instance_form('', array('plugin' => $plugin, 'typeid' => $typeid, 'instance' => $instance, 'contextid' => $contextid));
    /// Process the form data if any, or display
    if ($mform->is_cancelled()) {
        redirect($baseurl);
        exit;
Beispiel #4
0
 /**
  * get_real_path
  *
  * @param object $file
  * @param object $hotpot (optional, default=null)
  * @return string
  */
 public static function get_real_path($file, $hotpot = null)
 {
     global $CFG, $PAGE;
     // sanity check
     if (empty($file)) {
         return '';
     }
     // set default path (= cached file in filedir)
     $hash = $file->get_contenthash();
     $path = $CFG->dataroot . '/filedir/' . $hash[0] . $hash[1] . '/' . $hash[2] . $hash[3] . '/' . $hash;
     if (!method_exists($file, 'get_repository_id')) {
         return $path;
         // Moodle <= 2.2
     }
     if (!($repositoryid = $file->get_repository_id())) {
         return $path;
         // shoudn't happen !!
     }
     if (!($repository = repository::get_repository_by_id($repositoryid, $PAGE->context))) {
         return $path;
         // shouldn't happen
     }
     // get repository $type
     switch (true) {
         case isset($repository->options['type']):
             $type = $repository->options['type'];
             break;
         case isset($repository->instance->typeid):
             $type = repository::get_type_by_id($repository->instance->typeid);
             $type = $type->get_typename();
             break;
         default:
             $type = '';
             // shouldn't happen !!
     }
     // get path according to repository $type
     switch ($type) {
         case 'filesystem':
             if (method_exists($repository, 'get_rootpath')) {
                 $path = $repository->get_rootpath() . '/' . $file->get_reference();
             } else {
                 if (isset($repository->root_path)) {
                     $path = $repository->root_path . '/' . $file->get_reference();
                 }
             }
             break;
         case 'user':
         case 'coursefiles':
             // use the the default $path
             break;
     }
     return $path;
 }
Beispiel #5
0
/**
 * Gets main file in a file area
 *
 * if the main file is a link from an external repository
 * look for the target file in the main file's repository
 * Note: this functionality only exists in Moodle 2.3+
 *
 * @param stdclass $context
 * @param string $component 'mod_hotpot'
 * @param string $filearea  'sourcefile', 'entrytext' or 'exittext'
 * @param string $filepath  despite the name, this is a dir path with leading and trailing "/"
 * @param string $filename
 * @param array $file_record
 * @return stdclass if external file found, false otherwise
 */
function hotpot_pluginfile_externalfile($context, $component, $filearea, $filepath, $filename, $file_record)
{
    // get file storage
    $fs = get_file_storage();
    // get main file for this $component/$filearea
    // typically this will be the HotPot quiz file
    $mainfile = hotpot_pluginfile_mainfile($context, $component, $filearea);
    // get repository - cautiously :-)
    if (!$mainfile) {
        return false;
        // no main file - shouldn't happen !!
    }
    if (!method_exists($mainfile, 'get_repository_id')) {
        return false;
        // no file linking in Moodle 2.0 - 2.2
    }
    if (!($repositoryid = $mainfile->get_repository_id())) {
        return false;
        // $mainfile is not from an external repository
    }
    if (!($repository = repository::get_repository_by_id($repositoryid, $context))) {
        return false;
        // $repository is not accessible in this context - shouldn't happen !!
    }
    // get repository type
    switch (true) {
        case isset($repository->options['type']):
            $type = $repository->options['type'];
            break;
        case isset($repository->instance->typeid):
            $type = repository::get_type_by_id($repository->instance->typeid);
            $type = $type->get_typename();
            break;
        default:
            $type = '';
            // shouldn't happen !!
    }
    // set paths (within repository) to required file
    // how we do this depends on the repository $typename
    // "filesystem" path is in plain text, others are encoded
    $mainreference = $mainfile->get_reference();
    switch ($type) {
        case 'filesystem':
            $maindirname = dirname($mainreference);
            $encodepath = false;
            break;
        case 'user':
        case 'coursefiles':
            $params = file_storage::unpack_reference($mainreference, true);
            $maindirname = $params['filepath'];
            $encodepath = true;
            break;
        default:
            echo 'unknown repository type in hotpot_pluginfile_externalfile(): ' . $type;
            die;
    }
    // remove leading and trailing "/" from dir names
    $maindirname = trim($maindirname, '/');
    $dirname = trim($filepath, '/');
    // assume path to target dir is same as path to main dir
    $path = explode('/', $maindirname);
    // traverse back up folder hierarchy if necessary
    $count = count(explode('/', $dirname));
    array_splice($path, -$count);
    // reconstruct expected dir path for source file
    if ($dirname) {
        $path[] = $dirname;
    }
    $source = $path;
    $source[] = $filename;
    $source = implode('/', $source);
    $path = implode('/', $path);
    // filepaths in the repository to search for the file
    $paths = array();
    // add to the list of possible paths
    $paths[$path] = $source;
    if ($dirname) {
        $paths[$dirname] = $dirname . '/' . $filename;
    }
    if ($maindirname) {
        $paths[$maindirname] = $maindirname . '/' . $filename;
    }
    if ($maindirname && $dirname) {
        $paths[$maindirname . '/' . $dirname] = $maindirname . '/' . $dirname . '/' . $filename;
        $paths[$dirname . '/' . $maindirname] = $dirname . '/' . $maindirname . '/' . $filename;
    }
    // add leading and trailing "/" to dir names
    $dirname = $dirname == '' ? '/' : '/' . $dirname . '/';
    $maindirname = $maindirname == '' ? '/' : '/' . $maindirname . '/';
    // locate $dirname within $maindirname
    // typically it will be absent or occur just once,
    // but it could possibly occur several times
    $search = '/' . preg_quote($dirname, '/') . '/i';
    if (preg_match_all($search, $maindirname, $matches, PREG_OFFSET_CAPTURE)) {
        $i_max = count($matches[0]);
        for ($i = 0; $i < $i_max; $i++) {
            list($match, $start) = $matches[0][$i];
            $path = substr($maindirname, 0, $start) . $match;
            $path = trim($path, '/');
            // e.g. hp6.2/html_files
            $paths[$path] = $path . '/' . $filename;
        }
    }
    // setup $params for path encoding, if necessary
    $params = array();
    if ($encodepath) {
        $listing = $repository->get_listing();
        if (isset($listing['list'][0]['path'])) {
            $params = file_storage::unpack_reference($listing['list'][0]['path'], true);
        }
    }
    foreach ($paths as $path => $source) {
        if (!hotpot_pluginfile_dirpath_exists($path, $repository, $encodepath, $params)) {
            continue;
        }
        if ($encodepath) {
            $params['filepath'] = '/' . $path . ($path == '' ? '' : '/');
            $params['filename'] = '.';
            // "." signifies a directory
            $path = file_storage::pack_reference($params);
        }
        $listing = $repository->get_listing($path);
        foreach ($listing['list'] as $file) {
            if (empty($file['source'])) {
                continue;
                // a directory - shouldn't happen !!
            }
            if ($encodepath) {
                $file['source'] = file_storage::unpack_reference($file['source']);
                $file['source'] = trim($file['source']['filepath'], '/') . '/' . $file['source']['filename'];
            }
            if ($file['source'] == $source) {
                if ($encodepath) {
                    $params['filename'] = $filename;
                    $source = file_storage::pack_reference($params);
                }
                if ($file = $fs->create_file_from_reference($file_record, $repositoryid, $source)) {
                    return $file;
                }
                break;
                // couldn't create file, so give up and try a different $path
            }
        }
    }
    // external file not found (or found but not created)
    return false;
}
Beispiel #6
0
function xmldb_hotpot_locate_externalfile($contextid, $component, $filearea, $itemid, $filepath, $filename)
{
    global $CFG, $DB;
    if (!class_exists('repository')) {
        return false;
        // Moodle <= 2.2 has no repositories
    }
    static $repositories = null;
    if ($repositories === null) {
        $exclude_types = array('recent', 'upload', 'user', 'areafiles');
        $repositories = repository::get_instances();
        foreach (array_keys($repositories) as $id) {
            if (method_exists($repositories[$id], 'get_typename')) {
                $type = $repositories[$id]->get_typename();
            } else {
                $type = $repositories[$id]->options['type'];
            }
            if (in_array($type, $exclude_types)) {
                unset($repositories[$id]);
            }
        }
        // ensure upgraderunning is set
        if (empty($CFG->upgraderunning)) {
            $CFG->upgraderunning = null;
        }
    }
    // get file storage
    $fs = get_file_storage();
    // the following types repository use encoded params
    $encoded_types = array('user', 'areafiles', 'coursefiles');
    foreach ($repositories as $id => $repository) {
        // "filesystem" path is in plain text, others are encoded
        if (method_exists($repositories[$id], 'get_typename')) {
            $type = $repositories[$id]->get_typename();
        } else {
            $type = $repositories[$id]->options['type'];
        }
        $encodepath = in_array($type, $encoded_types);
        // save $root_path, because it may get messed up by
        // $repository->get_listing($path), if $path is non-existant
        if (method_exists($repository, 'get_rootpath')) {
            $root_path = $repository->get_rootpath();
        } else {
            if (isset($repository->root_path)) {
                $root_path = $repository->root_path;
            } else {
                $root_path = false;
            }
        }
        // get repository type
        switch (true) {
            case isset($repository->options['type']):
                $type = $repository->options['type'];
                break;
            case isset($repository->instance->typeid):
                $type = repository::get_type_by_id($repository->instance->typeid);
                $type = $type->get_typename();
                break;
            default:
                $type = '';
                // shouldn't happen !!
        }
        $path = $filepath;
        $source = trim($filepath . $filename, '/');
        // setup $params for path encoding, if necessary
        $params = array();
        if ($encodepath) {
            $listing = $repository->get_listing();
            switch (true) {
                case isset($listing['list'][0]['source']):
                    $param = 'source';
                    break;
                    // file
                // file
                case isset($listing['list'][0]['path']):
                    $param = 'path';
                    break;
                    // dir
                // dir
                default:
                    return false;
                    // shouldn't happen !!
            }
            $params = file_storage::unpack_reference($listing['list'][0][$param], true);
            $params['filepath'] = '/' . $path . ($path == '' ? '' : '/');
            $params['filename'] = '.';
            // "." signifies a directory
            $path = file_storage::pack_reference($params);
        }
        // reset $repository->root_path (filesystem repository only)
        if ($root_path) {
            $repository->root_path = $root_path;
        }
        // unset upgraderunning because it can cause get_listing() to fail
        $upgraderunning = $CFG->upgraderunning;
        $CFG->upgraderunning = null;
        // Note: we use "@" to suppress warnings in case $path does not exist
        $listing = @$repository->get_listing($path);
        // restore upgraderunning flag
        $CFG->upgraderunning = $upgraderunning;
        // check each file to see if it is the one we want
        foreach ($listing['list'] as $file) {
            switch (true) {
                case isset($file['source']):
                    $param = 'source';
                    break;
                    // file
                // file
                case isset($file['path']):
                    $param = 'path';
                    break;
                    // dir
                // dir
                default:
                    continue;
                    // shouldn't happen !!
            }
            if ($encodepath) {
                $file[$param] = file_storage::unpack_reference($file[$param]);
                $file[$param] = trim($file[$param]['filepath'], '/') . '/' . $file[$param]['filename'];
            }
            if ($file[$param] == $source) {
                if ($encodepath) {
                    $params['filename'] = $filename;
                    $source = file_storage::pack_reference($params);
                }
                $file_record = array('contextid' => $contextid, 'component' => $component, 'filearea' => $filearea, 'sortorder' => 0, 'itemid' => 0, 'filepath' => $filepath, 'filename' => $filename);
                if ($file = $fs->create_file_from_reference($file_record, $id, $source)) {
                    return $file;
                }
                break;
                // try another repository
            }
        }
    }
    // external file not found (or found but not created)
    return false;
}