2 * Client-side JavaScript for group management interface.
3 * @author vy-shane AT moodle.com
4 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * Class UpdatableGroupsCombo
12 function UpdatableGroupsCombo(wwwRoot, courseId) {
13 this.wwwRoot = wwwRoot;
14 this.courseId = courseId;
16 this.connectCallback = {
18 success: function(o) {
19 if (o.responseText !== undefined) {
20 var groupsComboEl = document.getElementById("groups");
21 var membersComboEl = document.getElementById("members");
24 // Clear the members list box.
25 while (membersComboEl.firstChild) {
26 membersComboEl.removeChild(membersComboEl.firstChild);
30 if (groupsComboEl && o.responseText) {
31 var groups = eval("("+o.responseText+")");
33 // Populate the groups list box.
34 for (var i=0; i<groups.length; i++) {
35 var optionEl = document.createElement("option");
36 optionEl.setAttribute("value", groups[i].id);
37 optionEl.title = groups[i].name;
38 optionEl.innerHTML = groups[i].name;
39 groupsComboEl.appendChild(optionEl);
43 // Remove the loader gif image.
44 removeLoaderImgs("groupsloader", "groupslabel");
47 failure: function(o) {
48 removeLoaderImgs("membersloader", "memberslabel");
49 this.currentTransId = null;
54 // Add onchange event to groups list box.
55 // Okay, this is not working in IE. The onchange is never fired...
56 // I'm hard coding the onchange in ../index.php. Not ideal, but it works
57 // then. vyshane AT moodle DOT com.
59 groupsComboEl = document.getElementById("groups");
61 groupsComboEl.setAttribute("onchange", "membersCombo.refreshMembers(this.options[this.selectedIndex].value);");
65 // Hide the updategroups input since AJAX will take care of this.
66 YAHOO.util.Dom.setStyle("updategroups", "display", "none");
71 * Class UpdatableMembersCombo
73 function UpdatableMembersCombo(wwwRoot, courseId) {
74 this.wwwRoot = wwwRoot;
75 this.courseId = courseId;
77 this.connectCallback = {
78 success: function(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 = 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(o) {
112 removeLoaderImgs("membersloader", "memberslabel");
117 // Hide the updatemembers input since AJAX will take care of this.
118 YAHOO.util.Dom.setStyle("updatemembers", "display", "none");
122 * When a group is selected, we need to update the members.
123 * The Add/Remove Users button also needs to be disabled/enabled
124 * depending on whether or not a group is selected
126 UpdatableMembersCombo.prototype.refreshMembers = function () {
128 // Get group selector and check selection type
129 var selectEl = document.getElementById("groups");
130 var selectionCount=0,groupId=0;
132 for (var i = 0; i < selectEl.options.length; i++) {
133 if(selectEl.options[i].selected) {
136 groupId=selectEl.options[i].value;
141 var singleSelection=selectionCount == 1;
143 // Add the loader gif image (we only load for single selections)
144 if(singleSelection) {
145 createLoaderImg("membersloader", "memberslabel", this.wwwRoot);
149 var spanEl = document.getElementById("thegroup");
150 if (singleSelection) {
151 spanEl.innerHTML = selectEl.options[selectEl.selectedIndex].title;
153 spanEl.innerHTML = ' ';
156 // Clear the members list box.
157 selectEl = document.getElementById("members");
159 while (selectEl.firstChild) {
160 selectEl.removeChild(selectEl.firstChild);
164 document.getElementById("showaddmembersform").disabled = !singleSelection;
165 document.getElementById("showeditgroupsettingsform").disabled = !singleSelection;
166 document.getElementById("deletegroup").disabled = selectionCount == 0;
168 if(singleSelection) {
169 var sUrl = this.wwwRoot+"/group/index.php?id="+this.courseId+"&group="+groupId+"&act_ajax_getmembersingroup";
170 YAHOO.util.Connect.asyncRequest("GET", sUrl, this.connectCallback, null);
176 var createLoaderImg = function (elClass, parentId, wwwRoot) {
177 var parentEl = document.getElementById(parentId);
181 if (document.getElementById("loaderImg")) {
182 // A loader image already exists.
185 var loadingImg = document.createElement("img");
187 loadingImg.setAttribute("src", wwwRoot+"/pix/i/ajaxloader.gif");
188 loadingImg.setAttribute("class", elClass);
189 loadingImg.setAttribute("alt", "Loading");
190 loadingImg.setAttribute("id", "loaderImg");
191 parentEl.appendChild(loadingImg);
197 var removeLoaderImgs = function (elClass, parentId) {
198 var parentEl = document.getElementById(parentId);
200 var loader = document.getElementById("loaderImg");
201 parentEl.removeChild(loader);