YUI.add("moodle-gradereport_history-userselector",function(R,e){var C="gradereport_history",U={AJAXURL:"ajaxurl",BASE:"base",CHECKBOX_NAME_PREFIX:"usp-u",COURSEID:"courseid",DIALOGUE_PREFIX:"moodle-dialogue",NAME:"gradereport_history_usp",PAGE:"page",PARAMS:"params",PERPAGE:"perPage",SEARCH:"search",SEARCHBTN:"searchbtn",SELECTEDUSERS:"selectedUsers",URL:"url",USERCOUNT:"userCount"},L={ACCESSHIDE:"accesshide",AJAXCONTENT:"usp-ajax-content",CHECKBOX:"usp-checkbox",CLOSE:"close",CLOSEBTN:"usp-finish",CONTENT:"usp-content",DETAILS:"details",EXTRAFIELDS:"extrafields",FIRSTADDED:"usp-first-added",FULLNAME:"fullname",HEADER:"usp-header",HIDDEN:"hidden",LIGHTBOX:"usp-loading-lightbox",LOADINGICON:"loading-icon",MORERESULTS:"usp-more-results",OPTIONS:"options",PICTURE:"usp-picture",RESULTSCOUNT:"usp-results-count",SEARCH:"usp-search",SEARCHBTN:"usp-search-btn",SEARCHFIELD:"usp-search-field",SEARCHRESULTS:"usp-search-results",SELECTED:"selected",USER:"usp-user",USERS:"usp-users",WRAP:"usp-wrap"},A={AJAXCONTENT:"."+L.AJAXCONTENT,FINISHBTN:"."+L.CLOSEBTN+" input",FIRSTADDED:"."+L.FIRSTADDED,FULLNAME:"."+L.FULLNAME+" label",LIGHTBOX:"."+L.LIGHTBOX,MORERESULTS:"."+L.MORERESULTS,OPTIONS:"."+L.OPTIONS,PICTURE:"."+L.USER+" .userpicture",RESULTSCOUNT:"."+L.RESULTSCOUNT,RESULTSUSERS:"."+L.SEARCHRESULTS+" ."+L.USERS,SEARCHBTN:"."+L.SEARCHBTN,SEARCHFIELD:"."+L.SEARCHFIELD,SELECTEDNAMES:".felement .selectednames",TRIGGER:".gradereport_history_plugin input.selectortrigger",USER:"."+L.USER,USERFULLNAMES:'input[name="userfullnames"]',USERIDS:'input[name="userids"]',USERSELECT:"."+L.CHECKBOX+" input[type=checkbox]"},s=function(){s.superclass.constructor.apply(this,arguments)};R.namespace("M.gradereport_history").UserSelector=R.extend(s,M.core.dialogue,{_firstDisplay:!0,_usersBufferList:null,_userTabFocus:null,_userTemplate:null,initializer:function(){var e,s,t,i=this.get("boundingBox");t=R.Handlebars.compile('<div class="{{CSS.WRAP}}"><div class="{{CSS.HEADER}}"><div class="{{CSS.SEARCH}}" role="search"><form><input type="text" class="{{CSS.SEARCHFIELD}}" aria-label="{{get_string "search" "moodle"}}" value="" /><input type="submit" class="{{CSS.SEARCHBTN}}"value="{{get_string "search" "moodle"}}"></form><div aria-live="polite" class="{{CSS.RESULTSCOUNT}}">{{get_string "loading" "admin"}}</div></div></div><div class="{{CSS.CONTENT}}"><form><div class="{{CSS.AJAXCONTENT}}" aria-live="polite"></div><div class="{{CSS.LIGHTBOX}} {{CSS.HIDDEN}}"><img class="{{CSS.LOADINGICON}}" alt="{{get_string "loading" "admin"}}"src="{{{loadingIcon}}}"></div><div class="{{CSS.CLOSEBTN}}"><input type="submit" value="{{get_string "finishselectingusers" COMPONENT}}"></div></form></div></div>'),e=R.Node.create(t({COMPONENT:C,CSS:L,loadingIcon:M.util.image_url("i/loading","moodle")})),this.getStdModNode(R.WidgetStdMod.HEADER).prepend(R.Node.create("<h1>"+this.get("title")+"</h1>")),this.setStdModContent(R.WidgetStdMod.BODY,e,R.WidgetStdMod.REPLACE),this.get("boundingBox").one(".moodle-dialogue-wrap").addClass("moodle-dialogue-content"),R.one(A.TRIGGER).on("click",this.show,this),i.one(A.FINISHBTN).on("click",this.finishSelectingUsers,this),i.delegate("key",this.userKeyboardNavigation,"down:38,40",A.AJAXCONTENT,this),R.delegate("click",this.selectUser,A.AJAXCONTENT,A.USERSELECT,this),R.delegate("click",this.selectUser,A.AJAXCONTENT,A.PICTURE,this),(s=this.get(U.PARAMS)).id=this.get(U.COURSEID),this.set(U.PARAMS,s),i.one(A.SEARCHBTN).on("click",this.search,this,!1)},show:function(e){var i;return this._usersBufferList=R.clone(this.get(U.SELECTEDUSERS)),this._firstDisplay?(this._firstDisplay=!1,this.search(e,!1)):((i=this.get("boundingBox")).all(A.USER).each(function(e){this.markUserNode(e,!1)},this),R.Object.each(this._usersBufferList,function(e,s){var t=i.one(A.USER+'[data-userid="'+s+'"]');t&&this.markUserNode(t,!0)},this),this.setUserTabFocus(i.one(A.USER))),R.namespace("M.gradereport_history.UserSelector").superclass.show.call(this)},search:function(e,s){var t;e&&e.preventDefault(),s?this.set(U.PAGE,this.get(U.PAGE)+1):(this.set(U.USERCOUNT,0),this.set(U.PAGE,0)),(t=this.get(U.PARAMS)).sesskey=M.cfg.sesskey,t.action="searchusers",t.search=this.get("boundingBox").one(A.SEARCHFIELD).get("value"),t.page=this.get(U.PAGE),t.perpage=this.get(U.PERPAGE),R.io(M.cfg.wwwroot+this.get(U.AJAXURL),{method:"POST",data:window.build_querystring(t),on:{start:this.preSearch,complete:this.processSearchResults,end:this.postSearch},context:this,arguments:{append:s}})},preSearch:function(e,s){var t=this.get("boundingBox");t.one(A.LIGHTBOX).removeClass(L.HIDDEN),s.append||t.one(A.RESULTSCOUNT).setHTML(M.util.get_string("loading","admin"))},postSearch:function(e,s){var t,i=this.get("boundingBox"),r=i.one(A.FIRSTADDED);i.one(A.LIGHTBOX).addClass(L.HIDDEN),s.append&&r?(this.setUserTabFocus(r),r.one(A.USERSELECT).focus()):(t=i.one(A.USER))&&this.setUserTabFocus(t)},processSearchResults:function(e,s,t){var i,r,a,n,o,u,l,S,d,E,c=!1,h=!1,T=this.get("boundingBox"),g=!0;try{(c=R.JSON.parse(s.responseText)).success&&!c.error||(h=!0)}catch(p){h=!0}if(h)return this.setContent(""),void T.one(A.RESULTSCOUNT).setHTML(M.util.get_string("errajaxsearch",C));for(o in i=t.append?T.one(A.RESULTSUSERS):R.Node.create('<div role="listbox" aria-activedescendant="" aria-multiselectable="true" class="'+L.USERS+'"></div>'),this._userTemplate||(this._userTemplate=R.Handlebars.compile('<div role="option" aria-selected="false" class="{{CSS.USER}} clearfix" data-userid="{{userId}}"><div class="{{CSS.CHECKBOX}}"><input name="{{USP.CHECKBOX_NAME_PREFIX}}{{userId}}" type="checkbox" tabindex="-1"id="{{checkboxId}}" aria-describedby="{{checkboxId}} {{extraFieldsId}}"/></div><div class="{{CSS.PICTURE}}">{{{picture}}}</div><div class="{{CSS.DETAILS}}"><div class="{{CSS.FULLNAME}}"><label for="{{checkboxId}}">{{fullname}}</label></div><div id="{{extraFieldsId}}" class="{{CSS.EXTRAFIELDS}}">{{extrafields}}</div></div></div>')),r=this._userTemplate,a=this.get(U.USERCOUNT),n="",c.response.users)a++,
E=c.response.users[o],n=!!R.Object.hasKey(this._usersBufferList,E.userid),u=R.Node.create(r({checkboxId:R.guid(),COMPONENT:C,count:a,CSS:L,extrafields:E.extrafields,extraFieldsId:R.guid(),fullname:E.fullname,picture:E.picture,userId:E.userid,USP:U})),this.markUserNode(u,n),t.append&&g&&(i.all(A.FIRSTADDED).removeClass(L.FIRSTADDED),u.addClass(L.FIRSTADDED),g=!1),i.append(u);this.set(U.USERCOUNT,a),d=parseInt(c.response.totalusers,10),t.append?d<=(this.get(U.PAGE)+1)*this.get(U.PERPAGE)&&T.one(A.MORERESULTS).remove():(0===d?(T.one(A.RESULTSCOUNT).setHTML(M.util.get_string("noresults","moodle")),l=""):(1===d?T.one(A.RESULTSCOUNT).setHTML(M.util.get_string("foundoneuser",C)):T.one(A.RESULTSCOUNT).setHTML(M.util.get_string("foundnusers",C,d)),l=R.Node.create('<div class="'+L.SEARCHRESULTS+'"></div>').append(i),c.response.totalusers>(this.get(U.PAGE)+1)*this.get(U.PERPAGE)&&((S=R.Node.create('<div class="'+L.MORERESULTS+'"><a href="#" role="button">'+M.util.get_string("loadmoreusers",C)+"</a></div>")).one("a").on("click",this.search,this,!0),S.one("a").on("key",this.search,"space",this,!0),l.append(S))),this.setContent(l))},finishSelectingUsers:function(e){e.preventDefault(),this.applySelection(),this.hide()},applySelection:function(){var e=R.Object.keys(this._usersBufferList);this.set(U.SELECTEDUSERS,R.clone(this._usersBufferList)).setNameDisplay(),R.one(A.USERIDS).set("value",e.join())},selectUser:function(e){var s=e.currentTarget.ancestor(A.USER),t=s.one(A.USERSELECT),i=s.one(A.FULLNAME).get("innerHTML"),r=t.get("checked"),a=s.getData("userid");e.currentTarget!==t&&(r=!r),r?this._usersBufferList[a]=i:(delete this._usersBufferList[a],delete this._usersBufferList[parseInt(a,10)]),this.markUserNode(s,r)},markUserNode:function(e,s){return s?e.addClass(L.SELECTED).set("aria-selected",!0).one(A.USERSELECT).set("checked",!0):e.removeClass(L.SELECTED).set("aria-selected",!1).one(A.USERSELECT).set("checked",!1),this},setContent:function(e){return this.get("boundingBox").one(A.AJAXCONTENT).setHTML(e),this},setNameDisplay:function(){var e=R.Object.values(this.get(U.SELECTEDUSERS));R.one(A.SELECTEDNAMES).set("innerHTML",e.join(", ")),R.one(A.USERFULLNAMES).set("value",e.join())},userKeyboardNavigation:function(e){var s,t=this.get("boundingBox").all(A.USER),i=1,r=e.target.ancestor(A.USER,!0);38===e.keyCode&&(i=-1),(s=this.findFocusableUser(t,r,i))&&(e.preventDefault(),s.one(A.USERSELECT).focus(),this.setUserTabFocus(s))},findFocusableUser:function(e,s,t){var i=e.indexOf(s);return e.size()<1?null:i<0?e.item(0):((i+=t)<0?i=e.size()-1:i>=e.size()&&(i=0),e.item(i))},setUserTabFocus:function(e){this._userTabFocus&&this._userTabFocus.setAttribute("tabindex","-1"),e&&(this._userTabFocus=e.one(A.USERSELECT),this._userTabFocus.setAttribute("tabindex","0"),this.get("boundingBox").one(A.RESULTSUSERS).setAttribute("aria-activedescendant",this._userTabFocus.generateID()))}},{NAME:U.NAME,CSS_PREFIX:U.CSS_PREFIX,ATTRS:{title:{validator:R.Lang.isString,valueFn:function(){return M.util.get_string("selectusers",C)}},url:{validator:R.Lang.isString,value:null},ajaxurl:{validator:R.Lang.isString,value:null},selectedUsers:{validator:R.Lang.isObject,value:null,getter:function(e){return null===e?{}:e}},courseid:{value:null},params:{validator:R.Lang.isArray,value:[]},page:{validator:R.Lang.isNumber,value:0},userCount:{value:0,validator:R.Lang.isNumber},perPage:{value:25,Validator:R.Lang.isNumber}}}),R.Base.modifyAttrs(R.namespace("M.gradereport_history.UserSelector"),{extraClasses:{value:["gradereport_history_usp"]},focusOnPreviousTargetAfterHide:{value:!0},width:{value:"500px"},visible:{value:!1},modal:{value:!0},draggable:{value:!0}}),R.namespace("M.gradereport_history.UserSelector").init=function(e){return new s(e)}},"@VERSION@",{requires:["escape","event-delegate","event-key","handlebars","io-base","json-parse","moodle-core-notification-dialogue"]});