3a3824c7b0c42a7e44d4f6095b211ab358688379
[moodle.git] / enrol / manual / yui / quickenrolment / quickenrolment.js
1 YUI.add('moodle-enrol_manual-quickenrolment', function(Y) {
3     var UEP = {
4         NAME : 'Enrolment Manager',
5         /** Properties **/
6         BASE : 'base',
7         SEARCH : 'search',
8         SEARCHBTN : 'searchbtn',
9         PARAMS : 'params',
10         URL : 'url',
11         AJAXURL : 'ajaxurl',
12         MULTIPLE : 'multiple',
13         PAGE : 'page',
14         COURSEID : 'courseid',
15         USERS : 'users',
16         USERCOUNT : 'userCount',
17         REQUIREREFRESH : 'requiresRefresh',
18         LASTSEARCH : 'lastPreSearchValue',
19         INSTANCES : 'instances',
20         OPTIONSTARTDATE : 'optionsStartDate',
21         DEFAULTROLE : 'defaultRole',
22         DEFAULTSTARTDATE : 'defaultStartDate',
23         DEFAULTDURATION : 'defaultDuration',
24         ASSIGNABLEROLES : 'assignableRoles',
25         DISABLEGRADEHISTORY : 'disableGradeHistory',
26         RECOVERGRADESDEFAULT : 'recoverGradesDefault',
27         ENROLCOUNT : 'enrolCount',
28         PERPAGE : 'perPage',
29         COHORTSAVAILABLE : 'cohortsAvailable',
30         COHORTCOUNT : 'cohortCount'
31     };
32     /** CSS classes for nodes in structure **/
33     var CSS = {
34         PANEL : 'user-enroller-panel',
35         WRAP : 'uep-wrap',
36         HEADER : 'uep-header',
37         CONTENT : 'uep-content',
38         AJAXCONTENT : 'uep-ajax-content',
39         SEARCHRESULTS : 'uep-search-results',
40         TOTALUSERS : 'totalusers',
41         USERS : 'users',
42         USER : 'user',
43         MORERESULTS : 'uep-more-results',
44         LIGHTBOX : 'uep-loading-lightbox',
45         LOADINGICON : 'loading-icon',
46         FOOTER : 'uep-footer',
47         ENROL : 'enrol',
48         ENROLLED : 'enrolled',
49         COUNT : 'count',
50         PICTURE : 'picture',
51         DETAILS : 'details',
52         FULLNAME : 'fullname',
53         EXTRAFIELDS : 'extrafields',
54         OPTIONS : 'options',
55         ODD  : 'odd',
56         EVEN : 'even',
57         HIDDEN : 'hidden',
58         RECOVERGRADES : 'recovergrades',
59         RECOVERGRADESTITLE : 'recovergradestitle',
60         SEARCHOPTIONS : 'uep-searchoptions',
61         COLLAPSIBLEHEADING : 'collapsibleheading',
62         COLLAPSIBLEAREA : 'collapsiblearea',
63         ENROLMENTOPTION : 'uep-enrolment-option',
64         SEARCHCONTROLS : 'uep-controls',
65         ROLE : 'role',
66         STARTDATE : 'startdate',
67         DURATION : 'duration',
68         ACTIVE : 'active',
69         SEARCH : 'uep-search',
70         SEARCHBTN : 'uep-search-btn',
71         CLOSE : 'close',
72         CLOSEBTN : 'close-button',
73         ENTITYSELECTOR : 'uep-entity-selector',
74         COHORTS : 'cohorts',
75         COHORT : 'cohort',
76         COHORTNAME : 'cohortname',
77         TOTALCOHORTS : 'totalcohorts'
78     };
79     var create = Y.Node.create;
81     var USERENROLLER = function(config) {
82         USERENROLLER.superclass.constructor.apply(this, arguments);
83     };
84     Y.extend(USERENROLLER, Y.Base, {
85         _searchTimeout : null,
86         _loadingNode : null,
87         _escCloseEvent : null,
88         initializer : function(config) {
89             var recovergrades = null;
90             if (this.get(UEP.DISABLEGRADEHISTORY) != true) {
91                 recovergrades = create('<div class="'+CSS.ENROLMENTOPTION+' '+CSS.RECOVERGRADES+'"></div>')
92                     .append(create('<label class="'+CSS.RECOVERGRADESTITLE+'" for="'+CSS.RECOVERGRADES+'">'+M.util.get_string('recovergrades', 'enrol')+'</label>'))
93                     .append(create('<input type="checkbox" id="'+CSS.RECOVERGRADES+'" name="'+CSS.RECOVERGRADES+'"'+ this.get(UEP.RECOVERGRADESDEFAULT) +' />'))
94             }
96             this.set(UEP.BASE, create('<div class="'+CSS.PANEL+' '+CSS.HIDDEN+'"></div>')
97                 .append(create('<div class="'+CSS.WRAP+'"></div>')
98                     .append(create('<div class="'+CSS.HEADER+' header"></div>')
99                         .append(create('<div class="'+CSS.CLOSE+'"></div>'))
100                         .append(create('<h2>'+M.util.get_string('enrolusers', 'enrol')+'</h2>')))
101                     .append(create('<div class="'+CSS.CONTENT+'"></div>')
102                         .append(create('<div class="'+CSS.SEARCHCONTROLS+'"></div>')
103                             .append(create('<div class="'+CSS.ENROLMENTOPTION+' '+CSS.ROLE+'"><label for="id_enrol_manual_assignable_roles">'+M.util.get_string('assignroles', 'role')+'</label></div>')
104                                     .append(create('<select id="id_enrol_manual_assignable_roles"><option value="">'+M.util.get_string('none', 'enrol')+'</option></select>'))
105                             )
106                             .append(create('<div class="'+CSS.ENTITYSELECTOR+'"></div>'))
107                             .append(create('<div class="'+CSS.SEARCHOPTIONS+'"></div>')
108                                 .append(create('<div class="'+CSS.COLLAPSIBLEHEADING+'"><img alt="" />'+M.util.get_string('enrolmentoptions', 'enrol')+'</div>'))
109                                 .append(create('<div class="'+CSS.COLLAPSIBLEAREA+' '+CSS.HIDDEN+'"></div>')
110                                     .append(recovergrades)
111                                     .append(create('<div class="'+CSS.ENROLMENTOPTION+' '+CSS.STARTDATE+'">'+M.util.get_string('startingfrom', 'moodle')+'</div>')
112                                         .append(create('<select></select>')))
113                                     .append(create('<div class="'+CSS.ENROLMENTOPTION+' '+CSS.DURATION+'">'+M.util.get_string('enrolperiod', 'enrol')+'</div>')
114                                         .append(create('<select><option value="0" selected="selected">'+M.util.get_string('unlimitedduration', 'enrol')+'</option></select>')))
115                                 )
116                             )
117                             .append(create('<div class="'+CSS.SEARCH+'"><label for="enrolusersearch" class="accesshide">'+M.util.get_string('usersearch', 'enrol')+'</label></div>')
118                                 .append(create('<input type="text" id="enrolusersearch" value="" />'))
119                                 .append(create('<input type="button" id="searchbtn" class="'+CSS.SEARCHBTN+'" value="'+M.util.get_string('usersearch', 'enrol')+'" />'))
120                             )
121                         )
122                         .append(create('<div class="'+CSS.AJAXCONTENT+'"></div>'))
123                         .append(create('<div class="'+CSS.LIGHTBOX+' '+CSS.HIDDEN+'"></div>')
124                             .append(create('<img alt="loading" class="'+CSS.LOADINGICON+'" />')
125                                 .setAttribute('src', M.util.image_url('i/loading', 'moodle')))
126                             .setStyle('opacity', 0.5)))
127                     .append(create('<div class="'+CSS.FOOTER+'"></div>')
128                         .append(create('<div class="'+CSS.CLOSEBTN+'"></div>')
129                             .append(create('<input type="button" value="'+M.util.get_string('finishenrollingusers', 'enrol')+'" />'))
130                         )
131                     )
132                 )
133             );
135             this.set(UEP.SEARCH, this.get(UEP.BASE).one('#enrolusersearch'));
136             this.set(UEP.SEARCHBTN, this.get(UEP.BASE).one('#searchbtn'));
137             Y.all('.enrol_manual_plugin input').each(function(node){
138                 if (node.getAttribute('type', 'submit')) {
139                     node.on('click', this.show, this);
140                 }
141             }, this);
142             this.get(UEP.BASE).one('.'+CSS.HEADER+' .'+CSS.CLOSE).on('click', this.hide, this);
143             this.get(UEP.BASE).one('.'+CSS.FOOTER+' .'+CSS.CLOSEBTN+' input').on('click', this.hide, this);
144             this._loadingNode = this.get(UEP.BASE).one('.'+CSS.CONTENT+' .'+CSS.LIGHTBOX);
145             var params = this.get(UEP.PARAMS);
146             params['id'] = this.get(UEP.COURSEID);
147             this.set(UEP.PARAMS, params);
149             Y.on('key', this.preSearch, this.get(UEP.SEARCH), 'down:13', this);
150             this.get(UEP.SEARCHBTN).on('click', this.preSearch, this);
152             if (this.get(UEP.COHORTSAVAILABLE)) {
153                 this.get(UEP.BASE).one('.'+CSS.ENTITYSELECTOR)
154                     .append(create('<input type="radio" id="id_enrol_manual_entity_users" name="enrol_manual_entity" value="users" checked="checked"/>'))
155                     .append(create('<label for="id_enrol_manual_entity_users">'+ M.util.get_string('browseusers', 'enrol_manual')+'</label>'))
156                     .append(create('<input type="radio" id="id_enrol_manual_entity_cohorts" name="enrol_manual_entity" value="cohorts"/>'))
157                     .append(create('<label for="id_enrol_manual_entity_cohorts">'+M.util.get_string('browsecohorts', 'enrol_manual')+'</label>'));
158                 this.get(UEP.BASE).one('#id_enrol_manual_entity_cohorts').on('change', this.search, this);
159                 this.get(UEP.BASE).one('#id_enrol_manual_entity_users').on('change', this.search, this);
160             } else {
161                 this.get(UEP.BASE).one('.'+CSS.ENTITYSELECTOR)
162                     .append(create('<input type="hidden" name="enrol_manual_entity" value="users"/>'));
163             }
165             Y.one(document.body).append(this.get(UEP.BASE));
167             var base = this.get(UEP.BASE);
168             base.plug(Y.Plugin.Drag);
169             base.dd.addHandle('.'+CSS.HEADER+' h2');
170             base.one('.'+CSS.HEADER+' h2').setStyle('cursor', 'move');
172             var collapsedimage = 't/collapsed'; // ltr mode
173             if ( Y.one(document.body).hasClass('dir-rtl') ) {
174                 collapsedimage = 't/collapsed_rtl';
175             } else {
176                 collapsedimage = 't/collapsed';
177             }
179             this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEHEADING).one('img').setAttribute('src', M.util.image_url(collapsedimage, 'moodle'));
180             this.populateStartDates();
181             this.populateDuration();
182             this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEHEADING).on('click', function(){
183                 this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEHEADING).toggleClass(CSS.ACTIVE);
184                 this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEAREA).toggleClass(CSS.HIDDEN);
185                 if (this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEAREA).hasClass(CSS.HIDDEN)) {
186                     this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEHEADING).one('img').setAttribute('src', M.util.image_url(collapsedimage, 'moodle'));
187                 } else {
188                     this.get(UEP.BASE).one('.'+CSS.SEARCHOPTIONS+' .'+CSS.COLLAPSIBLEHEADING).one('img').setAttribute('src', M.util.image_url('t/expanded', 'moodle'));
189                 }
190             }, this);
191             this.populateAssignableRoles();
192         },
193         populateAssignableRoles : function() {
194             this.on('assignablerolesloaded', function(){
195                 var roles = this.get(UEP.ASSIGNABLEROLES);
196                 var s = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.ROLE+' select');
197                 var v = this.get(UEP.DEFAULTROLE);
198                 var index = 0, count = 0;
199                 for (var i in roles) {
200                     count++;
201                     var option = create('<option value="'+i+'">'+roles[i]+'</option>');
202                     if (i == v) {
203                         index = count;
204                     }
205                     s.append(option);
206                 }
207                 s.set('selectedIndex', index);
208                 Y.one('#id_enrol_manual_assignable_roles').focus();
209             }, this);
210             this.getAssignableRoles();
211         },
212         populateStartDates : function() {
213             var select = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.STARTDATE+' select');
214             var defaultvalue = this.get(UEP.DEFAULTSTARTDATE);
215             var options = this.get(UEP.OPTIONSTARTDATE);
216             var index = 0, count = 0;
217             for (var i in options) {
218                 var option = create('<option value="'+i+'">'+options[i]+'</option>');
219                 if (i == defaultvalue) {
220                     index = count;
221                 }
222                 select.append(option);
223                 count++;
224             }
225             select.set('selectedIndex', index);
226         },
227         populateDuration : function() {
228             var select = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.DURATION+' select');
229             var defaultvalue = this.get(UEP.DEFAULTDURATION);
230             var index = 0, count = 0;
231             var durationdays = M.util.get_string('durationdays', 'enrol', '{a}');
232             for (var i = 1; i <= 365; i++) {
233                 count++;
234                 var option = create('<option value="'+i+'">'+durationdays.replace('{a}', i)+'</option>');
235                 if (i == defaultvalue) {
236                     index = count;
237                 }
238                 select.append(option);
239             }
240             if (!index) {
241                 select.append(create('<option value="'+defaultvalue+'">'+durationdays.replace('{a}', defaultvalue)+'</option>'));
242                 index = ++count;
243             }
244             select.set('selectedIndex', index);
245         },
246         getAssignableRoles : function(){
247             Y.io(M.cfg.wwwroot+'/enrol/ajax.php', {
248                 method:'POST',
249                 data:'id='+this.get(UEP.COURSEID)+'&action=getassignable&sesskey='+M.cfg.sesskey,
250                 on: {
251                     complete: function(tid, outcome, args) {
252                         try {
253                             var roles = Y.JSON.parse(outcome.responseText);
254                             this.set(UEP.ASSIGNABLEROLES, roles.response);
255                         } catch (e) {
256                             new M.core.exception(e);
257                         }
258                         this.getAssignableRoles = function() {
259                             this.fire('assignablerolesloaded');
260                         };
261                         this.getAssignableRoles();
262                     }
263                 },
264                 context:this
265             });
266         },
267         preSearch : function(e) {
268             this.search(e, false);
269             /*
270             var value = this.get(UEP.SEARCH).get('value');
271             if (value.length < 3 || value == this.get(UEP.LASTSEARCH)) {
272                 return;
273             }
274             this.set(UEP.LASTSEARCH, value);
275             if (this._searchTimeout) {
276                 clearTimeout(this._searchTimeout);
277                 this._searchTimeout = null;
278             }
279             var self = this;
280             this._searchTimeout = setTimeout(function(){
281                 self._searchTimeout = null;
282                 self.search(null, false);
283             }, 300);
284             */
285         },
286         show : function(e) {
287             e.preventDefault();
288             e.halt();
290             var base = this.get(UEP.BASE);
291             base.removeClass(CSS.HIDDEN);
292             var x = (base.get('winWidth') - 400)/2;
293             var y = (parseInt(base.get('winHeight'))-base.get('offsetHeight'))/2 + parseInt(base.get('docScrollY'));
294             if (y < parseInt(base.get('winHeight'))*0.1) {
295                 y = parseInt(base.get('winHeight'))*0.1;
296             }
297             base.setXY([x,y]);
298             var zindex = 0;
299             Y.all('.moodle-has-zindex').each(function() {
300                 if (parseInt(this.getComputedStyle('zIndex'), 10) > zindex) {
301                     zindex = parseInt(this.getComputedStyle('zIndex'), 10);
302                 }
303             });
304             base.setStyle('zIndex', zindex + 1);
306             if (this.get(UEP.USERS)===null) {
307                 this.search(e, false);
308             }
310             this._escCloseEvent = Y.on('key', this.hide, document.body, 'down:27', this);
311             var rolesselect = Y.one('#id_enrol_manual_assignable_roles');
312             if (rolesselect) {
313                 rolesselect.focus();
314             }
315         },
316         hide : function(e) {
317             if (this._escCloseEvent) {
318                 this._escCloseEvent.detach();
319                 this._escCloseEvent = null;
320             }
321             this.get(UEP.BASE).addClass(CSS.HIDDEN);
322             if (this.get(UEP.REQUIREREFRESH)) {
323                 window.location = this.get(UEP.URL);
324             }
325         },
326         currentEntity : function() {
327             var entity = CSS.USER;
328             var cohortsinput = Y.one('#id_enrol_manual_entity_cohorts');
329             if (cohortsinput && cohortsinput.get('checked')) {
330                 entity = CSS.COHORT;
331             }
332             return entity;
333         },
334         search : function(e, append) {
335             var entity = this.currentEntity();
337             if (e) {
338                 e.halt();
339                 e.preventDefault();
340             }
341             var on, params;
342             if (append) {
343                 this.set(UEP.PAGE, this.get(UEP.PAGE)+1);
344             } else {
345                 this.set(UEP.USERCOUNT, 0);
346                 this.set(UEP.COHORTCOUNT, 0);
347                 this.set(UEP.PAGE, 0);
348             }
349             params = this.get(UEP.PARAMS);
350             params['sesskey'] = M.cfg.sesskey;
351             params['action'] = (entity === CSS.USER) ? 'searchusers' : 'searchcohorts';
352             params['search'] = this.get(UEP.SEARCH).get('value');
353             params['page'] = this.get(UEP.PAGE);
354             params['enrolcount'] = this.get(UEP.ENROLCOUNT);
355             params['perpage'] = this.get(UEP.PERPAGE);
357             if (this.get(UEP.MULTIPLE)) {
358                 alert('oh no there are multiple');
359             } else {
360                 var instance = this.get(UEP.INSTANCES)[0];
361                 params['enrolid'] = instance.id;
362             }
363             Y.io(M.cfg.wwwroot+this.get(UEP.AJAXURL), {
364                 method:'POST',
365                 data:build_querystring(params),
366                 on : {
367                     start : this.displayLoading,
368                     complete: ((entity === CSS.USER) ? this.processSearchResults : this.processCohortsSearchResults),
369                     end : this.removeLoading
370                 },
371                 context:this,
372                 arguments:{
373                     append:append,
374                     enrolid:params['enrolid']
375                 }
376             });
377         },
378         displayLoading : function() {
379             this._loadingNode.removeClass(CSS.HIDDEN);
380         },
381         removeLoading : function() {
382             this._loadingNode.addClass(CSS.HIDDEN);
383         },
384         processSearchResults : function(tid, outcome, args) {
385             try {
386                 var result = Y.JSON.parse(outcome.responseText);
387                 if (result.error) {
388                     return new M.core.ajaxException(result);
389                 }
390             } catch (e) {
391                 new M.core.exception(e);
392             }
393             if (!result.success) {
394                 this.setContent = M.util.get_string('errajaxsearch', 'enrol');
395             }
396             var users;
397             if (!args.append) {
398                 users = create('<div class="'+CSS.USERS+'"></div>');
399             } else {
400                 users = this.get(UEP.BASE).one('.'+CSS.SEARCHRESULTS+' .'+CSS.USERS);
401             }
402             var count = this.get(UEP.USERCOUNT);
403             for (var i in result.response.users) {
404                 count++;
405                 var user = result.response.users[i];
406                 users.append(create('<div class="'+CSS.USER+' clearfix" rel="'+user.id+'"></div>')
407                     .addClass((count%2)?CSS.ODD:CSS.EVEN)
408                     .append(create('<div class="'+CSS.COUNT+'">'+count+'</div>'))
409                     .append(create('<div class="'+CSS.PICTURE+'"></div>')
410                         .append(create(user.picture)))
411                     .append(create('<div class="'+CSS.DETAILS+'"></div>')
412                         .append(create('<div class="'+CSS.FULLNAME+'">'+user.fullname+'</div>'))
413                         .append(create('<div class="'+CSS.EXTRAFIELDS+'">'+user.extrafields+'</div>')))
414                     .append(create('<div class="'+CSS.OPTIONS+'"></div>')
415                         .append(create('<input type="button" class="'+CSS.ENROL+'" value="'+M.util.get_string('enrol', 'enrol')+'" />')))
416                 );
417             }
418             this.set(UEP.USERCOUNT, count);
419             if (!args.append) {
420                 var usersstr = (result.response.totalusers == '1')?M.util.get_string('ajaxoneuserfound', 'enrol'):M.util.get_string('ajaxxusersfound','enrol', result.response.totalusers);
421                 var content = create('<div class="'+CSS.SEARCHRESULTS+'"></div>')
422                     .append(create('<div class="'+CSS.TOTALUSERS+'">'+usersstr+'</div>'))
423                     .append(users);
424                 if (result.response.totalusers > (this.get(UEP.PAGE)+1)*this.get(UEP.PERPAGE)) {
425                     var fetchmore = create('<div class="'+CSS.MORERESULTS+'"><a href="#">'+M.util.get_string('ajaxnext25', 'enrol')+'</a></div>');
426                     fetchmore.on('click', this.search, this, true);
427                     content.append(fetchmore)
428                 }
429                 this.setContent(content);
430                 Y.delegate("click", this.enrolUser, users, '.'+CSS.USER+' .'+CSS.ENROL, this, args);
431             } else {
432                 if (result.response.totalusers <= (this.get(UEP.PAGE)+1)*this.get(UEP.PERPAGE)) {
433                     this.get(UEP.BASE).one('.'+CSS.MORERESULTS).remove();
434                 }
435             }
436         },
437         processCohortsSearchResults : function(tid, outcome, args) {
438             try {
439                 var result = Y.JSON.parse(outcome.responseText);
440                 if (result.error) {
441                     return new M.core.ajaxException(result);
442                 }
443             } catch (e) {
444                 new M.core.exception(e);
445             }
446             if (!result.success) {
447                 this.setContent = M.util.get_string('errajaxsearch', 'enrol');
448             }
449             var cohorts;
450             if (!args.append) {
451                 cohorts = create('<div class="'+CSS.COHORTS+'"></div>');
452             } else {
453                 cohorts = this.get(UEP.BASE).one('.'+CSS.SEARCHRESULTS+' .'+CSS.COHORTS);
454             }
455             var count = this.get(UEP.COHORTCOUNT);
456             for (var i in result.response.cohorts) {
457                 count++;
458                 var cohort = result.response.cohorts[i];
459                 cohorts.append(create('<div class="'+CSS.COHORT+' clearfix" rel="'+cohort.id+'"></div>')
460                     .addClass((count%2)?CSS.ODD:CSS.EVEN)
461                     .append(create('<div class="'+CSS.COUNT+'">'+count+'</div>'))
462                     .append(create('<div class="'+CSS.DETAILS+'"></div>')
463                         .append(create('<div class="'+CSS.COHORTNAME+'">'+cohort.name+'</div>')))
464                     .append(create('<div class="'+CSS.OPTIONS+'"></div>')
465                         .append(create('<input type="button" class="' + CSS.ENROL + '" value="' + M.util.get_string('enrolxusers', 'enrol', cohort.cnt) + '" />')))
466                 );
467             }
468             this.set(UEP.COHORTCOUNT, count);
469             if (!args.append) {
470                 //var usersstr = (result.response.totalusers == '1')?M.util.get_string('ajaxoneuserfound', 'enrol'):M.util.get_string('ajaxxusersfound','enrol', result.response.totalusers);
471                 var cohortsstr = M.util.get_string('foundxcohorts', 'enrol', result.response.totalcohorts);
472                 var content = create('<div class="'+CSS.SEARCHRESULTS+'"></div>')
473                     .append(create('<div class="'+CSS.TOTALCOHORTS+'">'+cohortsstr+'</div>'))
474                     .append(cohorts);
475                 if (result.response.totalcohorts > (this.get(UEP.PAGE)+1)*this.get(UEP.PERPAGE)) {
476                     var fetchmore = create('<div class="'+CSS.MORERESULTS+'"><a href="#">'+M.util.get_string('ajaxnext25', 'enrol')+'</a></div>');
477                     fetchmore.on('click', this.search, this, true);
478                     content.append(fetchmore)
479                 }
480                 this.setContent(content);
481                 Y.delegate("click", this.enrolUser, cohorts, '.'+CSS.COHORT+' .'+CSS.ENROL, this, args);
482             } else {
483                 if (result.response.totalcohorts <= (this.get(UEP.PAGE)+1)*this.get(UEP.PERPAGE)) {
484                     this.get(UEP.BASE).one('.'+CSS.MORERESULTS).remove();
485                 }
486             }
487         },
488         enrolUser : function(e, args) {
489             var entityname = this.currentEntity();
491             var entity = e.currentTarget.ancestor('.'+entityname);
492             var params = [];
493             params['id'] = this.get(UEP.COURSEID);
494             if (entityname === CSS.USER) {
495                 params['userid'] = entity.getAttribute("rel");
496             } else {
497                 params['cohortid'] = entity.getAttribute("rel");
498             }
499             params['enrolid'] = args.enrolid;
500             params['sesskey'] = M.cfg.sesskey;
501             params['action'] = 'enrol';
502             params['role'] = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.ROLE+' select').get('value');
503             params['startdate'] = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.STARTDATE+' select').get('value');
504             params['duration'] = this.get(UEP.BASE).one('.'+CSS.ENROLMENTOPTION+'.'+CSS.DURATION+' select').get('value');
505             if (this.get(UEP.DISABLEGRADEHISTORY) != true) {
506                 params['recovergrades'] = this.get(UEP.BASE).one('#'+CSS.RECOVERGRADES).get('checked')?1:0;
507             } else {
508                 params['recovergrades'] = 0;
509             }
511             Y.io(M.cfg.wwwroot+this.get(UEP.AJAXURL), {
512                 method:'POST',
513                 data:build_querystring(params),
514                 on: {
515                     start : this.displayLoading,
516                     complete : function(tid, outcome, args) {
517                         try {
518                             var result = Y.JSON.parse(outcome.responseText);
519                             if (result.error) {
520                                 return new M.core.ajaxException(result);
521                             } else {
522                                 args.entityNode.addClass(CSS.ENROLLED);
523                                 args.entityNode.one('.'+CSS.ENROL).remove();
524                                 this.set(UEP.REQUIREREFRESH, true);
525                                 var countenrol = this.get(UEP.ENROLCOUNT)+1;
526                                 this.set(UEP.ENROLCOUNT, countenrol);
527                             }
528                         } catch (e) {
529                             new M.core.exception(e);
530                         }
531                     },
532                     end : this.removeLoading
533                 },
534                 context:this,
535                 arguments:{
536                     params : params,
537                     entityNode : entity
538                 }
539             });
541         },
542         setContent: function(content) {
543             this.get(UEP.BASE).one('.'+CSS.CONTENT+' .'+CSS.AJAXCONTENT).setContent(content);
544         }
545     }, {
546         NAME : UEP.NAME,
547         ATTRS : {
548             url : {
549                 validator : Y.Lang.isString
550             },
551             ajaxurl : {
552                 validator : Y.Lang.isString
553             },
554             base : {
555                 setter : function(node) {
556                     var n = Y.one(node);
557                     if (!n) {
558                         Y.fail(UEP.NAME+': invalid base node set');
559                     }
560                     return n;
561                 }
562             },
563             users : {
564                 validator : Y.Lang.isArray,
565                 value : null
566             },
567             courseid : {
568                 value : null
569             },
570             params : {
571                 validator : Y.Lang.isArray,
572                 value : []
573             },
574             instances : {
575                 validator : Y.Lang.isArray,
576                 setter : function(instances) {
577                     var i,ia = [], count=0;
578                     for (i in instances) {
579                         ia.push(instances[i]);
580                         count++;
581                     }
582                     this.set(UEP.MULTIPLE, (count>1));
583                 }
584             },
585             multiple : {
586                 validator : Y.Lang.isBool,
587                 value : false
588             },
589             page : {
590                 validator : Y.Lang.isNumber,
591                 value : 0
592             },
593             userCount : {
594                 value : 0,
595                 validator : Y.Lang.isNumber
596             },
597             requiresRefresh : {
598                 value : false,
599                 validator : Y.Lang.isBool
600             },
601             search : {
602                 setter : function(node) {
603                     var n = Y.one(node);
604                     if (!n) {
605                         Y.fail(UEP.NAME+': invalid search node set');
606                     }
607                     return n;
608                 }
609             },
610             lastPreSearchValue : {
611                 value : '',
612                 validator : Y.Lang.isString
613             },
614             strings  : {
615                 value : {},
616                 validator : Y.Lang.isObject
617             },
618             defaultRole : {
619                 value : 0
620             },
621             defaultStartDate : {
622                 value : 4,
623                 validator : Y.Lang.isNumber
624             },
625             defaultDuration : {
626                 value : ''
627             },
628             assignableRoles : {
629                 value : []
630             },
631             optionsStartDate : {
632                 value : []
633             },
634             disableGradeHistory : {
635                 value : 0
636             },
637             recoverGradesDefault : {
638                 value : ''
639             },
640             enrolCount : {
641                 value : 0,
642                 validator : Y.Lang.isNumber
643             },
644             perPage : {
645                 value: 25,
646                 Validator: Y.Lang.isNumber
647             },
648             cohortCount : {
649                 value : 0,
650                 validator : Y.Lang.isNumber
651             },
652             cohortsAvailable : {
653                 value : null
654             }
655         }
656     });
657     Y.augment(USERENROLLER, Y.EventTarget);
659     M.enrol_manual = M.enrol_manual || {};
660     M.enrol_manual.quickenrolment = {
661         init : function(cfg) {
662             new USERENROLLER(cfg);
663         }
664     }
666 }, '@VERSION@', {requires:['base','node', 'overlay', 'io-base', 'test', 'json-parse', 'event-delegate', 'dd-plugin', 'event-key', 'moodle-core-notification']});