Add 查詢學習狀態 & 增加API過期時間

This commit is contained in:
Yuan Chiu 2014-12-13 04:30:34 +08:00
parent 42721448e9
commit d0efd0ea4c
4 changed files with 147 additions and 26 deletions

View File

@ -331,7 +331,8 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) {
'token' => $token, 'token' => $token,
'error' => true, 'error' => true,
'msg' => 'No \''.$token.'\' session. Please login again.', '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, 'token' => $token,
'error' => true, 'error' => true,
'msg' => 'No \''.$token.'\' session. Please login again.', '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, 'token' => $token,
'error' => true, 'error' => true,
'msg' => 'No \''.$token.'\' session. Please login again.', '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) { $app->get('/:token/activitys', function ($token) use ($app) {
try { try {
@ -420,18 +423,19 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) {
)); ));
} }
catch (Exception\LoginTokenNoFoundException $e) { catch (Exception\LoginTokenNoFoundException $e) {
$app->render(404,array( $app->render(401,array(
'token' => $token, 'token' => $token,
'error' => true, 'error' => true,
'msg' => 'No \''.$token.'\' session. Please login again.', '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) { $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_id' => $sact->getThemeId(),
'theme_name' => $sact->getThemeName(), 'theme_name' => $sact->getThemeName(),
'start_time' => $sact->getStartTime(), 'start_time' => $sact->getStartTime(),
'expired_time' => $sact->getExpiredTime(),
'have_time' => $sact->getRealLearnTime(), 'have_time' => $sact->getRealLearnTime(),
'learn_time' => $sact->getLearnTime(), 'learn_time' => $sact->getLearnTime(),
'delay' => $sact->getDelay(), 'delay' => $sact->getDelay(),
@ -511,15 +516,16 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) {
'target_total' => $sact->getPointTotal(), 'target_total' => $sact->getPointTotal(),
'learned_total' => $sact->getLearnedPointTotal() 'learned_total' => $sact->getLearnedPointTotal()
), ),
'error' => false, 'error' => false
)); ));
} }
catch (Exception\LoginTokenNoFoundException $e) { catch (Exception\LoginTokenNoFoundException $e) {
$app->render(404,array( $app->render(401,array(
'token' => $token, 'token' => $token,
'error' => true, 'error' => true,
'msg' => 'No \''.$token.'\' session. Please login again.', 'msg' => 'No \''.$token.'\' session. Please login again.',
'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入' 'msg_cht' => '沒有\''.$token.'\'登入階段,請重新登入',
'substatus' => 204
)); ));
} }
catch (Exception\StudyActivityNoFoundException $e) { 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) { $app->get('/:token/activitys/:said', function ($token, $saId) use ($app) {
// TODO: 學習中狀況資料
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' => '沒有此學習活動'
));
}
}); });
/* /*

View File

@ -152,7 +152,12 @@ class DBStudyActivity extends Database {
protected function queryActivityByWhere($where) { protected function queryActivityByWhere($where) {
$sqlString = "SELECT `SaID`, `UID`, `ThID`, ". $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`, ". "`LearnTime`, `Delay`, `TimeForce`, ".
"`LMode`, `LModeForce`, `MMode`, ". "`LMode`, `LModeForce`, `MMode`, ".
@ -192,7 +197,9 @@ class DBStudyActivity extends Database {
array( 'activity_id' => (int)$thisResult['SaID'], array( 'activity_id' => (int)$thisResult['SaID'],
'user_id' => $thisResult['UID'], 'user_id' => $thisResult['UID'],
'theme_id' => (int)$thisResult['ThID'], 'theme_id' => (int)$thisResult['ThID'],
'theme_name' => $thisResult['ThName'],
'start_time' => $thisResult['StartTime'], 'start_time' => $thisResult['StartTime'],
'expired_time' => $thisResult['ExpiredTime'],
'end_time' => $thisResult['EndTime'], 'end_time' => $thisResult['EndTime'],
'learn_time' => (int)$thisResult['LearnTime'], 'learn_time' => (int)$thisResult['LearnTime'],
'delay' => (int)$thisResult['Delay'], 'delay' => (int)$thisResult['Delay'],

View File

@ -236,11 +236,7 @@ class StudyActivity {
* @since 2.0.0 * @since 2.0.0
*/ */
public function getThemeName() { public function getThemeName() {
// TODO: 改成由資料庫直接查詢名稱以增佳能 return $this->queryResultArray['theme_name'];
$themeId = $this->queryResultArray['theme_id'];
$theme = new Theme($themeId);
return $theme->getName();
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
@ -256,6 +252,16 @@ class StudyActivity {
return $this->queryResultArray['start_time']; return $this->queryResultArray['start_time'];
} }
/**
* 取得這次學習的過期時間
*
* @return string 過期時間
* @since 2.0.0
*/
public function getExpiredTime() {
return $this->queryResultArray['expired_time'];
}
/** /**
* 取得這次學習是什麼時候結束的 * 取得這次學習是什麼時候結束的
* *

View File

@ -7,6 +7,9 @@ namespace UElearning\Study;
require_once UELEARNING_LIB_ROOT.'/Database/DBStudyActivity.php'; require_once UELEARNING_LIB_ROOT.'/Database/DBStudyActivity.php';
require_once UELEARNING_LIB_ROOT.'/Study/Exception.php'; require_once UELEARNING_LIB_ROOT.'/Study/Exception.php';
require_once UELEARNING_LIB_ROOT.'/Study/StudyWill.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\Database;
use UElearning\Exception; use UElearning\Exception;
@ -48,6 +51,8 @@ class StudyActivityManager {
* @param int $learnStyle 將推薦幾個學習點 * @param int $learnStyle 將推薦幾個學習點
* @param bool $learnStyle_force 是否拒絕前往非推薦的學習點 * @param bool $learnStyle_force 是否拒絕前往非推薦的學習點
* @param string $materialMode 教材風格 * @param string $materialMode 教材風格
* @throw UElearning\Exception\UserNoFoundException
* @throw UElearning\Exception\ThemeNoFoundException
* @return int 本次學習活動的流水編號 * @return int 本次學習活動的流水編號
* @since 2.0.0 * @since 2.0.0
*/ */
@ -55,7 +60,46 @@ class StudyActivityManager {
$learnStyle, $learnStyle_force, $materialMode ) $learnStyle, $learnStyle_force, $materialMode )
{ {
if($this->checkDataIsExist($userId, $themeId, $materialMode)) { $user = new User\User($userId);
$theme = new Theme($themeId);
// 若無指定學習時間
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(); $db = new Database\DBStudyActivity();
@ -65,7 +109,6 @@ class StudyActivityManager {
// 傳回新增後得到的編號 // 傳回新增後得到的編號
return $resultId; return $resultId;
} }
}
/** /**
* 從預約開始進行學習活動 * 從預約開始進行學習活動