diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index bbbb7db..6132126 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -5,6 +5,7 @@ require_once __DIR__.'/src/ApiTemplates.php'; require_once UELEARNING_LIB_ROOT.'/User/User.php'; require_once UELEARNING_LIB_ROOT.'/User/UserSession.php'; require_once UELEARNING_LIB_ROOT.'/User/UserAdmin.php'; +require_once UELEARNING_LIB_ROOT.'/Study/StudyActivity.php'; require_once UELEARNING_LIB_ROOT.'/Study/StudyActivityManager.php'; use UElearning\User; use UElearning\Study; @@ -412,16 +413,18 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { // TODO: $studyList 分離重新包裝陣列 $app->render(200,array( - 'token' => $token, - 'user_id' => $user_id, - 'enable_study' => $studyList, - 'error' => false, + 'token' => $token, + 'user_id' => $user_id, + 'enable_activity' => $studyList, + 'error' => false, )); } catch (Exception\LoginTokenNoFoundException $e) { $app->render(404,array( 'token' => $token, 'error' => true, + 'msg' => 'No \''.$token.'\' session. Please login again.', + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' )); } }); @@ -430,8 +433,104 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { * 開始進行一場學習活動 * POST http://localhost/api/v2/tokens/{登入Token}/Activity */ - $app->post('/:token/activity', function ($token) use ($app) { - // TODO: 開始進行一場學習活動 + $app->post('/:token/activitys', function ($token) use ($app) { + + // 取得帶來的參數 + $cType = $app->request->getContentType(); + if($cType == 'application/x-www-form-urlencoded') { + $themeId = $_POST['theme_id']; + $learnTime = isset($_POST['learn_time']) + ? $_POST['learn_time'] : null; + $timeForce = isset($_POST['time_force']) + ? $_POST['time_force'] : null; + $learnStyle = isset($_POST['learnStyle_mode']) + ? $_POST['learnStyle_mode'] : null; + $learnStyle_force = isset($_POST['learnStyle_force']) + ? $_POST['learnStyle_force'] : null; + $materialMode = isset($_POST['material_mode']) + ? $_POST['material_mode'] : null; + } + else /*if($cType == 'application/json')*/ { + $postData = $app->request->getBody(); + $postDataArray = json_decode($postData); + //$user_id = $postDataArray->user_id; + $app->render(400, array( + 'Content-Type'=> $cType, + 'error' => true, + 'msg' => '', + 'msg_cht' => '輸入參數的Content-Type不在支援範圍內 或是沒有輸入', + 'substatus' => 102 + ) + ); + } + /*else { + $app->render(400, array( + 'Content-Type'=> $cType, + 'error' => true, + 'msg' => '', + 'msg_cht' => '輸入參數的Content-Type不在支援範圍內 或是沒有輸入', + 'substatus' => 102 + ) + ); + }*/ + + try { + // 查詢使用者 + $session = new User\UserSession(); + $user_id = $session->getUserId($token); + + // 開始進行學習活動 + $studyMgr = new Study\StudyActivityManager(); + $studyId = $studyMgr->startActivity($user_id, $themeId, + $learnTime, $timeForce, + $learnStyle, $learnStyle_force, + $materialMode); + + // 取得開始後的學習活動資訊 + $sact = new Study\StudyActivity($studyId); + + + // 噴出資訊 + $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(), + 'have_time' => $sact->getRealLearnTime(), + 'learn_time' => $sact->getLearnTime(), + 'delay' => $sact->getDelay(), + 'remaining_time' => $sact->getRealLearnTime(), + '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, + )); + } + catch (Exception\LoginTokenNoFoundException $e) { + $app->render(404,array( + 'token' => $token, + 'error' => true, + 'msg' => 'No \''.$token.'\' session. Please login again.', + 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' + )); + } + catch (Exception\StudyActivityNoFoundException $e) { + $app->render(500,array( + 'token' => $token, + 'error' => true, + 'msg' => 'Start activity fail.', + 'msg_cht' => '建立學習活動失敗' + )); + } + }); /* diff --git a/htdocs/lib/Database/DBStudyActivity.php b/htdocs/lib/Database/DBStudyActivity.php index e6c46e5..ebf85cc 100644 --- a/htdocs/lib/Database/DBStudyActivity.php +++ b/htdocs/lib/Database/DBStudyActivity.php @@ -189,19 +189,19 @@ class DBStudyActivity extends Database { else { $output_learnStyleForce = false; } array_push($result, - array( 'activity_id' => $thisResult['SaID'], + array( 'activity_id' => (int)$thisResult['SaID'], 'user_id' => $thisResult['UID'], - 'theme_id' => $thisResult['ThID'], + 'theme_id' => (int)$thisResult['ThID'], 'start_time' => $thisResult['StartTime'], 'end_time' => $thisResult['EndTime'], - 'learn_time' => $thisResult['LearnTime'], - 'delay' => $thisResult['Delay'], + 'learn_time' => (int)$thisResult['LearnTime'], + 'delay' => (int)$thisResult['Delay'], 'time_force' => $output_time_force, - 'learnStyle_mode' => $thisResult['LMode'], + 'learnStyle_mode' => (int)$thisResult['LMode'], 'learnStyle_force' => $output_learnStyleForce, 'material_mode' => $thisResult['MMode'], - 'target_total' => $thisResult['TargetTotal'], - 'learned_total' => $thisResult['LearnedTotal'] + 'target_total' => (int)$thisResult['TargetTotal'], + 'learned_total' => (int)$thisResult['LearnedTotal'] ) ); } diff --git a/htdocs/lib/Study/StudyActivity.php b/htdocs/lib/Study/StudyActivity.php index 29de6f9..4202cc4 100644 --- a/htdocs/lib/Study/StudyActivity.php +++ b/htdocs/lib/Study/StudyActivity.php @@ -7,6 +7,7 @@ namespace UElearning\Study; require_once UELEARNING_LIB_ROOT.'/Database/DBStudyActivity.php'; require_once UELEARNING_LIB_ROOT.'/User/User.php'; +require_once UELEARNING_LIB_ROOT.'/Study/Theme.php'; require_once UELEARNING_LIB_ROOT.'/Study/Exception.php'; use UElearning\Database; use UElearning\Exception; @@ -182,7 +183,7 @@ class StudyActivity { * @since 2.0.0 */ public function getId() { - return $this->id; + return (int)$this->id; } /** @@ -228,6 +229,20 @@ class StudyActivity { return $this->queryResultArray['theme_id']; } + /** + * 取得這次是的主題名稱 + * + * @return string 主題名稱 + * @since 2.0.0 + */ + public function getThemeName() { + // TODO: 改成由資料庫直接查詢名稱以增佳能 + $themeId = $this->queryResultArray['theme_id']; + $theme = new Theme($themeId); + + return $theme->getName(); + } + // ------------------------------------------------------------------------ // 時間控制: @@ -267,7 +282,7 @@ class StudyActivity { * @return int 可實際學習時間(分) * @since 2.0.0 */ - public function getRealLearnTimeWith() { + public function getRealLearnTime() { $learnTime = $this->queryResultArray['learn_time']; $delay = $this->queryResultArray['delay'];