Skip to content

Commit

Permalink
"MDL-19118, use exception instead of error code to check comments api…
Browse files Browse the repository at this point in the history
… errors"
  • Loading branch information
dongsheng committed Oct 12, 2009
1 parent f5b5a82 commit 15894c6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
56 changes: 26 additions & 30 deletions comment/comment_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,38 @@
}
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));
}
}
16 changes: 7 additions & 9 deletions comment/comment_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@

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:
Expand Down
22 changes: 12 additions & 10 deletions lib/commentlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

define('COMMENT_ERROR_DB', 1);
define('COMMENT_ERROR_INSUFFICIENT_CAPS', 2);
define('COMMENT_ERROR_MODULE_REJECT', 3);

/**
* comment is class to process moodle comments
*
Expand Down Expand Up @@ -370,7 +366,6 @@ public function get_comments($page = '') {
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;
Expand Down Expand Up @@ -462,7 +457,7 @@ public function get_pagination($page = 0) {
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;
Expand All @@ -478,7 +473,7 @@ public function add($content, $format = FORMAT_MOODLE) {
// 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');
}
}

Expand All @@ -493,7 +488,7 @@ public function add($content, $format = FORMAT_MOODLE) {
$newcmt->avatar = $OUTPUT->user_picture($userpic);
return $newcmt;
} else {
return COMMENT_ERROR_DB;
throw new comment_exception('dbupdatefailed');
}
}

Expand All @@ -519,10 +514,10 @@ public function delete($commentid) {
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));
}
Expand Down Expand Up @@ -588,3 +583,10 @@ public function print_comment($cmt) {
}
}

class comment_exception extends moodle_exception {
public $message;
function __construct($errorcode) {
$this->errorcode = $errorcode;
$this->message = get_string($errorcode, 'error');
}
}

0 comments on commit 15894c6

Please sign in to comment.