From: dongsheng <dongsheng>
Date: Thu, 5 Mar 2009 04:40:51 +0000 (+0000)
Subject: "REPOSITORY/MDL-18354, enabled local and upload repository plugins when upgrading"
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a75c78d3180308aeab027eac5f7ae83d4d078e79;p=moodle.git

"REPOSITORY/MDL-18354, enabled local and upload repository plugins when upgrading"
---

diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php
index afb002fc40..003b490099 100644
--- a/lang/en_utf8/repository.php
+++ b/lang/en_utf8/repository.php
@@ -79,6 +79,7 @@ $string['saveas'] = 'Save as';
 $string['saved'] = 'Saved';
 $string['saving'] = 'Saving';
 $string['settings'] = 'Settings';
+$string['setupdefaultplugins'] = 'Setting up default repository plugins';
 $string['search'] = 'Search ';
 $string['searching'] = 'Search in ';
 $string['siteinstances'] = 'Repositories instances of the site';
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index c661d18f2b..18cbdfa561 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -1472,7 +1472,6 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
     /// Main savepoint reached
         upgrade_main_savepoint($result, 2009021801);
     }
-
     /// Add default sort order for question types.
     if ($result && $oldversion < 2009030300) {
         set_config('multichoice_sortorder', 1, 'question');
@@ -1490,6 +1489,13 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
 
         upgrade_main_savepoint($result, 2009030300);
     }
+    if ($result && $oldversion < 2009030501) {
+    /// setup default repository plugins
+        require_once($CFG->dirroot . '/repository/lib.php');
+        repository_setup_default_plugins();
+    /// Main savepoint reached
+        upgrade_main_savepoint($result, 2009030501);
+    }
 
     return $result;
 }
diff --git a/repository/lib.php b/repository/lib.php
index ea8720ee7c..b3538b72d1 100644
--- a/repository/lib.php
+++ b/repository/lib.php
@@ -205,9 +205,12 @@ class repository_type {
 
     /**
      * Create a repository type (the type name must not already exist)
+     * @param boolean throw exception?
+     * @return mixed return int if create successfully, return false if
+     *         any errors
      * @global object $DB
      */
-    public function create() {
+    public function create($silent = false) {
         global $DB;
 
         //check that $type has been set
@@ -227,7 +230,9 @@ class repository_type {
         if (!$existingtype) {
             //run init function
             if (!repository::static_function($this->_typename, 'plugin_init')) {
-                throw new repository_exception('cannotcreatetype', 'repository');
+                if (!$silent) {
+                    throw new repository_exception('cannotcreatetype', 'repository');
+                }
             }
 
             //create the type
@@ -235,7 +240,7 @@ class repository_type {
             $newtype->type = $this->_typename;
             $newtype->visible = $this->_visible;
             $newtype->sortorder = $this->_sortorder;
-            $DB->insert_record('repository', $newtype);
+            $plugin_id = $DB->insert_record('repository', $newtype);
 
             //save the options in DB
             $this->update_options();
@@ -249,9 +254,19 @@ class repository_type {
                 $instanceoptions['name'] = $this->_typename;
                 repository::static_function($this->_typename, 'create', $this->_typename, 0, get_system_context(), $instanceoptions);
             }
+            if(!empty($plugin_id)) {
+                // return plugin_id if create successfully
+                return $plugin_id;
+            } else {
+                return false;
+            }
 
         } else {
-            throw new repository_exception('existingrepository', 'repository');
+            if (!$silent) {
+                throw new repository_exception('existingrepository', 'repository');
+            }
+            // If plugin existed, return false, tell caller no new plugins were created.
+            return false;
         }
     }
 
@@ -1806,4 +1821,14 @@ final class repository_type_form extends moodleform {
     }
 }
 
-
+function repository_setup_default_plugins() {
+    //if the plugin type has no multiple instance (e.g. has no instance option name)
+    //repository_type::create will create an instance automatically
+    $local_plugin = new repository_type('local', array(), true);
+    $local_plugin_id = $local_plugin->create(true);
+    $upload_plugin = new repository_type('upload', array(), true);
+    $upload_plugin_id = $upload_plugin->create(true);
+    if (is_int($local_plugin_id) or is_int($upload_plugin_id)) {
+        print_box(get_string('setupdefaultplugins', 'repository'));
+    }
+}
diff --git a/version.php b/version.php
index 9283d50468..63c4e11d9e 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 = 2009030300;  // YYYYMMDD   = date of the last version bump
+    $version = 2009030501;  // YYYYMMDD   = date of the last version bump
                             //         XX = daily increments
 
     $release = '2.0 dev (Build: 20090305)';  // Human-friendly version name