|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace local_datacleaner; |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * The text setting where input is encoded before save. |
| 22 | + * |
| 23 | + * @package local_datacleaner |
| 24 | + * @author Dustin Huynh <[email protected]> |
| 25 | + * @copyright 2025, Catalyst IT |
| 26 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 27 | + */ |
| 28 | +class admin_setting_configencodedtext extends \admin_setting_configtext { |
| 29 | + |
| 30 | + /** |
| 31 | + * Return the setting |
| 32 | + * |
| 33 | + * @return mixed returns config if successful else null |
| 34 | + */ |
| 35 | + public function get_setting() { |
| 36 | + global $CFG; |
| 37 | + return base64_decode($CFG->original_wwwroot) ?? null; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Write the setting |
| 42 | + */ |
| 43 | + public function write_setting($data) { |
| 44 | + global $CFG; |
| 45 | + if ($this->paramtype === PARAM_INT && $data === '') { |
| 46 | + $data = 0; |
| 47 | + } |
| 48 | + $validated = $this->validate($data); |
| 49 | + if ($validated !== true) { |
| 50 | + return $validated; |
| 51 | + } |
| 52 | + $originalwwwroot = base64_encode($data); |
| 53 | + return set_config('original_wwwroot', $originalwwwroot) ? '' : get_string('errorsetting', 'admin'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Return an XHTML string for the setting |
| 58 | + * @return string Returns an XHTML string |
| 59 | + */ |
| 60 | + public function output_html($data, $query = '') { |
| 61 | + global $CFG; |
| 62 | + $elementid = $this->get_id(); |
| 63 | + $textbox = parent::output_html($data, $query); |
| 64 | + $resetbutton = \html_writer::tag('button', |
| 65 | + get_string('resetbutton', 'local_datacleaner'), |
| 66 | + [ |
| 67 | + 'type' => 'button', |
| 68 | + 'onclick' => "document.getElementById('{$elementid}').value = '{$CFG->wwwroot}'", |
| 69 | + 'class' => 'btn btn-secondary', |
| 70 | + ], |
| 71 | + ); |
| 72 | + $html = \html_writer::div( |
| 73 | + $textbox . $resetbutton, |
| 74 | + 'mb-3' |
| 75 | + ); |
| 76 | + return $html; |
| 77 | + } |
| 78 | +} |
0 commit comments