MDL-48493 admin: Do not require selected plugin type on installation
[moodle.git] / admin / tool / installaddon / classes / installfromzip_form.php
CommitLineData
0056f2a3
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package tool_installaddon
20 * @subpackage classes
21 * @category form
22 * @copyright 2013 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once($CFG->libdir.'/formslib.php');
29
30/**
31 * Defines a simple form for uploading the add-on ZIP package
32 *
33 * @copyright 2013 David Mudrak <david@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
e835ef58 36class tool_installaddon_installfromzip_form extends moodleform {
0056f2a3
DM
37
38 /**
39 * Defines the form elements
40 */
41 public function definition() {
42
43 $mform = $this->_form;
44 $installer = $this->_customdata['installer'];
45
cbd125e8
DM
46 $mform->addElement('header', 'general', get_string('installfromzip', 'tool_installaddon'));
47 $mform->addHelpButton('general', 'installfromzip', 'tool_installaddon');
48
c192a330
DM
49 $mform->addElement('filepicker', 'zipfile', get_string('installfromzipfile', 'tool_installaddon'),
50 null, array('accepted_types' => '.zip'));
51 $mform->addHelpButton('zipfile', 'installfromzipfile', 'tool_installaddon');
52 $mform->addRule('zipfile', null, 'required', null, 'client');
53
0056f2a3 54 $options = $installer->get_plugin_types_menu();
ddab904b
DM
55 $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options,
56 array('id' => 'tool_installaddon_installfromzip_plugintype'));
0056f2a3 57 $mform->addHelpButton('plugintype', 'installfromziptype', 'tool_installaddon');
c192a330 58 $mform->setAdvanced('plugintype');
0056f2a3 59
ddab904b
DM
60 $mform->addElement('static', 'permcheck', '',
61 html_writer::span(get_string('permcheck', 'tool_installaddon'), '',
62 array('id' => 'tool_installaddon_installfromzip_permcheck')));
c192a330 63 $mform->setAdvanced('permcheck');
0056f2a3 64
cbd125e8
DM
65 $mform->addElement('text', 'rootdir', get_string('installfromziprootdir', 'tool_installaddon'));
66 $mform->addHelpButton('rootdir', 'installfromziprootdir', 'tool_installaddon');
67 $mform->setType('rootdir', PARAM_PLUGIN);
68 $mform->setAdvanced('rootdir');
69
0056f2a3
DM
70 $mform->addElement('checkbox', 'acknowledgement', get_string('acknowledgement', 'tool_installaddon'),
71 ' '.get_string('acknowledgementtext', 'tool_installaddon'));
72 $mform->addRule('acknowledgement', get_string('acknowledgementmust', 'tool_installaddon'), 'required', null, 'client');
73
74 $this->add_action_buttons(false, get_string('installfromzipsubmit', 'tool_installaddon'));
75 }
76
c192a330
DM
77 /**
78 * Switch the form to a mode that requires manual selection of the plugin type
79 */
80 public function require_explicit_plugintype() {
81
82 $mform = $this->_form;
83
84 $mform->addRule('plugintype', get_string('required'), 'required', null, 'client');
85 $mform->setAdvanced('plugintype', false);
86 $mform->setAdvanced('permcheck', false);
87
88 $typedetectionfailed = $mform->createElement('static', 'typedetectionfailed', '',
89 html_writer::span(get_string('typedetectionfailed', 'tool_installaddon'), 'error'));
90 $mform->insertElementBefore($typedetectionfailed, 'permcheck');
91 }
92
0056f2a3
DM
93 /**
94 * Validate the form fields
95 *
96 * @param array $data
97 * @param array $files
98 * @return array (string)field name => (string)validation error text
99 */
100 public function validation($data, $files) {
101
ddab904b 102 $installer = $this->_customdata['installer'];
0056f2a3
DM
103 $errors = parent::validation($data, $files);
104
c192a330
DM
105 if (!empty($data['plugintype'])) {
106 if (!$installer->is_plugintype_writable($data['plugintype'])) {
107 $path = $installer->get_plugintype_root($data['plugintype']);
108 $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path));
109 }
ddab904b
DM
110 }
111
0056f2a3
DM
112 return $errors;
113 }
114}