1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Select course categories for LTI tool.
|
|
|
18 |
*
|
|
|
19 |
* @module mod_lti/coursecategory
|
|
|
20 |
* @copyright 2023 Jackson D'souza <jackson.dsouza@catalyst-eu.net>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
* @since 4.3
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
define([], function() {
|
|
|
26 |
|
|
|
27 |
document.addEventListener('click', event => {
|
|
|
28 |
const checkedbox = event.target.closest(".lticoursecategories");
|
|
|
29 |
|
|
|
30 |
if (checkedbox) {
|
|
|
31 |
// Get checkbox status.
|
|
|
32 |
const checkboxstatus = checkedbox.checked;
|
|
|
33 |
|
|
|
34 |
// Check / Uncheck all child category checkboxes based on selected checkbox status.
|
|
|
35 |
const categorycontainer = document.querySelector('#collapse' + checkedbox.value);
|
|
|
36 |
if (categorycontainer) {
|
|
|
37 |
const categorycontainercheckbox = categorycontainer.querySelectorAll('input[type="checkbox"]');
|
|
|
38 |
for (let i = 0; i < categorycontainercheckbox.length; i++) {
|
|
|
39 |
categorycontainercheckbox[i].checked = checkboxstatus;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
const lticategorytree = document.querySelector('.modltitree');
|
|
|
44 |
const ltitreecheckbox = lticategorytree.querySelectorAll('input[type="checkbox"]');
|
|
|
45 |
let listvalue = '';
|
|
|
46 |
for (let i = 0; i < ltitreecheckbox.length; i++) {
|
|
|
47 |
if (ltitreecheckbox[i].checked) {
|
|
|
48 |
if (listvalue.length == 0) {
|
|
|
49 |
listvalue = ltitreecheckbox[i].value;
|
|
|
50 |
} else {
|
|
|
51 |
listvalue = listvalue + ',' + ltitreecheckbox[i].value;
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
document.querySelector('input[name="lti_coursecategories"]').value = listvalue;
|
|
|
56 |
}
|
|
|
57 |
});
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Get parent elements with class = accordion.
|
|
|
61 |
*
|
|
|
62 |
* @method getParents
|
|
|
63 |
* @private
|
|
|
64 |
* @param {string} elem DOM element.
|
|
|
65 |
* @return {array} Parent elements.
|
|
|
66 |
*/
|
|
|
67 |
function getParents(elem) {
|
|
|
68 |
// Set up a parent array
|
|
|
69 |
const parents = [];
|
|
|
70 |
|
|
|
71 |
// Push each parent element to the array
|
|
|
72 |
for (; elem && elem !== document; elem = elem.parentNode) {
|
|
|
73 |
if (elem.classList.contains('accordion-group')) {
|
|
|
74 |
parents.push(elem);
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
// Return our parent array
|
|
|
79 |
return parents;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return /** @alias module:mod_lti/coursecategory */ {
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Initialise this module.
|
|
|
86 |
* Loop through checkbox form elements starting with #cat-{N} and set it to checked
|
|
|
87 |
* if {N} is found in the Selected category(s) list. Show / Hide the parent UL element.
|
|
|
88 |
*
|
|
|
89 |
* @param {string} selectedcategories Selected category(s).
|
|
|
90 |
*/
|
|
|
91 |
init: function(selectedcategories) {
|
|
|
92 |
if (selectedcategories.length) {
|
|
|
93 |
const separator = ",";
|
|
|
94 |
const values = selectedcategories.split(separator);
|
|
|
95 |
|
|
|
96 |
for (let i = 0; i < values.length; i++) {
|
|
|
97 |
const categoryid = document.getElementById("cat-" + values[i]);
|
|
|
98 |
if (categoryid.value !== 0) {
|
|
|
99 |
categoryid.checked = true;
|
|
|
100 |
}
|
|
|
101 |
const parents = getParents(categoryid);
|
|
|
102 |
parents.forEach(function(element) {
|
|
|
103 |
const elem = element.querySelector('a.accordion-toggle');
|
|
|
104 |
const elembody = element.querySelector('.accordion-body');
|
|
|
105 |
|
|
|
106 |
if (elem && elem.classList.contains('collapsed')) {
|
|
|
107 |
elem.classList.remove('collapsed');
|
|
|
108 |
}
|
|
|
109 |
if (elembody) {
|
|
|
110 |
elembody.classList.remove('collapse');
|
|
|
111 |
elembody.classList.add('show');
|
|
|
112 |
}
|
|
|
113 |
});
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
};
|
|
|
118 |
});
|