From 15894c65e0536e357280baeb0a85ad02aed0620a Mon Sep 17 00:00:00 2001 From: dongsheng Date: Mon, 12 Oct 2009 05:46:34 +0000 Subject: [PATCH] "MDL-19118, use exception instead of error code to check comments api errors" --- comment/comment_ajax.php | 56 +++++++++++++++++++--------------------- comment/comment_post.php | 16 +++++------- lib/commentlib.php | 22 +++++++++------- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/comment/comment_ajax.php b/comment/comment_ajax.php index 87f36bbf72..b904befbd4 100644 --- a/comment/comment_ajax.php +++ b/comment/comment_ajax.php @@ -68,42 +68,38 @@ if (!empty($client_id)) { } switch ($action) { case 'add': - $cmt = $comment->add($content); - $cmt->count = $comment->count(); - if (!empty($cmt) && is_object($cmt)) { - $cmt->client_id = $client_id; - echo json_encode($cmt); - } else if ($cmt === COMMENT_ERROR_DB) { - $err->error = get_string('dbupdatefailed'); - echo json_encode($err); - } else if ($cmt === COMMENT_ERROR_MODULE_REJECT) { - $err->error = get_string('modulererejectcomment'); - echo json_encode($err); - } else if ($cmt === COMMENT_ERROR_INSUFFICIENT_CAPS) { - $err->error = get_string('nopermissiontocomment'); - echo json_encode($err); + try { + $cmt = $comment->add($content); + $cmt->count = $comment->count(); + if (!empty($cmt) && is_object($cmt)) { + $cmt->client_id = $client_id; + echo json_encode($cmt); + } + } catch (comment_exception $e) { + echo json_encode(array('error'=>$e->message)); } break; case 'delete': - $result = $comment->delete($commentid); - if ($result === true) { - echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid)); - } else if ($result == COMMENT_ERROR_INSUFFICIENT_CAPS) { - $err->error = get_string('nopermissiontoeditcomment'); - echo json_encode($err); - } else if ($result == COMMENT_ERROR_DB) { - $err->error = get_string('dbupdatefailed'); - echo json_encode($err); + try { + $result = $comment->delete($commentid); + if ($result === true) { + echo json_encode(array('client_id'=>$client_id, 'commentid'=>$commentid)); + } + } catch (comment_exception $e) { + echo json_encode(array('error'=>$e->message)); } break; case 'get': default: $ret = array(); - $comments = $comment->get_comments($page); - $ret['list'] = $comments; - $ret['count'] = $comment->count(); - $ret['pagination'] = $comment->get_pagination($page); - $ret['client_id'] = $client_id; - echo json_encode($ret); - exit; + try { + $comments = $comment->get_comments($page); + $ret['list'] = $comments; + $ret['count'] = $comment->count(); + $ret['pagination'] = $comment->get_pagination($page); + $ret['client_id'] = $client_id; + echo json_encode($ret); + } catch (comment_exception $e) { + echo json_encode(array('error'=>$e->message)); + } } diff --git a/comment/comment_post.php b/comment/comment_post.php index 9ebad5d70f..f4d7e0cf63 100644 --- a/comment/comment_post.php +++ b/comment/comment_post.php @@ -62,15 +62,13 @@ $comment = new comment($cmt); switch ($action) { case 'add': - $cmt = $comment->add($content); - if (!empty($cmt) && is_object($cmt)) { - redirect($returnurl, get_string('pageshouldredirect'), 0); - } else if ($cmt === COMMENT_ERROR_DB) { - print_error('dbupdatefailed'); - } else if ($cmt === COMMENT_ERROR_MODULE_REJECT) { - print_error('modulererejectcomment'); - } else if ($cmt === COMMENT_ERROR_INSUFFICIENT_CAPS) { - print_error('nopermissiontocomment'); + try { + $cmt = $comment->add($content); + if (!empty($cmt) && is_object($cmt)) { + redirect($returnurl, get_string('pageshouldredirect'), 0); + } + } catch(comment_exception $e) { + print_error($e->errorcode); } break; default: diff --git a/lib/commentlib.php b/lib/commentlib.php index e5c3ff9df8..1383163c60 100644 --- a/lib/commentlib.php +++ b/lib/commentlib.php @@ -15,10 +15,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -define('COMMENT_ERROR_DB', 1); -define('COMMENT_ERROR_INSUFFICIENT_CAPS', 2); -define('COMMENT_ERROR_MODULE_REJECT', 3); - /** * comment is class to process moodle comments * @@ -370,7 +366,6 @@ EOD; if (empty($this->viewcap)) { return false; } - // TODO: find a apropriate add page to add this option $CFG->commentsperpage = 15; if (!is_numeric($page)) { $page = 0; @@ -462,7 +457,7 @@ EOD; public function add($content, $format = FORMAT_MOODLE) { global $CFG, $DB, $USER, $OUTPUT; if (empty($this->postcap)) { - return COMMENT_ERROR_INSUFFICIENT_CAPS; + throw new comment_exception('nopermissiontocomment'); } $now = time(); $newcmt = new stdclass; @@ -478,7 +473,7 @@ EOD; // moodle module will check content $ret = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'add', array(&$newcmt, $this->options), true); if (!$ret) { - return COMMENT_ERROR_MODULE_REJECT; + throw new comment_exception('modulererejectcomment'); } } @@ -493,7 +488,7 @@ EOD; $newcmt->avatar = $OUTPUT->user_picture($userpic); return $newcmt; } else { - return COMMENT_ERROR_DB; + throw new comment_exception('dbupdatefailed'); } } @@ -519,10 +514,10 @@ EOD; global $DB, $USER; $candelete = has_capability('moodle/comment:delete', $this->context); if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) { - return COMMENT_ERROR_DB; + throw new comment_exception('dbupdatefailed'); } if (!($USER->id == $comment->userid || !empty($candelete))) { - return COMMENT_ERROR_INSUFFICIENT_CAPS; + throw new comment_exception('nopermissiontocomment'); } return $DB->delete_records('comments', array('id'=>$commentid)); } @@ -588,3 +583,10 @@ EOD; } } +class comment_exception extends moodle_exception { + public $message; + function __construct($errorcode) { + $this->errorcode = $errorcode; + $this->message = get_string($errorcode, 'error'); + } +} -- 2.39.5