Permalink
Browse files

MDL-38509 Initial version of the new admin tool to install add-ons

  • Loading branch information...
mudrd8mz committed Mar 16, 2013
1 parent b8efcb9 commit 0056f2a37b1880d35d8f3f9b170c271cf25a6c1d
@@ -0,0 +1,160 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Provides tool_installaddon_installer class
+ *
+ * @package tool_installaddon
+ * @subpackage classes
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Implements main plugin features.
+ *
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_installaddon_installer {
+
+ /**
+ * Factory method returning an instance of this class.
+ *
+ * @return tool_installaddon_installer
+ */
+ public static function instance() {
+ return new self();
+ }
+
+ /**
+ * Returns URL to the repository that addons can be searched in and installed from
+ *
+ * @return moodle_url
+ */
+ public function get_addons_repository_url() {
+ global $CFG;
+
+ if (!empty($CFG->config_php_settings['alternativeaddonsrepositoryurl'])) {
+ $url = $CFG->config_php_settings['alternativeaddonsrepositoryurl'];
+ } else {
+ $url = 'https://moodle.org/plugins/get.php';
+ }
+
+ if (!$this->should_send_site_info()) {
+ return new moodle_url($url);
+ }
+
+ // Append the basic information about our site.
+ $site = array(
+ 'fullname' => $this->get_site_fullname(),
+ 'url' => $this->get_site_url(),
+ 'major_version' => $this->get_site_major_version(),
+ );
+
+ $site = $this->encode_site_information($site);
+
+ return new moodle_url($url, array('site' => $site));
+ }
+
+ /**
+ * @return tool_installaddon_installfromzip
+ */
+ public function get_installfromzip_form() {
+ global $CFG;
+ require_once(dirname(__FILE__).'/installfromzip_form.php');
+
+ $action = new moodle_url('/admin/tool/installaddon/index.php');
+ $customdata = array('installer' => $this);
+
+ return new tool_installaddon_installfromzip($action, $customdata);
+ }
+
+ /**
+ * Returns localised list of available plugin types
+ *
+ * @return array (string)plugintype => (string)plugin name
+ */
+ public function get_plugin_types_menu() {
+ global $CFG;
+ require_once($CFG->libdir.'/pluginlib.php');
+
+ $pluginman = plugin_manager::instance();
+
+ $menu = array('' => get_string('choosedots'));
+ foreach (array_keys($pluginman->get_plugin_types()) as $plugintype) {
+ $menu[$plugintype] = $pluginman->plugintype_name($plugintype).' ('.$plugintype.')';
+ }
+
+ return $menu;
+ }
+
+ //// End of external API ///////////////////////////////////////////////////
+
+ /**
+ * @return string this site full name
+ */
+ protected function get_site_fullname() {
+ global $SITE;
+
+ return $SITE->fullname;
+ }
+
+ /**
+ * @return string this site URL
+ */
+ protected function get_site_url() {
+ global $CFG;
+
+ return $CFG->wwwroot;
+ }
+
+ /**
+ * @return string major version like 2.5, 2.6 etc.
+ */
+ protected function get_site_major_version() {
+ return moodle_major_version();
+ }
+
+ /**
+ * Encodes the given array in a way that can be safely appended as HTTP GET param
+ *
+ * Be ware! The recipient may rely on the exact way how the site information is encoded.
+ * Do not change anything here unless you know what you are doing and understand all
+ * consequences! (Don't you love warnings like that, too? :-p)
+ *
+ * @param array $info
+ * @return string
+ */
+ protected function encode_site_information(array $info) {
+ return base64_encode(json_encode($info));
+ }
+
+ /**
+ * Decide if the encoded site information should be sent to the add-ons repository site
+ *
+ * For now, we just return true. In the future, we may want to implement some
+ * privacy aware logic (based on site/user preferences for example).
+ *
+ * @return bool
+ */
+ protected function should_send_site_info() {
+ return true;
+ }
+}
@@ -0,0 +1,76 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package tool_installaddon
+ * @subpackage classes
+ * @category form
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->libdir.'/formslib.php');
+
+/**
+ * Defines a simple form for uploading the add-on ZIP package
+ *
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_installaddon_installfromzip extends moodleform {
+
+ /**
+ * Defines the form elements
+ */
+ public function definition() {
+
+ $mform = $this->_form;
+ $installer = $this->_customdata['installer'];
+
+ $options = $installer->get_plugin_types_menu();
+ $mform->addElement('select', 'plugintype', get_string('installfromziptype', 'tool_installaddon'), $options);
+ $mform->addHelpButton('plugintype', 'installfromziptype', 'tool_installaddon');
+ $mform->addRule('plugintype', null, 'required', null, 'client');
+
+ $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->addElement('checkbox', 'acknowledgement', get_string('acknowledgement', 'tool_installaddon'),
+ ' '.get_string('acknowledgementtext', 'tool_installaddon'));
+ $mform->addRule('acknowledgement', get_string('acknowledgementmust', 'tool_installaddon'), 'required', null, 'client');
+
+ $this->add_action_buttons(false, get_string('installfromzipsubmit', 'tool_installaddon'));
+ }
+
+ /**
+ * Validate the form fields
+ *
+ * @param array $data
+ * @param array $files
+ * @return array (string)field name => (string)validation error text
+ */
+ public function validation($data, $files) {
+
+ $errors = parent::validation($data, $files);
+
+ return $errors;
+ }
+}
@@ -0,0 +1,50 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The main screen of the tool.
+ *
+ * @package tool_installaddon
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require(dirname(__FILE__) . '/../../../config.php');
+require_once($CFG->libdir.'/adminlib.php');
+require_once(dirname(__FILE__).'/classes/installer.php');
+
+admin_externalpage_setup('tool_installaddon_index');
+
+if (!empty($CFG->disableonclickaddoninstall)) {
+ notice(get_string('featuredisabled', 'tool_installaddon'));
+}
+
+$installer = tool_installaddon_installer::instance();
+
+$form = $installer->get_installfromzip_form();
+
+if ($form->is_cancelled()) {
+ redirect($PAGE->url);
+
+} else if ($data = $form->get_data()) {
+ // todo $installer->process_installfromzip_form($data);
+}
+
+$output = $PAGE->get_renderer('tool_installaddon');
+$output->set_installer_instance($installer);
+
+echo $output->index_page();
@@ -0,0 +1,41 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Strings for the tool_installaddon component.
+ *
+ * @package tool_installaddon
+ * @category string
+ * @copyright 2013 David Mudrak <david@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+$string['acknowledgement'] = 'Acknowledgement';
+$string['acknowledgementmust'] = 'You must acknowledge this';
+$string['acknowledgementtext'] = 'I understand that it is my responsibility to have full backups of this site prior to installing add-ons. I accept and understand that add-ons (especially but not only those originating in unofficial sources) may contain security holes, can make the site unavailable, or cause private data leaks or loss.';
+$string['featuredisabled'] = 'Add-on installer is disabled at this site.';
+$string['installaddons'] = 'Install add-ons';
+$string['installfromrepo'] = 'Install add-ons from Moodle plugins directory';
+$string['installfromrepo_help'] = 'You will be redirected to the Moodle plugins directory to search for and install an add-on. Note that your site fullname, URL and major version will be sent as well, to make the installation process easier for you.';
+$string['installfromzipfile'] = 'ZIP package';
+$string['installfromzipfile_help'] = 'The plugin ZIP package must contain just one directory with the name of the plugin. The ZIP will be extracted into the appropriate location for the given plugin type. Packages downloaded from the Moodle plugins directory have this format.';
+$string['installfromzipsubmit'] = 'Install add-on from the ZIP file';
+$string['installfromziptype'] = 'Plugin type';
+$string['installfromziptype_help'] = 'Choose the correct type of plugin you are about to install. The installation procedure may fail badly when incorrect plugin type is provided.';
+$string['pluginname'] = 'Add-on installer';
Binary file not shown.
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 15.1.0, SVG Export Plug-In -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
+ <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
+]>
+<svg version="1.1"
+ xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
+ x="0px" y="0px" width="16px" height="16px" viewBox="0 0 16 16" style="overflow:visible;enable-background:new 0 0 16 16;"
+ xml:space="preserve" preserveAspectRatio="xMinYMid meet">
+<defs>
+</defs>
+<path style="fill:#999999;" d="M16,9v6c0,0.5-0.5,1-1,1h-1H2H1c-0.5,0-1-0.5-1-1V9c0-0.5,0.5-1,1-1h1c0.5,0,1,0.5,1,1v4h10V9
+ c0-0.5,0.5-1,1-1h1C15.5,8,16,8.5,16,9z M12.4,5.1l-0.7-0.7c-0.4-0.4-1-0.4-1.4,0L9.5,5.2V1c0-0.5-0.4-1-1-1h-1c-0.5,0-1,0.5-1,1
+ v4.2L5.7,4.4C5.3,4,4.7,4,4.3,4.4L3.6,5.1c-0.4,0.4-0.4,1,0,1.4l3.7,3.7c0.2,0.2,0.5,0.3,0.7,0.3c0.3,0,0.5-0.1,0.7-0.3l3.7-3.7
+ C12.8,6.2,12.8,5.5,12.4,5.1z"/>
+</svg>
Oops, something went wrong.

0 comments on commit 0056f2a

Please sign in to comment.