From 34e20eb42245e2a703fd9fedfc8eb69215f85779 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Wed, 31 Mar 2010 09:53:05 +0000 Subject: [PATCH] "MDL-19118, improve comments management" --- comment/admin.js | 44 ----------------------- comment/comment.js | 68 +++++++++++++++++++++++++++++++++++ comment/index.php | 35 +++++++++++------- comment/locallib.php | 23 +++++++----- lang/en_utf8/admin.php | 1 + lang/en_utf8/moodle.php | 1 + lib/outputrequirementslib.php | 4 ++- theme/base/style/core.css | 2 ++ 8 files changed, 111 insertions(+), 67 deletions(-) delete mode 100644 comment/admin.js diff --git a/comment/admin.js b/comment/admin.js deleted file mode 100644 index 4ff9c910287..00000000000 --- a/comment/admin.js +++ /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)); -} diff --git a/comment/comment.js b/comment/comment.js index b2b1802044a..466f9f4319e 100644 --- a/comment/comment.js +++ b/comment/comment.js @@ -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); + }); + } } }; diff --git a/comment/index.php b/comment/index.php index b7af2c3607f..db6a55aa2ae 100644 --- a/comment/index.php +++ b/comment/index.php @@ -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 '
'; $manager->print_comments($page); - echo '
'; - echo ''; - echo '
'; + echo ''; + echo '
'; } + +echo $OUTPUT->box_end(); echo $OUTPUT->footer(); diff --git a/comment/locallib.php b/comment/locallib.php index 9e51418d1d1..ecbceaa11dd 100644 --- a/comment/locallib.php +++ b/comment/locallib.php @@ -23,6 +23,11 @@ * @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 ('', '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 = ''; - $action = ''; - $action .= "".get_string('delete').''; - $action .= "
"; - if (!empty($url)) { - $action .= "".get_string('commentincontext').''; + $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); } diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index afe5d105a4f..5a7b7ea3760 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -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'; diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index 082eba7bc93..f0db75dee1e 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -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'; diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index fba2e5b3ee8..badd7c3e643 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -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', diff --git a/theme/base/style/core.css b/theme/base/style/core.css index dcb53e287f7..fff13c7735a 100644 --- a/theme/base/style/core.css +++ b/theme/base/style/core.css @@ -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