From: nicolasconnault Date: Wed, 26 Nov 2008 07:03:10 +0000 (+0000) Subject: MDL_13766 Refactoring of core repository code and added first unit tests X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=dbc01944584df2da22c3e9091b2f82b0c78cc4cd;p=moodle.git MDL_13766 Refactoring of core repository code and added first unit tests --- diff --git a/admin/mnet/adminlib.php b/admin/mnet/adminlib.php index 2725cfb701..3577e958ee 100644 --- a/admin/mnet/adminlib.php +++ b/admin/mnet/adminlib.php @@ -46,7 +46,7 @@ function mnet_get_functions($type, $parentname) { $relname = '/repository/' . $parentname . '/'. $docname; $filename = $CFG->dirroot . $relname; require_once($CFG->dirroot . '/repository/lib.php'); - $publishes = (array)repository_static_function($parentname, 'mnet_publishes'); + $publishes = (array)repository::static_function($parentname, 'mnet_publishes'); } else { // auth or enrol $relname = '/'.$type.'/'.$parentname.'/'.$docname; diff --git a/admin/repository.php b/admin/repository.php index cc17335ca0..88a732d631 100644 --- a/admin/repository.php +++ b/admin/repository.php @@ -39,7 +39,7 @@ $return = true; if (!empty($edit) || !empty($new)) { if (!empty($edit)) { - $repositorytype = repository_get_type_by_typename($edit); + $repositorytype = repository::get_type_by_typename($edit); $classname = 'repository_' . $repositorytype->get_typename(); $configs = call_user_func(array($classname,'get_type_option_names')); $plugin = $repositorytype->get_typename(); @@ -56,7 +56,7 @@ if (!empty($edit) || !empty($new)) { //detect if we create a new type without config (in this case if don't want to display a setting page during creation) $createnewtype = false; if (!empty($new)) { - $adminconfignames = repository_static_function($new, 'get_type_option_names'); + $adminconfignames = repository::static_function($new, 'get_type_option_names'); $createnewtype = empty($adminconfignames); } // end setup, begin output @@ -72,7 +72,7 @@ if (!empty($edit) || !empty($new)) { foreach($configs as $config) { $settings[$config] = $fromform->$config; } - $instanceoptionnames = repository_static_function($edit, 'get_instance_option_names'); + $instanceoptionnames = repository::static_function($edit, 'get_instance_option_names'); if (!empty($instanceoptionnames)) { if (array_key_exists('enablecourseinstances', $fromform)) { $settings['enablecourseinstances'] = $fromform->enablecourseinstances; @@ -108,8 +108,8 @@ if (!empty($edit) || !empty($new)) { print_heading(get_string('configplugin', 'repository_'.$plugin)); $displaysettingform = true; if ($edit) { - $typeoptionnames = repository_static_function($edit, 'get_type_option_names'); - $instanceoptionnames = repository_static_function($edit, 'get_instance_option_names'); + $typeoptionnames = repository::static_function($edit, 'get_type_option_names'); + $instanceoptionnames = repository::static_function($edit, 'get_instance_option_names'); if (empty($typeoptionnames) && empty($instanceoptionnames)) { $displaysettingform = false; } @@ -123,9 +123,9 @@ if (!empty($edit) || !empty($new)) { //display instances list and creation form if ($edit){ - $instanceoptionnames = repository_static_function($edit, 'get_instance_option_names'); + $instanceoptionnames = repository::static_function($edit, 'get_instance_option_names'); if (!empty($instanceoptionnames)){ - repository_display_instances_list(get_context_instance(CONTEXT_SYSTEM), $edit); + repository::display_instances_list(get_context_instance(CONTEXT_SYSTEM), $edit); } } @@ -134,12 +134,12 @@ if (!empty($edit) || !empty($new)) { if (!confirm_sesskey()) { print_error('confirmsesskeybad', '', $baseurl); } - $repositorytype = repository_get_type_by_typename($hide); + $repositorytype = repository::get_type_by_typename($hide); $repositorytype->switch_and_update_visibility(); $return = true; } else if (!empty($delete)) { admin_externalpage_print_header(); - $repositorytype = repository_get_type_by_typename($delete); + $repositorytype = repository::get_type_by_typename($delete); if ($sure) { if (!confirm_sesskey()) { print_error('confirmsesskeybad', '', $baseurl); @@ -157,7 +157,7 @@ if (!empty($edit) || !empty($new)) { $return = false; } else if (!empty($move) && !empty($type)) { - $repositorytype = repository_get_type_by_typename($type); + $repositorytype = repository::get_type_by_typename($type); $repositorytype->move_order($move); } diff --git a/admin/repositoryinstance.php b/admin/repositoryinstance.php index 812489032c..62800563e2 100644 --- a/admin/repositoryinstance.php +++ b/admin/repositoryinstance.php @@ -42,8 +42,8 @@ $return = true; if (!empty($edit) || !empty($new)) { if (!empty($edit)) { - $instance = repository_get_instance($edit); - $instancetype = repository_get_type_by_id($instance->typeid); + $instance = repository::get_instance($edit); + $instancetype = repository::get_type_by_id($instance->typeid); $classname = 'repository_' . $instancetype->get_typename(); $configs = $instance->get_instance_option_names(); $plugin = $instancetype->get_typename(); @@ -75,7 +75,7 @@ if (!empty($edit) || !empty($new)) { } $success = $instance->set_option($settings); } else { - $success = repository_static_function($plugin, 'create', $plugin, 0, get_system_context(), $fromform); + $success = repository::static_function($plugin, 'create', $plugin, 0, get_system_context(), $fromform); $data = data_submitted(); } if ($success) { @@ -99,14 +99,14 @@ if (!empty($edit) || !empty($new)) { if (!confirm_sesskey()) { print_error('confirmsesskeybad', '', $baseurl); } - $instance = repository_get_type_by_typename($hide); + $instance = repository::get_type_by_typename($hide); var_dump($instance); var_dump($hide); $instance->hide(); $return = true; -} else if (!empty($delete)) { +} else if (!empty($delete)) { admin_externalpage_print_header(); - $instance = repository_get_instance($delete); + $instance = repository::get_instance($delete); //if you try to delete an instance set as readonly, display an error message if ($instance->readonly) { throw new repository_exception('readonlyinstance', 'repository'); @@ -129,7 +129,7 @@ if (!empty($edit) || !empty($new)) { } if (!empty($return)) { - + redirect($baseurl); } -admin_externalpage_print_footer(); \ No newline at end of file +admin_externalpage_print_footer(); diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 583381184f..cb9edd5362 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -228,12 +228,12 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) $ADMIN->add('repositorysettings', new admin_externalpage('repositoryinstanceedit', get_string('editrepositoryinstance', 'repository'), $url, 'moodle/site:config', true), '', $url); - foreach (repository_get_types() + foreach (repository::get_types() as $repositorytype) { //display setup page for plugins with: general options or multiple instances (e.g. has instance config) - $typeoptionnames = repository_static_function($repositorytype->get_typename(), 'get_type_option_names'); - $instanceoptionnames = repository_static_function($repositorytype->get_typename(), 'get_instance_option_names'); + $typeoptionnames = repository::static_function($repositorytype->get_typename(), 'get_type_option_names'); + $instanceoptionnames = repository::static_function($repositorytype->get_typename(), 'get_instance_option_names'); if (!empty($typeoptionnames) || !empty($instanceoptionnames)) { $ADMIN->add('repositorysettings', new admin_externalpage('repositorysettings'.$repositorytype->get_typename(), diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php index c3aa4189b5..64939f8b6e 100644 --- a/blocks/admin/block_admin.php +++ b/blocks/admin/block_admin.php @@ -188,7 +188,7 @@ class block_admin extends block_list { /// Repository Instances require_once($CFG->dirroot.'/repository/lib.php'); - $editabletypes = repository_get_editable_types($context); + $editabletypes = repository::get_editable_types($context); if ($course->id !== SITEID && has_capability('moodle/course:update', $context) && !empty($editabletypes)) { $this->content->items[]=''.get_string('repositories').''; $this->content->icons[]=''; diff --git a/lib/adminlib.php b/lib/adminlib.php index f4bc86df4b..89d0ac81c8 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -6012,7 +6012,7 @@ class admin_setting_managerepository extends admin_setting { return true; } } - foreach (repository_get_types() as $instance) { + foreach (repository::get_types() as $instance) { $title = $instance->get_typename(); if (strpos($textlib->strtolower($title), $query) !== false) { return true; @@ -6030,7 +6030,7 @@ class admin_setting_managerepository extends admin_setting { $hiddenstr = get_string('hiddenshow', 'repository'); $deletestr = get_string('delete'); $plugins = get_list_of_plugins('repository'); - $instances = repository_get_types(); + $instances = repository::get_types(); $instancesnumber = count($instances); $alreadyplugins = array(); $table = new StdClass; @@ -6041,8 +6041,8 @@ class admin_setting_managerepository extends admin_setting { foreach ($instances as $i) { $settings = ''; //display edit link only if you can config the type or if it has multiple instances (e.g. has instance config) - $typeoptionnames = repository_static_function($i->get_typename(), 'get_type_option_names'); - $instanceoptionnames = repository_static_function($i->get_typename(), 'get_instance_option_names'); + $typeoptionnames = repository::static_function($i->get_typename(), 'get_type_option_names'); + $instanceoptionnames = repository::static_function($i->get_typename(), 'get_instance_option_names'); if ( !empty($typeoptionnames) || !empty($instanceoptionnames)) { $settings .= '' . $settingsstr .'' . "\n"; diff --git a/lib/simpletest/testrepositorylib.php b/lib/simpletest/testrepositorylib.php new file mode 100644 index 0000000000..bb63c3456f --- /dev/null +++ b/lib/simpletest/testrepositorylib.php @@ -0,0 +1,67 @@ +dirroot . '/repository/lib.php'); +require_once($CFG->dirroot . '/admin/generator.php'); + +// Generate a mock class for each plugin subclass present +$repository_plugins = get_list_of_plugins('repository'); + +foreach ($repository_plugins as $plugin) { + require_once($CFG->dirroot . "/repository/$plugin/repository.class.php"); + Mock::generatePartial("repository_$plugin", "partialmock_$plugin", array('send_package')); +} + +class repositorylib_test extends MoodleUnitTestCase { + + function setup() { + parent::setup(); + } + + public function test_plugins() { + $plugins = get_list_of_plugins('repository'); + + foreach ($plugins as $plugin) { + // Instantiate a fake plugin instance + $plugin_class = "partialmock_$plugin"; + $plugin = new $plugin_class(&$this); + + // add common plugin tests here + } + } +} +?> diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index 3aaed3a9f2..1bca8df601 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -271,7 +271,7 @@ class repository_flickr extends repository { $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40')); //retrieve the flickr instances - $instances = repository_get_instances(array(),null,false,"flickr"); + $instances = repository::get_instances(array(),null,false,"flickr"); if (empty($instances)) { $callbackurl = get_string("callbackwarning","repository_flickr"); $mform->addElement('static', null, '', $callbackurl); @@ -280,7 +280,7 @@ class repository_flickr extends repository { $callbackurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$instances[0]->id; $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl)); } - + $mform->addRule('api_key', $strrequired, 'required', null, 'client'); $mform->addRule('secret', $strrequired, 'required', null, 'client'); } diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index a1599c4f0c..fa571597e8 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -335,8 +335,8 @@ class repository_flickr_public extends repository { */ public static function plugin_init() { //here we create a default instance for this type - repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null)); - } + repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => get_string('repositoryname', 'repository_flickr_public'),'email_address' => null),1); + } } diff --git a/repository/javascript.php b/repository/javascript.php index 4b78d006c9..2a2f26ffa6 100644 --- a/repository/javascript.php +++ b/repository/javascript.php @@ -90,6 +90,15 @@ function repository_get_client($context, $filetypes = '*', $returnvalue = '*') { .fp-grid .label{height:48px;text-align:center} .fp-grid span{color:gray} + + EOD; $js = <<id); -$repos = repository_get_instances(array($user_context, $context, get_system_context()), null, true, null, $filetypes, $returnvalue); +$repos = repository::get_instances(array($user_context, $context, get_system_context())); foreach ($repos as $repo) { $info = $repo->ajax_info(); $js .= "\r\n"; diff --git a/repository/lib.php b/repository/lib.php index ba7be14011..fc66a0ec62 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -103,7 +103,7 @@ class repository_type { * @return boolean */ public function get_contextvisibility($contextlevel) { - + if ($contextlevel == CONTEXT_COURSE) { return $this->_options['enablecourseinstances']; } @@ -115,7 +115,7 @@ class repository_type { //the context is SITE return true; } - + /** @@ -136,7 +136,7 @@ class repository_type { //set options attribut $this->_options = array(); - $options = repository_static_function($typename,'get_type_option_names'); + $options = repository::static_function($typename,'get_type_option_names'); //check that the type can be setup if (!empty($options)) { //set the type options @@ -153,13 +153,13 @@ class repository_type { } else { $this->_options['enablecourseinstances'] = 0; } - + if (array_key_exists('enableuserinstances',$typeoptions)) { $this->_options['enableuserinstances'] = $typeoptions['enableuserinstances']; } else { $this->_options['enableuserinstances'] = 0; } - + } /** @@ -238,15 +238,15 @@ class repository_type { //if the plugin type has no multiple instance (e.g. has no instance option name) so it wont //be possible for the administrator to create a instance //in this case we need to create an instance - $instanceoptionnames = repository_static_function($this->_typename, 'get_instance_option_names'); + $instanceoptionnames = repository::static_function($this->_typename, 'get_instance_option_names'); if (empty($instanceoptionnames)) { $instanceoptions = array(); $instanceoptions['name'] = $this->_typename; - repository_static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions); + repository::static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions); } //run init function - if (!repository_static_function($this->_typename, 'plugin_init')) { + if (!repository::static_function($this->_typename, 'plugin_init')) { throw new repository_exception('cannotcreatetype', 'repository'); } @@ -332,7 +332,7 @@ class repository_type { public function move_order($move) { global $DB; - $types = repository_get_types(); // retrieve all types + $types = repository::get_types(); // retrieve all types /// retrieve this type into the returned array $i = 0; @@ -386,7 +386,7 @@ class repository_type { global $DB; //delete all instances of this type - $instances = repository_get_instances(array(),null,false,$this->_typename); + $instances = repository::get_instances(array(),null,false,$this->_typename); foreach ($instances as $instance) { $instance->delete(); } @@ -400,62 +400,6 @@ class repository_type { } } -/** - * Return a type for a given type name. - * @global object $DB - * @param string $typename the type name - * @return integer - */ -function repository_get_type_by_typename($typename) { - global $DB; - - if (!$record = $DB->get_record('repository',array('type' => $typename))) { - return false; - } - - return new repository_type($typename, (array)get_config($typename), $record->visible, $record->sortorder); -} - -/** - * Return a type for a given type id. - * @global object $DB - * @param string $typename the type name - * @return integer - */ -function repository_get_type_by_id($id) { - global $DB; - - if (!$record = $DB->get_record('repository',array('id' => $id))) { - return false; - } - - return new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder); -} - -/** - * Return all repository types ordered by sortorder - * first type in returnedarray[0], second type in returnedarray[1], ... - * @global object $DB - * @param boolean $visible can return types by visiblity, return all types if null - * @return array Repository types - */ -function repository_get_types($visible=null) { - global $DB; - - $types = array(); - $params = null; - if (!empty($visible)) { - $params = array('visible' => $visible); - } - if ($records = $DB->get_records('repository',$params,'sortorder')) { - foreach($records as $type) { - $types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder); - } - } - - return $types; -} - /** * This is the base class of the repository class * @@ -489,6 +433,610 @@ abstract class repository { public $options; public $readonly; + /** + * Return a type for a given type name. + * @global object $DB + * @param string $typename the type name + * @return integer + */ + public static function get_type_by_typename($typename) { + global $DB; + + if (!$record = $DB->get_record('repository',array('type' => $typename))) { + return false; + } + + return new repository_type($typename, (array)get_config($typename), $record->visible, $record->sortorder); + } + + /** + * Return a type for a given type id. + * @global object $DB + * @param string $typename the type name + * @return integer + */ + public static function get_type_by_id($id) { + global $DB; + + if (!$record = $DB->get_record('repository',array('id' => $id))) { + return false; + } + + return new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder); + } + + /** + * Return all repository types ordered by sortorder + * first type in returnedarray[0], second type in returnedarray[1], ... + * @global object $DB + * @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; + + $types = array(); + $params = null; + if (!empty($visible)) { + $params = array('visible' => $visible); + } + if ($records = $DB->get_records('repository',$params,'sortorder')) { + foreach($records as $type) { + $types[] = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder); + } + } + + return $types; + } + + /** + * Check context + * @param int $ctx_id + * @return boolean + */ + public static function 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 + */ + public static function 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 + * @return array repository instances + */ + public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null) { + 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 (!$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 + */ + public static function 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 $CFG + * @param $plugin + * @param $function + * @param type $nocallablereturnvalue default value if function not found + * it's mostly used when you don't want to display an error but + * return a boolean + * @return + */ + public static function static_function($plugin, $function) { + global $CFG; + + //check that the plugin exists + $typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/repository.class.php'; + if (!file_exists($typedirectory)) { + throw new repository_exception('invalidplugin', 'repository'); + } + + $pname = null; + if (is_object($plugin) || is_array($plugin)) { + $plugin = (object)$plugin; + $pname = $plugin->name; + } else { + $pname = $plugin; + } + + $args = func_get_args(); + if (count($args) <= 2) { + $args = array(); + } + else { + array_shift($args); + array_shift($args); + } + + require_once($typedirectory); + return call_user_func_array(array('repository_' . $plugin, $function), $args); + } + + /** + * Move file from download folder to file pool using FILE API + * @global object $DB + * @global object $CFG + * @global object $USER + * @param string $path file path in download folder + * @param string $name file name + * @param integer $itemid item id to identify a file in filepool + * @param string $filearea file area + * @return array information of file in file pool + */ + public static function move_to_filepool($path, $name, $itemid, $filearea = 'user_draft') { + global $DB, $CFG, $USER; + $context = get_context_instance(CONTEXT_USER, $USER->id); + $now = time(); + $entry = new object(); + $entry->filearea = $filearea; + $entry->contextid = $context->id; + $entry->filename = $name; + $entry->filepath = '/'.uniqid().'/'; + $entry->timecreated = $now; + $entry->timemodified = $now; + if(is_numeric($itemid)) { + $entry->itemid = $itemid; + } else { + $entry->itemid = 0; + } + $entry->mimetype = mimeinfo('type', $path); + $entry->userid = $USER->id; + $fs = get_file_storage(); + $browser = get_file_browser(); + if ($file = $fs->create_file_from_pathname($entry, $path)) { + $delete = unlink($path); + $ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); + if(!empty($ret)) { + return array('url'=>$ret->get_url(),'id'=>$file->get_itemid(), 'file'=>$file->get_filename()); + } else { + return null; + } + } else { + return null; + } + } + + public static function download_btn($repo_id, $ctx_id, $sesskey, $title, $src, $returnurl = '') { + global $CFG; + if (empty($returnurl)) { + $returnurl = get_referer(); + } + $str = '
'; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= ' '; + $str .= '
'; + return $str; + } + + /** + * Save file to local filesystem pool + * @param string $elname name of element + * @param string $filearea + * @param string $filepath + * @param string $filename - use specified filename, if not specified name of uploaded file used + * @param bool $override override file if exists + * @return mixed stored_file object or false if error; may throw exception if duplicate found + */ + public static function store_to_filepool($elname, $filearea='user_draft', $filepath='/', $filename = '', $override = false) { + global $USER; + if (!isset($_FILES[$elname])) { + return false; + } + + if (!$filename) { + $filename = $_FILES[$elname]['name']; + } + $context = get_context_instance(CONTEXT_USER, $USER->id); + $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); + $fs = get_file_storage(); + $browser = get_file_browser(); + + if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { + if ($override) { + $file->delete(); + } else { + return false; + } + } + + $file_record = new object(); + $file_record->contextid = $context->id; + $file_record->filearea = $filearea; + $file_record->itemid = $itemid; + $file_record->filepath = $filepath; + $file_record->filename = $filename; + $file_record->userid = $USER->id; + + $file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']); + $info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); + $ret = array('url'=>$info->get_url(),'id'=>$itemid, 'file'=>$file->get_filename()); + return $ret; + } + + /** + * Return the user arborescence in a format to be returned by the function get_listing + * @global $CFG + * @param $search + * @return + */ + public static function get_user_file_tree($search = ""){ + global $CFG; + $ret = array(); + $ret['nologin'] = true; + $ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary + $browser = get_file_browser(); + $itemid = null; + $filename = null; + $filearea = null; + $path = '/'; + $ret['dynload'] = false; + + if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) { + + $ret['path'] = array(); + $params = $fileinfo->get_params(); + $filearea = $params['filearea']; + $ret['path'][] = repository::encode_path($filearea, $path, $fileinfo->get_visible_name()); + if ($fileinfo->is_directory()) { + $level = $fileinfo->get_parent(); + while ($level) { + $params = $level->get_params(); + $ret['path'][] = repository::encode_path($params['filearea'], $params['filepath'], $level->get_visible_name()); + $level = $level->get_parent(); + } + } + $filecount = repository::build_tree($fileinfo, $search, $ret['dynload'], $ret['list']); + $ret['path'] = array_reverse($ret['path']); + } + + if (empty($ret['list'])) { + //exit(mnet_server_fault(9016, get_string('emptyfilelist', 'repository_local'))); + throw new Exception('emptyfilelist'); + } else { + return $ret; + } + + } + + /** + * + * @param $filearea + * @param $path + * @param $visiblename + * @return + */ + public static function encode_path($filearea, $path, $visiblename) { + return array('path'=>serialize(array($filearea, $path)), 'name'=>$visiblename); + } + + /** + * Builds a tree of files This function is + * then called recursively. + * + * @param $fileinfo an object returned by file_browser::get_file_info() + * @param $search searched string + * @param $dynamicmode bool no recursive call is done when in dynamic mode + * @param $list - the array containing the files under the passed $fileinfo + * @returns int the number of files found + * + * todo: take $search into account, and respect a threshold for dynamic loading + */ + public static function build_tree($fileinfo, $search, $dynamicmode, &$list) { + global $CFG; + + $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[] = repository::encode_path($params['filearea'], $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' => $CFG->pixpath .'/f/folder.gif' + ); + + //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; + } + + //} + + //Uncomment this following line if you wanna display all directory ()even empty + //if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) { + if ($_filecount) { + $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['filearea'], $params['itemid'], $params['filepath'], $params['filename'])); + $list[] = array( + 'title' => $filename, + 'size' => $filesize, + 'date' => $filedate, + //'source' => $child->get_url(), + 'source' => base64_encode($source), + 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo_from_type("icon", $filetype) + ); + $filecount++; + } + } + + return $filecount; + } + + + /** + * Display a repository instance list (with edit/delete/create links) + * @global object $CFG + * @global object $USER + * @param object $context the context for which we display the instance + * @param string $typename if set, we display only one type of instance + */ + public static function display_instances_list($context, $typename = null) { + global $CFG, $USER; + + $output = print_box_start('generalbox','',true); + //if the context is SYSTEM, so we call it from administration page + $admin = ($context->id == SYSCONTEXTID) ? true : false; + if ($admin) { + $baseurl = $CFG->httpswwwroot . '/admin/repositoryinstance.php?sesskey=' . sesskey(); + $output .= "

" . get_string('siteinstances', 'repository') . " "; + $output .= "

"; + } else { + $baseurl = $CFG->httpswwwroot . '/repository/manage_instances.php?contextid=' . $context->id . '&sesskey=' . sesskey(); + } + + $namestr = get_string('name'); + $pluginstr = get_string('plugin', 'repository'); + $settingsstr = get_string('settings'); + $deletestr = get_string('delete'); + $updown = get_string('updown', 'repository'); + //retrieve list of instances. In administration context we want to display all + //instances of a type, even if this type is not visible. In course/user context we + //want to display only visible instances, but for every type types. The repository_get_instances() + //third parameter displays only visible type. + $instances = repository::get_instances(array($context),null,!$admin,$typename); + $instancesnumber = count($instances); + $alreadyplugins = array(); + + $table = new StdClass; + $table->head = array($namestr, $pluginstr, $deletestr, $settingsstr); + $table->align = array('left', 'left', 'center','center'); + $table->data = array(); + + $updowncount = 1; + + foreach ($instances as $i) { + $settings = ''; + $delete = ''; + $settings .= '' . $settingsstr . '' . "\n"; + if (!$i->readonly) { + $delete .= '' . $deletestr . '' . "\n"; + } + + $type = repository::get_type_by_id($i->typeid); + $table->data[] = array($i->name, $type->get_readablename(), $delete, $settings); + + //display a grey row if the type is defined as not visible + if (isset($type) && !$type->get_visible()) { + $table->rowclass[] = 'dimmed_text'; + } else { + $table->rowclass[] = ''; + } + + if (!in_array($i->name, $alreadyplugins)) { + $alreadyplugins[] = $i->name; + } + } + $output .= print_table($table, true); + $instancehtml = '
'; + $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 $CFG - * @param $plugin - * @param $function - * @param type $nocallablereturnvalue default value if function not found - * it's mostly used when you don't want to display an error but - * return a boolean - * @return - */ -function repository_static_function($plugin, $function) { - global $CFG; - - //check that the plugin exists - $typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/repository.class.php'; - if (!file_exists($typedirectory)) { - throw new repository_exception('invalidplugin', 'repository'); - } - - $pname = null; - if (is_object($plugin) || is_array($plugin)) { - $plugin = (object)$plugin; - $pname = $plugin->name; - } else { - $pname = $plugin; - } - - $args = func_get_args(); - if (count($args) <= 2) { - $args = array(); - } - else { - array_shift($args); - array_shift($args); - } - - require_once($typedirectory); - return call_user_func_array(array('repository_' . $plugin, $function), $args); -} - -/** - * Move file from download folder to file pool using FILE API - * @global object $DB - * @global object $CFG - * @global object $USER - * @param string $path file path in download folder - * @param string $name file name - * @param integer $itemid item id to identify a file in filepool - * @param string $filearea file area - * @return array information of file in file pool - */ -function repository_move_to_filepool($path, $name, $itemid, $filearea = 'user_draft') { - global $DB, $CFG, $USER; - $context = get_context_instance(CONTEXT_USER, $USER->id); - $now = time(); - $entry = new object(); - $entry->filearea = $filearea; - $entry->contextid = $context->id; - $entry->filename = $name; - $entry->filepath = '/'.uniqid().'/'; - $entry->timecreated = $now; - $entry->timemodified = $now; - if(is_numeric($itemid)) { - $entry->itemid = $itemid; - } else { - $entry->itemid = 0; - } - $entry->mimetype = mimeinfo('type', $path); - $entry->userid = $USER->id; - $fs = get_file_storage(); - $browser = get_file_browser(); - if ($file = $fs->create_file_from_pathname($entry, $path)) { - $delete = unlink($path); - $ret = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - if(!empty($ret)) { - return array('url'=>$ret->get_url(),'id'=>$file->get_itemid(), 'file'=>$file->get_filename()); - } else { - return null; - } - } else { - return null; - } -} - -function repository_download_btn($repo_id, $ctx_id, $sesskey, $title, $src, $returnurl = '') { - global $CFG; - if (empty($returnurl)) { - $returnurl = get_referer(); - } - $str = '
'; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= ' '; - $str .= '
'; - return $str; -} - -/** - * Save file to local filesystem pool - * @param string $elname name of element - * @param string $filearea - * @param string $filepath - * @param string $filename - use specified filename, if not specified name of uploaded file used - * @param bool $override override file if exists - * @return mixed stored_file object or false if error; may throw exception if duplicate found - */ -function repository_store_to_filepool($elname, $filearea='user_draft', $filepath='/', $filename = '', $override = false) { - global $USER; - if (!isset($_FILES[$elname])) { - return false; - } - - if (!$filename) { - $filename = $_FILES[$elname]['name']; - } - $context = get_context_instance(CONTEXT_USER, $USER->id); - $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); - $fs = get_file_storage(); - $browser = get_file_browser(); - - if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) { - if ($override) { - $file->delete(); - } else { - return false; - } - } - - $file_record = new object(); - $file_record->contextid = $context->id; - $file_record->filearea = $filearea; - $file_record->itemid = $itemid; - $file_record->filepath = $filepath; - $file_record->filename = $filename; - $file_record->userid = $USER->id; - - $file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']); - $info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename()); - $ret = array('url'=>$info->get_url(),'id'=>$itemid, 'file'=>$file->get_filename()); - return $ret; -} - -/** - * Return the user arborescence in a format to be returned by the function get_listing - * @global $CFG - * @param $search - * @return - */ -function repository_get_user_file_tree($search = ""){ - global $CFG; - $ret = array(); - $ret['nologin'] = true; - $ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary - $browser = get_file_browser(); - $itemid = null; - $filename = null; - $filearea = null; - $path = '/'; - $ret['dynload'] = false; - - if ($fileinfo = $browser->get_file_info(get_system_context(), $filearea, $itemid, $path, $filename)) { - - $ret['path'] = array(); - $params = $fileinfo->get_params(); - $filearea = $params['filearea']; - $ret['path'][] = repository_encode_path($filearea, $path, $fileinfo->get_visible_name()); - if ($fileinfo->is_directory()) { - $level = $fileinfo->get_parent(); - while ($level) { - $params = $level->get_params(); - $ret['path'][] = repository_encode_path($params['filearea'], $params['filepath'], $level->get_visible_name()); - $level = $level->get_parent(); - } - } - $filecount = repository_build_tree($fileinfo, $search, $ret['dynload'], $ret['list']); - $ret['path'] = array_reverse($ret['path']); - } - - if (empty($ret['list'])) { - //exit(mnet_server_fault(9016, get_string('emptyfilelist', 'repository_local'))); - throw new Exception('emptyfilelist'); - } else { - return $ret; - } - -} - - /** - * - * @param $filearea - * @param $path - * @param $visiblename - * @return - */ - function repository_encode_path($filearea, $path, $visiblename) { - return array('path'=>serialize(array($filearea, $path)), 'name'=>$visiblename); - } - - /** - * Builds a tree of files This function is - * then called recursively. - * - * @param $fileinfo an object returned by file_browser::get_file_info() - * @param $search searched string - * @param $dynamicmode bool no recursive call is done when in dynamic mode - * @param $list - the array containing the files under the passed $fileinfo - * @returns int the number of files found - * - * todo: take $search into account, and respect a threshold for dynamic loading - */ - function repository_build_tree($fileinfo, $search, $dynamicmode, &$list) { - global $CFG; - - $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[] = repository_encode_path($params['filearea'], $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' => $CFG->pixpath .'/f/folder.gif' - ); - - //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; - } - - //} - - //Uncomment this following line if you wanna display all directory ()even empty - //if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) { - if ($_filecount) { - $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['filearea'], $params['itemid'], $params['filepath'], $params['filename'])); - $list[] = array( - 'title' => $filename, - 'size' => $filesize, - 'date' => $filedate, - //'source' => $child->get_url(), - 'source' => base64_encode($source), - 'thumbnail' => $CFG->pixpath .'/f/'. mimeinfo_from_type("icon", $filetype) - ); - $filecount++; - } - } - - return $filecount; - } /** @@ -1540,7 +1628,7 @@ final class repository_instance_form extends moodleform { //add fields if (!$this->instance) { - $result = repository_static_function($this->plugin, 'instance_config_form', $mform); + $result = repository::static_function($this->plugin, 'instance_config_form', $mform); } else { $data = array(); @@ -1609,14 +1697,14 @@ final class repository_type_form extends moodleform { // let the plugin add its specific fields if (!$this->instance) { - $result = repository_static_function($this->plugin, 'type_config_form', $mform); + $result = repository::static_function($this->plugin, 'type_config_form', $mform); } else { $classname = 'repository_' . $this->instance->get_typename(); $result = call_user_func(array($classname,'type_config_form'),$mform); } //add "enable course/user instances" checkboxes if multiple instances are allowed - $instanceoptionnames = repository_static_function($this->plugin, 'get_instance_option_names'); + $instanceoptionnames = repository::static_function($this->plugin, 'get_instance_option_names'); if (!empty($instanceoptionnames)){ $mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', 'repository')); $mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', 'repository')); @@ -1641,115 +1729,9 @@ final class repository_type_form extends moodleform { } $this->set_data($data); } - + $this->add_action_buttons(true, get_string('save','repository')); } } -/** - * Display a repository instance list (with edit/delete/create links) - * @global object $CFG - * @global object $USER - * @param object $context the context for which we display the instance - * @param string $typename if set, we display only one type of instance - */ -function repository_display_instances_list($context, $typename = null) { - global $CFG, $USER; - - $output = print_box_start('generalbox','',true); - //if the context is SYSTEM, so we call it from administration page - $admin = ($context->id == SYSCONTEXTID) ? true : false; - if ($admin) { - $baseurl = $CFG->httpswwwroot . '/admin/repositoryinstance.php?sesskey=' . sesskey(); - $output .= "

" . get_string('siteinstances', 'repository') . " "; - $output .= "

"; - } else { - $baseurl = $CFG->httpswwwroot . '/repository/manage_instances.php?contextid=' . $context->id . '&sesskey=' . sesskey(); - } - - $namestr = get_string('name'); - $pluginstr = get_string('plugin', 'repository'); - $settingsstr = get_string('settings'); - $deletestr = get_string('delete'); - $updown = get_string('updown', 'repository'); - //retrieve list of instances. In administration context we want to display all - //instances of a type, even if this type is not visible. In course/user context we - //want to display only visible instances, but for every type types. The repository_get_instances() - //third parameter displays only visible type. - $instances = repository_get_instances(array($context),null,!$admin,$typename); - $instancesnumber = count($instances); - $alreadyplugins = array(); - - $table = new StdClass; - $table->head = array($namestr, $pluginstr, $deletestr, $settingsstr); - $table->align = array('left', 'left', 'center','center'); - $table->data = array(); - - $updowncount = 1; - - foreach ($instances as $i) { - $settings = ''; - $delete = ''; - $settings .= '' . $settingsstr . '' . "\n"; - if (!$i->readonly) { - $delete .= '' . $deletestr . '' . "\n"; - } - - $type = repository_get_type_by_id($i->typeid); - $table->data[] = array($i->name, $type->get_readablename(), $delete, $settings); - - //display a grey row if the type is defined as not visible - if (isset($type) && !$type->get_visible()) { - $table->rowclass[] = 'dimmed_text'; - } else { - $table->rowclass[] = ''; - } - - if (!in_array($i->name, $alreadyplugins)) { - $alreadyplugins[] = $i->name; - } - } - $output .= print_table($table, true); - $instancehtml = '
'; - $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); -} diff --git a/repository/local/repository.class.php b/repository/local/repository.class.php index ca37dcaad5..bc5c3c95b2 100755 --- a/repository/local/repository.class.php +++ b/repository/local/repository.class.php @@ -77,15 +77,15 @@ class repository_local extends repository { */ public function get_listing($encodedpath = '', $search = '') { global $CFG; - + try { - return repository_get_user_file_tree($search); + return repository::get_user_file_tree($search); } catch (Exception $e) { throw new repository_exception('emptyfilelist', 'repository_local'); } } - + /** * Download a file, this function can be overridden by * subclass. @@ -121,10 +121,10 @@ class repository_local extends repository { $fs = get_file_storage(); $sf = $fs->get_file($contextid, $filearea, $itemid, $filepath, $filename); $contents = $sf->get_content(); - $fp = fopen($dir.$file, 'w'); + $fp = fopen($dir.$file, 'w'); fwrite($fp,$contents); fclose($fp); - + return $dir.$file; } @@ -143,4 +143,4 @@ class repository_local extends repository { return get_string('repositoryname', 'repository_local');; } } -?> \ No newline at end of file +?> diff --git a/repository/manage_instances.php b/repository/manage_instances.php index d4b96d903d..a0876e54a8 100644 --- a/repository/manage_instances.php +++ b/repository/manage_instances.php @@ -55,13 +55,13 @@ /// Security: we cannot perform any action if the type is not visible or if the context has been disabled if (!empty($new)){ - $type = repository_get_type_by_typename($new); + $type = repository::get_type_by_typename($new); } else if (!empty($edit)){ - $instance = repository_get_instance($edit); - $type = repository_get_type_by_id($instance->typeid); + $instance = repository::get_instance($edit); + $type = repository::get_type_by_id($instance->typeid); } else if (!empty($delete)){ - $instance = repository_get_instance($delete); - $type = repository_get_type_by_id($instance->typeid); + $instance = repository::get_instance($delete); + $type = repository::get_type_by_id($instance->typeid); } if (isset($type) && ( !$type->get_visible() || (!$type->get_contextvisibility($context->contextlevel)) ) ) { print_error('typenotvisible', 'repository', $baseurl); @@ -76,7 +76,7 @@ 'type' => 'misc'); $navlinks[] = array('name' => $pagename, 'link' => null, - 'type' => 'misc'); + 'type' => 'misc'); $fullname = $course->fullname; } else { $fullname = fullname($user); @@ -106,12 +106,12 @@ if (!empty($edit) || !empty($new)) { if (!empty($edit)) { - $instance = repository_get_instance($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); + $instancetype = repository::get_type_by_id($instance->typeid); $classname = 'repository_' . $instancetype->get_typename(); $configs = $instance->get_instance_option_names(); $plugin = $instancetype->get_typename(); @@ -142,7 +142,7 @@ } $success = $instance->set_option($settings); } else { - $success = repository_static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform); + $success = repository::static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform); $data = data_submitted(); } if ($success) { @@ -164,7 +164,7 @@ } } else if (!empty($delete)) { // admin_externalpage_print_header(); - $instance = repository_get_instance($delete); + $instance = repository::get_instance($delete); //if you try to delete an instance set as readonly, display an error message if ($instance->readonly) { throw new repository_exception('readonlyinstance', 'repository'); @@ -185,7 +185,7 @@ notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $baseurl . '&delete=' . $delete . '&sure=yes', $baseurl); $return = false; } else { - repository_display_instances_list($context); + repository::display_instances_list($context); $return = false; } diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index abbcdc3ee4..d98af32188 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -60,13 +60,13 @@ class repository_remotemoodle extends repository { exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username))); } - $file = unserialize(base64_decode($source)); + $file = unserialize(base64_decode($source)); $contextid = $file[0]; $filearea = $file[1]; $itemid = $file[2]; $filepath = $file[3]; $filename = $file[4]; - + ///check that the user has read permission on this file $browser = get_file_browser(); $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename); @@ -100,9 +100,9 @@ class repository_remotemoodle extends repository { if (empty($USER)) { exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle', $username))); } - + try { - return repository_get_user_file_tree($search); + return repository::get_user_file_tree($search); } catch (Exception $e) { exit(mnet_server_fault(9016, get_string('failtoretrievelist', 'repository_remotemoodle'))); @@ -125,7 +125,7 @@ class repository_remotemoodle extends repository { * @param $search_text * @return */ - public function search($search_text) { + public function search($search_text) { return $this->get_listing('', $search_text); } @@ -134,7 +134,7 @@ class repository_remotemoodle extends repository { * @global $MNET */ private function ensure_environment() { - global $MNET; + global $MNET; if (empty($MNET)) { $MNET = new mnet_environment(); $MNET->init(); @@ -184,7 +184,7 @@ class repository_remotemoodle extends repository { echo json_encode(array('e'=>get_string('connectionfailure','repository_remotemoodle'))); exit; } - + ///connect to the remote moodle and retrieve the list of files $client->set_method('repository/remotemoodle/repository.class.php/getFileList'); $client->add_param($USER->username); @@ -195,11 +195,11 @@ class repository_remotemoodle extends repository { $message =" "; foreach ($client->error as $errormessage) { $message .= "ERROR: $errormessage . "; - } + } echo json_encode(array('e'=>$message)); //display all error messages exit; } - + $services = $client->response; ///display error message if we could retrieve the list or if nothing were returned if (empty($services)) { @@ -210,7 +210,7 @@ class repository_remotemoodle extends repository { return $services; } - + /** * Download a file @@ -224,7 +224,7 @@ class repository_remotemoodle extends repository { global $CFG, $DB, $USER; ///set mnet environment and set the mnet host - require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); + require_once($CFG->dirroot . '/mnet/xmlrpc/client.php'); $this->ensure_environment(); $host = $DB->get_record('mnet_host',array('id' => $this->options['peer'])); //retrieve the host url $mnet_peer = new mnet_peer(); @@ -246,7 +246,7 @@ class repository_remotemoodle extends repository { exit; } - $services = $client->response; //service contains the file content in the first case of the array, + $services = $client->response; //service contains the file content in the first case of the array, //and the filename in the second //the content has been encoded in base64, need to decode it @@ -271,9 +271,9 @@ class repository_remotemoodle extends repository { $fp = fopen($dir.$file, 'w'); fwrite($fp,$content); fclose($fp); - + return $dir.$file; - + } /** @@ -284,7 +284,7 @@ class repository_remotemoodle extends repository { */ public function instance_config_form(&$mform) { global $CFG, $DB; - + //retrieve only Moodle peers $hosts = $DB->get_records_sql(' SELECT h.id, @@ -311,13 +311,13 @@ class repository_remotemoodle extends repository { array($CFG->mnet_localhost_id, 'moodle', 'All Hosts')); $peers = array(); foreach($hosts as $host) { - $peers[$host->id] = $host->name; + $peers[$host->id] = $host->name; } - + $mform->addElement('select', 'peer', get_string('peer', 'repository_remotemoodle'),$peers); $mform->addRule('peer', get_string('required'), 'required', null, 'client'); - + if (empty($peers)) { $mform->addElement('static', null, '', get_string('nopeer','repository_remotemoodle')); } @@ -332,4 +332,4 @@ class repository_remotemoodle extends repository { return array('peer'); } } -?> \ No newline at end of file +?> diff --git a/repository/simpletest/testrepository.php b/repository/simpletest/testrepository.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/repository/upload/repository.class.php b/repository/upload/repository.class.php index cd429f8e68..304d47ee55 100755 --- a/repository/upload/repository.class.php +++ b/repository/upload/repository.class.php @@ -26,7 +26,7 @@ class repository_upload extends repository { parent::__construct($repositoryid, $context, $options); if($action=='upload'){ $filepath = '/'.uniqid().'/'; - $this->info = repository_store_to_filepool('repo_upload_file', 'user_draft', $filepath); + $this->info = repository::store_to_filepool('repo_upload_file', 'user_draft', $filepath); } } @@ -83,7 +83,7 @@ class repository_upload extends repository { /** * - * @return + * @return */ public function get_name(){ return get_string('repositoryname', 'repository_upload');; diff --git a/repository/ws.php b/repository/ws.php index 66afe20876..9bfa782830 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -23,7 +23,7 @@ header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); /// Check permissions - if (! (isloggedin() && repository_check_context($ctx_id)) ) { + if (! (isloggedin() && repository::check_context($ctx_id)) ) { $err = new stdclass; $err->e = get_string('nopermissiontoaccess', 'repository'); die(json_encode($err)); @@ -36,7 +36,7 @@ switch ($action) { case 'gsearch': // Global Search - $repos = repository_get_instances(array(get_context_instance_by_id($ctx_id), get_system_context())); + $repos = repository::get_instances(array(get_context_instance_by_id($ctx_id), get_system_context())); $list = array(); foreach($repos as $repo){ if ($repo->global_search()) { @@ -158,7 +158,7 @@ EOD; if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $path)) { echo json_encode(array('url'=>$path, 'id'=>$path, 'file'=>$path)); } else { - $info = repository_move_to_filepool($path, $title, $itemid); + $info = repository::move_to_filepool($path, $title, $itemid); if ($env == 'form'){ echo json_encode($info); } else if ($env == 'editor') { diff --git a/user/repository.php b/user/repository.php index 37d96fc5a8..876e28d5c5 100644 --- a/user/repository.php +++ b/user/repository.php @@ -35,7 +35,7 @@ include('tabs.php'); print_heading($configstr); print_simple_box_start(); -if (!$instances = repository_get_instances($COURSE->context, $USER->id)) { +if (!$instances = repository::get_instances($COURSE->context, $USER->id)) { print_error('noinstances', 'repository', $CFG->wwwroot . '/user/view.php'); } @@ -46,7 +46,7 @@ $table->data = array(); foreach ($instances as $i) { $path = '/repository/'.$i->type.'/settings.php'; $settings = file_exists($CFG->dirroot.$path); - $table->data[] = array($i->name, $i->type, + $table->data[] = array($i->name, $i->type, $settings ? '' .get_string('settings', 'repository').'' : ''); } diff --git a/user/tabs.php b/user/tabs.php index 390e422e63..bcf0cd74ce 100644 --- a/user/tabs.php +++ b/user/tabs.php @@ -271,7 +271,7 @@ if (!empty($user) and $user->id == $USER->id) { require_once($CFG->dirroot . '/repository/lib.php'); $usercontext = get_context_instance(CONTEXT_USER,$user->id); - $editabletypes = repository_get_editable_types($usercontext); + $editabletypes = repository::get_editable_types($usercontext); if (!empty($usercontext) && $usercontext->contextlevel == CONTEXT_USER && !empty($editabletypes)) { $toprow[] = new tabobject('repositories', $CFG->wwwroot .'/repository/manage_instances.php?contextid='.$usercontext->id, get_string('repositories', 'repository')); }