diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index 6132126..e2a149d 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -331,7 +331,8 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { 'token' => $token, 'error' => true, 'msg' => 'No \''.$token.'\' session. Please login again.', - 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 )); } }); @@ -360,7 +361,8 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { 'token' => $token, 'error' => true, 'msg' => 'No \''.$token.'\' session. Please login again.', - 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 )); } }); @@ -392,7 +394,8 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { 'token' => $token, 'error' => true, 'msg' => 'No \''.$token.'\' session. Please login again.', - 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 )); } }); @@ -401,7 +404,7 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { /* * 取得可用的學習活動 - * GET http://localhost/api/v2/tokens/{登入Token}/Activity + * GET http://localhost/api/v2/tokens/{登入Token}/activitys */ $app->get('/:token/activitys', function ($token) use ($app) { try { @@ -420,18 +423,19 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { )); } catch (Exception\LoginTokenNoFoundException $e) { - $app->render(404,array( + $app->render(401,array( 'token' => $token, 'error' => true, 'msg' => 'No \''.$token.'\' session. Please login again.', - 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 )); } }); /* * 開始進行一場學習活動 - * POST http://localhost/api/v2/tokens/{登入Token}/Activity + * POST http://localhost/api/v2/tokens/{登入Token}/activitys */ $app->post('/:token/activitys', function ($token) use ($app) { @@ -500,6 +504,7 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { 'theme_id' => $sact->getThemeId(), 'theme_name' => $sact->getThemeName(), 'start_time' => $sact->getStartTime(), + 'expired_time' => $sact->getExpiredTime(), 'have_time' => $sact->getRealLearnTime(), 'learn_time' => $sact->getLearnTime(), 'delay' => $sact->getDelay(), @@ -511,15 +516,16 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { 'target_total' => $sact->getPointTotal(), 'learned_total' => $sact->getLearnedPointTotal() ), - 'error' => false, + 'error' => false )); } catch (Exception\LoginTokenNoFoundException $e) { - $app->render(404,array( + $app->render(401,array( 'token' => $token, 'error' => true, 'msg' => 'No \''.$token.'\' session. Please login again.', - 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入', + 'substatus' => 204 )); } catch (Exception\StudyActivityNoFoundException $e) { @@ -535,10 +541,69 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { /* * 取得學習中狀況資料 - * GET http://localhost/api/v2/tokens/{登入Token}/activity/{學習中活動編號} + * GET http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號} */ - $app->get('/:token/activity/:said', function ($token, $saId) use ($app) { - // TODO: 學習中狀況資料 + $app->get('/:token/activitys/:said', 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) { + + // 噴出資訊 + $app->render(200,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(), + 'expired_time' => $sact->getExpiredTime(), + 'have_time' => $sact->getRealLearnTime(), + 'learn_time' => $sact->getLearnTime(), + 'delay' => $sact->getDelay(), + 'remaining_time' => $sact->getRemainingTime(), + 'time_force' => $sact->isForceLearnTime(), + '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' => '沒有此學習活動' + )); + } }); /* diff --git a/htdocs/lib/Database/DBStudyActivity.php b/htdocs/lib/Database/DBStudyActivity.php index ebf85cc..808afa9 100644 --- a/htdocs/lib/Database/DBStudyActivity.php +++ b/htdocs/lib/Database/DBStudyActivity.php @@ -152,7 +152,12 @@ class DBStudyActivity extends Database { protected function queryActivityByWhere($where) { $sqlString = "SELECT `SaID`, `UID`, `ThID`, ". - "`StartTime`, `EndTime`, ". + "(SELECT `ThName` FROM `chu__Theme` AS `th` ". + "WHERE `th`.`ThID` = `sa`.`ThID`) AS `ThName`, ". + "`StartTime`, ". + "FROM_UNIXTIME(UNIX_TIMESTAMP(`StartTime`)+(`LearnTime`+`Delay`)*60)". + " AS `ExpiredTime`, ". + "`EndTime`, ". "`LearnTime`, `Delay`, `TimeForce`, ". "`LMode`, `LModeForce`, `MMode`, ". @@ -192,7 +197,9 @@ class DBStudyActivity extends Database { array( 'activity_id' => (int)$thisResult['SaID'], 'user_id' => $thisResult['UID'], 'theme_id' => (int)$thisResult['ThID'], + 'theme_name' => $thisResult['ThName'], 'start_time' => $thisResult['StartTime'], + 'expired_time' => $thisResult['ExpiredTime'], 'end_time' => $thisResult['EndTime'], 'learn_time' => (int)$thisResult['LearnTime'], 'delay' => (int)$thisResult['Delay'], diff --git a/htdocs/lib/Study/StudyActivity.php b/htdocs/lib/Study/StudyActivity.php index 4202cc4..3e0bd88 100644 --- a/htdocs/lib/Study/StudyActivity.php +++ b/htdocs/lib/Study/StudyActivity.php @@ -236,11 +236,7 @@ class StudyActivity { * @since 2.0.0 */ public function getThemeName() { - // TODO: 改成由資料庫直接查詢名稱以增佳能 - $themeId = $this->queryResultArray['theme_id']; - $theme = new Theme($themeId); - - return $theme->getName(); + return $this->queryResultArray['theme_name']; } // ------------------------------------------------------------------------ @@ -256,6 +252,16 @@ class StudyActivity { return $this->queryResultArray['start_time']; } + /** + * 取得這次學習的過期時間 + * + * @return string 過期時間 + * @since 2.0.0 + */ + public function getExpiredTime() { + return $this->queryResultArray['expired_time']; + } + /** * 取得這次學習是什麼時候結束的 * diff --git a/htdocs/lib/Study/StudyActivityManager.php b/htdocs/lib/Study/StudyActivityManager.php index 0ddbe3d..be3c346 100644 --- a/htdocs/lib/Study/StudyActivityManager.php +++ b/htdocs/lib/Study/StudyActivityManager.php @@ -7,6 +7,9 @@ namespace UElearning\Study; require_once UELEARNING_LIB_ROOT.'/Database/DBStudyActivity.php'; require_once UELEARNING_LIB_ROOT.'/Study/Exception.php'; require_once UELEARNING_LIB_ROOT.'/Study/StudyWill.php'; +require_once UELEARNING_LIB_ROOT.'/Study/Theme.php'; +require_once UELEARNING_LIB_ROOT.'/User/User.php'; +use UElearning\User; use UElearning\Database; use UElearning\Exception; @@ -48,6 +51,8 @@ class StudyActivityManager { * @param int $learnStyle 將推薦幾個學習點 * @param bool $learnStyle_force 是否拒絕前往非推薦的學習點 * @param string $materialMode 教材風格 + * @throw UElearning\Exception\UserNoFoundException + * @throw UElearning\Exception\ThemeNoFoundException * @return int 本次學習活動的流水編號 * @since 2.0.0 */ @@ -55,16 +60,54 @@ class StudyActivityManager { $learnStyle, $learnStyle_force, $materialMode ) { - if($this->checkDataIsExist($userId, $themeId, $materialMode)) { + $user = new User\User($userId); - // 存入資料庫 - $db = new Database\DBStudyActivity(); - $resultId = $db->insertActivity($userId, $themeId, null, null, - $learnTime, 0, $timeForce, $learnStyle, $learnStyle_force, $materialMode); + $theme = new Theme($themeId); - // 傳回新增後得到的編號 - return $resultId; + // 若無指定學習時間 + if(!isset($learnTime)) { + $learnTime = $theme->getLearnTime(); } + + // 若無指定強制時間 + if(!isset($timeForce)) { + $timeForce = false; + } + + // 若無指定學習導引模式 + if(!isset($learnStyle)) { + // 若使用者有偏好 + if($user->getLearnStyle() == null){ + $learnStyle = $user->getLearnStyle(); + } + else { + $learnStyle = LMODE; + } + } + + // 若無指定強制學習導引模式 + if(!isset($learnStyle_force)) { + $learnStyle_force = false; + } + + // 若無指定教材 + if(!isset($materialMode)) { + // 若使用者有偏好 + if($user->getMaterialStyle() == null) + $materialMode = $user->getMaterialStyle(); + else { + $materialMode = MMODE; + } + + } + + // 存入資料庫 + $db = new Database\DBStudyActivity(); + $resultId = $db->insertActivity($userId, $themeId, null, null, + $learnTime, 0, $timeForce, $learnStyle, $learnStyle_force, $materialMode); + + // 傳回新增後得到的編號 + return $resultId; } /**