Skip to content

Commit

Permalink
MDL-45184 tool_licenses: Add custom licenses
Browse files Browse the repository at this point in the history
This feature adds an admin tool for creating custom licenses.
Now custom licenses can be added and amended in Moodle, and the site
default can be set to a custom license.

Core licenses remain hard-coded and are uneditable, so they will always
require update within Moodle core updates, and maintain their
internationalisation through core language strings.

This also includes fundamental changes to the license API including
the addition of license caching and deprecation of no longer required
admin settings for license management.
  • Loading branch information
Tom Dickman committed May 26, 2020
1 parent 71965a8 commit 4399e47
Show file tree
Hide file tree
Showing 39 changed files with 2,433 additions and 265 deletions.
45 changes: 45 additions & 0 deletions admin/settings/license.php
@@ -0,0 +1,45 @@
<?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/>.

/**
* This file defines the settings pages for licenses.
*
* @package core
* @copyright 2020 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/licenselib.php');

if ($hassiteconfig) {

$temp = new admin_settingpage('licensesettings', new lang_string('licensesettings', 'admin'));

$licenses = license_manager::get_active_licenses_as_array();

$temp->add(new admin_setting_configselect('sitedefaultlicense',
new lang_string('configsitedefaultlicense', 'admin'),
new lang_string('configsitedefaultlicensehelp', 'admin'),
'unknown',
$licenses));
$temp->add(new admin_setting_configcheckbox('rememberuserlicensepref',
new lang_string('rememberuserlicensepref', 'admin'),
new lang_string('rememberuserlicensepref_help', 'admin'),
1));
$ADMIN->add('license', $temp);
}
14 changes: 0 additions & 14 deletions admin/settings/plugins.php
Expand Up @@ -182,20 +182,6 @@
$plugin->load_settings($ADMIN, 'mlbackendsettings', $hassiteconfig);
}

/// License types
$ADMIN->add('modules', new admin_category('licensesettings', new lang_string('licenses')));
$temp = new admin_settingpage('managelicenses', new lang_string('managelicenses', 'admin'));

require_once($CFG->libdir . '/licenselib.php');
$licenses = array();
$array = explode(',', $CFG->licenses);
foreach ($array as $value) {
$licenses[$value] = new lang_string($value, 'license');
}
$temp->add(new admin_setting_configselect('sitedefaultlicense', new lang_string('configsitedefaultlicense','admin'), new lang_string('configsitedefaultlicensehelp','admin'), 'allrightsreserved', $licenses));
$temp->add(new admin_setting_managelicenses());
$ADMIN->add('licensesettings', $temp);

/// Filter plugins
$ADMIN->add('modules', new admin_category('filtersettings', new lang_string('managefilters')));

Expand Down
1 change: 1 addition & 0 deletions admin/settings/top.php
Expand Up @@ -32,6 +32,7 @@
$ADMIN->add('root', new admin_category('competencies', new lang_string('competencies', 'core_competency')));
$ADMIN->add('root', new admin_category('badges', new lang_string('badges'), empty($CFG->enablebadges)));
$ADMIN->add('root', new admin_category('h5p', new lang_string('h5p', 'core_h5p')));
$ADMIN->add('root', new admin_category('license', new lang_string('license')));
$ADMIN->add('root', new admin_category('location', new lang_string('location','admin')));
$ADMIN->add('root', new admin_category('language', new lang_string('language')));
$ADMIN->add('root', new admin_category('messaging', new lang_string('messagingcategory', 'admin')));
Expand Down
2 changes: 2 additions & 0 deletions admin/tool/licensemanager/amd/build/delete_license.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions admin/tool/licensemanager/amd/src/delete_license.js
@@ -0,0 +1,51 @@
// 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/>.

/**
* Modal for confirming deletion of a custom license.
*
* @module tool_licensemanager/delete_license
* @class delete_license
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/modal_factory', 'core/modal_events', 'core/url', 'core/str'],
function($, ModalFactory, ModalEvents, Url, String) {

var trigger = $('.delete-license');
ModalFactory.create({
type: ModalFactory.types.SAVE_CANCEL,
title: String.get_string('deletelicense', 'tool_licensemanager'),
body: String.get_string('deletelicenseconfirmmessage', 'tool_licensemanager'),
preShowCallback: function(triggerElement, modal) {
triggerElement = $(triggerElement);
let params = {
'action': 'delete',
'license': triggerElement.data('license')
};
modal.deleteURL = Url.relativeUrl('/admin/tool/licensemanager/index.php', params, true);
},
large: true,
}, trigger)
.done(function(modal) {
modal.getRoot().on(ModalEvents.save, function(e) {
// Stop the default save button behaviour which is to close the modal.
e.preventDefault();
// Redirect to delete url.
window.location.href = modal.deleteURL;
});
});
});
124 changes: 124 additions & 0 deletions admin/tool/licensemanager/classes/form/edit_license.php
@@ -0,0 +1,124 @@
<?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/>.

/**
* Form for creating/updating a custom license.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_licensemanager\form;

use moodleform;
use tool_licensemanager\helper;
use tool_licensemanager\manager;

defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');

global $CFG;
require_once($CFG->libdir . '/formslib.php');

/**
* Form for creating/updating a custom license.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class edit_license extends moodleform {

/**
* @var string the action form is taking.
*/
private $action;

/**
* @var string license shortname if editing or empty string if creating license.
*/
private $licenseshortname;

/**
* edit_license constructor.
*
* @param string $action the license_manager action to be taken by form.
* @param string $licenseshortname the shortname of the license to edit.
*/
public function __construct(string $action, string $licenseshortname) {
$this->action = $action;
$this->licenseshortname = $licenseshortname;

if ($action == manager::ACTION_UPDATE && !empty($licenseshortname)) {
parent::__construct(helper::get_update_license_url($licenseshortname));
} else {
parent::__construct(helper::get_create_license_url());
}
}

/**
* Form definition for creation and editing of licenses.
*/
public function definition() {

$mform = $this->_form;

$mform->addElement('text', 'shortname', get_string('shortname', 'tool_licensemanager'));
$mform->setType('shortname', PARAM_ALPHANUMEXT);
// Shortname is only editable when user is creating a license.
if ($this->action != manager::ACTION_CREATE) {
$mform->freeze('shortname');
} else {
$mform->addRule('shortname', get_string('shortnamerequirederror', 'tool_licensemanager'), 'required');
}

$mform->addElement('text', 'fullname', get_string('fullname', 'tool_licensemanager'));
$mform->setType('fullname', PARAM_TEXT);
$mform->addRule('fullname', get_string('fullnamerequirederror', 'tool_licensemanager'), 'required');

$mform->addElement('text', 'source', get_string('source', 'tool_licensemanager'));
$mform->setType('source', PARAM_URL);
$mform->addHelpButton('source', 'source', 'tool_licensemanager');
$mform->addRule('source', get_string('sourcerequirederror', 'tool_licensemanager'), 'required');

$mform->addElement('date_selector', 'version', get_string('version', 'tool_licensemanager'), get_string('from'));
$mform->addHelpButton('version', 'version', 'tool_licensemanager');

$this->add_action_buttons();
}

/**
* Validate form data and return errors (if any).
*
* @param array $data array of ("fieldname"=>value) of submitted data
* @param array $files array of uploaded files "element_name"=>tmp_file_path
* @return array of "element_name"=>"error_description" if there are errors,
* or an empty array if everything is OK (true allowed for backwards compatibility too).
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);

if (array_key_exists('source', $data) && !filter_var($data['source'], FILTER_VALIDATE_URL)) {
$errors['source'] = get_string('invalidurl', 'tool_licensemanager');
}

if (array_key_exists('version', $data) && $data['version'] > time()) {
$errors['version'] = get_string('versioncannotbefuture', 'tool_licensemanager');
}

return $errors;
}
}

0 comments on commit 4399e47

Please sign in to comment.