Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / rating / module.js
blob00b4bb4e3a3a810b4259008989d27632315c0391
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             if(theinputs.item(i).get("name") != "returnurl") { // Dont include return url for ajax requests.
25                 thedata[theinputs.item(i).get("name")] = theinputs.item(i).get("value");
26             }
27         }
29         var scope = this;
30         var cfg = {
31             method: 'POST',
32             on: {
33                 complete : function(tid, outcome, args) {
34                     try {
35                         if (!outcome) {
36                             alert('IO FATAL');
37                             return false;
38                         }
40                         var data = scope.Y.JSON.parse(outcome.responseText);
41                         if (data.success){
42                             //if the user has access to the aggregate then update it
43                             if (data.itemid) { //do not test data.aggregate or data.count otherwise it doesn't refresh value=0 or no value
44                                 var itemid = data.itemid;
46                                 var node = scope.Y.one('#ratingaggregate' + itemid);
47                                 node.set('innerHTML',data.aggregate);
49                                 // Empty the count value if no ratings.
50                                 var node = scope.Y.one('#ratingcount' + itemid);
51                                 if (data.count > 0) {
52                                     node.set('innerHTML', "(" + data.count + ")");
53                                 } else {
54                                     node.set('innerHTML', "");
55                                 }
56                             }
57                             return true;
58                         }
59                         else if (data.error) {
60                             alert(data.error);
61                         }
62                     } catch(e) {
63                         alert(e.message + " " + outcome.responseText);
64                     }
65                     return false;
66                 }
67             },
68             arguments: {
69                 scope: scope
70             },
71             headers: {
72             },
73             data: build_querystring(thedata)
74         };
75         this.Y.io(this.api, cfg);
77     }