From 66a7d03b4d14cdaf2e78f8a4a68759e80160e261 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Sat, 14 Mar 2015 18:06:18 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Lib=20StudyManager:=20=E8=8B=A5=E9=9B=A2?= =?UTF-8?q?=E9=96=8B=E5=AD=B8=E7=BF=92=E9=BB=9E=E6=99=82=E6=B2=92=E6=9C=89?= =?UTF-8?q?=E9=80=B2=E5=8E=BB=E8=B3=87=E6=96=99=EF=BC=8C=E5=89=87=E9=80=81?= =?UTF-8?q?=E5=87=BA=E4=BE=8B=E5=A4=96=E3=80=82=20API:=20=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E9=9B=A2=E9=96=8B=E5=AD=B8=E7=BF=92=E9=BB=9E=E6=99=82?= =?UTF-8?q?=EF=BC=8C=E6=B2=92=E6=9C=89=E9=80=B2=E5=85=A5=E8=B3=87=E6=96=99?= =?UTF-8?q?=EF=BC=8C=E5=89=87=E7=95=B6=E6=88=90=E9=80=B2=E5=8E=BB=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=B9=8B=E5=BE=8C=E9=A6=AC=E4=B8=8A=E5=87=BA=E4=BE=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/api/v2/index.php | 35 ++++++++++++++++++++++-------- htdocs/lib/Study/Exception.php | 19 +++++++++++++++- htdocs/lib/Study/StudyActivity.php | 3 ++- htdocs/lib/Study/StudyManager.php | 4 ++++ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index 3f939bb..f319ef7 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -1100,7 +1100,7 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { }); /* - * 進入此學習點 + * 離開此學習點 * POST http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號}/points/{標的編號}/toout */ $app->post('/:token/activitys/:said/points/:tid/toout', function ($token, $saId, $tId) use ($app) { @@ -1117,15 +1117,32 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { if($sact->getUserId() == $user_id) { // 離開學習點 - $sact->toOutTarget($tId); + try { + $sact->toOutTarget($tId); - // 噴出結果 - $app->render(201,array( - 'token' => $token, - 'user_id' => $user_id, - 'activity_id' => $sact->getId(), - 'error' => false - )); + // 噴出結果 + $app->render(201,array( + 'token' => $token, + 'user_id' => $user_id, + 'activity_id' => $sact->getId(), + 'error' => false + )); + } + // 如果此標的尚未登記為已進入 + catch (Exception\NoInLearningException $e) { + // 當作進去此標的 + // TODO: 這邊先暫時當成是以實體方式進入,之後要修成Client發出離開訊息時,也一併帶入剛剛的為實體or虛擬 + $sact->toInTarget($tId, true); + $sact->toOutTarget($tId); + + // 噴出結果 + $app->render(201,array( + 'token' => $token, + 'user_id' => $user_id, + 'activity_id' => $sact->getId(), + 'error' => false + )); + } } // 若非本人所有,則視同無此活動 diff --git a/htdocs/lib/Study/Exception.php b/htdocs/lib/Study/Exception.php index 3a9e501..b6b8d45 100644 --- a/htdocs/lib/Study/Exception.php +++ b/htdocs/lib/Study/Exception.php @@ -39,7 +39,7 @@ class StudyNoFoundException extends \UnexpectedValueException { } /** - * 正在學習中例外(難道使用者有分身?) + * 正在學習點內學習中例外(難道使用者有分身?) * @since 2.0.0 * @package UElearning * @subpackage Study @@ -55,6 +55,23 @@ class InLearningException extends \UnexpectedValueException { } } +/** + * 不在學習點內的例外 + * @since 2.0.0 + * @package UElearning + * @subpackage Study + */ +class NoInLearningException extends \UnexpectedValueException { + + /** + * 使用者帳號例外 + * @param int $id 輸入的標的ID + */ + public function __construct() { + parent::__construct('NoInLearning'); + } +} + /** * 沒有找到此活動 * @since 2.0.0 diff --git a/htdocs/lib/Study/StudyActivity.php b/htdocs/lib/Study/StudyActivity.php index 8e72767..989e4c6 100644 --- a/htdocs/lib/Study/StudyActivity.php +++ b/htdocs/lib/Study/StudyActivity.php @@ -485,7 +485,7 @@ class StudyActivity { try{ return $sct->toInTarget($saId, $target_id, $is_entity); } - // 若狀態為正在標的內學習時,強制當成離開標的,重新進入 + // 若狀態為正在標的內學習時,送出例外 catch (Exception\InLearningException $e) { throw $e; } @@ -496,6 +496,7 @@ class StudyActivity { * 離開標的 * * @param int $target_id 標的編號 + * @throw UElearning\Exception\NoInLearningException */ public function toOutTarget($target_id) { diff --git a/htdocs/lib/Study/StudyManager.php b/htdocs/lib/Study/StudyManager.php index c845c53..492081a 100644 --- a/htdocs/lib/Study/StudyManager.php +++ b/htdocs/lib/Study/StudyManager.php @@ -100,6 +100,7 @@ class StudyManager { * * @param int $activity_id 活動編號 * @param int $target_id 標的編號 + * @throw UElearning\Exception\NoInLearningException */ public function toOutTarget($activity_id, $target_id) { @@ -124,5 +125,8 @@ class StudyManager { } } } + else { + throw new Exception\NoInLearningException(); + } } }