diff --git a/repository/lib.php b/repository/lib.php index 4418218..d57b23a 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -549,20 +549,19 @@ abstract class repository { /** * Return all repository types ordered by sortorder field * first repository type in returnedarray[0], second repository type in returnedarray[1], ... - * @global object $DB - * @global object $CFG + * + * @staticvar array $types Used to statically cache the initialised repository types * @param boolean $visible can return types by visiblity, return all types if null * @return array Repository types */ public static function get_types($visible=null) { global $DB, $CFG; - $types = array(); - $params = null; - if (!empty($visible)) { - $params = array('visible' => $visible); - } - if ($records = $DB->get_records('repository',$params,'sortorder')) { + static $types; + + if (!is_array($types)) { + $types = array(); + $records = $DB->get_records('repository', null, 'sortorder'); foreach($records as $type) { if (file_exists($CFG->dirroot . '/repository/'. $type->type .'/lib.php')) { $types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder); @@ -570,6 +569,16 @@ abstract class repository { } } + if ($visible) { + $visibletypes = array(); + foreach ($types as $type) { + if ($type->get_visible()) { + $visibletypes[] = $type; + } + } + return $visibletypes; + } + return $types; } @@ -709,25 +718,33 @@ abstract class repository { /** * 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 + * + * @staticvar array $editabletypes Used to statically cache the result of this function + * @param stdClass $context the context for which we want the editable types * @return array types */ public static function get_editable_types($context = null) { + static $editabletypes; + + if (is_array($editabletypes)) { + return $editabletypes; + } if (empty($context)) { $context = get_system_context(); } - $types= repository::get_types(true); + $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)) { - $editabletypes[]=$type; + $editabletypes[] = $type; } } } + return $editabletypes; }