first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -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_NOTAGS);
// 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'));
$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,0 +1,139 @@
<?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/>.
namespace tool_licensemanager;
use moodle_url;
/**
* License manager helper class.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Moodle relative path to the licenses manager.
*/
const MANAGER_PATH = '/admin/tool/licensemanager/index.php';
/**
* Get the URL for viewing the license manager interface.
*
* @return \moodle_url
*/
public static function get_licensemanager_url(): moodle_url {
return new moodle_url(self::MANAGER_PATH);
}
/**
* Get the URL for endpoint enabling a license.
*
* @param string $licenseshortname the shortname of license to enable.
*
* @return \moodle_url
*/
public static function get_enable_license_url(string $licenseshortname): moodle_url {
$url = new moodle_url(self::MANAGER_PATH,
['action' => manager::ACTION_ENABLE, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
return $url;
}
/**
* Get the URL for endpoint disabling a license.
*
* @param string $licenseshortname the shortname of license to disable.
*
* @return \moodle_url
*/
public static function get_disable_license_url(string $licenseshortname): moodle_url {
$url = new moodle_url(self::MANAGER_PATH,
['action' => manager::ACTION_DISABLE, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
return $url;
}
/**
* Get the URL endpoint to create a new license.
*
* @return \moodle_url
*/
public static function get_create_license_url(): moodle_url {
$url = self::get_licensemanager_url();
$url->params(['action' => manager::ACTION_CREATE]);
return $url;
}
/**
* Get the URL endpoint to update an existing license.
*
* @param string $licenseshortname the shortname of license to update.
*
* @return \moodle_url
*/
public static function get_update_license_url(string $licenseshortname): moodle_url {
$url = self::get_licensemanager_url();
$url->params(['action' => manager::ACTION_UPDATE, 'license' => $licenseshortname]);
return $url;
}
/**
* Get the URL endpoint to move a license up order.
*
* @param string $licenseshortname the shortname of license to move up.
*
* @return \moodle_url
*/
public static function get_moveup_license_url(string $licenseshortname): moodle_url {
$url = new moodle_url(self::MANAGER_PATH,
['action' => manager::ACTION_MOVE_UP, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
return $url;
}
/**
* Get the URL endpoint to move a license down order.
*
* @param string $licenseshortname the shortname of license to move down.
*
* @return \moodle_url
*/
public static function get_movedown_license_url(string $licenseshortname): moodle_url {
$url = new moodle_url(self::MANAGER_PATH,
['action' => manager::ACTION_MOVE_DOWN, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
return $url;
}
/**
* Convert a license version number string to a UNIX epoch.
*
* @param string $version
*
* @return int $epoch
*/
public static function convert_version_to_epoch(string $version): int {
$date = substr($version, 0, 8);
$epoch = strtotime($date);
return $epoch;
}
}
@@ -0,0 +1,253 @@
<?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/>.
namespace tool_licensemanager;
use tool_licensemanager\form\edit_license;
use license_manager;
use stdClass;
/**
* License manager, main controller for tool_licensemanager.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class manager {
/**
* Action for creating a new custom license.
*/
const ACTION_CREATE = 'create';
/**
* Action for updating a custom license's details.
*/
const ACTION_UPDATE = 'update';
/**
* Action for deleting a custom license.
*/
const ACTION_DELETE = 'delete';
/**
* Action for disabling a custom license.
*/
const ACTION_DISABLE = 'disable';
/**
* Action for enabling a custom license.
*/
const ACTION_ENABLE = 'enable';
/**
* Action for displaying the license list view.
*/
const ACTION_VIEW_LICENSE_MANAGER = 'viewlicensemanager';
/**
* Action for moving a license up order.
*/
const ACTION_MOVE_UP = 'moveup';
/**
* Action for moving a license down order.
*/
const ACTION_MOVE_DOWN = 'movedown';
/**
* Entry point for internal license manager.
*
* @param string $action the api action to carry out.
* @param string|object $license the license object or shortname of license to carry action out on.
*/
public function execute(string $action, $license): void {
admin_externalpage_setup('licensemanager');
// Convert license to a string if it's a full license object.
if (is_object($license)) {
$license = $license->shortname;
}
$viewmanager = true;
$message = null;
$redirect = helper::get_licensemanager_url();
switch ($action) {
case self::ACTION_DISABLE:
require_sesskey();
license_manager::disable($license);
redirect($redirect);
break;
case self::ACTION_ENABLE:
require_sesskey();
license_manager::enable($license);
redirect($redirect);
break;
case self::ACTION_DELETE:
require_sesskey();
try {
license_manager::delete($license);
} catch (\moodle_exception $e) {
$message = $e->getMessage();
}
redirect($redirect, $message);
break;
case self::ACTION_CREATE:
case self::ACTION_UPDATE:
$viewmanager = $this->edit($action, $license);
break;
case self::ACTION_MOVE_UP:
case self::ACTION_MOVE_DOWN:
require_sesskey();
$this->change_license_order($action, $license);
redirect($redirect);
break;
case self::ACTION_VIEW_LICENSE_MANAGER:
default:
break;
}
if ($viewmanager) {
$this->view_license_manager($message);
}
}
/**
* Edit an existing license or create a new license.
*
* @param string $action the form action to carry out.
* @param string $licenseshortname the shortname of the license to edit.
*
* @return bool true if license editing complete, false otherwise.
*/
private function edit(string $action, string $licenseshortname): bool {
if ($action != self::ACTION_CREATE && $action != self::ACTION_UPDATE) {
throw new \coding_exception('license edit actions are limited to create and update');
}
$form = new form\edit_license($action, $licenseshortname);
if ($form->is_cancelled()) {
return true;
} else if ($data = $form->get_data()) {
$license = new stdClass();
if ($action == self::ACTION_CREATE) {
// Check that license shortname isn't already in use.
if (!empty(license_manager::get_license_by_shortname($data->shortname))) {
throw new \moodle_exception('duplicatelicenseshortname', 'tool_licensemanager',
helper::get_licensemanager_url(),
$data->shortname);
}
$license->shortname = $data->shortname;
} else {
if (empty(license_manager::get_license_by_shortname($licenseshortname))) {
throw new \moodle_exception('licensenotfoundshortname', 'license',
helper::get_licensemanager_url(),
$licenseshortname);
}
$license->shortname = $licenseshortname;
}
$license->fullname = $data->fullname;
$license->source = $data->source;
// Legacy date format maintained to prevent breaking on upgrade.
$license->version = date('Ymd', $data->version) . '00';
license_manager::save($license);
return true;
} else {
$this->view_license_editor($action, $licenseshortname, $form);
return false;
}
}
/**
* Change license order by moving up or down license order.
*
* @param string $direction which direction to move, up or down.
* @param string $licenseshortname the shortname of the license to move up or down order.
*/
private function change_license_order(string $direction, string $licenseshortname): void {
if (!empty($licenseshortname)) {
if ($direction == self::ACTION_MOVE_UP) {
license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_UP, $licenseshortname);
} else if ($direction == self::ACTION_MOVE_DOWN) {
license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_DOWN, $licenseshortname);
}
}
}
/**
* View the license editor to create or edit a license.
*
* @param string $action
* @param string $licenseshortname the shortname of the license to create/edit.
* @param \tool_licensemanager\form\edit_license $form the form for submitting edit data.
*/
private function view_license_editor(string $action, string $licenseshortname, edit_license $form): void {
global $PAGE;
$renderer = $PAGE->get_renderer('tool_licensemanager');
if ($action == self::ACTION_UPDATE && $license = license_manager::get_license_by_shortname($licenseshortname)) {
$return = $renderer->render_edit_licence_headers($licenseshortname);
$form->set_data(['shortname' => $license->shortname]);
$form->set_data(['fullname' => $license->fullname]);
$form->set_data(['source' => $license->source]);
$form->set_data(['version' => helper::convert_version_to_epoch($license->version)]);
} else {
$return = $renderer->render_create_licence_headers();
}
$return .= $form->render();
$return .= $renderer->footer();
echo $return;
}
/**
* View the license manager.
*/
private function view_license_manager(string $message = null): void {
global $PAGE, $OUTPUT;
$renderer = $PAGE->get_renderer('tool_licensemanager');
$html = $renderer->header();
$html .= $renderer->heading(get_string('licensemanager', 'tool_licensemanager'));
if (!empty($message)) {
$html .= $OUTPUT->notification($message);
}
$table = new \tool_licensemanager\output\table();
$html .= $renderer->render($table);
$html .= $renderer->footer();
echo $html;
}
}
@@ -0,0 +1,96 @@
<?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/>.
/**
* Renderer for 'tool_licensemanager' component.
*
* @package tool_licensemanager
* @copyright Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_licensemanager\output;
defined('MOODLE_INTERNAL') || die();
use license_manager;
use plugin_renderer_base;
use tool_licensemanager\helper;
/**
* Renderer class for 'tool_licensemanager' component.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends plugin_renderer_base {
/**
* Render the headers for create license form.
*
* @return string html fragment for display.
*/
public function render_create_licence_headers(): string {
$this->page->navbar->add(get_string('createlicense', 'tool_licensemanager'),
helper::get_create_license_url());
$return = $this->header();
$return .= $this->heading(get_string('createlicense', 'tool_licensemanager'));
return $return;
}
/**
* Render the headers for edit license form.
*
* @param string $licenseshortname the shortname of license to edit.
*
* @return string html fragment for display.
*/
public function render_edit_licence_headers(string $licenseshortname): string {
$this->page->navbar->add(get_string('editlicense', 'tool_licensemanager'),
helper::get_update_license_url($licenseshortname));
$return = $this->header();
$return .= $this->heading(get_string('editlicense', 'tool_licensemanager'));
return $return;
}
/**
* Render the license manager table.
*
* @param \renderable $table the renderable.
*
* @return string HTML.
*/
public function render_table(\renderable $table) {
$licenses = license_manager::get_licenses();
// Add the create license button.
$html = $table->create_license_link();
// Add the table containing licenses for management.
$html .= $this->box_start('generalbox editorsui');
$html .= $table->create_license_manager_table($licenses, $this);
$html .= $this->box_end();
return $html;
}
}
@@ -0,0 +1,196 @@
<?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/>.
/**
* Renderable for display of license manager table.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_licensemanager\output;
use html_table;
use html_table_cell;
use html_table_row;
use html_writer;
use license_manager;
defined('MOODLE_INTERNAL') || die();
/**
* Renderable for display of license manager table.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class table implements \renderable {
/**
* 'Create License' link.
*
* @return string HTML string.
*/
public function create_license_link() {
$link = html_writer::link(\tool_licensemanager\helper::get_create_license_url(),
get_string('createlicensebuttontext', 'tool_licensemanager'),
['class' => 'btn btn-secondary mb-3']);
return $link;
}
/**
* Create the HTML table for license management.
*
* @param array $licenses
* @param \renderer_base $output
*
* @return string HTML for license manager table.
*/
public function create_license_manager_table(array $licenses, \renderer_base $output) {
$table = new html_table();
$table->head = [
get_string('enable'),
get_string('license', 'tool_licensemanager'),
get_string('version'),
get_string('order'),
get_string('edit'),
get_string('delete'),
];
$table->colclasses = [
'text-center',
'text-left',
'text-left',
'text-center',
'text-center',
'text-center',
];
$table->id = 'manage-licenses';
$table->attributes['class'] = 'admintable generaltable';
$table->data = [];
$rownumber = 0;
$rowcount = count($licenses);
foreach ($licenses as $key => $value) {
$canmoveup = $rownumber > 0;
$canmovedown = $rownumber < $rowcount - 1;
$table->data[] = $this->get_license_table_row_data($value, $canmoveup, $canmovedown, $output);
$rownumber++;
}
$html = html_writer::table($table);
return $html;
}
/**
* Get table row data for a license.
*
* @param object $license the license to populate row data for.
* @param bool $canmoveup can this row move up.
* @param bool $canmovedown can this row move down.
* @param \renderer_base $output the renderer
*
* @return \html_table_row of columns values for row.
*/
protected function get_license_table_row_data($license, bool $canmoveup, bool $canmovedown, \renderer_base $output) {
global $CFG;
$summary = $license->fullname . ' ('. $license->shortname . ')';
if (!empty($license->source)) {
$summary .= html_writer::empty_tag('br');
$summary .= html_writer::link($license->source, $license->source, ['target' => '_blank']);
}
$summarycell = new html_table_cell($summary);
$summarycell->attributes['class'] = 'license-summary';
$versioncell = new html_table_cell($license->version);
$versioncell->attributes['class'] = 'license-version';
$deletelicense = '';
if ($license->shortname == $CFG->sitedefaultlicense) {
$hideshow = $output->pix_icon('t/locked', get_string('sitedefaultlicenselock', 'tool_licensemanager'));
} else {
if ($license->enabled == license_manager::LICENSE_ENABLED) {
$hideshow = html_writer::link(\tool_licensemanager\helper::get_disable_license_url($license->shortname),
$output->pix_icon('t/hide', get_string('disablelicensename', 'tool_licensemanager', $license->fullname)));
} else {
$hideshow = html_writer::link(\tool_licensemanager\helper::get_enable_license_url($license->shortname),
$output->pix_icon('t/show', get_string('enablelicensename', 'tool_licensemanager', $license->fullname)));
}
if ($license->custom == license_manager::CUSTOM_LICENSE) {
$deletelink = new \moodle_url('/admin/tool/licensemanager/index.php', [
'action' => 'delete',
'license' => $license->shortname,
'sesskey' => sesskey(),
]);
$deletelicense = html_writer::link(
url: '#',
text: $output->pix_icon('i/trash', get_string('deletelicensename', 'tool_licensemanager', $license->fullname)),
attributes: [
'class' => 'delete-license',
'data-modal' => 'confirmation',
'data-modal-title-str' => json_encode(['deletelicense', 'tool_licensemanager']),
'data-modal-content-str' => json_encode(['deletelicenseconfirmmessage', 'tool_licensemanager']),
'data-modal-destination' => $deletelink->out(false),
],
);
}
}
$hideshowcell = new html_table_cell($hideshow);
$hideshowcell->attributes['class'] = 'license-status';
if ($license->custom == license_manager::CUSTOM_LICENSE) {
$editlicense = html_writer::link(\tool_licensemanager\helper::get_update_license_url($license->shortname),
$output->pix_icon('t/editinline', get_string('editlicensename', 'tool_licensemanager', $license->fullname)),
['class' => 'edit-license']);
} else {
$editlicense = '';
}
$editlicensecell = new html_table_cell($editlicense);
$editlicensecell->attributes['class'] = 'edit-license';
$spacer = $output->pix_icon('spacer', '', 'moodle', ['class' => 'iconsmall']);
$updown = '';
if ($canmoveup) {
$updown .= html_writer::link(\tool_licensemanager\helper::get_moveup_license_url($license->shortname),
$output->pix_icon('t/up', get_string('movelicenseupname', 'tool_licensemanager', $license->fullname),
'moodle', ['class' => 'iconsmall']),
['class' => 'move-up']) . '';
} else {
$updown .= $spacer;
}
if ($canmovedown) {
$updown .= '&nbsp;'.html_writer::link(\tool_licensemanager\helper::get_movedown_license_url($license->shortname),
$output->pix_icon('t/down', get_string('movelicensedownname', 'tool_licensemanager', $license->fullname),
'moodle', ['class' => 'iconsmall']),
['class' => 'move-down']);
} else {
$updown .= $spacer;
}
$updowncell = new html_table_cell($updown);
$updowncell->attributes['class'] = 'license-order';
$row = new html_table_row([$hideshowcell, $summarycell, $versioncell, $updowncell, $editlicensecell, $deletelicense]);
$row->attributes['data-license'] = $license->shortname;
$row->attributes['class'] = strtolower(get_string('license', 'tool_licensemanager'));
return $row;
}
}
@@ -0,0 +1,47 @@
<?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/>.
/**
* Privacy Subsystem implementation for tool_licensemanager implementing null_provider.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_licensemanager\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem implementation for tool_licensemanager implementing null_provider.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+40
View File
@@ -0,0 +1,40 @@
<?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/>.
/**
* License manager page.
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/licenselib.php');
require_admin();
$action = optional_param('action', '', PARAM_ALPHANUMEXT);
$license = optional_param('license', '', PARAM_NOTAGS);
// Route via the manager.
$licensemanager = new \tool_licensemanager\manager();
$PAGE->set_context(context_system::instance());
$PAGE->set_url(\tool_licensemanager\helper::get_licensemanager_url());
$PAGE->set_title(get_string('licensemanager', 'tool_licensemanager'));
$licensemanager->execute($action, $license);
@@ -0,0 +1,52 @@
<?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 component 'tool_licensemanager', language 'en'
*
* @package tool_licensemanager
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['pluginname'] = 'Licence manager';
$string['createlicense'] = 'Create custom licence';
$string['createlicensebuttontext'] = 'Create licence';
$string['deletelicense'] = 'Delete licence';
$string['deletelicenseconfirmmessage'] = 'Are you sure you want to delete this licence?';
$string['deletelicensename'] = 'Delete licence \'{$a}\'';
$string['disablelicensename'] = 'Disable licence \'{$a}\'';
$string['duplicatelicenseshortname'] = 'The licence short name must be unique.';
$string['editlicense'] = 'Edit licence';
$string['editlicensename'] = 'Edit licence \'{$a}\'';
$string['enablelicensename'] = 'Enable licence \'{$a}\'';
$string['fullname'] = 'Licence full name';
$string['fullnamerequirederror'] = 'You must enter a full name for the licence.';
$string['invalidurl'] = 'Invalid source URL';
$string['license'] = 'Licence';
$string['licensemanager'] = 'Licence manager';
$string['movelicensedownname'] = 'Move \'{$a}\' licence down';
$string['movelicenseupname'] = 'Move \'{$a}\' licence up';
$string['privacy:metadata'] = 'The tool_licensemanager plugin stores no personal data.';
$string['shortname'] = 'Licence short name';
$string['sitedefaultlicenselock'] = 'This is the site default licence. It cannot be disabled.';
$string['shortnamerequirederror'] = 'You must enter a short name for the licence.';
$string['source'] = 'Licence source';
$string['source_help'] = 'The URL (with http:// or https:// prefix) where the licence terms and conditions can be found.';
$string['sourcerequirederror'] = 'You must enter a valid URL for licence source.';
$string['version'] = 'Licence version';
$string['versioncannotbefuture'] = 'Licence version cannot be set to a future date.';
$string['version_help'] = 'Publication date of the licence version being utilised.';
+33
View File
@@ -0,0 +1,33 @@
<?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/>.
/**
* Settings page.
*
* @package tool_licensemanager
* @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;
if ($hassiteconfig) {
$temp = new admin_externalpage('licensemanager',
get_string('licensemanager', 'tool_licensemanager'),
\tool_licensemanager\helper::get_licensemanager_url());
$ADMIN->add('license', $temp);
}
@@ -0,0 +1,49 @@
@tool @tool_licensemanager
Feature: Delete custom licenses
In order to manage custom licenses
As an admin
I need to be able to delete custom licenses but not standard Moodle licenses
@javascript
Scenario: I can delete a custom license
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | MIT |
| fullname | MIT Licence |
| source | https://opensource.org/licenses/MIT |
| Licence version | ##1 March 2019## |
And I press "Save changes"
And I click on "Delete" "icon" in the "MIT" "table_row"
When I click on "Yes" "button" in the "Delete licence" "dialogue"
Then I should not see "MIT Licence" in the "manage-licenses" "table"
Scenario: I cannot delete a standard license
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
Then I should see "Licence not specified" in the "unknown" "table_row"
And I should not see "Delete" in the "unknown" "table_row"
@javascript @_file_upload
Scenario: I cannot delete a custom license in use
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | Test licence |
| fullname | Test licence |
| source | https://opensource.org/licenses/MIT |
| Licence version | ##1 March 2019## |
And I press "Save changes"
And I follow "Private files" in the user menu
And I upload "lib/tests/fixtures/gd-logo.png" file to "Files" filemanager
And I click on "gd-logo.png" "link"
And I set the field "Choose licence" to "Test licence"
And I press "Update"
And I press "Save changes"
And I am on site homepage
And I navigate to "Licence > Licence manager" in site administration
And I click on "Delete" "icon" in the "Test licence" "table_row"
When I click on "Yes" "button" in the "Delete licence" "dialogue"
Then I should see "Cannot delete a licence which is currently assigned to one or more files"
@@ -0,0 +1,71 @@
@tool @tool_licensemanager
Feature: Custom licences
In order to use custom licences
As an admin
I need to be able to add custom licences
Scenario: I am able to create custom licences
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | MIT |
| fullname | MIT Licence |
| source | https://opensource.org/licenses/MIT |
| Licence version | ##first day of January 2020## |
When I press "Save changes"
Then I should see "Licence manager"
And I should see "MIT Licence" in the "MIT" "table_row"
And I should see "https://opensource.org/licenses/MIT" in the "MIT" "table_row"
Scenario: I am only be able to make custom license with a valid url source (including scheme).
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | MIT |
| fullname | MIT Licence |
| source | opensource.org/licenses/MIT |
| Licence version | ##2020-01-01## |
When I press "Save changes"
Then I should see "Invalid source URL"
And I set the following fields to these values:
| source | mailto:tomdickman@catalyst-au.net |
And I press "Save changes"
And I should see "Invalid source URL"
And I set the following fields to these values:
| source | https://opensource.org/licenses/MIT |
And I press "Save changes"
And I should see "Licence manager"
And I should see "MIT Licence" in the "MIT" "table_row"
And I should see "https://opensource.org/licenses/MIT" in the "MIT" "table_row"
Scenario: Custom license version format must be YYYYMMDD00
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | MIT |
| fullname | MIT Licence |
| source | https://opensource.org/licenses/MIT |
| Licence version | ##1 March 2019## |
When I press "Save changes"
Then I should see "Licence manager"
And I should see "2019030100" in the "MIT" "table_row"
@javascript
Scenario: Custom license short name should not be editable after first creation
Given I log in as "admin"
And I navigate to "Licence > Licence manager" in site administration
And I click on "Create licence" "link"
And I set the following fields to these values:
| shortname | MIT |
| fullname | MIT Licence |
| source | https://opensource.org/licenses/MIT |
| Licence version | ##1 Mar 2019## |
And I press "Save changes"
And I should see "Licence manager"
And I should see "MIT Licence" in the "MIT" "table_row"
When I click on "Edit" "icon" in the "MIT" "table_row"
Then I should see "Edit licence"
And the "shortname" "field" should be disabled
@@ -0,0 +1,58 @@
@tool @tool_licensemanager
Feature: Licence manager
In order to manage licences
As an admin
I need to be able to view and alter licence preferences in the licence manager.
Scenario: I should be able to see the default Moodle licences.
Given I log in as "admin"
When I navigate to "Licence > Licence manager" in site administration
Then I should see "Licence not specified" in the "unknown" "table_row"
And I should see "All rights reserved" in the "allrightsreserved" "table_row"
And I should see "Public domain" in the "public" "table_row"
And I should see "Creative Commons - 4.0 International" in the "cc-4.0" "table_row"
And I should see "Creative Commons - NoDerivatives 4.0 International" in the "cc-nd-4.0" "table_row"
And I should see "Creative Commons - NonCommercial-NoDerivatives 4.0 International" in the "cc-nc-nd-4.0" "table_row"
And I should see "Creative Commons - NonCommercial-ShareAlike 4.0 International" in the "cc-nc-sa-4.0" "table_row"
And I should see "Creative Commons - ShareAlike 4.0 International" in the "cc-sa-4.0" "table_row"
And I should see "Creative Commons - NonCommercial 4.0 International" in the "cc-nc-4.0" "table_row"
Scenario: I should be able to enable and disable licences
Given I log in as "admin"
And I navigate to "Licence > Licence settings" in site administration
When I set the field "Default site licence" to "Public domain"
And I press "Save changes"
And I navigate to "Licence > Licence manager" in site administration
Then "This is the site default licence" "icon" should exist in the "public" "table_row"
And "Enable licence" "icon" should not exist in the "public" "table_row"
And "This is the site default licence" "icon" should not exist in the "cc-4.0" "table_row"
And I navigate to "Licence > Licence settings" in site administration
And I set the field "Default site licence" to "Creative Commons"
And I press "Save changes"
And I navigate to "Licence > Licence manager" in site administration
And "This is the site default licence" "icon" should exist in the "cc-4.0" "table_row"
And "Enable licence" "icon" should not exist in the "cc-4.0" "table_row"
And "This is the site default licence" "icon" should not exist in the "public" "table_row"
@javascript @_file_upload
Scenario Outline: User licence preference is remembered depending of setting value
Given the following config values are set as admin:
| sitedefaultlicense | cc-4.0 |
| rememberuserlicensepref | <rememberuserlicensepref> |
And I log in as "admin"
And I follow "Private files" in the user menu
And I follow "Add..."
And I follow "Upload a file"
And the field with xpath "//select[@name='license']" matches value "Creative Commons - 4.0 International"
And I click on "Close" "button" in the "File picker" "dialogue"
When I upload "lib/tests/fixtures/empty.txt" file to "Files" filemanager as:
| Save as | empty_copy.txt |
| license | Public domain |
And I press "Save changes"
And I follow "Add..."
Then the field with xpath "//select[@name='license']" matches value "<expectedlicence>"
Examples:
| rememberuserlicensepref | expectedlicence |
| 0 | Creative Commons - 4.0 International |
| 1 | Public domain |
@@ -0,0 +1,44 @@
<?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/>.
/**
* Tests for tool_licensemanager helper class.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Tests for tool_licensemanager helper class.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group tool_licensemanager
*/
class helper_test extends advanced_testcase {
public function test_convert_version_to_epoch(): void {
$version = '2020010100';
$expected = strtotime(20200101);
$this->assertEquals($expected, \tool_licensemanager\helper::convert_version_to_epoch($version));
}
}
@@ -0,0 +1,194 @@
<?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/>.
/**
* Tests for tool_licensemanager manager class.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/licenselib.php');
/**
* Tests for tool_licensemanager manager class.
*
* @package tool_licensemanager
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group tool_licensemanager
*/
class manager_test extends advanced_testcase {
/**
* Test editing a license.
*/
public function test_edit_existing_license(): void {
$this->resetAfterTest();
// Create initial custom license to edit.
$testlicense = new stdClass();
$testlicense->shortname = 'my-lic';
$testlicense->fullname = 'My License';
$testlicense->source = 'https://fakeurl.net';
$testlicense->version = date('Ymd', time()) . '00';
$testlicense->custom = license_manager::CUSTOM_LICENSE;
license_manager::save($testlicense);
license_manager::enable($testlicense->shortname);
$manager = new \tool_licensemanager\manager();
// Attempt to submit form data with altered details.
$formdata = [
'shortname' => 'new-value',
'fullname' => 'New License Name',
'source' => 'https://updatedfakeurl.net',
'version' => time()
];
// Attempt to submit form data with an altered shortname.
\tool_licensemanager\form\edit_license::mock_submit($formdata);
// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $testlicense->shortname);
// Should not create a new license when updating an existing license.
$this->assertEmpty(license_manager::get_license_by_shortname($formdata['shortname']));
$actual = license_manager::get_license_by_shortname('my-lic');
// Should not be able to update the shortname of the license.
$this->assertNotSame($formdata['shortname'], $actual->shortname);
// Should be able to update other details of the license.
$this->assertSame($formdata['fullname'], $actual->fullname);
$this->assertSame($formdata['source'], $actual->source);
$this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
}
public function test_edit_license_not_exists(): void {
$manager = new \tool_licensemanager\manager();
// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
// Attempt to update a license that doesn't exist.
$formdata = [
'shortname' => 'new-value',
'fullname' => 'New License Name',
'source' => 'https://updatedfakeurl.net',
'version' => time()
];
\tool_licensemanager\form\edit_license::mock_submit($formdata);
// Should not be able to update a license with a shortname that doesn't exist.
$this->expectException('moodle_exception');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $formdata['shortname']);
}
public function test_edit_license_no_shortname(): void {
$manager = new \tool_licensemanager\manager();
// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
// Attempt to update a license without passing license shortname.
$formdata = [
'fullname' => 'New License Name',
'source' => 'https://updatedfakeurl.net',
'version' => time()
];
\tool_licensemanager\form\edit_license::mock_submit($formdata);
// Should not be able to update empty license shortname.
$this->expectException('moodle_exception');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, '');
}
/**
* Test creating a new license.
*/
public function test_edit_create_license(): void {
$this->resetAfterTest();
$licensecount = count(license_manager::get_licenses());
$manager = new \tool_licensemanager\manager();
$formdata = [
'shortname' => 'new-value',
'fullname' => 'My License',
'source' => 'https://fakeurl.net',
'version' => time()
];
// Attempt to submit form data for a new license.
\tool_licensemanager\form\edit_license::mock_submit($formdata);
// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
// Should create a new license in database.
$this->assertCount($licensecount + 1, license_manager::get_licenses());
$actual = license_manager::get_license_by_shortname($formdata['shortname']);
$this->assertSame($formdata['shortname'], $actual->shortname);
$this->assertSame($formdata['fullname'], $actual->fullname);
$this->assertSame($formdata['source'], $actual->source);
$this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
// Attempt to submit form data for a duplicate license.
\tool_licensemanager\form\edit_license::mock_submit($formdata);
// Should not be able to create duplicate licenses.
$this->expectException('moodle_exception');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
}
/**
* Test changing the order of licenses.
*/
public function test_change_license_order(): void {
$this->resetAfterTest();
$licenseorder = array_keys(license_manager::get_licenses());
$initialposition = array_search('cc-nc-4.0', $licenseorder);
$manager = new tool_licensemanager\manager();
// We're testing a private method, so we need to setup reflector magic.
$method = new ReflectionMethod('\tool_licensemanager\manager', 'change_license_order');
$method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_UP, 'cc-nc-4.0');
$licenseorder = array_keys(license_manager::get_licenses());
$newposition = array_search('cc-nc-4.0', $licenseorder);
$this->assertLessThan($initialposition, $newposition);
$initialposition = array_search('allrightsreserved', $licenseorder);
$method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_DOWN, 'allrightsreserved');
$licenseorder = array_keys(license_manager::get_licenses());
$newposition = array_search('cc-nc-4.0', $licenseorder);
$this->assertGreaterThan($initialposition, $newposition);
}
}
+31
View File
@@ -0,0 +1,31 @@
<?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/>.
/**
* Version details for component 'tool_licensemanager'.
*
* @package tool_licensemanager
* @copyright Tom Dickman <tomdickman@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200;
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'tool_licensemanager';
$plugin->maturity = MATURITY_STABLE;