}
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));
+ }
}
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:
// 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
*
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;
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;
// 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');
}
}
$newcmt->avatar = $OUTPUT->user_picture($userpic);
return $newcmt;
} else {
- return COMMENT_ERROR_DB;
+ throw new comment_exception('dbupdatefailed');
}
}
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));
}
}
}
+class comment_exception extends moodle_exception {
+ public $message;
+ function __construct($errorcode) {
+ $this->errorcode = $errorcode;
+ $this->message = get_string($errorcode, 'error');
+ }
+}