MDL-40308: Fix javascript for lti mod_form
[moodle.git] / rating / module.js
CommitLineData
3dcdf440 1M.core_rating={
a09aeee4
AD
2
3 Y : null,
5c0452c6 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();
23 for ( var i=0; i<inputssize; i++ )
24 {
25 if(theinputs.item(i).get("name")!="returnurl") {//dont include return url for ajax requests
26 thedata[theinputs.item(i).get("name")] = theinputs.item(i).get("value");
27 }
28 }
8cbef19e 29
5c0452c6
AD
30 var scope = this;
31 var cfg = {
32 method: 'POST',
33 on: {
a09aeee4
AD
34 complete : function(tid, outcome, args) {
35 try {
4ca11043
AD
36 if (!outcome) {
37 alert('IO FATAL');
38 return false;
39 }
63e87951 40
5c0452c6 41 var data = scope.Y.JSON.parse(outcome.responseText);
4ca11043
AD
42 if (data.success){
43 //if the user has access to the aggregate then update it
279fcfcf 44 if (data.itemid) { //do not test data.aggregate or data.count otherwise it doesn't refresh value=0 or no value
4ca11043 45 var itemid = data.itemid;
8cbef19e 46
5c0452c6 47 var node = scope.Y.one('#ratingaggregate'+itemid);
4ca11043 48 node.set('innerHTML',data.aggregate);
63e87951 49
279fcfcf 50 //empty the count value if no ratings
5c0452c6 51 var node = scope.Y.one('#ratingcount'+itemid);
279fcfcf
AD
52 if (data.count > 0) {
53 node.set('innerHTML',"("+data.count+")");
54 } else {
55 node.set('innerHTML',"");
56 }
4ca11043
AD
57 }
58 return true;
59 }
60 else if (data.error){
61 alert(data.error);
d251b259 62 }
a09aeee4 63 } catch(e) {
63e87951 64 alert(e.message+" "+outcome.responseText);
a09aeee4 65 }
4ca11043 66 return false;
a09aeee4
AD
67 }
68 },
5c0452c6
AD
69 arguments: {
70 scope: scope
71 },
72 headers: {
73 },
74 data: build_querystring(thedata)
75 };
76 this.Y.io(this.api, cfg);
77
a09aeee4 78 }
8cbef19e 79};