See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace tool_licensemanager; 18 19 use tool_licensemanager\form\edit_license; 20 use license_manager; 21 use stdClass; 22 23 /** 24 * License manager, main controller for tool_licensemanager. 25 * 26 * @package tool_licensemanager 27 * @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class manager { 31 32 /** 33 * Action for creating a new custom license. 34 */ 35 const ACTION_CREATE = 'create'; 36 37 /** 38 * Action for updating a custom license's details. 39 */ 40 const ACTION_UPDATE = 'update'; 41 42 /** 43 * Action for deleting a custom license. 44 */ 45 const ACTION_DELETE = 'delete'; 46 47 /** 48 * Action for disabling a custom license. 49 */ 50 const ACTION_DISABLE = 'disable'; 51 52 /** 53 * Action for enabling a custom license. 54 */ 55 const ACTION_ENABLE = 'enable'; 56 57 /** 58 * Action for displaying the license list view. 59 */ 60 const ACTION_VIEW_LICENSE_MANAGER = 'viewlicensemanager'; 61 62 /** 63 * Action for moving a license up order. 64 */ 65 const ACTION_MOVE_UP = 'moveup'; 66 67 /** 68 * Action for moving a license down order. 69 */ 70 const ACTION_MOVE_DOWN = 'movedown'; 71 72 /** 73 * Entry point for internal license manager. 74 * 75 * @param string $action the api action to carry out. 76 * @param string|object $license the license object or shortname of license to carry action out on. 77 */ 78 public function execute(string $action, $license) : void { 79 80 admin_externalpage_setup('licensemanager'); 81 82 // Convert license to a string if it's a full license object. 83 if (is_object($license)) { 84 $license = $license->shortname; 85 } 86 87 $viewmanager = true; 88 $redirect = helper::get_licensemanager_url(); 89 90 switch ($action) { 91 case self::ACTION_DISABLE: 92 require_sesskey(); 93 license_manager::disable($license); 94 redirect($redirect); 95 break; 96 97 case self::ACTION_ENABLE: 98 require_sesskey(); 99 license_manager::enable($license); 100 redirect($redirect); 101 break; 102 103 case self::ACTION_DELETE: 104 require_sesskey(); 105 license_manager::delete($license); 106 redirect($redirect); 107 break; 108 109 case self::ACTION_CREATE: 110 case self::ACTION_UPDATE: 111 $viewmanager = $this->edit($action, $license); 112 break; 113 114 case self::ACTION_MOVE_UP: 115 case self::ACTION_MOVE_DOWN: 116 require_sesskey(); 117 $this->change_license_order($action, $license); 118 redirect($redirect); 119 break; 120 121 case self::ACTION_VIEW_LICENSE_MANAGER: 122 default: 123 break; 124 } 125 if ($viewmanager) { 126 $this->view_license_manager(); 127 } 128 } 129 130 /** 131 * Edit an existing license or create a new license. 132 * 133 * @param string $action the form action to carry out. 134 * @param string $licenseshortname the shortname of the license to edit. 135 * 136 * @return bool true if license editing complete, false otherwise. 137 */ 138 private function edit(string $action, string $licenseshortname) : bool { 139 140 if ($action != self::ACTION_CREATE && $action != self::ACTION_UPDATE) { 141 throw new \coding_exception('license edit actions are limited to create and update'); 142 } 143 144 $form = new form\edit_license($action, $licenseshortname); 145 146 if ($form->is_cancelled()) { 147 return true; 148 } else if ($data = $form->get_data()) { 149 150 $license = new stdClass(); 151 if ($action == self::ACTION_CREATE) { 152 // Check that license shortname isn't already in use. 153 if (!empty(license_manager::get_license_by_shortname($data->shortname))) { 154 throw new \moodle_exception('duplicatelicenseshortname', 'tool_licensemanager', 155 helper::get_licensemanager_url(), 156 $data->shortname); 157 } 158 $license->shortname = $data->shortname; 159 } else { 160 if (empty(license_manager::get_license_by_shortname($licenseshortname))) { 161 throw new \moodle_exception('licensenotfoundshortname', 'license', 162 helper::get_licensemanager_url(), 163 $licenseshortname); 164 } 165 $license->shortname = $licenseshortname; 166 } 167 $license->fullname = $data->fullname; 168 $license->source = $data->source; 169 // Legacy date format maintained to prevent breaking on upgrade. 170 $license->version = date('Ymd', $data->version) . '00'; 171 172 license_manager::save($license); 173 174 return true; 175 } else { 176 $this->view_license_editor($action, $licenseshortname, $form); 177 178 return false; 179 } 180 } 181 182 /** 183 * Change license order by moving up or down license order. 184 * 185 * @param string $direction which direction to move, up or down. 186 * @param string $licenseshortname the shortname of the license to move up or down order. 187 */ 188 private function change_license_order(string $direction, string $licenseshortname) : void { 189 190 if (!empty($licenseshortname)) { 191 if ($direction == self::ACTION_MOVE_UP) { 192 license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_UP, $licenseshortname); 193 } else if ($direction == self::ACTION_MOVE_DOWN) { 194 license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_DOWN, $licenseshortname); 195 } 196 } 197 } 198 199 /** 200 * View the license editor to create or edit a license. 201 * 202 * @param string $action 203 * @param string $licenseshortname the shortname of the license to create/edit. 204 * @param \tool_licensemanager\form\edit_license $form the form for submitting edit data. 205 */ 206 private function view_license_editor(string $action, string $licenseshortname, edit_license $form) : void { 207 global $PAGE; 208 209 $renderer = $PAGE->get_renderer('tool_licensemanager'); 210 211 if ($action == self::ACTION_UPDATE && $license = license_manager::get_license_by_shortname($licenseshortname)) { 212 $return = $renderer->render_edit_licence_headers($licenseshortname); 213 214 $form->set_data(['shortname' => $license->shortname]); 215 $form->set_data(['fullname' => $license->fullname]); 216 $form->set_data(['source' => $license->source]); 217 $form->set_data(['version' => helper::convert_version_to_epoch($license->version)]); 218 219 } else { 220 $return = $renderer->render_create_licence_headers(); 221 } 222 $return .= $form->render(); 223 $return .= $renderer->footer(); 224 225 echo $return; 226 } 227 228 /** 229 * View the license manager. 230 */ 231 private function view_license_manager() : void { 232 global $PAGE; 233 234 $PAGE->requires->js_call_amd('tool_licensemanager/delete_license'); 235 236 $renderer = $PAGE->get_renderer('tool_licensemanager'); 237 $html = $renderer->header(); 238 $html .= $renderer->heading(get_string('licensemanager', 'tool_licensemanager')); 239 240 $table = new \tool_licensemanager\output\table(); 241 $html .= $renderer->render($table); 242 $html .= $renderer->footer(); 243 244 echo $html; 245 } 246 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body