MDL-81676 core_course: Rename data-sectionid to data-sectionnum

Activity chooser is saving section number in a data attribute called
data-sectionid. We should use data-sectionnum instead to make it easier
to understand.
This commit is contained in:
Amaia Anabitarte
2024-06-10 15:42:09 +02:00
parent d3ae1391ab
commit 267a474154
12 changed files with 53 additions and 35 deletions
@@ -0,0 +1,7 @@
issueNumber: MDL-81676
notes:
core_course:
- message: >-
The data-sectionid attribute in the activity chooser has been
deprecated. Please update your code to use data-sectionnum instead.
type: deprecated
@@ -35,7 +35,7 @@
href="#"
data-action="show-moodlenet"
data-courseid="{{courseID}}"
data-sectionID="{{sectionID}}"
data-sectionnum="{{sectionnum}}"
{{/advanced}}
>
{{#str}} footermessage , tool_moodlenet{{/str}}
@@ -45,7 +45,7 @@
class="form-control"
data-var="mnet-link"
data-courseid="{{courseID}}"
data-sectionid="{{sectionID}}"
data-sectionnum="{{sectionnum}}"
placeholder="{{#str}} instanceplaceholder, tool_moodlenet {{/str}}"
aria-label="{{#str}} aria:enterprofile, tool_moodlenet {{/str}}"
autocomplete="off"
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+33 -22
View File
@@ -98,10 +98,10 @@ const registerListenerEvents = (courseId, chooserConfig) => {
const fetchFooterData = (() => {
let footerInnerPromise = null;
return (sectionId) => {
return (sectionnum) => {
if (!footerInnerPromise) {
footerInnerPromise = new Promise((resolve) => {
resolve(Repository.fetchFooterData(courseId, sectionId));
resolve(Repository.fetchFooterData(courseId, sectionnum));
});
}
@@ -116,21 +116,32 @@ const registerListenerEvents = (courseId, chooserConfig) => {
document.addEventListener(event, async(e) => {
if (e.target.closest(selectors.elements.sectionmodchooser)) {
let caller;
let sectionnum;
// We need to know who called this.
// Standard courses use the ID in the main section info.
const sectionDiv = e.target.closest(selectors.elements.section);
// Front page courses need some special handling.
const button = e.target.closest(selectors.elements.sectionmodchooser);
// If we don't have a section ID use the fallback ID.
// We always want the sectionDiv caller first as it keeps track of section ID's after DnD changes.
// If we don't have a section number use the fallback ID.
// We always want the sectionDiv caller first as it keeps track of section number's after DnD changes.
// The button attribute is always just a fallback for us as the section div is not always available.
// A YUI change could be done maybe to only update the button attribute but we are going for minimal change here.
if (sectionDiv !== null && sectionDiv.hasAttribute('data-sectionid')) {
if (sectionDiv !== null && sectionDiv.hasAttribute('data-number')) {
// We check for attributes just in case of outdated contrib course formats.
caller = sectionDiv;
sectionnum = sectionDiv.getAttribute('data-number');
} else {
caller = button;
if (caller.hasAttribute('data-sectionid')) {
window.console.warn(
'The data-sectionid attribute has been deprecated. ' +
'Please update your code to use data-sectionnum instead.'
);
caller.setAttribute('data-sectionnum', caller.dataset.sectionid);
}
sectionnum = caller.dataset.sectionnum;
}
// We want to show the modal instantly but loading whilst waiting for our data.
@@ -139,7 +150,7 @@ const registerListenerEvents = (courseId, chooserConfig) => {
bodyPromiseResolver = resolve;
});
const footerData = await fetchFooterData(caller.dataset.sectionid);
const footerData = await fetchFooterData(sectionnum);
const sectionModal = buildModal(bodyPromise, footerData);
// Now we have a modal we should start fetching data.
@@ -156,18 +167,18 @@ const registerListenerEvents = (courseId, chooserConfig) => {
return;
}
// Apply the section id to all the module instance links.
const builtModuleData = sectionIdMapper(
// Apply the section num to all the module instance links.
const builtModuleData = sectionMapper(
data,
caller.dataset.sectionid,
caller.dataset.sectionreturnid,
sectionnum,
caller.dataset.sectionreturnnum,
caller.dataset.beforemod
);
ChooserDialogue.displayChooser(
sectionModal,
builtModuleData,
partiallyAppliedFavouriteManager(data, caller.dataset.sectionid),
partiallyAppliedFavouriteManager(data, sectionnum),
footerData,
);
@@ -184,22 +195,22 @@ const registerListenerEvents = (courseId, chooserConfig) => {
/**
* Given the web service data and an ID we want to make a deep copy
* of the WS data then add on the section ID to the addoption URL
* of the WS data then add on the section num to the addoption URL
*
* @method sectionIdMapper
* @method sectionMapper
* @param {Object} webServiceData Our original data from the Web service call
* @param {Number} id The ID of the section we need to append to the links
* @param {Number|null} sectionreturnid The ID of the section return we need to append to the links
* @param {Number} num The number of the section we need to append to the links
* @param {Number|null} sectionreturnnum The number of the section return we need to append to the links
* @param {Number|null} beforemod The ID of the cm we need to append to the links
* @return {Array} [modules] with URL's built
*/
const sectionIdMapper = (webServiceData, id, sectionreturnid, beforemod) => {
const sectionMapper = (webServiceData, num, sectionreturnnum, beforemod) => {
// We need to take a fresh deep copy of the original data as an object is a reference type.
const newData = JSON.parse(JSON.stringify(webServiceData));
newData.content_items.forEach((module) => {
module.link += '&section=' + id + '&beforemod=' + (beforemod ?? 0);
if (sectionreturnid) {
module.link += '&sr=' + sectionreturnid;
module.link += '&section=' + num + '&beforemod=' + (beforemod ?? 0);
if (sectionreturnnum) {
module.link += '&sr=' + sectionreturnnum;
}
});
return newData.content_items;
@@ -352,10 +363,10 @@ const nullFavouriteDomManager = (favouriteTabNav, modalBody) => {
*
* @method partiallyAppliedFavouriteManager
* @param {Array} moduleData This is our raw WS data that we need to manipulate
* @param {Number} sectionId We need this to add the sectionID to the URL's in the faves area after rerender
* @param {Number} sectionnum We need this to add the sectionnum to the URL's in the faves area after rerender
* @return {Function} partially applied function so we can manipulate DOM nodes easily & update our internal array
*/
const partiallyAppliedFavouriteManager = (moduleData, sectionId) => {
const partiallyAppliedFavouriteManager = (moduleData, sectionnum) => {
/**
* Curried function that is being returned.
*
@@ -378,7 +389,7 @@ const partiallyAppliedFavouriteManager = (moduleData, sectionId) => {
// eslint-disable-next-line camelcase
newFaves.content_items = moduleData.content_items.filter(mod => mod.favourite === true);
const builtFaves = sectionIdMapper(newFaves, sectionId);
const builtFaves = sectionMapper(newFaves, sectionnum);
const {html, js} = await Templates.renderForPromise('core_course/local/activitychooser/favourites',
{favourites: builtFaves});
@@ -146,7 +146,7 @@ class section implements named_templatable, renderable {
$data = (object)[
'num' => $section->section ?? '0',
'id' => $section->id,
'sectionreturnid' => $format->get_sectionnum(),
'sectionreturnnum' => $format->get_sectionnum(),
'insertafter' => false,
'summary' => $summary->export_for_template($output),
'highlightedlabel' => $format->get_section_highlighted_name(),
@@ -78,7 +78,7 @@
},
"cmcontrols": "[Add an activity or resource]",
"iscoursedisplaymultipage": true,
"sectionreturnid": 0,
"sectionreturnnum": 0,
"contentcollapsed": false,
"insertafter": true,
"numsections": 42,
@@ -91,7 +91,7 @@
{{#ishidden}} hidden {{/ishidden}} {{#iscurrent}}{{^displayonesection}} current {{/displayonesection}}{{/iscurrent}}
{{#isstealth}} orphaned {{/isstealth}}"
data-sectionid="{{num}}"
data-sectionreturnid="{{sectionreturnid}}"
data-sectionreturnnum="{{sectionreturnnum}}"
data-for="section"
data-id="{{id}}"
data-number="{{num}}"
@@ -77,7 +77,7 @@
},
"cmcontrols": "[Add an activity or resource]",
"iscoursedisplaymultipage": true,
"sectionreturnid": 0,
"sectionreturnnum": 0,
"contentcollapsed": false,
"insertafter": true,
"numsections": 42,
+1 -1
View File
@@ -225,7 +225,7 @@ class core_course_renderer extends plugin_renderer_base {
}
$data = [
'sectionid' => $section,
'sectionnum' => $section,
'sectionreturn' => $sectionreturn
];
$ajaxcontrol = $this->render_from_template('course/activitychooserbutton', $data);
@@ -34,8 +34,8 @@
{{$content}}
<button class="btn add-content section-modchooser section-modchooser-link d-flex justify-content-center align-items-center py-1 px-2"
data-action="open-chooser"
data-sectionid="{{sectionid}}"
{{#sectionreturn}}data-sectionreturnid="{{.}}"{{/sectionreturn}}
data-sectionnum="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnnum="{{.}}"{{/sectionreturn}}
>
{{#pix}} t/add, core {{/pix}}
<span class="activity-add-text pr-1">{{#str}}addresourceoractivity, core{{/str}}</span>
@@ -33,8 +33,8 @@
}}
<button class="btn add-content section-modchooser section-modchooser-link activitychooser-button d-flex justify-content-center align-items-center p-1 icon-no-margin"
data-action="open-chooser"
data-sectionid="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnid="{{.}}"{{/sectionreturn}}
data-sectionnum="{{sectionnum}}"
{{#sectionreturn}}data-sectionreturnnum="{{.}}"{{/sectionreturn}}
data-beforemod="{{id}}"
aria-label="{{#str}}insertresourceoractivitybefore, core, { "activityname": {{#quote}} {{activityname}} {{/quote}} } {{/str}}"
tabindex="0"