3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Provides tool_installaddon_installer class
21 * @package tool_installaddon
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 * Implements main plugin features.
32 * @copyright 2013 David Mudrak <david@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class tool_installaddon_installer {
37 /** @var tool_installaddon_installfromzip */
38 protected $installfromzipform = null;
41 * Factory method returning an instance of this class.
43 * @return tool_installaddon_installer
45 public static function instance() {
50 * Returns URL to the repository that addons can be searched in and installed from
54 public function get_addons_repository_url() {
57 if (!empty($CFG->config_php_settings['alternativeaddonsrepositoryurl'])) {
58 $url = $CFG->config_php_settings['alternativeaddonsrepositoryurl'];
60 $url = 'https://moodle.org/plugins/get.php';
63 if (!$this->should_send_site_info()) {
64 return new moodle_url($url);
67 // Append the basic information about our site.
69 'fullname' => $this->get_site_fullname(),
70 'url' => $this->get_site_url(),
71 'major_version' => $this->get_site_major_version(),
74 $site = $this->encode_site_information($site);
76 return new moodle_url($url, array('site' => $site));
80 * @return tool_installaddon_installfromzip
82 public function get_installfromzip_form() {
84 require_once(dirname(__FILE__).'/installfromzip_form.php');
86 if (!is_null($this->installfromzipform)) {
87 return $this->installfromzipform;
90 $action = new moodle_url('/admin/tool/installaddon/index.php');
91 $customdata = array('installer' => $this);
93 $this->installfromzipform = new tool_installaddon_installfromzip($action, $customdata);
95 return $this->installfromzipform;
99 * Saves the ZIP file from the {@link tool_installaddon_installfromzip} form
101 * The file is saved into the given temporary location for inspection and eventual
102 * deployment. The form is expected to be submitted and validated.
104 * @param tool_installaddon_installfromzip $form
105 * @param string $targetdir full path to the directory where the ZIP should be stored to
106 * @return string filename of the saved file relative to the given target
108 public function save_installfromzip_file(tool_installaddon_installfromzip $form, $targetdir) {
110 $filename = clean_param($form->get_new_filename('zipfile'), PARAM_FILE);
111 $form->save_file('zipfile', $targetdir.'/'.$filename);
117 * Returns localised list of available plugin types
119 * @return array (string)plugintype => (string)plugin name
121 public function get_plugin_types_menu() {
123 require_once($CFG->libdir.'/pluginlib.php');
125 $pluginman = plugin_manager::instance();
127 $menu = array('' => get_string('choosedots'));
128 foreach (array_keys($pluginman->get_plugin_types()) as $plugintype) {
129 $menu[$plugintype] = $pluginman->plugintype_name($plugintype).' ('.$plugintype.')';
136 * Returns the full path of the root of the given plugin type
138 * Null is returned if the plugin type is not known. False is returned if the plugin type
139 * root is expected but not found. Otherwise, string is returned.
141 * @param string $plugintype
142 * @return string|bool|null
144 public function get_plugintype_root($plugintype) {
146 $plugintypepath = null;
147 foreach (get_plugin_types() as $type => $fullpath) {
148 if ($type === $plugintype) {
149 $plugintypepath = $fullpath;
153 if (is_null($plugintypepath)) {
157 if (!is_dir($plugintypepath)) {
161 return $plugintypepath;
165 * Is it possible to create a new plugin directory for the given plugin type?
167 * @throws coding_exception for invalid plugin types or non-existing plugin type locations
168 * @param string $plugintype
171 public function is_plugintype_writable($plugintype) {
173 $plugintypepath = $this->get_plugintype_root($plugintype);
175 if (is_null($plugintypepath)) {
176 throw new coding_exception('Unknown plugin type!');
179 if ($plugintypepath === false) {
180 throw new coding_exception('Plugin type location does not exist!');
183 return is_writable($plugintypepath);
186 //// End of external API ///////////////////////////////////////////////////
189 * @return string this site full name
191 protected function get_site_fullname() {
194 return $SITE->fullname;
198 * @return string this site URL
200 protected function get_site_url() {
203 return $CFG->wwwroot;
207 * @return string major version like 2.5, 2.6 etc.
209 protected function get_site_major_version() {
210 return moodle_major_version();
214 * Encodes the given array in a way that can be safely appended as HTTP GET param
216 * Be ware! The recipient may rely on the exact way how the site information is encoded.
217 * Do not change anything here unless you know what you are doing and understand all
218 * consequences! (Don't you love warnings like that, too? :-p)
223 protected function encode_site_information(array $info) {
224 return base64_encode(json_encode($info));
228 * Decide if the encoded site information should be sent to the add-ons repository site
230 * For now, we just return true. In the future, we may want to implement some
231 * privacy aware logic (based on site/user preferences for example).
235 protected function should_send_site_info() {