1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Client-side JavaScript for group management interface.
18 * @copyright vy-shane AT moodle.com
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * Class UpdatableGroupsCombo
27 function UpdatableGroupsCombo(wwwRoot, courseId) {
28 this.wwwRoot = wwwRoot;
29 this.courseId = courseId;
31 this.connectCallback = {
33 success: function(o) {
34 if (o.responseText !== undefined) {
35 var groupsComboEl = document.getElementById("groups");
36 var membersComboEl = document.getElementById("members");
38 // Clear the members list box.
39 while (membersComboEl.firstChild) {
40 membersComboEl.removeChild(membersComboEl.firstChild);
44 if (groupsComboEl && o.responseText) {
45 var groups = eval("("+o.responseText+")");
47 // Populate the groups list box.
48 for (var i=0; i<groups.length; i++) {
49 var optionEl = document.createElement("option");
50 optionEl.setAttribute("value", groups[i].id);
51 optionEl.title = groups[i].name;
52 optionEl.innerHTML = groups[i].name;
53 groupsComboEl.appendChild(optionEl);
57 // Remove the loader gif image.
58 removeLoaderImgs("groupsloader", "groupslabel");
61 failure: function(o) {
62 removeLoaderImgs("membersloader", "memberslabel");
63 this.currentTransId = null;
71 * Class UpdatableMembersCombo
73 function UpdatableMembersCombo(wwwRoot, courseId) {
74 this.wwwRoot = wwwRoot;
75 this.courseId = courseId;
77 this.connectCallback = {
78 success: function(t, o) {
80 if (o.responseText !== undefined) {
81 var selectEl = document.getElementById("members");
82 if (selectEl && o.responseText) {
83 var roles = eval("("+o.responseText+")");
85 // Clear the members list box.
87 while (selectEl.firstChild) {
88 selectEl.removeChild(selectEl.firstChild);
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);
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 = Y.Escape.html(roles[i].users[j].name);
101 optgroupEl.appendChild(optionEl);
103 selectEl.appendChild(optgroupEl);
107 // Remove the loader gif image.
108 removeLoaderImgs("membersloader", "memberslabel");
111 failure: function() {
112 removeLoaderImgs("membersloader", "memberslabel");
117 // Hide the updatemembers input since AJAX will take care of this.
118 var updatemembers = Y.one('#updatemembers');
120 updatemembers.hide();
125 * When a group is selected, we need to update the members.
126 * The Add/Remove Users button also needs to be disabled/enabled
127 * depending on whether or not a group is selected
129 UpdatableMembersCombo.prototype.refreshMembers = function () {
131 // Get group selector and check selection type
132 var selectEl = document.getElementById("groups");
133 var selectionCount=0,groupId=0;
135 for (var i = 0; i < selectEl.options.length; i++) {
136 if(selectEl.options[i].selected) {
139 groupId=selectEl.options[i].value;
144 var singleSelection=selectionCount == 1;
146 // Add the loader gif image (we only load for single selections)
147 if(singleSelection) {
148 createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
152 var spanEl = document.getElementById("thegroup");
153 if (singleSelection) {
154 spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
156 spanEl.innerHTML = ' ';
159 // Clear the members list box.
160 selectEl = document.getElementById("members");
162 while (selectEl.firstChild) {
163 selectEl.removeChild(selectEl.firstChild);
167 document.getElementById("showaddmembersform").disabled = !singleSelection;
168 document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
169 document.getElementById("deletegroup").disabled = selectionCount == 0;
171 if(singleSelection) {
172 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
174 YUI().use('io', function (Y) {
178 on: self.connectCallback
186 var createLoaderImg = function (elClass, parentId, wwwRoot) {
187 var parentEl = document.getElementById(parentId);
191 if (document.getElementById("loaderImg")) {
192 // A loader image already exists.
195 var loadingImg = document.createElement("img");
197 loadingImg.setAttribute("src", M.util.image_url('/i/ajaxloader', 'moodle'));
198 loadingImg.setAttribute("class", elClass);
199 loadingImg.setAttribute("alt", "Loading");
200 loadingImg.setAttribute("id", "loaderImg");
201 parentEl.appendChild(loadingImg);
207 var removeLoaderImgs = function (elClass, parentId) {
208 var parentEl = document.getElementById(parentId);
210 var loader = document.getElementById("loaderImg");
212 parentEl.removeChild(loader);
218 * Updates the current groups information shown about a user when a user is selected.
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.
224 function updateUserSummary() {
225 var selectEl = document.getElementById('addselect'),
226 summaryDiv = document.getElementById('group-usersummary'),
227 length = selectEl.length,
232 for (i = 0; i < length; i++) {
233 if (selectEl.options[i].selected) {
239 if (selectCnt == 1 && userSummaries[selectIdx]) {
240 summaryDiv.innerHTML = userSummaries[selectIdx];
242 summaryDiv.innerHTML = '';
248 function 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);
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);
263 addselect = document.getElementById('addselect');
264 addselect.onchange = updateUserSummary;