Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_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   */
  36  class tool_installaddon_installfromzip_form extends moodleform {
  37  
  38      /**
  39       * Defines the form elements
  40       */
  41      public function definition() {
  42  
  43          $mform = $this->_form;
  44          $installer = $this->_customdata['installer'];
  45  
  46          $mform->addElement('header', 'general', get_string('installfromzip', 'tool_installaddon'));
  47          $mform->addHelpButton('general', 'installfromzip', 'tool_installaddon');
  48  
  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  
  54          $options = $installer->get_plugin_types_menu();
  55          $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options,
  56              array('id' => 'tool_installaddon_installfromzip_plugintype'));
  57          $mform->addHelpButton('plugintype', 'installfromziptype', 'tool_installaddon');
  58          $mform->setAdvanced('plugintype');
  59  
  60          $mform->addElement('static', 'permcheck', '',
  61              html_writer::span(get_string('permcheck', 'tool_installaddon'), '',
  62                  array('id' => 'tool_installaddon_installfromzip_permcheck')));
  63          $mform->setAdvanced('permcheck');
  64  
  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  
  70          $this->add_action_buttons(false, get_string('installfromzipsubmit', 'tool_installaddon'));
  71      }
  72  
  73      /**
  74       * Switch the form to a mode that requires manual selection of the plugin type
  75       */
  76      public function require_explicit_plugintype() {
  77  
  78          $mform = $this->_form;
  79  
  80          $mform->addRule('plugintype', get_string('required'), 'required', null, 'client');
  81          $mform->setAdvanced('plugintype', false);
  82          $mform->setAdvanced('permcheck', false);
  83  
  84          $typedetectionfailed = $mform->createElement('static', 'typedetectionfailed', '',
  85              html_writer::span(get_string('typedetectionfailed', 'tool_installaddon'), 'error'));
  86          $mform->insertElementBefore($typedetectionfailed, 'permcheck');
  87      }
  88  
  89      /**
  90       * Warn that the selected plugin type does not match the detected one.
  91       *
  92       * @param string $detected detected plugin type
  93       */
  94      public function selected_plugintype_mismatch($detected) {
  95  
  96          $mform = $this->_form;
  97          $mform->addRule('plugintype', get_string('required'), 'required', null, 'client');
  98          $mform->setAdvanced('plugintype', false);
  99          $mform->setAdvanced('permcheck', false);
 100          $mform->insertElementBefore($mform->createElement('static', 'selectedplugintypemismatch', '',
 101              html_writer::span(get_string('typedetectionmismatch', 'tool_installaddon', $detected), 'error')), 'permcheck');
 102      }
 103  
 104      /**
 105       * Validate the form fields
 106       *
 107       * @param array $data
 108       * @param array $files
 109       * @return array (string)field name => (string)validation error text
 110       */
 111      public function validation($data, $files) {
 112  
 113          $pluginman = core_plugin_manager::instance();
 114          $errors = parent::validation($data, $files);
 115  
 116          if (!empty($data['plugintype'])) {
 117              if (!$pluginman->is_plugintype_writable($data['plugintype'])) {
 118                  $path = $pluginman->get_plugintype_root($data['plugintype']);
 119                  $errors['plugintype'] = get_string('permcheckresultno', 'tool_installaddon', array('path' => $path));
 120              }
 121          }
 122  
 123          return $errors;
 124      }
 125  }