Skip to content

Commit

Permalink
Merge pull request #13 from sudile/master
Browse files Browse the repository at this point in the history
Reworked widget plugin system and fixed minor issues
  • Loading branch information
ponlawat-w authored Aug 24, 2024
2 parents a5532e1 + fc475a3 commit 56cc11b
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 30 deletions.
5 changes: 4 additions & 1 deletion admin/manageenabledwidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
* @var moodle_database $DB
*/

use local_accessibility\admin\enablewidgetform;

require_once(__DIR__ . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
admin_externalpage_setup('local_accessibility');

require_once(__DIR__ . '/../lib.php');
require_once(__DIR__ . '/../classes/admin/enablewidgetform.php');

$url = new moodle_url('/local/accessibility/admin/manageenabledwidgets.php');

Expand All @@ -46,6 +47,7 @@

$action = optional_param('action', null, PARAM_TEXT);
if ($action) {
require_sesskey();
$id = required_param('id', PARAM_INT);
$widget = $DB->get_record('local_accessibility_widgets', ['id' => $id, 'enabled' => 1], '*', MUST_EXIST);
if ($action == 'moveup') {
Expand Down Expand Up @@ -77,6 +79,7 @@
echo $OUTPUT->render_from_template('local_accessibility/admin/enabledwidgets', [
'widgets' => $context,
'baseurl' => $url,
'sesskey' => sesskey(),
]);
echo html_writer::start_tag('hr');
$enablewidgetform->display();
Expand Down
8 changes: 6 additions & 2 deletions classes/admin/enablewidgetform.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_accessibility\admin;

use moodleform;

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

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

/**
* Form class to enable a widget
Expand All @@ -36,6 +39,7 @@ class enablewidgetform extends moodleform {
* @return string[]
*/
private function getdisabledwidgets() {
require_once(__DIR__ . '/../../lib.php'); // Load on demand if not already loaded.
$results = [];
$enabledwidgets = local_accessibility_getenabledwidgetnames();
$allwidgets = local_accessibility_getinstalledwidgetnames();
Expand Down
4 changes: 2 additions & 2 deletions classes/external/savewidgetconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

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

require_once(__DIR__ . '/../../lib.php');
require_once(__DIR__ . '/../../../../lib/externallib.php');
require_once($CFG->libdir.'/externallib.php');

if ($CFG->version < 2022112999) { // Moodle < 4.2.
class_alias('\\external_api', '\\core_external\\external_api');
Expand Down Expand Up @@ -76,6 +75,7 @@ public static function execute_returns(): \core_external\external_single_structu
* @return array
*/
public static function execute(string $widget, string $configvalue = null): array {
require_once(__DIR__ . '/../../lib.php'); // Load on demand if not already loaded.
$params = self::validate_parameters(self::execute_parameters(), ['widget' => $widget, 'configvalue' => $configvalue]);
$widgetinstance = local_accessibility_getwidgetinstancebyname($params['widget']);
$widgetinstance->setuserconfig($params['configvalue']);
Expand Down
166 changes: 166 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace local_accessibility\privacy;

use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\core_userlist_provider;
use core_privacy\local\request\transform;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;

/**
* Privacy Subsystem implementation for local_accessibility.
*
* @package local_accessibility
* @copyright 2023 Ponlawat Weerapanpisit <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// This plugin has data.
\core_privacy\local\metadata\provider,
// This plugin has some sitewide user preferences to export.
core_userlist_provider,
\core_privacy\local\request\plugin\provider {

/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid The user to search.
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$contextlist = new contextlist();
$contextlist->add_system_context();
return $contextlist;
}

/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts to export information for.
* @throws dml_exception
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
$systemcontext = \context_system::instance();
$user = $contextlist->get_user();
if (!$contextlist->valid() || $contextlist->current()->contextlevel != CONTEXT_SYSTEM) {
return;
}
$rs = $DB->get_records_sql('SELECT c.widget, c.configvalue, c.userid
FROM {local_accessibility_configs} c
WHERE c.userid = :userid
ORDER BY c.widget, c.configvalue, c.userid',
['userid' => $user->id]);
if (count($rs) == 0) {
return;
}
foreach ($rs as $config) {
$contextdata = (object) [
'widget' => $config->widget,
'configvalue' => $config->configvalue,
'userid' => transform::user($config->userid),
];
writer::with_context($systemcontext)->export_data(["local_accessibility_config"], $contextdata);
}
}

/**
* Delete all data for all users in the specified context.
*
* @param \context $context The specific context to delete data for.
* @throws dml_exception
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if ($context->contextlevel != CONTEXT_SYSTEM) {
return;
}
// We just delete all data from the system context since we don't have other assignments?
$DB->delete_records('local_accessibility_configs');
}

/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
* @throws dml_exception
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
if (!$contextlist->valid() || $contextlist->current()->contextlevel != CONTEXT_SYSTEM) {
return;
}
$DB->delete_records('local_accessibility_configs', ['userid' => $contextlist->get_user()->id]);
}

/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!is_a($context, \context_system::class)) {
return;
}
// Since we only have one system context all data lay here!
$sql = "SELECT userid FROM {local_accessibility_configs}";
$userlist->add_from_sql('userid', $sql, []);
}

/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
* @throws dml_exception
* @throws coding_exception
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
$userids = $userlist->get_userids();
if ($context->contextlevel != CONTEXT_SYSTEM) {
return;
}
[$insql, $inparams] = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$sql = "userid {$insql}";
$DB->delete_records_select('local_accessibility_configs', $sql, $inparams);
}

/**
* Returns metadata about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection): collection {
$collection->add_database_table(
'local_accessibility_configs',
[
'widget' => 'privacy:metadata:configs:widget',
'configvalue' => 'privacy:metadata:configs:configvalue',
'userid' => 'privacy:metadata:configs:userid',
],
'privacy:metadata:configs'
);
return $collection;
}
}
36 changes: 36 additions & 0 deletions classes/widgets/apply_style.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

/**
* Interface which can be implemented by subplugins for css usage.
*
* @package local_accessibility
* @copyright 2023 Ponlawat Weerapanpisit <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_accessibility\widgets;
/**
* Used by widgets to mark that they have styles which need to be applied.
*/
interface apply_style {
/**
* Apply widget css will be served over the styles.php file.
*
* @return string
*/
public function apply_style(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
abstract class colourwidget extends widgetbase {

/** @var $class Colourpicker is big, so it takes the whole row */
/** @var string $class Colourpicker is big, so it takes the whole row */
protected $class = 'col-12';

/**
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion classes/widgetbase.php → classes/widgets/widgetbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ protected function getguestconfig() {
protected function setguestconfig($value) {
global $SESSION;
/** @var stdClass $SESSION */ $SESSION;

if (!isset($SESSION->local_accessibility)) {
$SESSION->local_accessibility = new stdClass();
}
Expand Down
7 changes: 5 additions & 2 deletions lang/en/local_accessibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

$string['pluginname'] = 'Accessibility';

$string['accessibilitywidgets'] = 'Accessibility Widgets';
Expand All @@ -38,3 +36,8 @@

$string['reset'] = 'Reset';
$string['resetall'] = 'Reset All';

$string['privacy:metadata:configs'] = 'A config for a widget in the plugin Accessibility Widgets';
$string['privacy:metadata:configs:widget'] = 'Widget name';
$string['privacy:metadata:configs:configvalue'] = 'Used config';
$string['privacy:metadata:configs:userid'] = 'The user who is assigned to the config';
17 changes: 3 additions & 14 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once(__DIR__ . '/classes/widgetbase.php');
require_once(__DIR__ . '/classes/rangewidget.php');
require_once(__DIR__ . '/classes/colourwidget.php');

/**
* Return names of installed widgets
*
Expand Down Expand Up @@ -266,14 +260,9 @@ function local_accessibility_getwidgetinstances() {
*/
function local_accessibility_getwidgetinstancebyname($widgetname) {
global $CFG;
$filepath = "{$CFG->dirroot}/local/accessibility/widgets/{$widgetname}/accessibility_{$widgetname}.php";
if (!file_exists($filepath)) {
throw new moodle_exception("File {$filepath} does not exist");
}
require_once($filepath);
$classname = 'local_accessibility\widgets\\' . $widgetname;
$classname = 'accessibility_' . $widgetname. '\\' . $widgetname;
if (!class_exists($classname)) {
throw new moodle_exception("Class {$classname} does not exist in {$filepath}");
throw new moodle_exception("Class {$classname} does not exist");
}
return new $classname();
}
Expand All @@ -289,8 +278,8 @@ function local_accessibility_before_http_headers() {
if (!count($widgetinstances)) {
return;
}
/** @var \moodle_page $PAGE */
$PAGE->requires->css('/local/accessibility/styles.css');
$PAGE->requires->css('/local/accessibility/styles.php');
foreach ($widgetinstances as $widgetinstance) {
$widgetinstance->init();
}
Expand Down
2 changes: 1 addition & 1 deletion resetall.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

require_sesskey();

$returnurl = optional_param('returnurl', new moodle_url('/'), PARAM_URL);
$returnurl = optional_param('returnurl', new moodle_url('/'), PARAM_LOCALURL);

$widgets = local_accessibility_getwidgetinstances();
foreach ($widgets as $widget) {
Expand Down
7 changes: 5 additions & 2 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@

if ($hassiteconfig) {
/** @var admin_root $ADMIN */
$ADMIN->add('modules', new admin_category('accessibilitywidgets', get_string('accessibilitywidgets', 'local_accessibility')));
$ADMIN->add('modules',
new admin_category('accessibilitywidgets', new lang_string('accessibilitywidgets', 'local_accessibility')));
$url = new moodle_url('/local/accessibility/admin/manageenabledwidgets.php');
$settings = new admin_externalpage('local_accessibility', get_string('manageenabledwidgets', 'local_accessibility'), $url);
$settings = new admin_externalpage('local_accessibility',
new lang_string('manageenabledwidgets', 'local_accessibility'),
$url);
$ADMIN->add('accessibilitywidgets', $settings);
}
Loading

0 comments on commit 56cc11b

Please sign in to comment.