-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from catalyst/add-capability
Add manage capability and move rules to own page
- Loading branch information
Showing
7 changed files
with
201 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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 to manage redirect rules. | ||
* | ||
* @package tool_redirects | ||
* @author Benjamin Walker <[email protected]> | ||
* @copyright 2024, Catalyst IT | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace tool_redirects\form; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once("$CFG->libdir/formslib.php"); | ||
|
||
/** | ||
* Manage redirects form. | ||
*/ | ||
class manage extends \moodleform { | ||
/** @var array config settings attached to the form */ | ||
protected const FORM_CONFIG = [ | ||
'rules', | ||
]; | ||
|
||
/** @var array config */ | ||
protected $config; | ||
|
||
/** | ||
* Form definition | ||
* | ||
* @return void | ||
*/ | ||
public function definition(): void { | ||
global $CFG, $DB, $USER; | ||
|
||
$mform = $this->_form; | ||
|
||
// Rules. | ||
$mform->addElement('textarea', 'rules', get_string('rules', 'tool_redirects'), ['cols' => 60, 'rows' => 8]); | ||
$mform->setType('rules', PARAM_RAW); | ||
$mform->addElement('static', 'rules_help', '', get_string('rules_desc', 'tool_redirects')); | ||
|
||
$this->add_action_buttons(false); | ||
} | ||
|
||
/** | ||
* Loads the current values of the form config. | ||
* @return void | ||
*/ | ||
public function load_form_config(): void { | ||
$config = []; | ||
foreach (self::FORM_CONFIG as $name) { | ||
$config[$name] = get_config('tool_redirects', $name); | ||
} | ||
|
||
// Save empty config values but don't set them. Needed for comparisons. | ||
$this->config = $config; | ||
$this->set_data(array_filter($config)); | ||
} | ||
|
||
/** | ||
* Saves the new form data to config. | ||
* @param \stdClass $data submitted data | ||
* @return void | ||
*/ | ||
public function save_form_config(\stdClass $data): void { | ||
foreach ($data as $key => $value) { | ||
if (in_array($key, self::FORM_CONFIG) && $value !== $this->config[$key]) { | ||
set_config($key, $value, 'tool_redirects'); | ||
add_to_config_log($key, (string) $this->config[$key], $value, 'tool_redirects'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Capabilities. | ||
* | ||
* This files lists capabilities related to tool_redirects. | ||
* | ||
* @package tool_redirects | ||
* @author Benjamin Walker <[email protected]> | ||
* @copyright 2024, Catalyst IT | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
$capabilities = [ | ||
'tool/redirects:manage' => [ | ||
'riskbitmask' => RISK_XSS | RISK_CONFIG, | ||
'captype' => 'write', | ||
'contextlevel' => CONTEXT_SYSTEM, | ||
'archetypes' => [ | ||
'manager' => CAP_ALLOW, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Page to manage redirect rules | ||
* | ||
* @package tool_redirects | ||
* @author Benjamin Walker <[email protected]> | ||
* @copyright 2024, Catalyst IT | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
require_once(__DIR__ . '/../../../config.php'); | ||
|
||
// This is locked behind a separate capability so the task can be delegated to non-admins. | ||
require_login(); | ||
$context = context_system::instance(); | ||
require_capability('tool/redirects:manage', $context); | ||
|
||
// Set up the page. | ||
$url = new moodle_url('/admin/tool/redirects/index.php'); | ||
$PAGE->set_url($url); | ||
$PAGE->set_context($context); | ||
$PAGE->set_pagelayout('admin'); | ||
$PAGE->set_title(get_string('redirects:manage', 'tool_redirects')); | ||
$PAGE->set_heading($SITE->fullname); | ||
|
||
// Create the form and load current config. | ||
$mform = new \tool_redirects\form\manage(); | ||
$mform->load_form_config(); | ||
|
||
// Handle form submission. | ||
if ($mform->is_cancelled()) { | ||
redirect(new moodle_url('/')); | ||
} else if ($data = $mform->get_data()) { | ||
$mform->save_form_config($data); | ||
redirect($url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS); | ||
} | ||
|
||
// Display the form. | ||
echo $OUTPUT->header(); | ||
echo $OUTPUT->heading(get_string('redirects:manage', 'tool_redirects')); | ||
$mform->display(); | ||
echo $OUTPUT->footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters