Permalink
Browse files

MDL-38509 Add new tool_installaddon_installer::get_plugintype_root() …

…method
  • Loading branch information...
mudrd8mz committed Mar 22, 2013
1 parent a4fcf56 commit 30bec5ba8dfb1dc8b18aa085e2870700d9b6cf1c
@@ -115,13 +115,15 @@ public function get_plugin_types_menu() {
}
/**
- * 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 @@ public function is_plugintype_writable($plugintype) {
}
}
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!');
}
@@ -78,8 +78,7 @@ public function validation($data, $files) {
$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));
}
@@ -53,20 +53,15 @@
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)) {

0 comments on commit 30bec5b

Please sign in to comment.