From: jerome Date: Tue, 16 Sep 2008 09:08:36 +0000 (+0000) Subject: MDL-15402: implement readonly repository instances (support flickr_public) X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=948c2860257530768acd02c5e209d5b6a5d91182;p=moodle.git MDL-15402: implement readonly repository instances (support flickr_public) --- diff --git a/admin/repositoryinstance.php b/admin/repositoryinstance.php index ac8da10cb8..d5a2aff2ff 100644 --- a/admin/repositoryinstance.php +++ b/admin/repositoryinstance.php @@ -10,19 +10,15 @@ $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); -$move = optional_param('move', '', PARAM_ALPHA); -$type = optional_param('type', '', PARAM_ALPHA); +$type = optional_param('type', '', PARAM_ALPHAEXT); $context = get_context_instance(CONTEXT_SYSTEM); -$display = true; // fall through to normal display - $pagename = 'repositorycontroller'; if ($edit){ $pagename = 'repositoryinstanceedit'; -}else -if ($delete) { +} else if ($delete) { $pagename = 'repositorydelete'; } else if ($new) { $pagename = 'repositoryinstancenew'; @@ -47,6 +43,10 @@ $return = true; if (!empty($edit) || !empty($new)) { if (!empty($edit)) { $instance = repository_get_instance($edit); + //if you try to edit an instance set as readonly, display an error message + if ($instance->readonly) { + throw new repository_exception('readonlyinstance', 'repository'); + } $instancetype = repository_get_type_by_id($instance->typeid); $classname = 'repository_' . $instancetype->get_typename(); $configs = $instance->get_instance_option_names(); @@ -109,6 +109,10 @@ if (!empty($edit) || !empty($new)) { } else if (!empty($delete)) { admin_externalpage_print_header(); $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'); + } if ($sure) { if (!confirm_sesskey()) { print_error('confirmsesskeybad', '', $baseurl); diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php index 988fcb37b0..082eabd0d4 100644 --- a/lang/en_utf8/repository.php +++ b/lang/en_utf8/repository.php @@ -61,6 +61,7 @@ $string['personalrepositories'] = 'Personal repositories'; $string['plugin'] = 'Repository plug-ins'; $string['preview'] = 'Preview'; $string['popup'] = 'Open a popup window to login'; +$string['readonlyinstance'] = 'You cannot edit/delete a read-only instance'; $string['refresh'] = 'Refresh'; $string['removed'] = 'Repository removed'; $string['repository'] = 'Repository'; diff --git a/lib/db/install.xml b/lib/db/install.xml index 516b751bb6..81e28831a6 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -1951,7 +1951,7 @@ - +
@@ -1961,7 +1961,8 @@ - + + @@ -2009,4 +2010,4 @@ - + \ No newline at end of file diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index bc191fcdad..f40ac1fd7c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -825,6 +825,23 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint($result, 2008091000); } + //Add a readonly field to the repository_instances table + //in order to support instance created automatically by a repository plugin + if ($result && $oldversion < 2008091611) { + + /// Define field readonly to be added to repository_instances + $table = new xmldb_table('repository_instances'); + $field = new xmldb_field('readonly', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timemodified'); + + /// Conditionally launch add field readonly + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + /// Main savepoint reached + upgrade_main_savepoint($result, 2008091611); + } + return $result; } diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index 9868c52389..beab04675e 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -43,10 +43,10 @@ class repository_flickr_public extends repository { } } - public function __construct($repositoryid, $context = SITEID, $options = array()) { + public function __construct($repositoryid, $context = SITEID, $options = array(), $readonly=0) { global $CFG; $options['page'] = optional_param('p', 1, PARAM_INT); - parent::__construct($repositoryid, $context, $options); + parent::__construct($repositoryid, $context, $options,$readonly); $this->api_key = $this->get_option('api_key'); $this->flickr = new phpFlickr($this->api_key); $this->flickr_account = $this->get_option('email_address'); @@ -182,7 +182,7 @@ class repository_flickr_public extends repository { public function instance_config_form(&$mform) { $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public')); - //$mform->addRule('email_address', get_string('required'), 'required', null, 'client'); + $mform->addRule('email_address', get_string('required'), 'required', null, 'client'); } public static function get_instance_option_names() { @@ -203,8 +203,15 @@ class repository_flickr_public extends repository { return array('api_key'); } - public static function type_init() { + + public static function plugin_init() { //here we create a default instances for this type + repository_static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name' => 'default instance','email_address' => null),1); + //create(0, get_system_context(), array('name' => 'default instance'),1); + /* + $success = repository_static_function($plugin, 'create', $plugin, 0, get_context_instance_by_id($contextid), $fromform); + $defaultinstance = new repository (); + * */ } } diff --git a/repository/lib.php b/repository/lib.php index 01a820b63f..704bcfcbb9 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -207,6 +207,10 @@ class repository_type { $instanceoptions['name'] = $this->_typename; repository_static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions); } + + //run init function + repository_static_function($this->_typename,"plugin_init"); + } else { throw new repository_exception('existingrepository', 'repository'); } @@ -441,6 +445,7 @@ abstract class repository { public $id; public $context; public $options; + public $readonly; /** * 1. Initialize context and options @@ -450,9 +455,10 @@ abstract class repository { * @param integer $contextid * @param array $options */ - public function __construct($repositoryid, $contextid = SITEID, $options = array()) { + public function __construct($repositoryid, $contextid = SITEID, $options = array(), $readonly = 0) { $this->id = $repositoryid; $this->context = get_context_instance_by_id($contextid); + $this->readonly = $readonly; $this->options = array(); if (is_array($options)) { $options = array_merge($this->get_option(), $options); @@ -621,7 +627,7 @@ abstract class repository { * @param array $params the options for this instance * @return */ - final public static function create($type, $userid, $context, $params) { + final public static function create($type, $userid, $context, $params, $readonly=0) { global $CFG, $DB; $params = (array)$params; require_once($CFG->dirroot . '/repository/'. $type . '/repository.class.php'); @@ -633,6 +639,7 @@ abstract class repository { $record->timecreated = time(); $record->timemodified = time(); $record->contextid = $context->id; + $record->readonly = $readonly; $record->userid = $userid; $id = $DB->insert_record('repository_instances', $record); $options = array(); @@ -902,7 +909,7 @@ abstract class repository { * a class extended from repository class, the init() for type has been placed * into the repository. */ - public static function type_init(){ + public static function plugin_init(){ } @@ -1069,7 +1076,7 @@ function repository_get_instances($contexts=array(), $userid = null, $onlyvisibl $options['type'] = $repo->repositorytype; $options['typeid'] = $repo->typeid; $classname = 'repository_' . $repo->repositorytype; - $ret[] = new $classname($repo->id, $repo->contextid, $options); + $ret[] = new $classname($repo->id, $repo->contextid, $options, $repo->readonly); } return $ret; } @@ -1096,7 +1103,7 @@ function repository_get_instance($id) { $options['typeid'] = $instance->typeid; $options['type'] = $instance->repositorytype; $options['name'] = $instance->name; - return new $classname($instance->id, $instance->contextid, $options); + return new $classname($instance->id, $instance->contextid, $options, $instance->readonly); } /** @@ -1412,8 +1419,11 @@ function repository_display_instances_list($context, $typename = null) { foreach ($instances as $i) { $settings = ''; - $settings .= '' . $settingsstr . '' . "\n"; - $delete = '' . $deletestr . '' . "\n"; + $delete = ''; + if (!$i->readonly) { + $settings .= '' . $settingsstr . '' . "\n"; + $delete .= '' . $deletestr . '' . "\n"; + } $type = repository_get_type_by_id($i->typeid); $table->data[] = array($i->name, $type->get_readablename(), $delete, $settings); diff --git a/repository/manage_instances.php b/repository/manage_instances.php index 44fc8b65a1..3b70f58942 100644 --- a/repository/manage_instances.php +++ b/repository/manage_instances.php @@ -10,8 +10,6 @@ $sure = optional_param('sure', '', PARAM_ALPHA); $contextid = optional_param('contextid', 0, PARAM_INT); - $display = true; // fall through to normal display - if ($edit){ $pagename = 'repositoryinstanceedit'; } else if ($delete) { @@ -98,6 +96,10 @@ if (!empty($edit) || !empty($new)) { if (!empty($edit)) { $instance = repository_get_instance($edit); + //if you try to edit an instance set as readonly, display an error message + if ($instance->readonly) { + throw new repository_exception('readonlyinstance', 'repository'); + } $instancetype = repository_get_type_by_id($instance->typeid); $classname = 'repository_' . $instancetype->get_typename(); $configs = $instance->get_instance_option_names(); @@ -152,6 +154,10 @@ } else if (!empty($delete)) { // admin_externalpage_print_header(); $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'); + } if ($sure) { if (!confirm_sesskey()) { print_error('confirmsesskeybad', '', $baseurl); diff --git a/version.php b/version.php index 697ceacf23..1b5c4157cf 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2008091500; // YYYYMMDD = date of the last version bump + $version = 2008091611; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20080916)'; // Human-friendly version name