clientlib.js 8.6 KB

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