Merge branch 'MDL-38825' of git://github.com/stronk7/moodle
[moodle.git] / rating / module.js
blobf577f9e219238b4a2a1053a5aabd12f45eb55eb5
1 M.core_rating={
3     Y : null,
4     api: M.cfg.wwwroot+'/rating/rate_ajax.php',
6     init : function(Y){
7         this.Y = Y;
8         Y.all('select.postratingmenu').each(this.attach_rating_events, this);
10         //hide the submit buttons
11         this.Y.all('input.postratingmenusubmit').setStyle('display', 'none');
12     },
14     attach_rating_events : function(selectnode) {
15         selectnode.on('change', this.submit_rating, this, selectnode);
16     },
18     submit_rating : function(e, selectnode){
19         var theinputs = selectnode.ancestor('form').all('.ratinginput');
20         var thedata = [];
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         }
30         var scope = this;
31         var cfg = {
32             method: 'POST',
33             on: {
34                 complete : function(tid, outcome, args) {
35                     try {
36                         if (!outcome) {
37                             alert('IO FATAL');
38                             return false;
39                         }
41                         var data = scope.Y.JSON.parse(outcome.responseText);
42                         if (data.success){
43                             //if the user has access to the aggregate then update it
44                             if (data.itemid) { //do not test data.aggregate or data.count otherwise it doesn't refresh value=0 or no value
45                                 var itemid = data.itemid;
47                                 var node = scope.Y.one('#ratingaggregate'+itemid);
48                                 node.set('innerHTML',data.aggregate);
50                                 //empty the count value if no ratings
51                                 var node = scope.Y.one('#ratingcount'+itemid);
52                                 if (data.count > 0) {
53                                     node.set('innerHTML',"("+data.count+")");
54                                 } else {
55                                     node.set('innerHTML',"");
56                                 }
57                             }
58                             return true;
59                         }
60                         else if (data.error){
61                             alert(data.error);
62                         }
63                     } catch(e) {
64                         alert(e.message+" "+outcome.responseText);
65                     }
66                     return false;
67                 }
68             },
69             arguments: {
70                 scope: scope
71             },
72             headers: {
73             },
74             data: build_querystring(thedata)
75         };
76         this.Y.io(this.api, cfg);
78     }