MDL-20636 Implement upgrade of calculated* question type attempts.
[moodle.git] / comment / comment.js
blob87f0e9e7ce9364b9f6212b0f1d533b6e0a28aa3a
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Comment Helper
18  * @author Dongsheng Cai <dongsheng@moodle.com>
19  */
20 M.core_comment = {
21     /**
22      * Initialize commenting system
23      */
24     init: function(Y, options) {
25         var CommentHelper = function(args) {
26             CommentHelper.superclass.constructor.apply(this, arguments);
27         };
28         CommentHelper.NAME = "COMMENT";
29         CommentHelper.ATTRS = {
30             options: {},
31             lang: {}
32         };
33         Y.extend(CommentHelper, Y.Base, {
34             api: M.cfg.wwwroot+'/comment/comment_ajax.php',
35             initializer: function(args) {
36                 var scope = this;
37                 this.client_id = args.client_id;
38                 this.itemid = args.itemid;
39                 this.commentarea = args.commentarea;
40                 this.component = args.component;
41                 this.courseid = args.courseid;
42                 this.contextid = args.contextid;
43                 this.autostart = (args.autostart);
44                 // expand comments?
45                 if (this.autostart) {
46                     this.view(args.page);
47                 }
48                 // load comments
49                 var handle = Y.one('#comment-link-'+this.client_id);
50                 // hide toggle link
51                 if (handle) {
52                     if (args.notoggle) {
53                         handle.setStyle('display', 'none');
54                     }
55                     handle.on('click', function(e) {
56                         e.preventDefault();
57                         this.view(0);
58                         return false;
59                     }, this);
60                 }
61                 CommentHelper.confirmoverlay = new Y.Overlay({
62 bodyContent: '<div class="comment-delete-confirm"><a href="#" id="confirmdelete-'+this.client_id+'">'+M.str.moodle.yes+'</a> <a href="#" id="canceldelete-'+this.client_id+'">'+M.str.moodle.no+'</a></div>',
63                                         visible: false
64                                         });
65                 CommentHelper.confirmoverlay.render(document.body);
66             },
67             post: function() {
68                 var ta = Y.one('#dlg-content-'+this.client_id);
69                 var scope = this;
70                 var value = ta.get('value');
71                 if (value && value != M.str.moodle.addcomment) {
72                     var params = {'content': value};
73                     this.request({
74                         action: 'add',
75                         scope: scope,
76                         params: params,
77                         callback: function(id, obj, args) {
78                             var scope = args.scope;
79                             var cid = scope.client_id;
80                             var ta = Y.one('#dlg-content-'+cid);
81                             ta.set('value', '');
82                             var container = Y.one('#comment-list-'+cid);
83                             var result = scope.render([obj], true);
84                             var newcomment = Y.Node.create(result.html);
85                             container.appendChild(newcomment);
86                             var ids = result.ids;
87                             var linktext = Y.one('#comment-link-text-'+cid);
88                             if (linktext) {
89                                 linktext.set('innerHTML', M.str.moodle.comments + ' ('+obj.count+')');
90                             }
91                             for(var i in ids) {
92                                 var attributes = {
93                                     color: { to: '#06e' },
94                                     backgroundColor: { to: '#FFE390' }
95                                 };
96                                 var anim = new YAHOO.util.ColorAnim(ids[i], attributes);
97                                 anim.animate();
98                             }
99                             scope.register_pagination();
100                             scope.register_delete_buttons();
101                         }
102                     }, true);
103                 } else {
104                     var attributes = {
105                         backgroundColor: { from: '#FFE390', to:'#FFFFFF' }
106                     };
107                     var anim = new YAHOO.util.ColorAnim('dlg-content-'+cid, attributes);
108                     anim.animate();
109                 }
110             },
111             request: function(args, noloading) {
112                 var params = {};
113                 var scope = this;
114                 if (args['scope']) {
115                     scope = args['scope'];
116                 }
117                 //params['page'] = args.page?args.page:'';
118                 // the form element only accept certain file types
119                 params['sesskey']   = M.cfg.sesskey;
120                 params['action']    = args.action?args.action:'';
121                 params['client_id'] = this.client_id;
122                 params['itemid']    = this.itemid;
123                 params['area']      = this.commentarea;
124                 params['courseid']  = this.courseid;
125                 params['contextid'] = this.contextid;
126                 params['component'] = this.component;
127                 if (args['params']) {
128                     for (i in args['params']) {
129                         params[i] = args['params'][i];
130                     }
131                 }
132                 var cfg = {
133                     method: 'POST',
134                     on: {
135                         complete: function(id,o,p) {
136                             if (!o) {
137                                 alert('IO FATAL');
138                                 return false;
139                             }
140                             var data = Y.JSON.parse(o.responseText);
141                             if (data.error) {
142                                 if (data.error == 'require_login') {
143                                     args.callback(id,data,p);
144                                     return true;
145                                 }
146                                 alert(data.error);
147                                 return false;
148                             } else {
149                                 args.callback(id,data,p);
150                                 return true;
151                             }
152                         }
153                     },
154                     arguments: {
155                         scope: scope
156                     },
157                     headers: {
158                         'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
159                         'User-Agent': 'MoodleComment/3.0'
160                     },
161                     data: build_querystring(params)
162                 };
163                 if (args.form) {
164                     cfg.form = args.form;
165                 }
166                 Y.io(this.api, cfg);
167                 if (!noloading) {
168                     this.wait();
169                 }
170             },
171             render: function(list, newcmt) {
172                 var ret = {};
173                 ret.ids = [];
174                 var template = Y.one('#cmt-tmpl');
175                 var html = '';
176                 for(var i in list) {
177                     var htmlid = 'comment-'+list[i].id+'-'+this.client_id;
178                     var val = template.get('innerHTML');
179                     if (list[i].profileurl) {
180                         val = val.replace('___name___', '<a href="'+list[i].profileurl+'">'+list[i].fullname+'</a>');
181                     } else {
182                         val = val.replace('___name___', list[i].fullname);
183                     }
184                     if (list[i]['delete']||newcmt) {
185                         list[i].content = '<div class="comment-delete"><a href="#" id ="comment-delete-'+this.client_id+'-'+list[i].id+'" title="'+M.str.moodle.deletecomment+'"><img src="'+M.util.image_url('t/delete', 'core')+'" /></a></div>' + list[i].content;
186                     }
187                     val = val.replace('___time___', list[i].time);
188                     val = val.replace('___picture___', list[i].avatar);
189                     val = val.replace('___content___', list[i].content);
190                     val = '<li id="'+htmlid+'">'+val+'</li>';
191                     ret.ids.push(htmlid);
192                     html = (val+html);
193                 }
194                 ret.html = html;
195                 return ret;
196             },
197             load: function(page) {
198                 var scope = this;
199                 var container = Y.one('#comment-ctrl-'+this.client_id);
200                 var params = {
201                     'action': 'get',
202                     'page': page
203                 };
204                 this.request({
205                     scope: scope,
206                     params: params,
207                     callback: function(id, ret, args) {
208                         var linktext = Y.one('#comment-link-text-'+scope.client_id);
209                         if (ret.count && linktext) {
210                             linktext.set('innerHTML', M.str.moodle.comments + ' ('+ret.count+')');
211                         }
212                         var container = Y.one('#comment-list-'+scope.client_id);
213                         var pagination = Y.one('#comment-pagination-'+scope.client_id);
214                         if (ret.pagination) {
215                             pagination.set('innerHTML', ret.pagination);
216                         } else {
217                             //empty paging bar
218                             pagination.set('innerHTML', '');
219                         }
220                         if (ret.error == 'require_login') {
221                             var result = {};
222                             result.html = M.str.moodle.commentsrequirelogin;
223                         } else {
224                             var result = scope.render(ret.list);
225                         }
226                         container.set('innerHTML', result.html);
227                         var img = Y.one('#comment-img-'+scope.client_id);
228                         if (img) {
229                             img.set('src', M.util.image_url('t/expanded', 'core'));
230                         }
231                         args.scope.register_pagination();
232                         args.scope.register_delete_buttons();
233                     }
234                 });
235             },
237             dodelete: function(id) { // note: delete is a reserved word in javascript, chrome and safary do not like it at all here!
238                 var scope = this;
239                 var params = {'commentid': id};
240                 scope.cancel_delete();
241                 function remove_dom(type, anim, cmt) {
242                     cmt.remove();
243                 }
244                 this.request({
245                     action: 'delete',
246                     scope: scope,
247                     params: params,
248                     callback: function(id, resp, args) {
249                         var htmlid= 'comment-'+resp.commentid+'-'+resp.client_id;
250                         var attributes = {
251                             width:{to:0},
252                             height:{to:0}
253                         };
254                         var cmt = Y.one('#'+htmlid);
255                         cmt.setStyle('overflow', 'hidden');
256                         var anim = new YAHOO.util.Anim(htmlid, attributes, 1, YAHOO.util.Easing.easeOut);
257                         anim.onComplete.subscribe(remove_dom, cmt, this);
258                         anim.animate();
259                     }
260                 }, true);
261             },
262             register_actions: function() {
263                 // add new comment
264                 var action_btn = Y.one('#comment-action-post-'+this.client_id);
265                 if (action_btn) {
266                     action_btn.on('click', function(e) {
267                         e.preventDefault();
268                         this.post();
269                         return false;
270                     }, this);
271                 }
272                 // cancel comment box
273                 var cancel = Y.one('#comment-action-cancel-'+this.client_id);
274                 if (cancel) {
275                     cancel.on('click', function(e) {
276                         e.preventDefault();
277                         this.view(0);
278                         return false;
279                     }, this);
280                 }
281             },
282             register_delete_buttons: function() {
283                 var scope = this;
284                 // page buttons
285                 Y.all('div.comment-delete a').each(
286                     function(node, id) {
287                         var theid = node.get('id');
288                         var parseid = new RegExp("comment-delete-"+scope.client_id+"-(\\d+)", "i");
289                         var commentid = theid.match(parseid);
290                         if (!commentid) {
291                             return;
292                         }
293                         if (commentid[1]) {
294                             Y.Event.purgeElement('#'+theid, false, 'click');
295                         }
296                         node.on('click', function(e, node) {
297                             e.preventDefault();
298                             var width = CommentHelper.confirmoverlay.bodyNode.getStyle('width');
299                             var re = new RegExp("(\\d+).*", "i");
300                             var result = width.match(re);
301                             if (result[1]) {
302                                 width = Number(result[1]);
303                             } else {
304                                 width = 0;
305                             }
306                             //CommentHelper.confirmoverlay.set('xy', [e.pageX-(width/2), e.pageY]);
307                             CommentHelper.confirmoverlay.set('xy', [e.pageX-width-5, e.pageY]);
308                             CommentHelper.confirmoverlay.set('visible', true);
309                             Y.one('#canceldelete-'+scope.client_id).on('click', function(e) {
310                                                                 e.preventDefault();
311                                 scope.cancel_delete();
312                                 });
313                             Y.Event.purgeElement('#confirmdelete-'+scope.client_id, false, 'click');
314                             Y.one('#confirmdelete-'+scope.client_id).on('click', function(e) {
315                                                                         e.preventDefault();
316                                     if (commentid[1]) {
317                                         scope.dodelete(commentid[1]);
318                                     }
319                                 });
320                         }, scope, node);
321                     }
322                 );
323             },
324             cancel_delete: function() {
325                 CommentHelper.confirmoverlay.set('visible', false);
326             },
327             register_pagination: function() {
328                 var scope = this;
329                 // page buttons
330                 Y.all('#comment-pagination-'+this.client_id+' a').each(
331                     function(node, id) {
332                         node.on('click', function(e, node) {
333                             e.preventDefault();
334                             var id = node.get('id');
335                             var re = new RegExp("comment-page-"+this.client_id+"-(\\d+)", "i");
336                             var result = id.match(re);
337                             this.load(result[1]);
338                         }, scope, node);
339                     }
340                 );
341             },
342             view: function(page) {
343                 var container = Y.one('#comment-ctrl-'+this.client_id);
344                 var ta = Y.one('#dlg-content-'+this.client_id);
345                 var img = Y.one('#comment-img-'+this.client_id);
346                 var d = container.getStyle('display');
347                 if (d=='none'||d=='') {
348                     // show
349                     if (!this.autostart) {
350                         this.load(page);
351                     } else {
352                         this.register_delete_buttons();
353                         this.register_pagination();
354                     }
355                     container.setStyle('display', 'block');
356                     if (img) {
357                         img.set('src', M.util.image_url('t/expanded', 'core'));
358                     }
359                 } else {
360                     // hide
361                     container.setStyle('display', 'none');
362                     img.set('src', M.util.image_url('t/collapsed', 'core'));
363                     if (ta) {
364                         ta.set('value','');
365                     }
366                 }
367                 if (ta) {
368                     //toggle_textarea.apply(ta, [false]);
369                     //// reset textarea size
370                     ta.on('click', function() {
371                         this.toggle_textarea(true);
372                     }, this);
373                     //ta.onkeypress = function() {
374                         //if (this.scrollHeight > this.clientHeight && !window.opera)
375                             //this.rows += 1;
376                     //}
377                     ta.on('blur', function() {
378                         this.toggle_textarea(false);
379                     }, this);
380                 }
381                 this.register_actions();
382                 return false;
383             },
384             toggle_textarea: function(focus) {
385                 var t = Y.one('#dlg-content-'+this.client_id);
386                 if (focus) {
387                     if (t.get('value') == M.str.moodle.addcomment) {
388                         t.set('value', '');
389                         t.setStyle('color', 'black');
390                     }
391                 }else{
392                     if (t.get('value') == '') {
393                         t.set('value', M.str.moodle.addcomment);
394                         t.setStyle('color','grey');
395                         t.set('rows', 2);
396                     }
397                 }
398             },
399             wait: function() {
400                 var container = Y.one('#comment-list-'+this.client_id);
401                 container.set('innerHTML', '<div class="mdl-align"><img src="'+M.util.image_url('i/loading_small', 'core')+'" /></div>');
402             }
403         });
405         new CommentHelper(options);
406     },
407     init_admin: function(Y) {
408         var select_all = Y.one('#comment_select_all');
409         select_all.on('click', function(e) {
410             var comments = document.getElementsByName('comments');
411             var checked = false;
412             for (var i in comments) {
413                 if (comments[i].checked) {
414                     checked=true;
415                 }
416             }
417             for (i in comments) {
418                 comments[i].checked = !checked;
419             }
420             this.set('checked', !checked);
421         });
423         var comments_delete = Y.one('#comments_delete');
424         if (comments_delete) {
425             comments_delete.on('click', function(e) {
426                 e.preventDefault();
427                 var list = '';
428                 var comments = document.getElementsByName('comments');
429                 for (var i in comments) {
430                     if (typeof comments[i] == 'object' && comments[i].checked) {
431                         list += (comments[i].value + '-');
432                     }
433                 }
434                 if (!list) {
435                     return;
436                 }
437                 var args = {};
438                 args.message = M.str.admin.confirmdeletecomments;
439                 args.callback = function() {
440                     var url = M.cfg.wwwroot + '/comment/index.php';
442                     var data = {
443                         'commentids': list,
444                         'sesskey': M.cfg.sesskey,
445                         'action': 'delete'
446                     };
447                     var cfg = {
448                         method: 'POST',
449                         on: {
450                             complete: function(id,o,p) {
451                                 if (!o) {
452                                     alert('IO FATAL');
453                                     return;
454                                 }
455                                 if (o.responseText == 'yes') {
456                                     location.reload();
457                                 }
458                             }
459                         },
460                         arguments: {
461                             scope: this
462                         },
463                         headers: {
464                             'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
465                             'User-Agent': 'MoodleComment/3.0'
466                         },
467                         data: build_querystring(data)
468                     };
469                     Y.io(url, cfg);
470                 };
471                 M.util.show_confirm_dialog(e, args);
472             });
473         }
474     }