MDL-76974 javascript: convert remaining uses of old preferences API.

Implement component preference definition callbacks, update JS code
to use the `core_user/repository` module instead of the now deprecated
API.
This commit is contained in:
Paul Holden 2023-06-18 19:11:36 +01:00
parent d096c60e0c
commit b30245b3e2
No known key found for this signature in database
GPG key ID: A81A96D6045F6164
21 changed files with 180 additions and 38 deletions

View file

@ -141,10 +141,6 @@ class block_settings extends block_base {
} else {
$this->content->footer = '';
}
if (!empty($this->config->enabledock) && $this->config->enabledock == 'yes') {
user_preference_allow_ajax_update('nav_in_tab_panel_settingsnav'.block_settings::$navcount, PARAM_INT);
}
}
$this->contentgenerated = true;

View file

@ -979,6 +979,60 @@ class core_user {
'permissioncallback' => function($user, $preferencename) {
return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
});
$preferences['filemanager_recentviewmode'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 1,
'choices' => [1, 2, 3],
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['filepicker_recentrepository'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['filepicker_recentlicense'] = [
'type' => PARAM_SAFEDIR,
'null' => NULL_NOT_ALLOWED,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['filepicker_recentviewmode'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => 1,
'choices' => [1, 2, 3],
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['userselector_optionscollapsed'] = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => true,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['userselector_autoselectunique'] = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['userselector_preserveselected'] = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['userselector_searchanywhere'] = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [static::class, 'is_current_user'],
];
$preferences['question_bank_advanced_search'] = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [static::class, 'is_current_user'],
];
$choices = [HOMEPAGE_SITE];
if (!empty($CFG->enabledashboard)) {

View file

@ -1218,8 +1218,10 @@ M.form_filemanager.init = function(Y, options) {
},
set_preference: function(name, value) {
if (this.userprefs[name] != value) {
M.util.set_user_preference('filemanager_' + name, value);
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference('filemanager_' + name, value);
this.userprefs[name] = value;
}.bind(this));
}
},
});

View file

@ -453,7 +453,6 @@ class form_filemanager implements renderable {
$this->options->userprefs = array();
$this->options->userprefs['recentviewmode'] = get_user_preferences('filemanager_recentviewmode', '');
user_preference_allow_ajax_update('filemanager_recentviewmode', PARAM_INT);
// building file picker options
$params = new stdClass();

View file

@ -160,7 +160,9 @@ M.util.CollapsibleRegion = function(Y, id, userpref, strtooltip) {
animation.set('reverse', this.div.hasClass('collapsed'));
// Update the user preference.
if (this.userpref) {
M.util.set_user_preference(this.userpref, !this.div.hasClass('collapsed'));
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference(this.userpref, !this.div.hasClass('collapsed'));
}.bind(this));
}
animation.run();
}, this, animation);
@ -577,7 +579,9 @@ M.util.init_block_hider = function(Y, config) {
}
},
updateState : function(e, hide) {
M.util.set_user_preference(this.get('preference'), hide);
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference(this.get('preference'), hide);
}.bind(this));
if (hide) {
this.get('block').addClass('hidden');
this.get('block').one('.block-hider-show').focus();

View file

@ -2560,7 +2560,6 @@ function print_collapsible_region_start($classes, $id, $caption, $userpref = '',
// Work out the initial state.
if (!empty($userpref) and is_string($userpref)) {
user_preference_allow_ajax_update($userpref, PARAM_BOOL);
$collapsed = get_user_preferences($userpref, $default);
} else {
$collapsed = $default;

View file

@ -72,9 +72,6 @@ class grading_app implements templatable, renderable {
$this->userid = $userid;
$this->groupid = $groupid;
$this->assignment = $assignment;
user_preference_allow_ajax_update('assign_filter', PARAM_ALPHA);
user_preference_allow_ajax_update('assign_workflowfilter', PARAM_ALPHA);
user_preference_allow_ajax_update('assign_markerfilter', PARAM_ALPHANUMEXT);
$this->participants = $assignment->list_participants_with_filter_status_and_group($groupid);
if (!$this->userid && count($this->participants)) {
$this->userid = reset($this->participants)->id;

View file

@ -2252,6 +2252,38 @@ function workshop_get_coursemodule_info($coursemodule) {
return $result;
}
/**
* Get the current user preferences that are available
*
* @return array[]
*/
function mod_workshop_user_preferences(): array {
$preferencedefinition = [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [core_user::class, 'is_current_user'],
];
return [
'workshop-viewlet-allexamples-collapsed' => $preferencedefinition,
'workshop-viewlet-allsubmissions-collapsed' => $preferencedefinition,
'workshop-viewlet-assessmentform-collapsed' => $preferencedefinition,
'workshop-viewlet-assignedassessments-collapsed' => $preferencedefinition,
'workshop-viewlet-cleargrades-collapsed' => $preferencedefinition,
'workshop-viewlet-conclusion-collapsed' => $preferencedefinition,
'workshop-viewlet-examples-collapsed' => $preferencedefinition,
'workshop-viewlet-examplesfail-collapsed' => $preferencedefinition,
'workshop-viewlet-gradereport-collapsed' => $preferencedefinition,
'workshop-viewlet-instructauthors-collapsed' => $preferencedefinition,
'workshop-viewlet-instructreviewers-collapsed' => $preferencedefinition,
'workshop-viewlet-intro-collapsed' => $preferencedefinition,
'workshop-viewlet-ownsubmission-collapsed' => $preferencedefinition,
'workshop-viewlet-publicsubmissions-collapsed' => $preferencedefinition,
'workshop-viewlet-yourgrades-collapsed' => $preferencedefinition,
];
}
/**
* Callback to fetch the activity event type lang string.
*

View file

@ -0,0 +1,41 @@
<?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/>.
/**
* Callback methods for preview question component
*
* @package qbank_previewquestion
* @copyright 2023 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
/**
* Get the current user preferences that are available
*
* @return array[]
*/
function qbank_previewquestion_user_preferences(): array {
return [
'core_question_preview_techinfo_collapsed' => [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => true,
'permissioncallback' => [core_user::class, 'is_current_user'],
],
];
}

View file

@ -2125,8 +2125,10 @@ M.core_filepicker.init = function(Y, options) {
},
set_preference: function(name, value) {
if (this.options.userprefs[name] != value) {
M.util.set_user_preference('filepicker_' + name, value);
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference('filepicker_' + name, value);
this.options.userprefs[name] = value;
}.bind(this));
}
},
in_iframe: function () {

View file

@ -3190,11 +3190,6 @@ function initialise_filepicker($args) {
$return->userprefs['recentlicense'] = get_user_preferences('filepicker_recentlicense', '');
$return->userprefs['recentviewmode'] = get_user_preferences('filepicker_recentviewmode', '');
user_preference_allow_ajax_update('filepicker_recentrepository', PARAM_INT);
user_preference_allow_ajax_update('filepicker_recentlicense', PARAM_SAFEDIR);
user_preference_allow_ajax_update('filepicker_recentviewmode', PARAM_INT);
// provided by form element
$return->accepted_types = file_get_typegroup('extension', $args->accepted_types);
$return->return_types = $args->return_types;

View file

@ -5,6 +5,6 @@
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("theme_boost/drawer",["jquery","core/custom_interaction_events","core/log","core/pubsub","core/aria"],(function($,CustomEvents,Log,PubSub,Aria){var SELECTORS_TOGGLE_REGION='[data-region="drawer-toggle"]',SELECTORS_TOGGLE_ACTION='[data-action="toggle-drawer"]',SELECTORS_BODY="body",SELECTORS_SECTION='.list-group-item[href*="#section-"]',SELECTORS_DRAWER="#nav-drawer",small=$(document).width()<768,Drawer=function(){$(SELECTORS_TOGGLE_REGION).length||Log.debug("Page is missing a drawer region"),$(SELECTORS_TOGGLE_ACTION).length||Log.debug("Page is missing a drawer toggle link"),$(SELECTORS_TOGGLE_REGION).each(function(index,ele){var trigger=$(ele).find(SELECTORS_TOGGLE_ACTION),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),hidden="false"==trigger.attr("aria-expanded"),side=trigger.attr("data-side"),body=$(SELECTORS_BODY),preference=trigger.attr("data-preference");small&&M.util.set_user_preference(preference,"false"),drawer.on("mousewheel DOMMouseScroll",this.preventPageScroll),hidden?trigger.attr("aria-expanded","false"):(body.addClass("drawer-open-"+side),trigger.attr("aria-expanded","true"))}.bind(this)),this.registerEventListeners(),small&&this.closeAll()};return Drawer.prototype.closeAll=function(){$(SELECTORS_TOGGLE_REGION).each((function(index,ele){var trigger=$(ele).find(SELECTORS_TOGGLE_ACTION),side=trigger.attr("data-side"),body=$(SELECTORS_BODY),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),preference=trigger.attr("data-preference");trigger.attr("aria-expanded","false"),body.removeClass("drawer-open-"+side),Aria.hide(drawer.get()),drawer.addClass("closed"),small||M.util.set_user_preference(preference,"false")}))},Drawer.prototype.toggleDrawer=function(e){var trigger=$(e.target).closest("[data-action=toggle-drawer]"),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),body=$(SELECTORS_BODY),side=trigger.attr("data-side"),preference=trigger.attr("data-preference");small&&M.util.set_user_preference(preference,"false"),body.addClass("drawer-ease");var open="true"==trigger.attr("aria-expanded");open?(body.removeClass("drawer-open-"+side),trigger.attr("aria-expanded","false"),drawer.addClass("closed").delay(500).queue((function(){$(this).hasClass("closed")&&Aria.hide(this),$(this).dequeue()})),small||M.util.set_user_preference(preference,"false")):(trigger.attr("aria-expanded","true"),Aria.unhide(drawer.get()),drawer.focus(),body.addClass("drawer-open-"+side),drawer.removeClass("closed"),small||M.util.set_user_preference(preference,"true")),PubSub.publish("nav-drawer-toggle-start",open)},Drawer.prototype.preventPageScroll=function(e){var delta=e.wheelDelta||e.originalEvent&&e.originalEvent.wheelDelta||-e.originalEvent.detail,bottomOverflow=this.scrollTop+$(this).outerHeight()-this.scrollHeight>=0,topOverflow=this.scrollTop<=0;(delta<0&&bottomOverflow||delta>0&&topOverflow)&&e.preventDefault()},Drawer.prototype.registerEventListeners=function(){$(SELECTORS_TOGGLE_ACTION).each(function(index,element){CustomEvents.define($(element),[CustomEvents.events.activate]),$(element).on(CustomEvents.events.activate,function(e,data){this.toggleDrawer(data.originalEvent),data.originalEvent.preventDefault()}.bind(this))}.bind(this)),$(SELECTORS_SECTION).click(function(){small&&this.closeAll()}.bind(this)),$(SELECTORS_DRAWER).on("webkitTransitionEnd msTransitionEnd transitionend",(function(e){var open=!!$(e.target).closest(SELECTORS_DRAWER).attr("aria-hidden");PubSub.publish("nav-drawer-toggle-end",open)}))},{init:function(){return new Drawer}}}));
define("theme_boost/drawer",["jquery","core/custom_interaction_events","core/log","core/pubsub","core/aria","core_user/repository"],(function($,CustomEvents,Log,PubSub,Aria,UserRepository){var SELECTORS_TOGGLE_REGION='[data-region="drawer-toggle"]',SELECTORS_TOGGLE_ACTION='[data-action="toggle-drawer"]',SELECTORS_BODY="body",SELECTORS_SECTION='.list-group-item[href*="#section-"]',SELECTORS_DRAWER="#nav-drawer",small=$(document).width()<768,Drawer=function(){$(SELECTORS_TOGGLE_REGION).length||Log.debug("Page is missing a drawer region"),$(SELECTORS_TOGGLE_ACTION).length||Log.debug("Page is missing a drawer toggle link"),$(SELECTORS_TOGGLE_REGION).each(function(index,ele){var trigger=$(ele).find(SELECTORS_TOGGLE_ACTION),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),hidden="false"==trigger.attr("aria-expanded"),side=trigger.attr("data-side"),body=$(SELECTORS_BODY),preference=trigger.attr("data-preference");small&&UserRepository.setUserPreference(preference,!1),drawer.on("mousewheel DOMMouseScroll",this.preventPageScroll),hidden?trigger.attr("aria-expanded","false"):(body.addClass("drawer-open-"+side),trigger.attr("aria-expanded","true"))}.bind(this)),this.registerEventListeners(),small&&this.closeAll()};return Drawer.prototype.closeAll=function(){$(SELECTORS_TOGGLE_REGION).each((function(index,ele){var trigger=$(ele).find(SELECTORS_TOGGLE_ACTION),side=trigger.attr("data-side"),body=$(SELECTORS_BODY),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),preference=trigger.attr("data-preference");trigger.attr("aria-expanded","false"),body.removeClass("drawer-open-"+side),Aria.hide(drawer.get()),drawer.addClass("closed"),small||UserRepository.setUserPreference(preference,!1)}))},Drawer.prototype.toggleDrawer=function(e){var trigger=$(e.target).closest("[data-action=toggle-drawer]"),drawerid=trigger.attr("aria-controls"),drawer=$(document.getElementById(drawerid)),body=$(SELECTORS_BODY),side=trigger.attr("data-side"),preference=trigger.attr("data-preference");small&&UserRepository.setUserPreference(preference,!1),body.addClass("drawer-ease");var open="true"==trigger.attr("aria-expanded");open?(body.removeClass("drawer-open-"+side),trigger.attr("aria-expanded","false"),drawer.addClass("closed").delay(500).queue((function(){$(this).hasClass("closed")&&Aria.hide(this),$(this).dequeue()})),small||UserRepository.setUserPreference(preference,!1)):(trigger.attr("aria-expanded","true"),Aria.unhide(drawer.get()),drawer.focus(),body.addClass("drawer-open-"+side),drawer.removeClass("closed"),small||UserRepository.setUserPreference(preference,!0)),PubSub.publish("nav-drawer-toggle-start",open)},Drawer.prototype.preventPageScroll=function(e){var delta=e.wheelDelta||e.originalEvent&&e.originalEvent.wheelDelta||-e.originalEvent.detail,bottomOverflow=this.scrollTop+$(this).outerHeight()-this.scrollHeight>=0,topOverflow=this.scrollTop<=0;(delta<0&&bottomOverflow||delta>0&&topOverflow)&&e.preventDefault()},Drawer.prototype.registerEventListeners=function(){$(SELECTORS_TOGGLE_ACTION).each(function(index,element){CustomEvents.define($(element),[CustomEvents.events.activate]),$(element).on(CustomEvents.events.activate,function(e,data){this.toggleDrawer(data.originalEvent),data.originalEvent.preventDefault()}.bind(this))}.bind(this)),$(SELECTORS_SECTION).click(function(){small&&this.closeAll()}.bind(this)),$(SELECTORS_DRAWER).on("webkitTransitionEnd msTransitionEnd transitionend",(function(e){var open=!!$(e.target).closest(SELECTORS_DRAWER).attr("aria-hidden");PubSub.publish("nav-drawer-toggle-end",open)}))},{init:function(){return new Drawer}}}));
//# sourceMappingURL=drawer.min.js.map

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -20,8 +20,8 @@
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', 'core/aria'],
function($, CustomEvents, Log, PubSub, Aria) {
define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', 'core/aria', 'core_user/repository'],
function($, CustomEvents, Log, PubSub, Aria, UserRepository) {
var SELECTORS = {
TOGGLE_REGION: '[data-region="drawer-toggle"]',
@ -55,7 +55,7 @@ define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', '
var body = $(SELECTORS.BODY);
var preference = trigger.attr('data-preference');
if (small) {
M.util.set_user_preference(preference, 'false');
UserRepository.setUserPreference(preference, false);
}
drawer.on('mousewheel DOMMouseScroll', this.preventPageScroll);
@ -88,7 +88,7 @@ define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', '
Aria.hide(drawer.get());
drawer.addClass('closed');
if (!small) {
M.util.set_user_preference(preference, 'false');
UserRepository.setUserPreference(preference, false);
}
});
};
@ -107,7 +107,7 @@ define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', '
var side = trigger.attr('data-side');
var preference = trigger.attr('data-preference');
if (small) {
M.util.set_user_preference(preference, 'false');
UserRepository.setUserPreference(preference, false);
}
body.addClass('drawer-ease');
@ -120,7 +120,7 @@ define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', '
body.addClass('drawer-open-' + side);
drawer.removeClass('closed');
if (!small) {
M.util.set_user_preference(preference, 'true');
UserRepository.setUserPreference(preference, true);
}
} else {
// Close.
@ -134,7 +134,7 @@ define(['jquery', 'core/custom_interaction_events', 'core/log', 'core/pubsub', '
$(this).dequeue();
});
if (!small) {
M.util.set_user_preference(preference, 'false');
UserRepository.setUserPreference(preference, false);
}
}

View file

@ -27,6 +27,7 @@ import {dispatchEvent} from 'core/event_dispatcher';
import {debounce} from 'core/utils';
import {isSmall, isLarge} from 'core/pagehelpers';
import Pending from 'core/pending';
import {setUserPreference} from 'core_user/repository';
// The jQuery module is only used for interacting with Boostrap 4. It can we removed when MDL-71979 is integrated.
import jQuery from 'jquery';
@ -436,7 +437,7 @@ export default class Drawers {
const preference = this.drawerNode.dataset.preference;
if (preference && !isSmall() && (this.drawerNode.dataset.forceopen != 1)) {
M.util.set_user_preference(preference, true);
setUserPreference(preference, true);
}
const state = this.drawerNode.dataset.state;
@ -501,7 +502,7 @@ export default class Drawers {
const preference = this.drawerNode.dataset.preference;
if (preference && updatePreferences && !isSmall()) {
M.util.set_user_preference(preference, false);
setUserPreference(preference, false);
}
const state = this.drawerNode.dataset.state;

View file

@ -30,9 +30,6 @@ require_once($CFG->dirroot . '/course/lib.php');
// Add block button in editing mode.
$addblockbutton = $OUTPUT->addblockbutton();
user_preference_allow_ajax_update('drawer-open-index', PARAM_BOOL);
user_preference_allow_ajax_update('drawer-open-block', PARAM_BOOL);
if (isloggedin()) {
$courseindexopen = (get_user_preferences('drawer-open-index', true) == true);
$blockdraweropen = (get_user_preferences('drawer-open-block') == true);

View file

@ -93,6 +93,28 @@ function theme_boost_pluginfile($course, $cm, $context, $filearea, $args, $force
}
}
/**
* Get the current user preferences that are available
*
* @return array[]
*/
function theme_boost_user_preferences(): array {
return [
'drawer-open-block' => [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => false,
'permissioncallback' => [core_user::class, 'is_current_user'],
],
'drawer-open-index' => [
'type' => PARAM_BOOL,
'null' => NULL_NOT_ALLOWED,
'default' => true,
'permissioncallback' => [core_user::class, 'is_current_user'],
],
];
}
/**
* Returns the main SCSS content.
*

View file

@ -679,7 +679,6 @@ abstract class user_selector_base {
'" value="1"' . $checked . ' /> ' . $label .
"</label>
</div>\n";
user_preference_allow_ajax_update($name, PARAM_BOOL);
return $output;
}

View file

@ -368,7 +368,9 @@ M.core_user.init_user_selector_options_tracker = function(Y) {
* @param {string} name The name of the preference to set
*/
set_user_preference : function(e, name) {
M.util.set_user_preference(name, Y.one('#' + name + 'id').get('checked'));
require(['core_user/repository'], function(UserRepository) {
UserRepository.setUserPreference(name, Y.one('#' + name + 'id').get('checked'));
});
}
};
// Initialise the options tracker