From c192a330018c6e22e64b53765ff30ac2893d0c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= <david@moodle.com> Date: Fri, 12 Dec 2014 13:32:50 +0100 Subject: [PATCH] MDL-48493 admin: Do not require selected plugin type on installation Now we allow the plugin type left unselected and we attempt to auto-detect it. Only when the auto-detection fails, the admin has to manually select the type of the plugin. --- .../classes/installfromzip_form.php | 45 +++++++++++++------ admin/tool/installaddon/index.php | 26 ++++++++--- .../lang/en/tool_installaddon.php | 6 ++- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/admin/tool/installaddon/classes/installfromzip_form.php b/admin/tool/installaddon/classes/installfromzip_form.php index 8a6d6a05082..46b271ca026 100644 --- a/admin/tool/installaddon/classes/installfromzip_form.php +++ b/admin/tool/installaddon/classes/installfromzip_form.php @@ -46,21 +46,22 @@ class tool_installaddon_installfromzip_form extends moodleform { $mform->addElement('header', 'general', get_string('installfromzip', 'tool_installaddon')); $mform->addHelpButton('general', 'installfromzip', 'tool_installaddon'); - $options = $installer->get_plugin_types_menu(); - $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options, - array('id' => 'tool_installaddon_installfromzip_plugintype')); - $mform->addHelpButton('plugintype', 'installfromziptype', 'tool_installaddon'); - $mform->addRule('plugintype', null, 'required', null, 'client'); - - $mform->addElement('static', 'permcheck', '', - html_writer::span(get_string('permcheck', 'tool_installaddon'), '', - array('id' => 'tool_installaddon_installfromzip_permcheck'))); - $mform->addElement('filepicker', 'zipfile', get_string('installfromzipfile', 'tool_installaddon'), null, array('accepted_types' => '.zip')); $mform->addHelpButton('zipfile', 'installfromzipfile', 'tool_installaddon'); $mform->addRule('zipfile', null, 'required', null, 'client'); + $options = $installer->get_plugin_types_menu(); + $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options, + array('id' => 'tool_installaddon_installfromzip_plugintype')); + $mform->addHelpButton('plugintype', 'installfromziptype', 'tool_installaddon'); + $mform->setAdvanced('plugintype'); + + $mform->addElement('static', 'permcheck', '', + html_writer::span(get_string('permcheck', 'tool_installaddon'), '', + array('id' => 'tool_installaddon_installfromzip_permcheck'))); + $mform->setAdvanced('permcheck'); + $mform->addElement('text', 'rootdir', get_string('installfromziprootdir', 'tool_installaddon')); $mform->addHelpButton('rootdir', 'installfromziprootdir', 'tool_installaddon'); $mform->setType('rootdir', PARAM_PLUGIN); @@ -73,6 +74,22 @@ class tool_installaddon_installfromzip_form extends moodleform { $this->add_action_buttons(false, get_string('installfromzipsubmit', 'tool_installaddon')); } + /** + * Switch the form to a mode that requires manual selection of the plugin type + */ + public function require_explicit_plugintype() { + + $mform = $this->_form; + + $mform->addRule('plugintype', get_string('required'), 'required', null, 'client'); + $mform->setAdvanced('plugintype', false); + $mform->setAdvanced('permcheck', false); + + $typedetectionfailed = $mform->createElement('static', 'typedetectionfailed', '', + html_writer::span(get_string('typedetectionfailed', 'tool_installaddon'), 'error')); + $mform->insertElementBefore($typedetectionfailed, 'permcheck'); + } + /** * Validate the form fields * @@ -85,9 +102,11 @@ class tool_installaddon_installfromzip_form extends moodleform { $installer = $this->_customdata['installer']; $errors = parent::validation($data, $files); - if (!$installer->is_plugintype_writable($data['plugintype'])) { - $path = $installer->get_plugintype_root($data['plugintype']); - $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path)); + if (!empty($data['plugintype'])) { + if (!$installer->is_plugintype_writable($data['plugintype'])) { + $path = $installer->get_plugintype_root($data['plugintype']); + $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path)); + } } return $errors; diff --git a/admin/tool/installaddon/index.php b/admin/tool/installaddon/index.php index 5249694de7a..d7eb99bf66e 100644 --- a/admin/tool/installaddon/index.php +++ b/admin/tool/installaddon/index.php @@ -52,14 +52,26 @@ if ($form->is_cancelled()) { $jobid = md5(rand().uniqid('', true)); $sourcedir = make_temp_directory('tool_installaddon/'.$jobid.'/source'); $zipfilename = $installer->save_installfromzip_file($form, $sourcedir); + if (empty($data->plugintype)) { + $versiondir = make_temp_directory('tool_installaddon/'.$jobid.'/version'); + $detected = $installer->detect_plugin_component($sourcedir.'/'.$zipfilename, $versiondir); + list($detectedtype, $detectedname) = core_component::normalize_component($detected); + if ($detectedtype and $detectedname and $detectedtype !== 'core') { + $data->plugintype = $detectedtype; + } else { + $form->require_explicit_plugintype(); + } + } // Redirect to the validation page. - $nexturl = new moodle_url('/admin/tool/installaddon/validate.php', array( - 'sesskey' => sesskey(), - 'jobid' => $jobid, - 'zip' => $zipfilename, - 'type' => $data->plugintype, - 'rootdir' => $data->rootdir)); - redirect($nexturl); + if (!empty($data->plugintype)) { + $nexturl = new moodle_url('/admin/tool/installaddon/validate.php', array( + 'sesskey' => sesskey(), + 'jobid' => $jobid, + 'zip' => $zipfilename, + 'type' => $data->plugintype, + 'rootdir' => $data->rootdir)); + redirect($nexturl); + } } // Output starts here. diff --git a/admin/tool/installaddon/lang/en/tool_installaddon.php b/admin/tool/installaddon/lang/en/tool_installaddon.php index b0135b9f0aa..236ad0057bb 100644 --- a/admin/tool/installaddon/lang/en/tool_installaddon.php +++ b/admin/tool/installaddon/lang/en/tool_installaddon.php @@ -43,7 +43,8 @@ $string['installfromziprootdir'] = 'Rename the root directory'; $string['installfromziprootdir_help'] = 'Some ZIP packages, such as those generated by Github, may contain an incorrect root directory name. If so, the correct name may be entered here.'; $string['installfromzipsubmit'] = 'Install plugin from the ZIP file'; $string['installfromziptype'] = 'Plugin type'; -$string['installfromziptype_help'] = 'Choose the correct type of plugin you are about to install. Warning: The installation procedure can fail badly if an incorrect plugin type is specified.'; +$string['installfromziptype_help'] = 'For plugins that correctly declare their component name, the installer is able to detect the plugin type automatically. If the auto-detection fails, choose the correct type of plugin manually. Warning: The installation procedure can fail badly if an incorrect plugin type is specified.'; +$string['installfromziptype_link'] = 'Development:Plugins'; $string['permcheck'] = 'Make sure the plugin type root location is writable by the web server process.'; $string['permcheckerror'] = 'Error while checking for write permission'; $string['permcheckprogress'] = 'Checking for write permission ...'; @@ -55,13 +56,14 @@ $string['remoterequestconfirm'] = 'There is a request to install plugin <strong> $string['remoterequestinvalid'] = 'There is a request to install a plugin from the Moodle plugins directory on this site. Unfortunately the request is not valid and so the plugin cannot be installed.'; $string['remoterequestpermcheck'] = 'There is a request to install plugin {$a->name} ({$a->component}) version {$a->version} from the Moodle plugins directory on this site. However, the location <strong>{$a->typepath}</strong> is <strong>not writable</strong>. You need to give write access for the web server user to the location, then press the continue button to repeat the check.'; $string['remoterequestpluginfoexception'] = 'Oops... An error occurred while trying to obtain information about the plugin {$a->name} ({$a->component}) version {$a->version}. The plugin cannot be installed. Turn debugging mode on to see details of the error.'; +$string['typedetectionfailed'] = 'Unable to detect the plugin type. Please choose the plugin type manually.'; $string['validation'] = 'Plugin package validation'; $string['validationmsg_componentmatch'] = 'Full component name'; $string['validationmsg_componentmismatchname'] = 'Plugin name mismatch'; $string['validationmsg_componentmismatchname_help'] = 'Some ZIP packages, such as those generated by Github, may contain an incorrect root directory name. You need to fix the name of the root directory to match the declared plugin name.'; $string['validationmsg_componentmismatchname_info'] = 'The plugin declares its name is \'{$a}\' but that does not match the name of the root directory.'; $string['validationmsg_componentmismatchtype'] = 'Plugin type mismatch'; -$string['validationmsg_componentmismatchtype_info'] = 'You have selected the type \'{$a->expected}\' but the plugin declares its type is \'{$a->found}\'.'; +$string['validationmsg_componentmismatchtype_info'] = 'Expected type \'{$a->expected}\' but the plugin declares its type is \'{$a->found}\'.'; $string['validationmsg_filenotexists'] = 'Extracted file not found'; $string['validationmsg_filesnumber'] = 'Not enough files found in the package'; $string['validationmsg_filestatus'] = 'Unable to extract all files';