MDL-29519 move plagiarism cron to after event queue
[moodle.git] / admin / roles / module.js
blob7326cf34252f1f976fd9a6f4e5ec18bb872e9cb1
1 /**
2  * This class filters the rows of a table like the one on the define or
3  * override roles pages. It adds a search box just above the table, and if
4  * content is typed into that box, it hides any rows in the table where the
5  * capability name does not contain that text.
6  */
8 /**
9  * Role namespace
10  */
11 M.core_role = {};
13 /**
14  * @param {YUI} Y
15  * @param {string} tableid
16  * @param {int} contextid
17  */
18 M.core_role.init_cap_table_filter = function(Y, tableid, contextid) {
20     var CapTableFilter = function(tableid) {
21         this.tableid = tableid;
22         this.context = contextid;
23         this.initializer();
24     };
25     CapTableFilter.prototype = {
26         tableid : null,     // ID of the cap table
27         context : null,    // Context ID associated with what ever we are looking at
28         delayhandle : -1,
29         searchdelay : 100,  // milliseconds
30         table : null,
31         div : null,
32         input : null,
33         label : null,
34         button : null,
35         /**
36          * Initialises the CapTableFilter object.
37          * This is called initializer so that a move to convert this to a proper
38          * YUI module will be easier.
39          */
40         initializer : function() {
41             // Get any existing filter value
42             var filtervalue = this.getFilterCookieValue();
44             // Find the form controls.
45             this.table = Y.one('#'+this.tableid);
47             // Create a div to hold the search UI.
48             this.div = Y.Node.create('<div class="capabilitysearchui"></div>').setStyles({
49                 width : this.table.get('offsetWidth'),
50                 marginLeft : 'auto',
51                 marginRight : 'auto'
52             });
53             // Create the capability search input.
54             this.input = Y.Node.create('<input type="text" id="'+this.table.get('id')+'capabilitysearch" value="'+filtervalue+'" />');
55             // Create a label for the search input.
56             this.label = Y.Node.create('<label for="'+this.input.get('id')+'">'+M.str.moodle.filter+' </label>');
57             // Create a clear button to clear the input.
58             this.button = Y.Node.create('<input type="button" value="'+M.str.moodle.clear+'" />').set('disabled', filtervalue=='');
60             // Tie it all together
61             this.div.append(this.label).append(this.input).append(this.button);
63             // Insert it into the div
64             this.table.ancestor().insert(this.div, this.table);
66             // Wire the events so it actually does something
67             this.input.on('keyup', this.change, this);
68             this.button.on('click', this.clear, this);
70             if (filtervalue != '') {
71                 this.filter();
72             }
73         },
74         /**
75          * Sets a cookie that describes the filter value.
76          * The cookie stores the context, and the time it was created and upon
77          * retrieval is checked to ensure that the cookie is for the correct
78          * context and is no more than an hour old.
79          */
80         setFilterCookieValue : function(value) {
81             var cookie = {
82                 fltcontext : this.context,
83                 flttime : new Date().getTime(),
84                 fltvalue : value
85             }
86             Y.Cookie.setSubs("captblflt", cookie);
87         },
88         /**
89          * Gets the existing filter value if there is one.
90          * The cookie stores the context, and the time it was created and upon
91          * retrieval is checked to ensure that the cookie is for the correct
92          * context and is no more than an hour old.
93          */
94         getFilterCookieValue : function() {
95             var cookie = Y.Cookie.getSubs('captblflt');
96             if (cookie!=null && cookie.fltcontext && cookie.fltcontext == this.context && parseInt(cookie.flttime) > new Date().getTime()-(60*60*1000)) {
97                 return cookie.fltvalue;
98             }
99             return '';
100         },
101         /**
102          * Clears the filter value.
103          */
104         clear : function() {
105             this.input.set('value', '');
106             if (this.delayhandle != -1) {
107                 clearTimeout(this.delayhandle);
108                 this.delayhandle = -1;
109             }
110             this.filter();
111         },
112         /**
113          * Event callback for when the filter value changes
114          */
115         change : function() {
116             var self = this;
117             var handle = setTimeout(function(){self.filter();}, this.searchdelay);
118             if (this.delayhandle != -1) {
119                 clearTimeout(this.delayhandle);
120             }
121             this.delayhandle = handle;
122         },
123         /**
124          * Marks a row as visible or hidden
125          */
126         setVisible : function(row, visible) {
127             if (visible) {
128                 row.removeClass('hiddenrow');
129             } else {
130                 row.addClass('hiddenrow');
131             }
132         },
133         /**
134          * Filters the capability table
135          */
136         filter : function() {
137             var filtertext = this.input.get('value').toLowerCase(),
138                 lastheading = null;
140             this.setFilterCookieValue(filtertext);
142             this.button.set('disabled', (filtertext == ''));
144             this.table.all('tr').each(function(row){
145                 if (row.hasClass('rolecapheading')) {
146                     this.setVisible(row, false);
147                     lastheading = row;
148                 }
149                 if (row.hasClass('rolecap')) {
150                     var capname = row.one('.cap-name').get('text') + '|' + row.one('.cap-desc a').get('text').toLowerCase();
151                     if (capname.indexOf(filtertext) >= 0) {
152                         this.setVisible(row, true);
153                         if (lastheading) {
154                             this.setVisible(lastheading, true);
155                             lastheading = null;
156                         }
157                     } else {
158                         this.setVisible(row, false);
159                     }
160                 }
161             }, this);
162         }
163     }
165     new CapTableFilter(tableid);
168 M.core_role.init_add_assign_page = function(Y) {
169     var add = Y.one('#add');
170     var addselect = M.core_user.get_user_selector('addselect');
171     add.set('disabled', addselect.is_selection_empty());
172     addselect.on('user_selector:selectionchanged', function(isempty) {
173         add.set('disabled', isempty);
174     });
176     var remove = Y.one('#remove');
177     var removeselect = M.core_user.get_user_selector('removeselect');
178     remove.set('disabled', removeselect.is_selection_empty());
179     removeselect.on('user_selector:selectionchanged', function(isempty) {
180         remove.set('disabled', isempty);
181     });