weekly release 3.10.1+
[moodle.git] / group / clientlib.js
CommitLineData
4d8e2417
AG
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
3cdc1e28 16/**
17 * Client-side JavaScript for group management interface.
4d8e2417
AG
18 * @copyright vy-shane AT moodle.com
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20 * @package core_group
3cdc1e28 21 */
22
23
24/**
25 * Class UpdatableGroupsCombo
26 */
27function UpdatableGroupsCombo(wwwRoot, courseId) {
28 this.wwwRoot = wwwRoot;
29 this.courseId = courseId;
30
ac433d41 31 this.connectCallback = {
3cdc1e28 32
95bea1ed 33 success: function(o) {
f1303e92 34 if (o.responseText !== undefined) {
3cdc1e28 35 var groupsComboEl = document.getElementById("groups");
36 var membersComboEl = document.getElementById("members");
3cdc1e28 37 if (membersComboEl) {
e254aa34 38 // Clear the members list box.
3cdc1e28 39 while (membersComboEl.firstChild) {
40 membersComboEl.removeChild(membersComboEl.firstChild);
41 }
42 }
43
f1303e92 44 if (groupsComboEl && o.responseText) {
45 var groups = eval("("+o.responseText+")");
29848126 46
f1303e92 47 // Populate the groups list box.
29848126 48 for (var i=0; i<groups.length; i++) {
49 var optionEl = document.createElement("option");
50 optionEl.setAttribute("value", groups[i].id);
eb674f8b 51 optionEl.title = groups[i].name;
29848126 52 optionEl.innerHTML = groups[i].name;
53 groupsComboEl.appendChild(optionEl);
3cdc1e28 54 }
3cdc1e28 55 }
f1303e92 56 }
57 // Remove the loader gif image.
58 removeLoaderImgs("groupsloader", "groupslabel");
29848126 59 },
60
61 failure: function(o) {
62 removeLoaderImgs("membersloader", "memberslabel");
95bea1ed 63 this.currentTransId = null;
3cdc1e28 64 }
95bea1ed 65
ac433d41 66 };
3cdc1e28 67}
68
3cdc1e28 69
70/**
71 * Class UpdatableMembersCombo
72 */
73function UpdatableMembersCombo(wwwRoot, courseId) {
74 this.wwwRoot = wwwRoot;
75 this.courseId = courseId;
76
ac433d41 77 this.connectCallback = {
a42791cb 78 success: function(t, o) {
3cdc1e28 79
f1303e92 80 if (o.responseText !== undefined) {
3cdc1e28 81 var selectEl = document.getElementById("members");
f1303e92 82 if (selectEl && o.responseText) {
e254aa34 83 var roles = eval("("+o.responseText+")");
29848126 84
e254aa34 85 // Clear the members list box.
53b16b2b 86 if (selectEl) {
87 while (selectEl.firstChild) {
88 selectEl.removeChild(selectEl.firstChild);
89 }
90 }
e254aa34 91 // Populate the members list box.
92 for (var i=0; i<roles.length; i++) {
93 var optgroupEl = document.createElement("optgroup");
94 optgroupEl.setAttribute("label",roles[i].name);
95
96 for(var j=0; j<roles[i].users.length; j++) {
97 var optionEl = document.createElement("option");
98 optionEl.setAttribute("value", roles[i].users[j].id);
99 optionEl.title = roles[i].users[j].name;
100 optionEl.innerHTML = roles[i].users[j].name;
101 optgroupEl.appendChild(optionEl);
102 }
103 selectEl.appendChild(optgroupEl);
3cdc1e28 104 }
3cdc1e28 105 }
f1303e92 106 }
107 // Remove the loader gif image.
108 removeLoaderImgs("membersloader", "memberslabel");
29848126 109 },
110
a42791cb 111 failure: function() {
29848126 112 removeLoaderImgs("membersloader", "memberslabel");
3cdc1e28 113 }
95bea1ed 114
ac433d41 115 };
116
3cdc1e28 117 // Hide the updatemembers input since AJAX will take care of this.
da942d13
AN
118 var updatemembers = Y.one('#updatemembers');
119 if (updatemembers) {
120 updatemembers.hide();
121 }
3cdc1e28 122}
123
124/**
125 * When a group is selected, we need to update the members.
fa2d60c5 126 * The Add/Remove Users button also needs to be disabled/enabled
127 * depending on whether or not a group is selected
3cdc1e28 128 */
f6eece19 129UpdatableMembersCombo.prototype.refreshMembers = function () {
130
131 // Get group selector and check selection type
132 var selectEl = document.getElementById("groups");
133 var selectionCount=0,groupId=0;
134 if( selectEl ) {
135 for (var i = 0; i < selectEl.options.length; i++) {
136 if(selectEl.options[i].selected) {
137 selectionCount++;
138 if(!groupId) {
139 groupId=selectEl.options[i].value;
140 }
141 }
142 }
143 }
144 var singleSelection=selectionCount == 1;
145
146 // Add the loader gif image (we only load for single selections)
147 if(singleSelection) {
148 createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
149 }
6f5e0852 150
eb674f8b 151 // Update the label.
eb674f8b 152 var spanEl = document.getElementById("thegroup");
f6eece19 153 if (singleSelection) {
eb674f8b 154 spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
f6eece19 155 } else {
156 spanEl.innerHTML = '&nbsp;';
eb674f8b 157 }
8bfa86fd 158
e254aa34 159 // Clear the members list box.
eb674f8b 160 selectEl = document.getElementById("members");
29848126 161 if (selectEl) {
162 while (selectEl.firstChild) {
163 selectEl.removeChild(selectEl.firstChild);
164 }
165 }
6f5e0852 166
f6eece19 167 document.getElementById("showaddmembersform").disabled = !singleSelection;
168 document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
169 document.getElementById("deletegroup").disabled = selectionCount == 0;
170
171 if(singleSelection) {
172 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
a42791cb
AN
173 var self = this;
174 YUI().use('io', function (Y) {
175 Y.io(sUrl, {
176 method: 'GET',
177 context: this,
178 on: self.connectCallback
179 });
e3c1d655 180 });
f6eece19 181 }
ac433d41 182};
3cdc1e28 183
184
185
ac433d41 186var createLoaderImg = function (elClass, parentId, wwwRoot) {
187 var parentEl = document.getElementById(parentId);
188 if (!parentEl) {
189 return false;
190 }
95bea1ed 191 if (document.getElementById("loaderImg")) {
192 // A loader image already exists.
ac433d41 193 return false;
194 }
8bfa86fd 195 var loadingImg = document.createElement("img");
196
96bf0b3a 197 loadingImg.setAttribute("src", M.util.image_url('/i/ajaxloader', 'moodle'));
ac433d41 198 loadingImg.setAttribute("class", elClass);
8bfa86fd 199 loadingImg.setAttribute("alt", "Loading");
95bea1ed 200 loadingImg.setAttribute("id", "loaderImg");
ac433d41 201 parentEl.appendChild(loadingImg);
202
203 return true;
204};
8bfa86fd 205
206
ac433d41 207var removeLoaderImgs = function (elClass, parentId) {
208 var parentEl = document.getElementById(parentId);
ac433d41 209 if (parentEl) {
95bea1ed 210 var loader = document.getElementById("loaderImg");
6a021f59
RT
211 if (loader) {
212 parentEl.removeChild(loader);
213 }
ac433d41 214 }
215};
97873016 216
0b3f8547
SH
217/**
218 * Updates the current groups information shown about a user when a user is selected.
219 *
220 * @global {Array} userSummaries
221 * userSummaries is added to the page via /user/selector/lib.php - group_non_members_selector::print_user_summaries()
222 * as a global that can be used by this function.
223 */
bf1ac6d9 224function updateUserSummary() {
0b3f8547
SH
225 var selectEl = document.getElementById('addselect'),
226 summaryDiv = document.getElementById('group-usersummary'),
227 length = selectEl.length,
228 selectCnt = 0,
229 selectIdx = -1,
230 i;
231
232 for (i = 0; i < length; i++) {
233 if (selectEl.options[i].selected) {
234 selectCnt++;
235 selectIdx = i;
236 }
237 }
238
239 if (selectCnt == 1 && userSummaries[selectIdx]) {
240 summaryDiv.innerHTML = userSummaries[selectIdx];
241 } else {
242 summaryDiv.innerHTML = '';
243 }
244
245 return true;
bf1ac6d9
AD
246}
247
456e4852
SH
248function init_add_remove_members_page(Y) {
249 var add = Y.one('#add');
250 var addselect = M.core_user.get_user_selector('addselect');
251 add.set('disabled', addselect.is_selection_empty());
252 addselect.on('user_selector:selectionchanged', function(isempty) {
253 add.set('disabled', isempty);
97873016 254 });
255
456e4852
SH
256 var remove = Y.one('#remove');
257 var removeselect = M.core_user.get_user_selector('removeselect');
258 remove.set('disabled', removeselect.is_selection_empty());
259 removeselect.on('user_selector:selectionchanged', function(isempty) {
260 remove.set('disabled', isempty);
97873016 261 });
bf1ac6d9
AD
262
263 addselect = document.getElementById('addselect');
264 addselect.onchange = updateUserSummary;
a42791cb 265}