"MDL-19118, improve comments management"
authorDongsheng Cai <unoter@gmail.com>
Wed, 31 Mar 2010 09:53:05 +0000 (09:53 +0000)
committerDongsheng Cai <unoter@gmail.com>
Wed, 31 Mar 2010 09:53:05 +0000 (09:53 +0000)
comment/admin.js [deleted file]
comment/comment.js
comment/index.php
comment/locallib.php
lang/en_utf8/admin.php
lang/en_utf8/moodle.php
lib/outputrequirementslib.php
theme/base/style/core.css

diff --git a/comment/admin.js b/comment/admin.js
deleted file mode 100644 (file)
index 4ff9c91..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-YAHOO.util.Event.onDOMReady(init);
-function init() {
-    var select_all = document.getElementById('comment_select_all');
-    select_all.onclick = function() {
-        var comments = document.getElementsByName('comments');
-        var checked = false;
-        for (var i in comments) {
-            if (comments[i].checked) {
-                checked=true;
-            }
-        }
-        for (var i in comments) {
-            comments[i].checked = !checked;
-        }
-        this.checked = !checked;
-    }
-    var comments_delete = document.getElementById('comments_delete');
-    comments_delete.onclick = function() {
-        delete_comments();
-    }
-}
-function delete_comments() {
-    var url = M.cfg.wwwroot + '/comment/index.php';
-    var cb = {
-        success:function(o) {
-            if (o.responseText == 'yes') {
-                location.reload();
-            }
-        }
-    }
-    var comments = document.getElementsByName('comments');
-    var list = '';
-    for (var i in comments) {
-        if (comments[i].checked) {
-            list += (comments[i].value + '-');
-        }
-    }
-    var data = {
-        'commentids': list,
-        'sesskey': M.cfg.sesskey
-    }
-    var trans = YAHOO.util.Connect.asyncRequest('POST',
-        url+'?action=delete', cb, build_querystring(data));
-}
index b2b1802..466f9f4 100644 (file)
@@ -337,5 +337,73 @@ M.core_comment = {
         });
 
         new CommentHelper(options);
+    },
+    init_admin: function(Y) {
+        var select_all = Y.one('#comment_select_all');
+        select_all.on('click', function() {
+            var comments = document.getElementsByName('comments');
+            var checked = false;
+            for (var i in comments) {
+                if (comments[i].checked) {
+                    checked=true;
+                }
+            }
+            for (var i in comments) {
+                comments[i].checked = !checked;
+            }
+            this.set('checked', !checked);
+        });
+
+        var comments_delete = Y.one('#comments_delete');
+        if (comments_delete) {
+            comments_delete.on('click', function(e) {
+                e.preventDefault();
+                var list = '';
+                var comments = document.getElementsByName('comments');
+                for (var i in comments) {
+                    if (typeof comments[i] == 'object' && comments[i].checked) {
+                        list += (comments[i].value + '-');
+                    }
+                }
+                if (!list) {
+                    return;
+                }
+                var args = {};
+                args.message = M.str.admin.confirmdeletecomments;
+                args.callback = function() {
+                    var url = M.cfg.wwwroot + '/comment/index.php';
+
+                    var data = {
+                        'commentids': list,
+                        'sesskey': M.cfg.sesskey,
+                        'action': 'delete'
+                    }
+                    var cfg = {
+                        method: 'POST',
+                        on: {
+                            complete: function(id,o,p) {
+                                if (!o) {
+                                    alert('IO FATAL');
+                                    return;
+                                }
+                                if (o.responseText == 'yes') {
+                                    location.reload();
+                                }
+                            }
+                        },
+                        arguments: {
+                            scope: this
+                        },
+                        headers: {
+                            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
+                            'User-Agent': 'MoodleComment/3.0'
+                        },
+                        data: build_querystring(data)
+                    };
+                    Y.io(url, cfg);
+                }
+                M.util.show_confirm_dialog(e, args);
+            });
+        }
     }
 };
index b7af2c3..db6a55a 100644 (file)
@@ -28,18 +28,13 @@ admin_externalpage_setup('comments');
 $context = get_context_instance(CONTEXT_SYSTEM);
 require_capability('moodle/comment:delete', $context);
 
-$PAGE->requires->yui2_lib('yahoo');
-$PAGE->requires->yui2_lib('dom');
-$PAGE->requires->yui2_lib('event');
-$PAGE->requires->yui2_lib('animation');
-$PAGE->requires->yui2_lib('json');
-$PAGE->requires->yui2_lib('connection');
-$PAGE->requires->js('/comment/admin.js');
+$PAGE->requires->js_init_call('M.core_comment.init_admin', null, true);
 
 $action     = optional_param('action', '', PARAM_ALPHA);
 $commentid  = optional_param('commentid', 0, PARAM_INT);
 $commentids = optional_param('commentids', '', PARAM_ALPHANUMEXT);
 $page       = optional_param('page', 0, PARAM_INT);
+$confirm    = optional_param('confirm', 0, PARAM_INT);
 
 $manager = new comment_manager();
 
@@ -51,10 +46,21 @@ if ($action and !confirm_sesskey()) {
 if ($action === 'delete') {
     // delete a single comment
     if (!empty($commentid)) {
-        if ($manager->delete_comment($commentid)) {
-            redirect($CFG->httpswwwroot.'/comment/', get_string('deleted'));
+        if (!$confirm) {
+            echo $OUTPUT->header();
+            $optionsyes = array('action'=>'delete', 'commentid'=>$commentid, 'confirm'=>1, 'sesskey'=>sesskey());
+            $optionsno  = array('sesskey'=>sesskey());
+            $buttoncontinue = new single_button(new moodle_url('/comment/index.php', $optionsyes), get_string('delete'));
+            $buttoncancel = new single_button(new moodle_url('/comment/index.php', $optionsno), get_string('cancel'));
+            echo $OUTPUT->confirm(get_string('confirmdeletecomments', 'admin'), $buttoncontinue, $buttoncancel);
+            echo $OUTPUT->footer();
+            die;
         } else {
-            $err = 'cannotdeletecomment';
+            if ($manager->delete_comment($commentid)) {
+                redirect($CFG->httpswwwroot.'/comment/');
+            } else {
+                $err = 'cannotdeletecomment';
+            }
         }
     }
     // delete a list of comments
@@ -69,13 +75,16 @@ if ($action === 'delete') {
 
 echo $OUTPUT->header();
 echo $OUTPUT->heading(get_string('comments'));
+echo $OUTPUT->box_start('generalbox commentsreport');
 if (!empty($err)) {
     print_error($err, 'error', $CFG->httpswwwroot.'/comment/');
 }
 if (empty($action)) {
+    echo '<form method="post">';
     $manager->print_comments($page);
-    echo '<div class="mdl-align">';
-    echo '<button id="comments_delete">'.get_string('delete').'</button>';
-    echo '</div>';
+    echo '<input type="submit" id="comments_delete" name="batchdelete" value="'.get_string('delete').'" />';
+    echo '</form>';
 }
+
+echo $OUTPUT->box_end();
 echo $OUTPUT->footer();
index 9e51418..ecbceaa 100644 (file)
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 class comment_manager {
+    private $perpage;
+    function __construct() {
+        global $CFG;
+        $this->perpage = $CFG->commentsperpage;
+    }
 
     /**
      * Return comments by pages
@@ -97,25 +102,25 @@ class comment_manager {
      */
     function print_comments($page=0) {
         global $CFG, $OUTPUT, $DB;
-        $this->perpage = 10;
         $count = $DB->count_records_sql('SELECT COUNT(*) FROM {comments} c');
         $comments = $this->get_comments($page);
         $table = new html_table();
-        $table->head = array ('<input type="checkbox" id="comment_select_all" />', 'author', 'content', 'action');
+        $table->head = array (html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')), get_string('author', 'search'), get_string('content'), get_string('action'));
         $table->align = array ('left', 'left', 'left', 'left');
         $table->width = "95%";
         $table->data = array();
+        $linkbase = $CFG->wwwroot.'/comment/index.php?action=delete&sesskey='.sesskey();
         foreach ($comments as $c) {
+            $link = $linkbase . '&commentid='. $c->id;
             $this->setup_plugin($c);
             if (!empty($this->plugintype)) {
-                $url = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'url', array($c));
+                $context_url = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'url', array($c));
             }
-            $checkbox = '<input type="checkbox" name="comments" value="'. $c->id .'" />';
-            $action = '';
-            $action .= "<a href='{$CFG->wwwroot}/comment/index.php?action=delete&amp;sesskey=".sesskey()."&amp;commentid={$c->id}'>".get_string('delete').'</a>';
-            $action .= "<br />";
-            if (!empty($url)) {
-                $action .= "<a target='_blank' href='{$url}'>".get_string('commentincontext').'</a>';
+            $checkbox = html_writer::checkbox('comments', $c->id, false);
+            $action = html_writer::link($link, get_string('delete'));
+            if (!empty($context_url)) {
+                $action .= html_writer::tag('br', null);
+                $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
             }
             $table->data[] = array($checkbox, $c->username, $c->content, $action);
         }
index afe5d10..5a7b7ea 100644 (file)
@@ -327,6 +327,7 @@ $string['configzip'] = 'Indicate the location of your zip program (Unix only, op
 $string['confirmation'] = 'Confirmation';
 $string['confirmed'] = 'Confirmed';
 $string['confirminstall'] = 'You are about to install language pack ($a), are you sure?';
+$string['confirmdeletecomments'] = 'You are about to delete comments, are you sure?';
 $string['cookiehttponly'] = 'Only http cookies';
 $string['cookiesecure'] = 'Secure cookies only';
 $string['country'] = 'Default country';
index 082eba7..f0db75d 100644 (file)
@@ -261,6 +261,7 @@ $string['confirm'] = 'Confirm';
 $string['confirmcheckfull'] = 'Are you absolutely sure you want to confirm $a ?';
 $string['confirmed'] = 'Your registration has been confirmed';
 $string['confirmednot'] = 'Your registration has not yet been confirmed!';
+$string['content'] = 'Content';
 $string['continue'] = 'Continue';
 $string['continuetocourse'] = 'Click here to enter your course';
 $string['convertingwikitomarkdown'] = 'Converting Wiki to Markdown';
index fba2e5b..badd7c3 100644 (file)
@@ -364,7 +364,9 @@ class page_requirements_manager {
                 case 'core_comment':
                     $module = array('name'     => 'core_comment',
                                     'fullpath' => '/comment/comment.js',
-                                    'requires' => array('base', 'io', 'node', 'json', 'yui2-animation'));
+                                    'requires' => array('base', 'io', 'node', 'json', 'yui2-animation'),
+                                    'strings' => array(array('confirmdeletecomments', 'admin'))
+                                );
                     break;
                 case 'core_role':
                     $module = array('name'     => 'core_role',
index dcb53e2..fff13c7 100644 (file)
@@ -322,6 +322,8 @@ table.mod_index {width:100%;}
 .comment-content p {padding:0;margin:0 18px 0 0;}
 .comment-delete {float:right;text-align:right;}
 .comment-container {float:left;width: 305px;margin: 4px;}
+.comment-report-selectall{display:none}
+.jsenabled .comment-report-selectall{display:inline}
 
 /**
  * Completion progress report