';
+ $addable = 0;
+
+ //if no type is set, we can create all type of instance
+ if (!$typename) {
+ $instancehtml .= '
';
+ $instancehtml .= get_string('createrepository', 'repository');
+ $instancehtml .= '
';
+
+ } else {
+ $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
+ if (!empty($instanceoptionnames)) { //create a unique type of instance
+ $addable = 1;
+ $instancehtml .= "
";
+ }
+ }
+
+ if ($addable) {
+ $instancehtml .= '
';
+ $output .= $instancehtml;
+ }
+
+ $output .= print_box_end(true);
+
+ //print the list + creation links
+ print($output);
+ }
/**
* 1. Initialize context and options
* 2. Accept necessary parameters
@@ -627,9 +1175,9 @@ abstract class repository {
* (is the type visible ? is the context enable ?)
* @return boolean
*/
- public function is_visible() {
- $type = repository_get_type_by_id($this->typeid);
- $instanceoptions = repository_static_function($type->get_typename(), 'get_instance_option_names');
+ public function is_visible() {
+ $type = repository::get_type_by_id($this->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
@@ -733,7 +1281,7 @@ abstract class repository {
if (!empty($id)) {
unset($options['name']);
- $instance = repository_get_instance($id);
+ $instance = repository::get_instance($id);
$instance->set_option($options);
return $id;
} else {
@@ -920,7 +1468,7 @@ abstract class repository {
/**
* Search files in repository
* When doing global search, $search_text will be used as
- * keyword.
+ * keyword.
*
* @return mixed, see get_listing()
*/
@@ -1000,7 +1548,7 @@ abstract class repository {
*/
public function type_config_form(&$mform) {
}
-
+
/**
* Edit/Create Instance Settings Moodle form
* @param object $ Moodle form (passed by reference)
@@ -1042,466 +1590,6 @@ abstract class repository {
class repository_exception extends moodle_exception {
}
-/**
- * Check context
- * @param int $ctx_id
- * @return boolean
- */
-function repository_check_context($ctx_id) {
- global $USER;
-
- $context = get_context_instance_by_id($ctx_id);
- $level = $context->contextlevel;
-
- if ($level == CONTEXT_COURSE) {
- if (!has_capability('moodle/course:view', $context)) {
- return false;
- } else {
- return true;
- }
- } else if ($level == CONTEXT_USER) {
- $c = get_context_instance(CONTEXT_USER, $USER->id);
- if ($c->id == $ctx_id) {
- return true;
- } else {
- return false;
- }
- } else if ($level == CONTEXT_SYSTEM) {
- // it is always ok in system level
- return true;
- }
- return false;
-}
-
-/**
- * Return all types that you a user can create/edit and which are also visible
- * Note: Mostly used in order to know if at least one editable type can be set
- * @param object $context the context for which we want the editable types
- * @return array types
- */
-function repository_get_editable_types($context = null) {
-
- if (empty($context)) {
- $context = get_system_context();
- }
-
- $types= repository_get_types(true);
- $editabletypes = array();
- foreach ($types as $type) {
- $instanceoptionnames = repository_static_function($type->get_typename(), 'get_instance_option_names');
- if (!empty($instanceoptionnames)) {
- if ($type->get_contextvisibility($context->contextlevel)) {
- $editabletypes[]=$type;
- }
- }
- }
- return $editabletypes;
-}
-
-/**
- * Return repository instances
- * @global object $DB
- * @global object $CFG
- * @global object $USER
- * @param object $contexts contexts for which the instances are set
- * @param integer $userid
- * @param boolean $onlyvisible if visible == true, return visible instances only,
- * otherwise, return all instances
- * @param string $type a type name to retrieve
- * @param string $filetypes supported file types
- * @param string $returnvalue supportted returned value
- * @return array repository instances
- */
-function repository_get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $filetypes = '*', $returnvalue = '*') {
- global $DB, $CFG, $USER;
-
- $params = array();
- $sql = 'SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible FROM {repository} r, {repository_instances} i WHERE ';
- $sql .= 'i.typeid = r.id ';
-
- if (!empty($userid) && is_numeric($userid)) {
- $sql .= ' AND (i.userid = 0 or i.userid = ?)';
- $params[] = $userid;
- }
-
- foreach ($contexts as $context) {
- if (empty($firstcontext)) {
- $firstcontext = true;
- $sql .= ' AND ((i.contextid = ?)';
- } else {
- $sql .= ' OR (i.contextid = ?)';
- }
- $params[] = $context->id;
- }
-
- if (!empty($firstcontext)) {
- $sql .=')';
- }
-
- if ($onlyvisible == true) {
- $sql .= ' AND (r.visible = 1)';
- }
-
- if (isset($type)) {
- $sql .= ' AND (r.type = ?)';
- $params[] = $type;
- }
- $sql .= ' order by r.sortorder, i.name';
-
- if (!$repos = $DB->get_records_sql($sql, $params)) {
- $repos = array();
- }
-
- $ret = array();
- foreach ($repos as $repo) {
- require_once($CFG->dirroot . '/repository/'. $repo->repositorytype.'/repository.class.php');
- $options['visible'] = $repo->visible;
- $options['name'] = $repo->name;
- $options['type'] = $repo->repositorytype;
- $options['typeid'] = $repo->typeid;
- $classname = 'repository_' . $repo->repositorytype;//
-
- $repository = new $classname($repo->id, $repo->contextid, $options, $repo->readonly);
- if ($filetypes !== '*' and $repository->supported_mimetype() !== '*') {
- $mimetypes = $repository->supported_mimetype();
- $is_supported = false;
- foreach ($mimetypes as $type) {
- if (in_array($type, $filetypes)) {
- $is_supported = true;
- }
- }
- if (!$is_supported) {
- continue;
- }
- }
- if ($returnvalue !== '*' and $repository->supported_return_value() !== '*') {
- $tmp = $repository->supported_return_value();
- if ($tmp == $returnvalue) {
- continue;
- }
- }
- if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) {
- $ret[] = $repository;
- }
- }
- return $ret;
-}
-
-/**
- * Get single repository instance
- * @global object $DB
- * @global object $CFG
- * @param integer $id repository id
- * @return object repository instance
- */
-function repository_get_instance($id) {
- global $DB, $CFG;
- $sql = 'SELECT i.*, r.type AS repositorytype, r.visible FROM {repository} r, {repository_instances} i WHERE ';
- $sql .= 'i.typeid = r.id AND ';
- $sql .= 'i.id = '.$id;
-
- if(!$instance = $DB->get_record_sql($sql)) {
- return false;
- }
- require_once($CFG->dirroot . '/repository/'. $instance->repositorytype
- . '/repository.class.php');
- $classname = 'repository_' . $instance->repositorytype;
- $options['typeid'] = $instance->typeid;
- $options['type'] = $instance->repositorytype;
- $options['name'] = $instance->name;
- return new $classname($instance->id, $instance->contextid, $options, $instance->readonly);
-}
-
-/**
- * call a static function
- * @global