Merge branch 'w12_MDL-38364_m23_phpunitloading' of git://github.com/skodak/moodle...
[moodle.git] / group / clientlib.js
blobf818348595798ecf746203b6f4e5f26b09e194bf
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.
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/>.
16 /**
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
20  * @package core_group
21  */
24 /**
25  * Class UpdatableGroupsCombo
26  */
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");
37                 if (membersComboEl) {
38                     // Clear the members list box.
39                     while (membersComboEl.firstChild) {
40                         membersComboEl.removeChild(membersComboEl.firstChild);
41                     }
42                 }
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);
54                     }
55                 }
56             }
57             // Remove the loader gif image.
58             removeLoaderImgs("groupsloader", "groupslabel");
59         },
61         failure: function(o) {
62             removeLoaderImgs("membersloader", "memberslabel");
63             this.currentTransId = null;
64         }
66     };
68     // Add onchange event to groups list box.
69     // Okay, this is not working in IE. The onchange is never fired...
70     // I'm hard coding the onchange in ../index.php. Not ideal, but it works
71     // then. vyshane AT moodle DOT com.
72     /*
73     groupsComboEl = document.getElementById("groups");
74     if (groupsComboEl) {
75         groupsComboEl.setAttribute("onchange", "membersCombo.refreshMembers(this.options[this.selectedIndex].value);");
76     }
77     */
79     // Hide the updategroups input since AJAX will take care of this.
80     YAHOO.util.Dom.setStyle("updategroups", "display", "none");
84 /**
85  * Class UpdatableMembersCombo
86  */
87 function UpdatableMembersCombo(wwwRoot, courseId) {
88     this.wwwRoot = wwwRoot;
89     this.courseId = courseId;
91     this.connectCallback = {
92         success: function(o) {
94             if (o.responseText !== undefined) {
95                 var selectEl = document.getElementById("members");
96                 if (selectEl && o.responseText) {
97                     var roles = eval("("+o.responseText+")");
99                     // Clear the members list box.
100                     if (selectEl) {
101                         while (selectEl.firstChild) {
102                             selectEl.removeChild(selectEl.firstChild);
103                         }
104                     }
105                     // Populate the members list box.
106                     for (var i=0; i<roles.length; i++) {
107                         var optgroupEl = document.createElement("optgroup");
108                         optgroupEl.setAttribute("label",roles[i].name);
110                         for(var j=0; j<roles[i].users.length; j++) {
111                             var optionEl = document.createElement("option");
112                             optionEl.setAttribute("value", roles[i].users[j].id);
113                             optionEl.title = roles[i].users[j].name;
114                             optionEl.innerHTML = roles[i].users[j].name;
115                             optgroupEl.appendChild(optionEl);
116                         }
117                         selectEl.appendChild(optgroupEl);
118                     }
119                 }
120             }
121             // Remove the loader gif image.
122             removeLoaderImgs("membersloader", "memberslabel");
123         },
125         failure: function(o) {
126             removeLoaderImgs("membersloader", "memberslabel");
127         }
129     };
131     // Hide the updatemembers input since AJAX will take care of this.
132     YAHOO.util.Dom.setStyle("updatemembers", "display", "none");
136  * When a group is selected, we need to update the members.
137  * The Add/Remove Users button also needs to be disabled/enabled
138  * depending on whether or not a group is selected
139  */
140 UpdatableMembersCombo.prototype.refreshMembers = function () {
142     // Get group selector and check selection type
143     var selectEl = document.getElementById("groups");
144     var selectionCount=0,groupId=0;
145     if( selectEl ) {
146         for (var i = 0; i < selectEl.options.length; i++) {
147             if(selectEl.options[i].selected) {
148                 selectionCount++;
149                 if(!groupId) {
150                     groupId=selectEl.options[i].value;
151                 }
152             }
153         }
154     }
155     var singleSelection=selectionCount == 1;
157     // Add the loader gif image (we only load for single selections)
158     if(singleSelection) {
159         createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
160     }
162     // Update the label.
163     var spanEl = document.getElementById("thegroup");
164     if (singleSelection) {
165         spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
166     } else {
167         spanEl.innerHTML = '&nbsp;';
168     }
170     // Clear the members list box.
171     selectEl = document.getElementById("members");
172     if (selectEl) {
173         while (selectEl.firstChild) {
174             selectEl.removeChild(selectEl.firstChild);
175         }
176     }
178     document.getElementById("showaddmembersform").disabled = !singleSelection;
179     document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
180     document.getElementById("deletegroup").disabled = selectionCount == 0;
182     if(singleSelection) {
183         var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
184         YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
185     }
190 var createLoaderImg = function (elClass, parentId, wwwRoot) {
191     var parentEl = document.getElementById(parentId);
192     if (!parentEl) {
193         return false;
194     }
195     if (document.getElementById("loaderImg")) {
196         // A loader image already exists.
197         return false;
198     }
199     var loadingImg = document.createElement("img");
201     loadingImg.setAttribute("src", M.util.image_url('/i/ajaxloader', 'moodle'));
202     loadingImg.setAttribute("class", elClass);
203     loadingImg.setAttribute("alt", "Loading");
204     loadingImg.setAttribute("id", "loaderImg");
205     parentEl.appendChild(loadingImg);
207     return true;
211 var removeLoaderImgs = function (elClass, parentId) {
212     var parentEl = document.getElementById(parentId);
213     if (parentEl) {
214         var loader = document.getElementById("loaderImg");
215         parentEl.removeChild(loader);
216     }
220  * Updates the current groups information shown about a user when a user is selected.
222  * @global {Array} userSummaries
223  *      userSummaries is added to the page via /user/selector/lib.php - group_non_members_selector::print_user_summaries()
224  *      as a global that can be used by this function.
225  */
226 function updateUserSummary() {
227     var selectEl = document.getElementById('addselect'),
228         summaryDiv = document.getElementById('group-usersummary'),
229         length = selectEl.length,
230         selectCnt = 0,
231         selectIdx = -1,
232         i;
234     for (i = 0; i < length; i++) {
235         if (selectEl.options[i].selected) {
236             selectCnt++;
237             selectIdx = i;
238         }
239     }
241     if (selectCnt == 1 && userSummaries[selectIdx]) {
242         summaryDiv.innerHTML = userSummaries[selectIdx];
243     } else {
244         summaryDiv.innerHTML = '';
245     }
247     return true;
250 function init_add_remove_members_page(Y) {
251     var add = Y.one('#add');
252     var addselect = M.core_user.get_user_selector('addselect');
253     add.set('disabled', addselect.is_selection_empty());
254     addselect.on('user_selector:selectionchanged', function(isempty) {
255         add.set('disabled', isempty);
256     });
258     var remove = Y.one('#remove');
259     var removeselect = M.core_user.get_user_selector('removeselect');
260     remove.set('disabled', removeselect.is_selection_empty());
261     removeselect.on('user_selector:selectionchanged', function(isempty) {
262         remove.set('disabled', isempty);
263     });
265     addselect = document.getElementById('addselect');
266     addselect.onchange = updateUserSummary;