enrol MDL-22867 Added functionality to add cohort sycn or preform a one off member...
[moodle.git] / enrol / yui / quickcohortenrolment / quickcohortenrolment.js
1 YUI.add('moodle-enrol-quickcohortenrolment', function(Y) {
3     var CONTROLLERNAME = 'Quick cohort enrolment controller',
4         COHORTNAME = 'Cohort',
5         COHORTID = 'cohortid',
6         ENROLLED = 'enrolled',
7         NAME = 'name',
8         USERS = 'users',
9         COURSEID = 'courseid',
10         ASSIGNABLEROLES = 'assignableRoles',
11         COHORTS = 'cohorts',
12         PANELID = 'qce-panel-',
13         URL = 'url',
14         AJAXURL = 'ajaxurl',
15         MANUALENROLMENT = 'manualEnrolment',
16         CSS = {
17             COHORT : 'qce-cohort',
18             COHORTS : 'qce-cohorts',
19             COHORTBUTTON : 'qce-cohort-button',
20             COHORTENROLLED : 'qce-cohort-enrolled',
21             COHORTNAME : 'qce-cohort-name',
22             COHORTUSERS : 'qce-cohort-users',
23             PANEL : 'qce-panel',
24             PANELCONTENT : 'qce-panel-content',
25             PANELCOHORTS : 'qce-enrollable-cohorts',
26             PANELROLES : 'qce-assignable-roles',
27             PANELCONTROLS : 'qce-panel-controls',
28             ENROLUSERS : 'canenrolusers'
29         },
30         COUNT = 0;
33     var CONTROLLER = function(config) {
34         CONTROLLER.superclass.constructor.apply(this, arguments);
35     }
36     Y.extend(CONTROLLER, Y.Base, {
37         initializer : function(config) {
38             COUNT ++;
39             this.publish('assignablerolesloaded');
40             this.publish('cohortsloaded');
41             
42             var close = Y.Node.create('<div class="close"></div>');
43             var panel = new Y.Overlay({
44                 headerContent : Y.Node.create('<div></div>').append(Y.Node.create('<h2>'+M.str.enrol.enrolcohort+'</h2>')).append(close),
45                 bodyContent : Y.Node.create('<div class="loading"></div>').append(Y.Node.create('<img alt="loading" />').setAttribute('src', M.cfg.loadingicon)),
46                 constrain : true,
47                 centered : true,
48                 id : PANELID+COUNT,
49                 visible : false
50             });
51             panel.get('boundingBox').addClass(CSS.PANEL);
52             panel.render(Y.one(document.body));
53             this.on('show', panel.show, panel);
54             this.on('hide', function() {
55                 this.set('bodyContent', Y.Node.create('<div class="loading"></div>').append(Y.Node.create('<img alt="loading" />').setAttribute('src', M.cfg.loadingicon)));
56                 this.hide();
57             }, panel);
58             this.on('assignablerolesloaded', this.updateContent, this, panel);
59             this.on('cohortsloaded', this.updateContent, this, panel);
60             close.on('click', this.hide, this);
62             Y.all('.enrolcohortbutton input').each(function(node){
63                 if (node.getAttribute('type', 'submit')) {
64                     node.on('click', this.show, this);
65                 }
66             }, this);
68             var base = panel.get('boundingBox');
69             base.plug(Y.Plugin.Drag);
70             base.dd.addHandle('.yui3-widget-hd h2');
71             base.one('.yui3-widget-hd h2').setStyle('cursor', 'move');
72         },
73         show : function(e) {
74             e.preventDefault();
75             this.getCohorts();
76             this.getAssignableRoles();
77             this.fire('show');
78         },
79         updateContent : function(e, panel) {
80             if (panel.get('contentBox').one('.loading')) {
81                 panel.set('bodyContent', Y.Node.create('<div class="'+CSS.PANELCONTENT+'"></div>')
82                     .append(Y.Node.create('<div class="'+CSS.PANELCOHORTS+'"><div class="'+CSS.COHORT+' headings"><div class="'+CSS.COHORTBUTTON+'"></div><div class="'+CSS.COHORTNAME+'">'+M.str.cohort.cohort+'</div><div class="'+CSS.COHORTUSERS+'">'+M.str.moodle.users+'</div></div></div>'))
83                     .append(Y.Node.create('<div class="'+CSS.PANELROLES+'"></div>')));
84             }
85             var content, i, roles, cohorts, count=0, supportmanual = this.get(MANUALENROLMENT);
86             switch (e.type.replace(/^[^:]+:/, '')) {
87                 case 'cohortsloaded' :
88                     cohorts = this.get(COHORTS);
89                     content = Y.Node.create('<div class="'+CSS.COHORTS+'"></div>');
90                     if (supportmanual) {
91                         content.addClass(CSS.ENROLUSERS);
92                     }
93                     for (i in cohorts) {
94                         count++;
95                         cohorts[i].on('enrolchort', this.enrolCohort, this, cohorts[i], panel.get('contentBox'), false);
96                         cohorts[i].on('enrolusers', this.enrolCohort, this, cohorts[i], panel.get('contentBox'), true);
97                         content.append(cohorts[i].toHTML(supportmanual).addClass((count%2)?'even':'odd'));
98                     }
99                     panel.get('contentBox').one('.'+CSS.PANELCOHORTS).append(content);
100                     break;
101                 case 'assignablerolesloaded':
102                     roles = this.get(ASSIGNABLEROLES);
103                     content = Y.Node.create('<select></select>');
104                     for (i in roles) {
105                         content.append(Y.Node.create('<option value="'+i+'">'+roles[i]+'</option>'));
106                     }
107                     panel.get('contentBox').one('.'+CSS.PANELROLES).setContent(Y.Node.create('<div>'+M.str.role.assignroles+': </div>').append(content));
108                     break;
109             }
110         },
111         hide : function() {
112             this.fire('hide');
113         },
114         getCohorts : function() {
115             Y.io(M.cfg.wwwroot+this.get(AJAXURL), {
116                 method:'POST',
117                 data:'id='+this.get(COURSEID)+'&action=getcohorts&sesskey='+M.cfg.sesskey,
118                 on: {
119                     complete: function(tid, outcome, args) {
120                         try {
121                             var cohorts = Y.JSON.parse(outcome.responseText);
122                             this.setCohorts(cohorts.response);
123                         } catch (e) {
124                             Y.fail(CONTROLLERNAME+': Failed to load cohorts');
125                         }
126                         this.getCohorts = function() {
127                             this.fire('cohortsloaded');
128                         }
129                         this.getCohorts();
130                     }
131                 },
132                 context:this
133             });
134         },
135         setCohorts : function(rawcohorts) {
136             var cohorts = [], i=0;
137             for (i in rawcohorts) {
138                 cohorts[rawcohorts[i].cohortid] = new COHORT(rawcohorts[i]);
139             }
140             this.set(COHORTS, cohorts);
141         },
142         getAssignableRoles : function() {
143             Y.io(M.cfg.wwwroot+this.get(AJAXURL), {
144                 method:'POST',
145                 data:'id='+this.get(COURSEID)+'&action=getassignable&sesskey='+M.cfg.sesskey,
146                 on: {
147                     complete: function(tid, outcome, args) {
148                         try {
149                             var roles = Y.JSON.parse(outcome.responseText);
150                             this.set(ASSIGNABLEROLES, roles.response);
151                         } catch (e) {
152                             Y.fail(CONTROLLERNAME+': Failed to load assignable roles');
153                         }
154                         this.getAssignableRoles = function() {
155                             this.fire('assignablerolesloaded');
156                         }
157                         this.getAssignableRoles();
158                     }
159                 },
160                 context:this
161             });
162         },
163         enrolCohort : function(e, cohort, node, usersonly) {
164             var params = {
165                 id : this.get(COURSEID),
166                 roleid : node.one('.'+CSS.PANELROLES+' select').get('value'),
167                 cohortid : cohort.get(COHORTID),
168                 action : (usersonly)?'enrolcohortusers':'enrolcohort',
169                 sesskey : M.cfg.sesskey
170             }
171             Y.io(M.cfg.wwwroot+this.get(AJAXURL), {
172                 method:'POST',
173                 data:build_querystring(params),
174                 on: {
175                     complete: function(tid, outcome, args) {
176                         try {
177                             var result = Y.JSON.parse(outcome.responseText);
178                             if (result.success) {
179                                 if (result.response && result.response.message) {
180                                     alert(result.response.message);
181                                 }
182                                 if (result.response.users) {
183                                     window.location.href = this.get(URL);
184                                 }
185                             } else {
186                                 alert('Failed to enrol cohort');
187                             }
188                         } catch (e) {
189                             Y.fail(CONTROLLERNAME+': Failed to enrol cohort');
190                         }
191                     }
192                 },
193                 context:this
194             });
195         }
196     }, {
197         NAME : CONTROLLERNAME,
198         ATTRS : {
199             url : {
200                 validator : Y.Lang.isString
201             },
202             ajaxurl : {
203                 validator : Y.Lang.isString
204             },
205             courseid : {
206                 value : null
207             },
208             cohorts : {
209                 validator : Y.Lang.isArray,
210                 value : null
211             },
212             assignableRoles : {
213                 value : null
214             },
215             manualEnrolment : {
216                 value : false
217             }
218         }
219     });
220     Y.augment(CONTROLLER, Y.EventTarget);
222     var COHORT = function(config) {
223         COHORT.superclass.constructor.apply(this, arguments);
224     }
225     Y.extend(COHORT, Y.Base, {
226         toHTML : function(supportmanualenrolment){
227             var button, result, name, users, syncbutton, usersbutton;
228             result = Y.Node.create('<div class="'+CSS.COHORT+'"></div>');
229             if (this.get(ENROLLED)) {
230                 button = Y.Node.create('<div class="'+CSS.COHORTBUTTON+' alreadyenrolled">'+M.str.enrol.synced+'</div>');
231             } else {
232                 button = Y.Node.create('<div></div>');
234                 syncbutton = Y.Node.create('<a class="'+CSS.COHORTBUTTON+' notenrolled enrolcohort">'+M.str.enrol.enrolcohort+'</a>');
235                 syncbutton.on('click', function(){this.fire('enrolchort');}, this);
236                 button.append(syncbutton);
238                 if (supportmanualenrolment) {
239                     usersbutton = Y.Node.create('<a class="'+CSS.COHORTBUTTON+' notenrolled enrolusers">'+M.str.enrol.enrolcohortusers+'</a>');
240                     usersbutton.on('click', function(){this.fire('enrolusers');}, this);
241                     button.append(usersbutton);
242                 }
243             }
244             name = Y.Node.create('<div class="'+CSS.COHORTNAME+'">'+this.get(NAME)+'</div>');
245             users = Y.Node.create('<div class="'+CSS.COHORTUSERS+'">'+this.get(USERS)+'</div>');
246             if (this.get(ENROLLED)) {
247                 button.one(CSS.COHORTENROLLED);
248             }
249             return result.append(button).append(name).append(users);
250         }
251     }, {
252         NAME : COHORTNAME,
253         ATTRS : {
254             cohortid : {
255                 
256             },
257             name : {
258                 validator : Y.Lang.isString
259             },
260             enrolled : {
261                 value : false
262             },
263             users : {
264                 value : 0
265             }
266         }
267     });
268     Y.augment(COHORT, Y.EventTarget);
270     M.enrol = M.enrol || {};
271     M.enrol.quickcohortenrolment = {
272         init : function(cfg) {
273             new CONTROLLER(cfg);
274         }
275     }
277 }, '@VERSION@', {requires:['base','node', 'overlay', 'io', 'test', 'json-parse', 'event-delegate', 'dd-plugin', 'event-key']});