From 6f030a1b1bcfccb45613bd4b66a689901af86f87 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Wed, 24 Dec 2014 23:20:20 +0800 Subject: [PATCH] =?UTF-8?q?Add=20api:=20=E7=B5=90=E6=9D=9F=E5=AD=B8?= =?UTF-8?q?=E7=BF=92=E6=B4=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/api/v2/index.php | 87 ++++++++++++++++++++++++++++++ htdocs/lib/Study/StudyActivity.php | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index e2a149d..28cb381 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -606,6 +606,93 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { } }); + /* + * 結束這場學習活動 + * POST http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號}/finish + */ + $app->post('/:token/activitys/:said/finish', function ($token, $saId) use ($app) { + + try { + // 查詢使用者 + $session = new User\UserSession(); + $user_id = $session->getUserId($token); + + // 取得開始後的學習活動資訊 + $sact = new Study\StudyActivity($saId); + + // 確認此學習活動是否為本人所有 + if($sact->getUserId() == $user_id) { + + // 結束學習活動 + + $sact->finishActivity(); + + // 噴出學習完畢後的活動資料 + $app->render(201,array( + 'token' => $token, + 'user_id' => $user_id, + 'activity_id' => $sact->getId(), + 'activity' => array( + 'activity_id' => $sact->getId(), + 'theme_id' => $sact->getThemeId(), + 'theme_name' => $sact->getThemeName(), + 'start_time' => $sact->getStartTime(), + 'end_time' => $sact->getEndTime(), + 'learnStyle_mode' => $sact->getLearnStyle(), + 'learnStyle_force' => $sact->isForceLearnStyle(), + 'material_mode' => $sact->getMaterialStyle(), + 'target_total' => $sact->getPointTotal(), + 'learned_total' => $sact->getLearnedPointTotal() + ), + 'error' => false + )); + } + // 若非本人所有,則視同無此活動 + else { + throw new Exception\StudyActivityNoFoundException($saId); + } + } + catch (Exception\LoginTokenNoFoundException $e) { + $app->render(401,array( + 'token' => $token, + 'error' => true, + 'msg' => 'No \''.$token.'\' session. Please login again.', + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 + )); + } + catch (Exception\StudyActivityNoFoundException $e) { + $app->render(404,array( + 'token' => $token, + 'error' => true, + 'msg' => 'No found this activity.', + 'msg_cht' => '沒有此學習活動' + )); + } + catch (Exception\StudyActivityFinishedException $e) { + $app->render(405,array( + 'token' => $token, + 'user_id' => $user_id, + 'activity_id' => $sact->getId(), + 'activity' => array( + 'activity_id' => $sact->getId(), + 'theme_id' => $sact->getThemeId(), + 'theme_name' => $sact->getThemeName(), + 'start_time' => $sact->getStartTime(), + 'end_time' => $sact->getEndTime(), + 'learnStyle_mode' => $sact->getLearnStyle(), + 'learnStyle_force' => $sact->isForceLearnStyle(), + 'material_mode' => $sact->getMaterialStyle(), + 'target_total' => $sact->getPointTotal(), + 'learned_total' => $sact->getLearnedPointTotal() + ), + 'error' => true, + 'msg' => 'The activity is endded', + 'msg_cht' => '此活動已經結束了' + )); + } + }); + /* * 預約學習活動資料 * GET http://localhost/api/v2/tokens/{登入Token}/will/{預約編號} diff --git a/htdocs/lib/Study/StudyActivity.php b/htdocs/lib/Study/StudyActivity.php index 3e0bd88..4d3de89 100644 --- a/htdocs/lib/Study/StudyActivity.php +++ b/htdocs/lib/Study/StudyActivity.php @@ -106,7 +106,7 @@ class StudyActivity { /** * 結束這次學習 * * - * @throw \UElearning\Exception\StudyActivityNoFoundException + * @throw \UElearning\Exception\StudyActivityFinishedException * @since 2.0.0 */ public function finishActivity() {