MDL-15402, add admin setting page, to meet requirement, make some changes to reposito...
authordongsheng <dongsheng>
Wed, 13 Aug 2008 04:09:13 +0000 (04:09 +0000)
committerdongsheng <dongsheng>
Wed, 13 Aug 2008 04:09:13 +0000 (04:09 +0000)
12 files changed:
admin/repository.php [new file with mode: 0644]
admin/settings/plugins.php
lang/en_utf8/repository.php
lib/adminlib.php
repository/boxnet/lang/en_utf8/repository_boxnet.php
repository/boxnet/repository.class.php
repository/flickr/lang/en_utf8/repository_flickr.php
repository/flickr/repository.class.php
repository/lib.php
repository/local/repository.class.php
user/repository.php
user/tabs.php

diff --git a/admin/repository.php b/admin/repository.php
new file mode 100644 (file)
index 0000000..3805b09
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+
+require_once(dirname(dirname(__FILE__)) . '/config.php');
+require_once($CFG->dirroot . '/repository/lib.php');
+require_once($CFG->libdir . '/adminlib.php');
+
+
+$CFG->pagepath = 'admin/managerepositories';
+
+// id of repository
+$edit    = optional_param('edit', 0, PARAM_INT);
+$new     = optional_param('new', '', PARAM_FORMAT);
+$hide    = optional_param('hide', 0, PARAM_INT);
+$delete  = optional_param('delete', 0, PARAM_INT);
+$sure    = optional_param('sure', '', PARAM_ALPHA);
+
+$display = true; // fall through to normal display
+
+$pagename = 'repositorycontroller';
+
+if ($edit) {
+    $pagename = 'repositorysettings' . $edit;
+} else if ($delete) {
+    $pagename = 'repositorydelete';
+} else if ($new) {
+    $pagename = 'repositorynew';
+}
+admin_externalpage_setup($pagename);
+require_login(SITEID, false);
+require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
+
+$sesskeyurl = $CFG->wwwroot . '/' . $CFG->admin . '/repository.php?sesskey=' . sesskey();
+$baseurl    = $CFG->wwwroot . '/admin/settings.php?section=managerepositories';
+
+$configstr  = get_string('managerepositories', 'repository');
+
+$return = true;
+
+if (!empty($edit) || !empty($new)) {
+    if (!empty($edit)) {
+        $instance = repository_instance($edit);
+        $configs  = $instance->get_option_names();
+        $plugin = $instance->type;
+    } else {
+        $plugin = $new;
+        $instance = null;
+    }
+    $CFG->pagepath = 'admin/managerepository/' . $plugin;
+    // display the edit form for this instance
+    $mform = new repository_admin_form('', array('plugin' => $plugin, 'instance' => $instance));
+    // end setup, begin output
+   
+    if ($mform->is_cancelled()){
+        redirect($baseurl);
+        exit;
+    } else if ($fromform = $mform->get_data()){
+        if (!confirm_sesskey()) {
+            print_error('confirmsesskeybad', '', $baseurl);
+        }
+        if ($edit) {
+            $settings = array();
+            $settings['name'] = $fromform->name;
+            foreach($configs as $config) {
+                $settings[$config] = $fromform->$config;
+            }
+            $success = $instance->set_option($settings);
+        } else {
+            $success = repository_static_function($plugin, 'create', $plugin, 0, get_system_context(), $fromform);
+            $data = data_submitted();
+        }
+        if ($success) {
+            $savedstr = get_string('configsaved', 'repository');
+            admin_externalpage_print_header();
+            print_heading($savedstr);
+            redirect($baseurl, $savedstr, 3);
+        } else {
+            print_error('instancenotsaved', 'repository', $baseurl);
+        }
+        exit;
+    } else {
+        admin_externalpage_print_header();
+        print_heading(get_string('configplugin', 'repository_'.$plugin));
+        print_simple_box_start();
+        $mform->display();
+        print_simple_box_end();
+        $return = false;
+    }
+} else if (!empty($hide)) {
+    if (!confirm_sesskey()) {
+        print_error('confirmsesskeybad', '', $baseurl);
+    }
+    $instance = repository_instance($hide);
+    $instance->hide();
+    $return = true;
+} else if (!empty($delete)) {
+    admin_externalpage_print_header();
+    $instance = repository_instance($delete);
+    if ($sure) {
+        if (!confirm_sesskey()) {
+            print_error('confirmsesskeybad', '', $baseurl);
+        }
+        if ($instance->delete()) {
+            $deletedstr = get_string('instancedeleted', 'repository');
+            print_heading($deletedstr);
+            redirect($baseurl, $deletedstr, 3);
+        } else {
+            print_error('instancenotdeleted', 'repository', $baseurl);
+        }
+        exit;
+    }
+    notice_yesno(get_string('confirmdelete', 'repository', $instance->name), $sesskeyurl . '&delete=' . $delete . '&sure=yes', $baseurl);
+    $return = false;
+}
+
+
+if (!empty($return)) {
+    redirect($baseurl);
+}
+admin_externalpage_print_footer();
index 20bf0f9..75cb575 100644 (file)
@@ -174,5 +174,34 @@ if ($hassiteconfig) {
             $url . ' ?edit=' . $portfolio->get('id')
         );
     }
+
+    // repository setting
+    require_once("$CFG->dirroot/repository/lib.php");
+    $catname =get_string('repositories', 'repository');
+    $manage = get_string('manage', 'repository');
+    $url = "$CFG->wwwroot/$CFG->admin/repository.php";
+    $ADMIN->add('modules', new admin_category('repositorysettings', $catname));
+    $temp = new admin_settingpage('managerepositories', $manage);
+    $temp->add(new admin_setting_managerepository());
+    $ADMIN->add('repositorysettings', $temp);
+    $ADMIN->add('repositorysettings', new admin_externalpage('repositorynew', 
+        get_string('createrepository', 'repository'), $url, 'moodle/site:config', true), 
+        '', $url);
+    $ADMIN->add('repositorysettings', new admin_externalpage('repositorydelete',
+        get_string('deleterepository', 'repository'), $url, 'moodle/site:config', true),
+        '', $url);
+    $ADMIN->add('repositorysettings', new admin_externalpage('repositorycontroller',
+        get_string('managerepositories', 'repository'), $url, 'moodle/site:config', true),
+        '', $url);
+    foreach (repository_instances(get_context_instance(CONTEXT_SYSTEM), 
+                null, false) as $repository) 
+    {
+        if ($repository->has_admin_config()) {
+            $ADMIN->add('repositorysettings',
+                new admin_externalpage('repositorysettings'.$repository->id, 
+                        $repository->name,
+                        $url . '?edit=' . $repository->id),
+                        'moodle/site:config');
+        }
+    }
 }
-?>
index 21d671a..2ca3155 100644 (file)
@@ -1,14 +1,19 @@
 <?php //$Id$
-$string['add'] = 'Add';
+$string['add'] = 'Add a repository:';
 $string['activaterep'] = 'Active repositories';
 $string['back'] = '&lt; Back';
 $string['close'] = 'Close';
+$string['configsaved'] = 'Configuration saved!';
+$string['confirmdelete'] = 'Are you sure to delete this repository - $a?';
+$string['createrepository'] = 'Create a repository';
+$string['deleterepository'] = 'Delete this repository';
 $string['download'] = 'Download';
 $string['downloadsucc'] = 'Download Successfully!';
 $string['filename'] = 'Filename';
 $string['filesaved'] = 'File Saved!';
 $string['invalidrepositoryid'] = 'Invalid Repository ID';
 $string['invalidplugin'] = 'Invalid Repository Plug-in';
+$string['manage'] = 'Manage repositories';
 $string['manageuserrepository'] = 'Manage individual repository';
 $string['noenter'] = 'Nothing entered';
 $string['plugin'] = 'Repository Plug-ins';
index f0d08e8..974fabc 100644 (file)
@@ -5083,5 +5083,97 @@ function print_plugin_tables() {
 }
 
 
+class admin_setting_managerepository extends admin_setting {
+    private $baseurl;
+    function admin_setting_managerepository() {
+        global $CFG;
+        parent::admin_setting('managerepository', get_string('managerepository', 'repository'), '', '');
+        $this->baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/repository.php?sesskey=' . sesskey();
+    }
+    function get_setting() {
+        return true;
+    }
+
+    function get_defaultsetting() {
+        return true;
+    }
+
+    function get_full_name() {
+        return 's_managerepository';
+    }
+
+    function write_setting($data) {
+        $url = $this->baseurl . '&amp;new=' . $data;
+        // TODO
+        // Should not use redirect and exit here
+        // Find a better way to do this.
+        // redirect($url);
+        // exit;
+    }
+
+    function is_related($query) {
+        if (parent::is_related($query)) {
+            return true;
+        }
+
+        $textlib = textlib_get_instance();
+        $repositories= get_list_of_plugins('repository');
+        foreach ($repositories as $p) {
+            if (strpos($p, $query) !== false) {
+                return true;
+            }
+        }
+        foreach (repository_instances(get_context_instance(CONTEXT_SYSTEM), null, false) as $instance) {
+            $title = $instance->name;
+            if (strpos($textlib->strtolower($title), $query) !== false) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    function output_html($data, $query='') {
+        global $CFG, $USER;
+        $output = print_simple_box_start(true);
+        $namestr = get_string('name');
+        $pluginstr = get_string('plugin', 'repository');
+        $plugins = get_list_of_plugins('repository');
+        $instances = repository_instances(get_context_instance(CONTEXT_SYSTEM), null, false);
+        $alreadyplugins = array();
+        $table = new StdClass;
+        $table->head = array($namestr, $pluginstr, '');
+        $table->data = array();
+
+        foreach ($instances as $i) {
+            $row = '';
+            $row .= '<a href="' . $this->baseurl . '&edit=' . $i->id . '"><img src="' . $CFG->pixpath . '/t/edit.gif" alt="' . get_string('edit') . '" /></a>' . "\n";
+            $row .= '<a href="' . $this->baseurl . '&delete=' .  $i->id . '"><img src="' . $CFG->pixpath . '/t/delete.gif" alt="' . get_string('delete') . '" /></a>' . "\n";
+            $row .= ' <a href="' . $this->baseurl . '&hide=' . $i->id . '"><img src="' . $CFG->pixpath . '/t/' . ($i->visible ? 'hide' : 'show') . '.gif" alt="' . get_string($i->visible ? 'hide' : 'show') . '" /></a>' . "\n";
+            $table->data[] = array($i->name, $i->type, $row);
+            if (!in_array($i->type, $alreadyplugins)) {
+                $alreadyplugins[] = $i->name;
+            }
+        }
 
-?>
+        $output .= print_table($table, true);
+        $instancehtml = '<div><p>';
+        $addable = 0;
+        $instancehtml .= get_string('add', 'repository');
+        $instancehtml .= '</p>';
+        $addable = 0;
+        foreach ($plugins as $p) {
+            $instancehtml .= '<a href="'.$CFG->wwwroot.'/admin/repository.php?sesskey='.$USER->sesskey.'&new='.$p.'">'.$p.'</a><br/>';
+            $addable++;
+        }
+
+        if ($addable) {
+            $instancehtml .= '</div>';
+            $output .= $instancehtml;
+        }
+
+        $output .= print_simple_box_end(true);
+
+        return highlight($query, $output);
+    }
+}
index 184380b..ae13cab 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+$string['apikey'] = 'API Key';
 $string['repositoryname'] = 'Box.net';
 $string['repositorydesc'] = 'Repository for box.net';
 $string['configplugin'] = 'Box.net configuration';
index 3844228..7511979 100755 (executable)
@@ -12,7 +12,6 @@ require_once($CFG->dirroot.'/repository/boxnet/'.'boxlibphp5.php');
 
 class repository_boxnet extends repository{
     private $box;
-    public $type = 'boxnet';
 
     public function __construct($repositoryid, $context = SITEID, $options = array()){
         global $SESSION, $action;
@@ -23,6 +22,9 @@ class repository_boxnet extends repository{
         // reset session
         $reset = optional_param('reset', 0, PARAM_INT);
         if(!empty($reset)) {
+            // TODO
+            // think about muliti-instance
+            // must improve!
             unset($SESSION->box_token);
         }
         // do login
@@ -58,7 +60,7 @@ class repository_boxnet extends repository{
 
     public function get_login(){
         global $DB;
-        if ($entry = $DB->get_record('repository', array('id'=>$this->repositoryid))) {
+        if ($entry = $DB->get_record('repository', array('id'=>$this->id))) {
             $ret->username = $entry->username;
             $ret->password = $entry->password;
         } else {
@@ -121,7 +123,7 @@ class repository_boxnet extends repository{
                 $str .= '<form id="moodle-repo-login">';
                 $str .= '<input type="hidden" name="ticket" value="'.
                     $t['ticket'].'" />';
-                $str .= '<input type="hidden" name="id" value="'.$this->repositoryid.'" />';
+                $str .= '<input type="hidden" name="id" value="'.$this->id.'" />';
                 $str .= '<label for="box_username">Username: <label><br/>';
                 $str .= '<input type="text" id="box_username" name="username" value="'.$ret->username.'" />';
                 $str .= '<br/>';
@@ -136,7 +138,7 @@ class repository_boxnet extends repository{
 
                     $e2->type = 'hidden';
                     $e2->name = 'repo_id';
-                    $e2->value = $this->repositoryid;
+                    $e2->value = $this->id;
 
                     $e3->label = get_string('username', 'repository_boxnet');
                     $e3->id    = 'box_username';
@@ -162,6 +164,17 @@ class repository_boxnet extends repository{
     public function print_search(){
         return false;
     }
+    public static function has_admin_config() {
+        return true;
+    }
+    public function admin_config_form(&$mform) {
+        $strrequired = get_string('required');
+        $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'));
+        $mform->addRule('api_key', $strrequired, 'required', null, 'client');
+    }
+    public static function get_option_names(){
+        return array('api_key');
+    }
 }
 
 ?>
index c5c55c2..70efec8 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+$string['configplugin'] = 'Flickr configureation';
 $string['repositoryname'] = 'Flickr';
 $string['repositorydesc'] = 'Repository for flickr.com';
 $string['notitle'] = 'notitle';
index c0d53c7..0207fc0 100755 (executable)
@@ -13,7 +13,6 @@ require_once($CFG->dirroot.'/repository/flickr/'.'phpFlickr.php');
 class repository_flickr extends repository{
     private $flickr;
     public $photos;
-    public $type = 'flickr';
 
     public function __construct($repositoryid, $context = SITEID, $options = array()){
         global $SESSION, $action, $CFG;
@@ -68,7 +67,7 @@ class repository_flickr extends repository{
 <form id="moodle-repo-login">
 <label for="account">Account (Email)</label><br/>
 <input type='text' name='flickrmail' id='account' />
-<input type='hidden' name='id' value='$this->repositoryid' /><br/>
+<input type='hidden' name='id' value='$this->id' /><br/>
 <input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
 <p><input type='button' onclick="repository_client.login()" value="Go" /></p>
 </form>
@@ -87,7 +86,7 @@ EOD;
 
                 $e3->type = 'hidden';
                 $e3->name = 'repo_id';
-                $e3->value = $this->repositoryid;
+                $e3->value = $this->id;
                 $ret['l'] = array($e1, $e2, $e3);
                 return $ret;
             }else{
@@ -156,7 +155,7 @@ EOD;
 EOD;
         $str .= '<div id="paging">';
         for($i=1; $i <= $this->photos['pages']; $i++) {
-            $str .= '<a href="###" onclick="cr('.$this->repositoryid.', '.$i.', 0)">';
+            $str .= '<a href="###" onclick="cr('.$this->id.', '.$i.', 0)">';
             $str .= $i;
             $str .= '</a> ';
         }
@@ -198,4 +197,15 @@ EOD;
         ));
         return $dir.$file;
     }
+    public static function has_admin_config() {
+        return true;
+    }
+    public function admin_config_form(&$mform) {
+        $strrequired = get_string('required');
+        $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'));
+        $mform->addRule('api_key', $strrequired, 'required', null, 'client');
+    }
+    public static function get_option_names(){
+        return array('api_key');
+    }
 }
index 25ddcb2..a1bd353 100644 (file)
  */
 require_once(dirname(dirname(__FILE__)) . '/config.php');
 require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
+require_once(dirname(dirname(__FILE__)).'/lib/formslib.php');
 
 abstract class repository {
-    protected $options;
-    public    $name;
-    public    $context;
-    public    $repositoryid;
-    public    $listing;
+    public $id;
+    public $context;
+    public $options;
 
     /**
      * Take an array as a parameter, which contains necessary information
@@ -70,14 +69,16 @@ abstract class repository {
      * @return array the list of files, including meta infomation
      */
     public function __construct($repositoryid, $contextid = SITEID, $options = array()){
-        $this->repositoryid = $repositoryid;
-        $this->context      = get_context_instance_by_id($contextid);
-        $this->options      = array();
+        $this->id = $repositoryid;
+        $this->context = get_context_instance_by_id($contextid);
+        $this->options = array();
         if (is_array($options)) {
-            $options = array_merge(repository_get_option($repositoryid), $options);
-            foreach ($options as $n => $v) {
-                $this->options[$n] = $v;
-            }
+            $options = array_merge($this->get_option(), $options);
+        } else {
+            $options = $this->get_option();
+        }
+        foreach ($options as $n => $v) {
+            $this->options[$n] = $v;
         }
     }
 
@@ -100,6 +101,7 @@ abstract class repository {
     public function __toString() {
         return 'Repository class: '.__CLASS__;
     }
+
     /**
      * Given a URL, get a file from there.
      * @param string $url the url of file
@@ -127,16 +129,6 @@ abstract class repository {
         return $dir.$file;
     }
 
-    /**
-     * Given a path, and perhaps a search, get a list of files.
-     *
-     * @param string $parent The parent path, this parameter can
-     * a folder name, or a identification of folder
-     * @param string $search The text will be searched.
-     * @return array the list of files, including meta infomation
-     */
-    abstract public function get_listing($parent = '/', $search = '');
-
     /**
      * Print a list or return string
      *
@@ -179,18 +171,51 @@ abstract class repository {
     }
 
     /**
-     * Show the login screen, if required
-     * This is an abstract function, it must be overriden.
-     *
+     * Return data for creating ajax request
+     * @return object
      */
-    abstract public function print_login();
+    public function ajax_info() {
+        global $CFG;
+        $repo = new stdclass;
+        $repo->name = $this->options['name'];
+        $repo->type = $this->options['type'];
+        $repo->id   = $this->options['id'];
+        $repo->icon = $CFG->wwwroot.'/repository/'.$repo->type.'/icon.png';
+        return $repo;
+    }
 
     /**
-     * Show the search screen, if required
-     *
-     * @return null
+     * delete a repository instance
      */
-    abstract public function print_search();
+    public function delete(){
+        global $DB;
+        $DB->delete_records('repository', array('id'=>$this->id));
+        return true;
+    }
+    /**
+     * Hide/Show a repository
+     * @param boolean
+     */
+    public function hide($hide = 'toggle'){
+        global $DB;
+        if ($entry = $DB->get_record('repository', array('id'=>$this->id))) {
+            if ($hide === 'toggle' ) {
+                if (!empty($entry->visible)) {
+                    $entry->visible = 0;
+                } else {
+                    $entry->visible = 1;
+                }
+            } else {
+                if (!empty($hide)) {
+                    $entry->visible = 0;
+                } else {
+                    $entry->visible = 1;
+                }
+            }
+            return $DB->update_record('repository', $entry);
+        }
+        return false;
+    }
 
     /**
      * Cache login details for repositories
@@ -204,8 +229,8 @@ abstract class repository {
         global $DB;
 
         $repository = new stdclass;
-        if (!empty($this->repositoryid)) {
-            $repository->id = $this->repositoryid;
+        if (!empty($this->id)) {
+            $repository->id = $this->id;
         } else {
             $repository->userid         = $userid;
             $repository->repositorytype = $this->type;
@@ -225,104 +250,180 @@ abstract class repository {
     }
 
     /**
-     * Defines operations that happen occasionally on cron
+     * Save settings for repository instance
+     * $repo->set_option(array('api_key'=>'f2188bde132', 
+     *                          'name'=>'dongsheng'));
      *
+     * @param array settings
+     * @return int Id of the record
      */
-    public function cron() {
-        return true;
+    public function set_option($options = array()){
+        global $DB;
+        if (is_array($options)) {
+            $options = array_merge($this->get_option(), $options);
+        } else {
+            $options = $this->get_option();
+        }
+        $repository = new stdclass;
+        $position = 1;
+        $options   = serialize($options);
+        if ($entry = $DB->get_record('repository', array('id'=>$this->id))) {
+            $field = 'data'.$position;
+            $repository->id = $entry->id;
+            $repository->$field = $options;
+            return $DB->update_record('repository', $repository);
+        }
+        return false;
     }
-}
 
-/**
- * exception class for repository api
- *
- */
+    /**
+     * Get settings for repository instance
+     *
+     * @param int repository Id
+     * @return array Settings
+     */
+    public function get_option($config = ''){
+        global $DB;
+        $entry = $DB->get_record('repository', array('id'=>$this->id));
+        if (!empty($entry->visible)) {
+            $ret['visible'] = 1;
+        } else {
+            $ret['visible'] = 0;
+        }
+        $ret['type'] = $entry->repositorytype;
+        $ret['name'] = $entry->repositoryname;
+        $ret['id']   = $entry->id;
+        for ($i=1;$i<6;$i++) {
+            $field = 'data'.$i;
+            $data = unserialize($entry->$field);
+            if (!empty($data)) {
+                if (is_array($data)) {
+                    $ret = array_merge($ret, $data);
+                }
+            }
+        }
+        if (!empty($config)) {
+            return $ret[$config];
+        } else {
+            return $ret;
+        }
+    }
 
-class repository_exception extends moodle_exception {
-}
+    /**
+     * Given a path, and perhaps a search, get a list of files.
+     *
+     * @param string $parent The parent path, this parameter can
+     * a folder name, or a identification of folder
+     * @param string $search The text will be searched.
+     * @return array the list of files, including meta infomation
+     */
+    abstract public function get_listing($parent = '/', $search = '');
 
-/**
- * Listing object describing a listing of files and directories
- */
 
-abstract class repository_listing {
-}
+    /**
+     * Show the login screen, if required
+     * This is an abstract function, it must be overriden.
+     *
+     */
+    abstract public function print_login();
 
-/**
- * Save settings for repository instance
- *
- * @param int repository Id
- * @param int from 1 to 5
- * @param array settings
- * @return int Id of the record
- */
-function repository_set_option($id, $position, $config = array()){
-    global $DB;
-    $repository = new stdclass;
-    $position = (int)$position;
-    $config   = serialize($config);
-    if( $position < 1 || $position > 5){
-        print_error('invalidoption', 'repository', '', $position);
+    /**
+     * Show the search screen, if required
+     *
+     * @return null
+     */
+    abstract public function print_search();
+
+    public static function has_admin_config() {
+        return false;
+    }
+
+    public static function create($type, $userid, $context, $params) {
+        global $CFG, $DB;
+        $params = (array)$params;
+        require_once($CFG->dirroot . '/repository/'. $type . '/repository.class.php');
+        $classname = 'repository_' . $type;
+        if (self::has_admin_config()) {
+            $configs = self::get_option_names();
+            $options = array();
+            foreach ($configs as $config) {
+                $options[$config] = $params[$config];
+            }
+            $record->data1     = serialize($options);
+        }
+        $record = new stdclass;
+        $record->repositoryname = $params['name'];
+        $record->repositorytype = $type;
+        $record->timecreated  = time();
+        $record->timemodified = time();
+        $record->contextid = $context->id;
+        $record->visible   = 1;
+        $record->userid    = $userid;
+        $id = $DB->insert_record('repository', $record);
+        if (!empty($id)) {
+            return $id;
+        } else {
+            return null;
+        }
     }
-    if ($entry = $DB->get_record('repository', array('id'=>$id))) {
-        $option = 'data'.$position;
-        $repository->id = $entry->id;
-        $repository->$option = $config;
-        return $DB->update_record('repository', $repository);
+
+    /**
+     * Defines operations that happen occasionally on cron
+     *
+     */
+    public function cron() {
+        return true;
     }
-    return false;
 }
 
 /**
- * Get settings for repository instance
+ * exception class for repository api
  *
- * @param int repository Id
- * @return array Settings
  */
-function repository_get_option($id){
-    global $DB;
-    $entry = $DB->get_record('repository', array('id'=>$id));
-    if (!empty($entry->visible)) {
-        $ret['visible'] = 1;
-    } else {
-        $ret['visible'] = 0;
-    }
-    for ($i=1;$i<6;$i++) {
-        $field = 'data'.$i;
-        $data = unserialize($entry->$field);
-        if (!empty($data)) {
-            if (is_array($data)) {
-                $ret = array_merge($ret, $data);
-            }
-        }
-    }
-    return $ret;
+
+class repository_exception extends moodle_exception {
 }
 
+
 /**
- * Get user's repositories
+ * Return repository instances
  *
- * @param object context object
- * @return array repository list
+ * @param object context
+ * @param int userid
+ * @param boolean if visible == true, return visible instances only,
+ *                otherwise, return all instances
+ * @return array repository instances
  */
-function repository_user_instances($context){
+function repository_instances($context, $userid = null, $visible = true){
     global $DB, $CFG, $USER;
     $params = array();
     $sql = 'SELECT * FROM {repository} r WHERE ';
-    $sql .= ' (r.userid = 0 or r.userid = ?) ';
-    $params[] = $USER->id;
-    if($context->id == SITEID) {
-        $sql .= 'AND (r.contextid = ?)';
-        $params[] = SITEID;
+    if (!empty($userid) && is_numeric($userid)) {
+        $sql .= ' (r.userid = 0 or r.userid = ?) AND ';
+        $params[] = $userid;
+    }
+    if($context->id == SYSCONTEXTID) {
+        $sql .= ' (r.contextid = ?)';
+        $params[] = SYSCONTEXTID;
     } else {
-        $sql .= 'AND (r.contextid = ? or r.contextid = ?)';
-        $params[] = SITEID;
+        $sql .= ' (r.contextid = ? or r.contextid = ?)';
+        $params[] = SYSCONTEXTID;
         $params[] = $context->id;
     }
+    if($visible == true) {
+        $sql .= ' AND (r.visible = 1)';
+    }
     if(!$repos = $DB->get_records_sql($sql, $params)) {
         $repos = array();
     }
-    return $repos;
+    $ret = array();
+    foreach($repos as $repo) {
+        require_once($CFG->dirroot . '/repository/'. $repo->repositorytype 
+            . '/repository.class.php');
+        $classname = 'repository_' . $repo->repositorytype;
+        $ret[] = new $classname($repo->id, $repo->contextid);
+    }
+    return $ret;
 }
 
 /**
@@ -343,28 +444,28 @@ function repository_instance($id){
     return new $classname($instance->id, $instance->contextid);
 }
 
-/**
- * Get list of repository plugin
- *
- * @return array repository plugin list
- */
-function repository_get_plugins(){
+function repository_static_function($plugin, $function) {
     global $CFG;
-    $repo = $CFG->dirroot.'/repository/';
-    $ret = array();
-    if($dir = opendir($repo)){
-        while (false !== ($file = readdir($dir))) {
-            if(is_dir($file) && $file != '.' && $file != '..'
-                && file_exists($repo.$file.'/repository.class.php')){
-                require_once($repo.$file.'/version.php');
-                $ret[] = array('name'=>$plugin->name,
-                        'version'=>$plugin->version,
-                        'path'=>$repo.$file,
-                        'settings'=>file_exists($repo.$file.'/settings.php'));
-            }
-        }
+
+    $pname = null;
+    if (is_object($plugin) || is_array($plugin)) {
+        $plugin = (object)$plugin;
+        $pname = $plugin->name;
+    } else {
+        $pname = $plugin;
     }
-    return $ret;
+
+    $args = func_get_args();
+    if (count($args) <= 2) {
+        $args = array();
+    }
+    else {
+        array_shift($args);
+        array_shift($args);
+    }
+
+    require_once($CFG->dirroot . '/repository/' . $plugin .  '/repository.class.php');
+    return call_user_func_array(array('repository_' . $plugin, $function), $args);
 }
 
 /**
@@ -568,7 +669,7 @@ function get_repository_client($context){
                         var link = document.createElement('a');
                         link.href = '###';
                         link.id = 'repo-call-$suffix-'+repo.id;
-                        link.innerHTML = ' '+repo.repositoryname;
+                        link.innerHTML = ' '+repo.name;
                         link.className = 'repo-name';
                         link.onclick = function(){
                             var re = /repo-call-$suffix-(\d+)/i;
@@ -874,11 +975,10 @@ function get_repository_client($context){
     })();
 EOD;
 
-    $repos = repository_user_instances($context);
+    $repos = repository_instances($context);
     foreach($repos as $repo) {
-        $repo->icon = $CFG->wwwroot.'/repository/'.$repo->repositorytype.'/icon.png';
         $js .= "\r\n";
-        $js .= 'repository_client_'.$suffix.'.repos.push('.json_encode($repo).');'."\n";
+        $js .= 'repository_client_'.$suffix.'.repos.push('.json_encode($repo->ajax_info()).');'."\n";
     }
     $js .= "\r\n";
 
@@ -907,3 +1007,67 @@ EOD;
 EOD;
     return array('html'=>$html, 'js'=>$js, 'suffix'=>$suffix);
 }
+
+final class repository_admin_form extends moodleform {
+    protected $instance;
+    protected $plugin;
+
+    public function definition() {
+        global $CFG;
+        // type of plugin, string
+        $this->plugin = $this->_customdata['plugin'];
+        $this->instance = (isset($this->_customdata['instance'])
+                && is_subclass_of($this->_customdata['instance'], 'repository'))
+            ? $this->_customdata['instance'] : null;
+
+        $mform =& $this->_form;
+        $strrequired = get_string('required');
+
+        $mform->addElement('hidden', 'edit',  ($this->instance) ? $this->instance->id : 0);
+        $mform->addElement('hidden', 'new',   $this->plugin);
+        $mform->addElement('hidden', 'plugin', $this->plugin);
+
+        $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
+        $mform->addRule('name', $strrequired, 'required', null, 'client');
+
+        // let the plugin add the fields they want (either statically or not)
+        if (repository_static_function($this->plugin, 'has_admin_config')) {
+            if (!$this->instance) {
+                $result = repository_static_function($this->plugin, 'admin_config_form', $mform);
+            } else {
+                $result = $this->instance->admin_config_form($mform);
+            }
+        }
+
+        // and set the data if we have some.
+        if ($this->instance) {
+            $data = array();
+            $data['name'] = $this->instance->name;
+            foreach ($this->instance->get_option_names() as $config) {
+                $data[$config] = $this->instance->$config;
+            }
+            $this->set_data($data);
+        }
+        $this->add_action_buttons(true, get_string('submit'));
+    }
+
+    public function validation($data) {
+        global $DB;
+
+        $errors = array();
+        if ($DB->count_records('repository', array('repositoryname' => $data['name'], 'repositorytype' => $data['plugin'])) > 1) {
+            $errors = array('name' => get_string('err_uniquename', 'repository'));
+        }
+
+        $pluginerrors = array();
+        if ($this->instance) {
+            //$pluginerrors = $this->instance->admin_config_validation($data);
+        } else {
+            //$pluginerrors = repository_static_function($this->plugin, 'admin_config_validation', $data);
+        }
+        if (is_array($pluginerrors)) {
+            $errors = array_merge($errors, $pluginerrors);
+        }
+        return $errors;
+    }
+}
index 33a8c04..2914c0c 100755 (executable)
@@ -8,7 +8,6 @@
  */
 
 class repository_local extends repository{
-    public $type = 'local';
 
     public function __construct($repositoryid, $context = SITEID, $options = array()){
         global $SESSION, $action, $CFG;
@@ -16,6 +15,7 @@ class repository_local extends repository{
         // get the parameter from client side
         // $this->context can be used here.
     }
+
     public function print_login($ajax = true){
         global $SESSION;
         // TODO
index f818135..0a5aa96 100644 (file)
@@ -35,7 +35,7 @@ include('tabs.php');
 print_heading($configstr);
 print_simple_box_start();
 
-if (!$instances = repository_user_instances($COURSE->context)) {
+if (!$instances = repository_instances($COURSE->context, $USER->id)) {
     print_error('noinstances', 'repository', $CFG->wwwroot . '/user/view.php');
 }
 
@@ -44,9 +44,9 @@ $table->head = array($namestr, $pluginstr, '');
 $table->data = array();
 
 foreach ($instances as $i) {
-    $path = '/repository/'.$i->repositorytype.'/settings.php';
+    $path = '/repository/'.$i->type.'/settings.php';
     $settings = file_exists($CFG->dirroot.$path);
-    $table->data[] = array($i->repositoryname, $i->repositorytype, 
+    $table->data[] = array($i->name, $i->type, 
         $settings ? '<a href="'.$CFG->wwwroot.$path.'">'
             .get_string('settings', 'repository').'</a>' : '');
 }
index c993594..aa08ecb 100644 (file)
     // Repository Tab
     if ($user->id == $USER->id) {
         require_once($CFG->dirroot . '/repository/lib.php');
-        if (repository_user_instances($COURSE->context)) {
+        if (repository_instances($COURSE->context, $USER->id)) {
             $toprow[] = new tabobject('repositories', $CFG->wwwroot .'/user/repository.php', get_string('repositories', 'repository'));
         }
     }