Skip to content

Commit

Permalink
MDL-48493 admin: Do not require selected plugin type on installation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mudrd8mz committed Jan 15, 2015
1 parent bbf3cd4 commit c192a33
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
37 changes: 28 additions & 9 deletions admin/tool/installaddon/classes/installfromzip_form.php
Expand Up @@ -46,20 +46,21 @@ public function definition() {
$mform->addElement('header', 'general', get_string('installfromzip', 'tool_installaddon'));
$mform->addHelpButton('general', 'installfromzip', 'tool_installaddon');

$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->addRule('plugintype', null, 'required', null, 'client');
$mform->setAdvanced('plugintype');

$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');
$mform->setAdvanced('permcheck');

$mform->addElement('text', 'rootdir', get_string('installfromziprootdir', 'tool_installaddon'));
$mform->addHelpButton('rootdir', 'installfromziprootdir', 'tool_installaddon');
Expand All @@ -73,6 +74,22 @@ public function definition() {
$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
*
Expand All @@ -85,9 +102,11 @@ public function validation($data, $files) {
$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;
Expand Down
26 changes: 19 additions & 7 deletions admin/tool/installaddon/index.php
Expand Up @@ -52,14 +52,26 @@
$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.
Expand Down
6 changes: 4 additions & 2 deletions admin/tool/installaddon/lang/en/tool_installaddon.php
Expand Up @@ -43,7 +43,8 @@
$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 ...';
Expand All @@ -55,13 +56,14 @@
$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';
Expand Down

0 comments on commit c192a33

Please sign in to comment.