MDL-65992 travis: Migrate to Xenial distro and default MySQL service
[moodle.git] / rating / module.js
CommitLineData
d28a6a5f 1M.core_rating = {
a09aeee4
AD
2
3 Y : null,
d28a6a5f 4 api: M.cfg.wwwroot + '/rating/rate_ajax.php',
a09aeee4
AD
5
6 init : function(Y){
7 this.Y = Y;
8 Y.all('select.postratingmenu').each(this.attach_rating_events, this);
9
10 //hide the submit buttons
11 this.Y.all('input.postratingmenusubmit').setStyle('display', 'none');
12 },
13
14 attach_rating_events : function(selectnode) {
15 selectnode.on('change', this.submit_rating, this, selectnode);
16 },
17
18 submit_rating : function(e, selectnode){
8cbef19e 19 var theinputs = selectnode.ancestor('form').all('.ratinginput');
a09aeee4
AD
20 var thedata = [];
21
22 var inputssize = theinputs.size();
d28a6a5f
AD
23 for (var i = 0; i < inputssize; i++) {
24 if(theinputs.item(i).get("name") != "returnurl") { // Dont include return url for ajax requests.
a09aeee4
AD
25 thedata[theinputs.item(i).get("name")] = theinputs.item(i).get("value");
26 }
27 }
8cbef19e 28
5c0452c6
AD
29 var scope = this;
30 var cfg = {
31 method: 'POST',
32 on: {
a09aeee4
AD
33 complete : function(tid, outcome, args) {
34 try {
4ca11043
AD
35 if (!outcome) {
36 alert('IO FATAL');
37 return false;
38 }
63e87951 39
5c0452c6 40 var data = scope.Y.JSON.parse(outcome.responseText);
4ca11043
AD
41 if (data.success){
42 //if the user has access to the aggregate then update it
279fcfcf 43 if (data.itemid) { //do not test data.aggregate or data.count otherwise it doesn't refresh value=0 or no value
4ca11043 44 var itemid = data.itemid;
8cbef19e 45
d28a6a5f 46 var node = scope.Y.one('#ratingaggregate' + itemid);
4ca11043 47 node.set('innerHTML',data.aggregate);
63e87951 48
d28a6a5f
AD
49 // Empty the count value if no ratings.
50 var node = scope.Y.one('#ratingcount' + itemid);
279fcfcf 51 if (data.count > 0) {
d28a6a5f 52 node.set('innerHTML', "(" + data.count + ")");
279fcfcf 53 } else {
d28a6a5f 54 node.set('innerHTML', "");
279fcfcf 55 }
4ca11043
AD
56 }
57 return true;
58 }
d28a6a5f 59 else if (data.error) {
4ca11043 60 alert(data.error);
d251b259 61 }
a09aeee4 62 } catch(e) {
d28a6a5f 63 alert(e.message + " " + outcome.responseText);
a09aeee4 64 }
4ca11043 65 return false;
a09aeee4
AD
66 }
67 },
5c0452c6
AD
68 arguments: {
69 scope: scope
70 },
71 headers: {
72 },
73 data: build_querystring(thedata)
74 };
75 this.Y.io(this.api, cfg);
76
a09aeee4 77 }
d28a6a5f 78};