進入/離開學習點功能實作

This commit is contained in:
Yuan Chiu 2015-01-16 15:59:35 +08:00
parent 357a7e4262
commit 5b7feb1332
4 changed files with 204 additions and 30 deletions

View File

@ -7,6 +7,7 @@ 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';
require_once UELEARNING_LIB_ROOT.'/Study/StudyManager.php';
require_once UELEARNING_LIB_ROOT.'/Target/Target.php';
require_once UELEARNING_LIB_ROOT.'/Target/TargetManager.php';
use UElearning\User;
@ -891,6 +892,148 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) {
}
});
/*
* 進入此學習點
* POST http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號}/points/{標的編號}/toin
*/
$app->post('/:token/activitys/:said/points/:tid/toin', function ($token, $saId, $tId) use ($app) {
// 取得帶來的參數
$cType = $app->request->getContentType();
if($cType == 'application/x-www-form-urlencoded') {
$is_entity = isset($_POST['is_entity']) ? $_POST['is_entity'] : true;
}
else /*if($cType == 'application/json')*/ {
$postData = $app->request->getBody();
$postDataArray = json_decode($postData);
$is_entity = isset($postDataArray->is_entity)
? $postDataArray->is_entity : true;
}
/*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);
// 取得開始後的學習活動資訊
$sact = new Study\StudyActivity($saId);
// 確認此學習活動是否為本人所有
if($sact->getUserId() == $user_id) {
$sct = new Study\StudyManager();
// 進入學習點
try{
$sid = $sct->toInTarget($saId, $tId, $is_entity);
}
// 若狀態為正在標的內學習時,強制當成離開標的,重新進入
catch (Exception\InLearningException $e) {
$sct->toOutTarget($saId, $tId);
$sid = $sct->toInTarget($saId, $tId, $is_entity);
}
// 噴出結果
$app->render(200,array(
'token' => $token,
'user_id' => $user_id,
'activity_id' => $sact->getId(),
'study_id' => $sid,
'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' => '沒有此學習活動'
));
}
});
/*
* 進入此學習點
* POST http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號}/points/{標的編號}/toout
*/
$app->post('/:token/activitys/:said/points/:tid/toout', function ($token, $saId, $tId) use ($app) {
try {
// 查詢使用者
$session = new User\UserSession();
$user_id = $session->getUserId($token);
// 取得開始後的學習活動資訊
$sact = new Study\StudyActivity($saId);
// 確認此學習活動是否為本人所有
if($sact->getUserId() == $user_id) {
$sct = new Study\StudyManager();
// 離開學習點
$sct->toOutTarget($saId, $tId);
// 噴出結果
$app->render(201,array(
'token' => $token,
'user_id' => $user_id,
'activity_id' => $sact->getId(),
'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

@ -279,6 +279,25 @@ class DBStudy extends Database {
$query->execute();
}
/**
* 此活動內離開所有學習點
*
* @param string $activity_id 活動編號
* @since 2.0.0
*/
public function allToOutTaeget($activity_id)
{
// 寫入
$sqlString = "UPDATE `".$this->table('Study').
"` SET `Out_TargetTime` = NOW()
WHERE `SaID` = :id ";
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":id", $activity_id);
$query->execute();
}
/**
* 取得目前正在進行的標的編號
*
@ -334,28 +353,39 @@ class DBStudy extends Database {
/**
* 取得所有在此標的的記錄編號
*
* 正常來說只會有一個,但考量使用者可能在這次活動內同一標的進出兩次以上,故還是以陣列輸出。
* 正常來說只會有一個,但考量使用者可能在這次活動內同一標的進出兩次以上,故還是以二維陣列輸出。
*
* @param int $activity_id 活動編號
* @param int $target_id 標的編號
* @return array 所有進出記錄編號
* @return array 所有進出記錄資訊陣列
* @since 2.0.0
*/
public function getAllStudyIdByTargetId($activity_id, $target_id) {
$sqlString = "SELECT `SID` FROM `".$this->table('Study')."` ".
"WHERE `TID` = :tid AND `SaID` = :said ";
$queryResultAll = $this->queryByWhere(
"`TID` = ".$this->connDB->quote($target_id).
" AND `SaID` = ".$this->connDB->quote($activity_id));
$query = $this->connDB->prepare($sqlString);
$query->bindParam(":said", $activity_id);
$query->bindParam(":tid", $target_id);
$query->execute();
return $queryResultAll;
}
$output = array();
/**
* 取得在此標學習中的記錄編號
*
* 正常來說只會有一個,但考量使用者可能在這次活動內同一標的進出兩次以上,故還是以陣列輸出。
*
* @param int $activity_id 活動編號
* @param int $target_id 標的編號
* @return array 所有進出記錄資訊陣列
* @since 2.0.0
*/
public function getInStudyIdByTargetId($activity_id, $target_id) {
while($queryResult = $query->fetch()) {
array_push($output, $queryResult[0]);
}
return $output;
$queryResultAll = $this->queryByWhere(
"`TID` = ".$this->connDB->quote($target_id).
" AND `SaID` = ".$this->connDB->quote($activity_id).
" AND `Out_TargetTime` IS NULL");
return $queryResultAll;
}
}

View File

@ -50,7 +50,7 @@ class InLearningException extends \UnexpectedValueException {
* 使用者帳號例外
* @param int $id 輸入的標的ID
*/
public function __construct($id) {
public function __construct() {
parent::__construct('Learning');
}
}

View File

@ -46,17 +46,17 @@ class StudyManager {
/**
* 進入標的
*
* @param string $activity_id 活動編號
* @param string $target_id 標的編號
* @param string $is_entity 是否為現場學習
* @param int $activity_id 活動編號
* @param int $target_id 標的編號
* @param bool $is_entity 是否為現場學習
* return int 進出紀錄編號
*/
public function toInTarget($activity_id, $target_id, $is_entity) {
// 若沒有任一個點正在學習中
if($this->getCurrentInTargetId($activity_id) != null) {
if($this->getCurrentInTargetId($activity_id) == null) {
$db = new Database\DBStudy();
return $db->toInTaeget(string $activity_id, string $target_id, string $is_entity);
return $db->toInTaeget($activity_id, $target_id, $is_entity);
}
else {
throw new Exception\InLearningException();
@ -66,20 +66,21 @@ class StudyManager {
/**
* 離開標的
*
* @param string $activity_id 活動編號
* @param string $target_id 標的編號
* @param string $is_entity 是否為現場學習
* return int 進出紀錄編號
* @param int $activity_id 活動編號
* @param int $target_id 標的編號
*/
public function toOutTarget($activity_id, $target_id) {
// 若沒有任一個點正在學習中
if($this->getCurrentInTargetId($activity_id) != null) {
$db = new Database\DBStudy();
return $db->toInTaeget(string $activity_id, string $target_id, string $is_entity);
}
else {
throw new Exception\InLearningException();
// 從資料庫取得此活動此標的學習中資料
$db = new Database\DBStudy();
$learning_array = $db->getInStudyIdByTargetId($activity_id, $target_id);
if(isset($learning_array)) {
foreach($learning_array as $thisArray) {
$db->toOutTaeget($thisArray['study_id']);
}
}
}
}