]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-19118, use exception instead of error code to check comments api errors"
authordongsheng <dongsheng>
Mon, 12 Oct 2009 05:46:34 +0000 (05:46 +0000)
committerdongsheng <dongsheng>
Mon, 12 Oct 2009 05:46:34 +0000 (05:46 +0000)
comment/comment_ajax.php
comment/comment_post.php
lib/commentlib.php

index 87f36bbf72c48f4c7f5df66b35738601bab8b700..b904befbd45ab939c33b10a26d16745457721bb7 100644 (file)
@@ -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));
+    }
 }
index 9ebad5d70fe9ec550a0e34adfeaefabd9df2eaaf..f4d7e0cf636c928ee02be56f970035043c19bc80 100644 (file)
@@ -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:
index e5c3ff9df85f6bd7abcf8ee1b945fe482fb18de6..1383163c600b0746d568901bb045023ef362d0e1 100644 (file)
 // 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
  *
@@ -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');
+    }
+}