MDL-38509 Add new tool_installaddon_installer::get_plugintype_root() method
authorDavid Mudrák <david@moodle.com>
Fri, 22 Mar 2013 16:38:59 +0000 (17:38 +0100)
committerDavid Mudrák <david@moodle.com>
Thu, 28 Mar 2013 10:54:06 +0000 (11:54 +0100)
admin/tool/installaddon/classes/installer.php
admin/tool/installaddon/classes/installfromzip_form.php
admin/tool/installaddon/permcheck.php

index 2833254..60397ca 100644 (file)
@@ -115,13 +115,15 @@ class tool_installaddon_installer {
     }
 
     /**
-     * Is it possible to create a new plugin directory for the given plugin type?
+     * Returns the full path of the root of the given plugin type
+     *
+     * Null is returned if the plugin type is not known. False is returned if the plugin type
+     * root is expected but not found. Otherwise, string is returned.
      *
-     * @throws coding_exception for invalid plugin types or non-existing plugin type locations
      * @param string $plugintype
-     * @return boolean
+     * @return string|bool|null
      */
-    public function is_plugintype_writable($plugintype) {
+    public function get_plugintype_root($plugintype) {
 
         $plugintypepath = null;
         foreach (get_plugin_types() as $type => $fullpath) {
@@ -131,10 +133,32 @@ class tool_installaddon_installer {
             }
         }
         if (is_null($plugintypepath)) {
-            throw new coding_exception('Unknown plugin type!');
+            return null;
         }
 
         if (!is_dir($plugintypepath)) {
+            return false;
+        }
+
+        return $plugintypepath;
+    }
+
+    /**
+     * Is it possible to create a new plugin directory for the given plugin type?
+     *
+     * @throws coding_exception for invalid plugin types or non-existing plugin type locations
+     * @param string $plugintype
+     * @return boolean
+     */
+    public function is_plugintype_writable($plugintype) {
+
+        $plugintypepath = $this->get_plugintype_root($plugintype);
+
+        if (is_null($plugintypepath)) {
+            throw new coding_exception('Unknown plugin type!');
+        }
+
+        if ($plugintypepath === false) {
             throw new coding_exception('Plugin type location does not exist!');
         }
 
index aef05c7..9942679 100644 (file)
@@ -78,8 +78,7 @@ class tool_installaddon_installfromzip extends moodleform {
         $errors = parent::validation($data, $files);
 
         if (!$installer->is_plugintype_writable($data['plugintype'])) {
-            $paths = get_plugin_types(true);
-            $path = $paths[$data['plugintype']];
+            $path = $installer->get_plugintype_root($data['plugintype']);
             $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path));
         }
 
index d2125bb..f38d109 100644 (file)
@@ -53,20 +53,15 @@ if (is_null($plugintype)) {
     die();
 }
 
-$plugintypepath = null;
-foreach (get_plugin_types() as $type => $fullpath) {
-    if ($type === $plugintype) {
-        $plugintypepath = $fullpath;
-        break;
-    }
-}
-if (is_null($plugintypepath)) {
+$installer = tool_installaddon_installer::instance();
+
+$plugintypepath = $installer->get_plugintype_root($plugintype);
+
+if (empty($plugintypepath)) {
     header('HTTP/1.1 400 Bad Request');
     die();
 }
 
-$installer = tool_installaddon_installer::instance();
-
 $response = array('path' => $plugintypepath);
 
 if ($installer->is_plugintype_writable($plugintype)) {