diff --git a/htdocs/api/v2/index.php b/htdocs/api/v2/index.php index bea0211..b11c273 100644 --- a/htdocs/api/v2/index.php +++ b/htdocs/api/v2/index.php @@ -806,6 +806,91 @@ $app->group('/tokens', 'APIrequest', function () use ($app, $app_template) { } }); + /* + * 取得此標的資料 + * GET http://localhost/api/v2/tokens/{登入Token}/activitys/{學習中活動編號}/points/{標的編號} + */ + $app->get('/:token/activitys/:said/points/:tid', 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) { + + // 取得此活動的主題 + $thid = $sact->getThemeId(); + + // 取得本次採用的教材風格 + $materialMode = $sact->getMaterialStyle(); + + // 取得主題內所有的標的資訊 + $target = new Target\Target($thid); + $materialUrl = $target->getMaterialUrl(true, $materialMode); + $virtualMaterialUrl = $target->getMaterialUrl(false, $materialMode); + + // 處理噴出結果 + $output_targets = array( + 'theme_id' => $thid, + 'target_id' => $target->getId(), + 'hall_id' => $target->getHallId(), + //'hall_name' => $thisTargetArray['hall_name'], + 'area_id' => $target->getAreaId(), + //'area_name' => $thisTargetArray['area_name'], + //'floor' => $thisTargetArray['floor'], + //'area_number' => $thisTargetArray['area_number'], + 'target_number' => $target->getNumber(), + 'name' => $target->getName(), + 'map_url' => $target->getMapUrl(), + 'material_url' => $materialUrl, + 'virtual_material_url' => $virtualMaterialUrl, + 'learn_time' => $target->getLearnTime(), + 'PLj' => $target->getPLj(), + 'Mj' => $target->getMj(), + 'S' => $target->getS(), + 'Fj' => $target->getFj() + ); + + // 噴出結果 + $app->render(200,array( + 'token' => $token, + 'user_id' => $user_id, + 'activity_id' => $sact->getId(), + 'target' => $output_targets, + '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/DBStudy.php b/htdocs/lib/Database/DBStudy.php new file mode 100644 index 0000000..e12a568 --- /dev/null +++ b/htdocs/lib/Database/DBStudy.php @@ -0,0 +1,361 @@ +queryAllStudyByUserId('yuan'); + * echo '
';print_r($data);echo '
'; + * + * @author Yuan Chiu + * @version 2.0.0 + * @package UElearning + * @subpackage Database + */ +class DBStudy extends Database { + + /** + * 內部使用的查詢動作 + * @param string $where 查詢語法 + * @return array 查詢結果陣列 + */ + protected function queryByWhere($where) { + + $sqlString = "SELECT `SID`, `SaID`, ". + "`TID`, `IsEntity`, `In_TargetTime`, `Out_TargetTime` ". + "FROM `".$this->table('Study')."` ". + "WHERE ".$where; + + $query = $this->connDB->prepare($sqlString); + $query->execute(); + + $queryResultAll = $query->fetchAll(); + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + // 製作回傳結果陣列 + $result = array(); + foreach($queryResultAll as $key => $thisResult) { + + if($thisResult['IsEntity'] != '0') { + $output_entity = true; + } + else { $output_entity = false; } + + array_push($result, + array( 'study_id' => (int)$thisResult['SID'], + 'activity_id' => (int)$thisResult['SaID'], + 'target_id' => (int)$thisResult['TID'], + 'is_entity' => $output_entity, + 'in_target_time' => $thisResult['In_TargetTime'], + 'out_target_time' => $thisResult['Out_TargetTime'] + ) + ); + } + return $result; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 以活動編號查詢所有進出標的資料 + * + * @return array 進出標的資料陣列,格式為: + * + * array( + * array( + * 'study_id' => <流水編號>, + * 'activity_id' => <活動編號>, + * 'target_id' => <標的編號>, + * 'is_entity' => <是否為現場學習>, + * 'in_target_time' => <進入時間>, + * 'out_target_time' => <離開時間> + * ) + * ); + * + */ + public function queryById($id) { + $queryResultAll = $this->queryByWhere("`SID`=".$this->connDB->quote($id)); + + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + return $queryResultAll[0]; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 以活動編號查詢所有進出標的資料 + * + * @return array 進出標的資料陣列,格式為: + * + * array( + * array( + * 'study_id' => <流水編號>, + * 'activity_id' => <活動編號>, + * 'target_id' => <標的編號>, + * 'is_entity' => <是否為現場學習>, + * 'in_target_time' => <進入時間>, + * 'out_target_time' => <離開時間> + * ) + * ); + * + */ + public function queryByActivityId($id) { + + return $this->queryByWhere("`SID`=".$this->connDB->quote($id)); + } + + /** + * 查詢所有進出標的資料 + * + * @return array 進出標的資料陣列,格式為: + * + * array( + * array( + * 'study_id' => <流水編號>, + * 'activity_id' => <活動編號>, + * 'target_id' => <標的編號>, + * 'is_entity' => <是否為現場學習>, + * 'in_target_time' => <進入時間>, + * 'out_target_time' => <離開時間> + * ) + * ); + * + */ + public function queryAll() { + + return $this->queryByWhere("1"); + } + + /** + * 建立學習點進出記錄 + * + * @param string $activity_id 活動編號 + * @param string $target_id 標的編號 + * @param string $is_entity 是否為現場學習 + * @param int $in_target_time 進入時間 + * @param int $out_target_time 離開時間 + * + * @return int 剛新增的記錄編號 + * @since 2.0.0 + */ + public function insert($activity_id, $target_id, + $is_entity, $in_target_time, $out_target_time) + { + + if(!isset($is_entity)) { + $is_entity = true; + } + + // 寫入 + $sqlString = "INSERT INTO `".$this->table('Study'). + "` (`SaID`, `TID`, `IsEntity`, `In_TargetTime`, `Out_TargetTime`) + VALUES ( :said , :tid , :entity , :intime , :outtime )"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":said", $activity_id); + $query->bindParam(":tid", $target_id); + $query->bindParam(":entity", $is_entity); + $query->bindParam(":intime", $in_target_time); + $query->bindParam(":outtime", $out_target_time); + $query->execute(); + + // 取得剛剛加入的ID + $sqlString = "SELECT LAST_INSERT_ID()"; + $query = $this->connDB->query($sqlString); + $queryResult = $query->fetch(); + $resultId = $queryResult[0]; + + return $resultId; + } + + /** + * 移除一筆進出標的資料 + * @param int $id 進出標的編號 + * @since 2.0.0 + */ + public function delete($id) { + + $sqlString = "DELETE FROM ".$this->table('Study'). + " WHERE `SID` = :id "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $id); + $query->execute(); + } + + /** + * 移除此學習活動的所有進出標的資料 + * @param int $id 學習活動編號 + * @since 2.0.0 + */ + public function deleteByActivityId($id) { + + $sqlString = "DELETE FROM ".$this->table('Study'). + " WHERE `SaID` = :id "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $id); + $query->execute(); + } + + /** + * 進入學習點 + * + * @param string $activity_id 活動編號 + * @param string $target_id 標的編號 + * @param string $is_entity 是否為現場學習 + * @return int 剛新增的記錄編號 + * @since 2.0.0 + */ + public function toInTaeget($activity_id, $target_id, $is_entity) + { + + if(!isset($is_entity)) { + $is_entity = true; + } + + // 寫入 + $sqlString = "INSERT INTO `".$this->table('Study'). + "` (`SaID`, `TID`, `IsEntity`, `In_TargetTime`, `Out_TargetTime`) + VALUES ( :said , :tid , :entity , NOW() , NULL )"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":said", $activity_id); + $query->bindParam(":tid", $target_id); + $query->bindParam(":entity", $is_entity); + $query->execute(); + + // 取得剛剛加入的ID + $sqlString = "SELECT LAST_INSERT_ID()"; + $query = $this->connDB->query($sqlString); + $queryResult = $query->fetch(); + $resultId = $queryResult[0]; + + return $resultId; + } + + /** + * 離開學習點 + * + * @param string $study_id 此記錄編號 + * @since 2.0.0 + */ + public function toOutTaeget($study_id) + { + + // 寫入 + $sqlString = "UPDATE `".$this->table('Study'). + "` SET `Out_TargetTime` = NOW() + WHERE `SID` = :id "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $study_id); + $query->execute(); + } + + /** + * 取得目前正在進行的標的編號 + * + * @param int $activity_id 活動編號 + * @return int 標的編號 + * @since 2.0.0 + */ + public function getCurrentInTargetId($activity_id) { + + $sqlString = "SELECT `TID` FROM `".$this->table('Study')."` ". + "WHERE `Out_TargetTime` IS NULL AND `SaID` = :said "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":said", $activity_id); + $query->execute(); + + $queryResult = $query->fetch(); + // 如果有查到一筆以上 + if( count($queryResult) >= 1 ) { + return (int)$queryResult[0]; + } + else { + return null; + } + } + + /** + * 取得目前正在進行的紀錄編號 + * + * @param int $activity_id 活動編號 + * @return int 進出紀錄編號 + * @since 2.0.0 + */ + public function getCurrentInStudyId($activity_id) { + + $sqlString = "SELECT `SID` FROM `".$this->table('Study')."` ". + "WHERE `Out_TargetTime` IS NULL AND `SaID` = :said "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":said", $activity_id); + $query->execute(); + + $queryResult = $query->fetch(); + // 如果有查到一筆以上 + if( count($queryResult) >= 1 ) { + return (int)$queryResult[0]; + } + else { + return null; + } + } + + /** + * 取得所有在此標的的記錄編號 + * + * 正常來說只會有一個,但考量使用者可能在這次活動內同一標的進出兩次以上,故還是以陣列輸出。 + * + * @param int $activity_id 活動編號 + * @param int $target_id 標的編號 + * @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 "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":said", $activity_id); + $query->bindParam(":tid", $target_id); + $query->execute(); + + $output = array(); + + while($queryResult = $query->fetch()) { + array_push($output, $queryResult[0]); + } + return $output; + } +} diff --git a/htdocs/lib/Database/DBStudyActivity.php b/htdocs/lib/Database/DBStudyActivity.php index 9757ba3..c6bd534 100644 --- a/htdocs/lib/Database/DBStudyActivity.php +++ b/htdocs/lib/Database/DBStudyActivity.php @@ -54,6 +54,8 @@ class DBStudyActivity extends Database { * @param bool $learnStyle_force 拒絕前往非推薦的學習點 * @param bool $enable_virtual 是否啟用虛擬教材 * @param string $materialMode 教材模式 + * + * @return int 剛新增的活動編號 * @since 2.0.0 */ public function insertActivity($userId, $themeId, $startTime, $endTime, @@ -408,6 +410,8 @@ class DBStudyActivity extends Database { * @param bool $enable_virtual 是否啟用虛擬教材 * @param string $materialMode 教材模式 * @param string $isLock 是否鎖定不讓學生更改 + * + * @return int 剛新增的活動編號 * @since 2.0.0 */ public function insertWillActivity($userId, $themeId, $startTime, $expiredTime, diff --git a/htdocs/lib/Study/Exception.php b/htdocs/lib/Study/Exception.php index 01afc55..223a2bc 100644 --- a/htdocs/lib/Study/Exception.php +++ b/htdocs/lib/Study/Exception.php @@ -7,6 +7,54 @@ namespace UElearning\Exception; // TODO: 將以下類別濃縮 +/** + * 沒有找到此標的進出記錄 + * @since 2.0.0 + * @package UElearning + * @subpackage Study + */ +class StudyNoFoundException extends \UnexpectedValueException { + /** + * 指定的學習活動ID + * @type int + */ + private $id; + + /** + * 使用者帳號例外 + * @param int $id 輸入的標的ID + */ + public function __construct($id) { + $this->id = $id; + parent::__construct('No Study: '.$this->id); + } + + /** + * 取得輸入的標的ID + * @return int 標的ID + */ + public function getId() { + return $this->id; + } +} + +/** + * 正在學習中例外(難道使用者有分身?) + * @since 2.0.0 + * @package UElearning + * @subpackage Study + */ +class InLearningException extends \UnexpectedValueException { + + /** + * 使用者帳號例外 + * @param int $id 輸入的標的ID + */ + public function __construct($id) { + parent::__construct('Learning'); + } +} + /** * 沒有找到此活動 * @since 2.0.0 diff --git a/htdocs/lib/Study/Study.php b/htdocs/lib/Study/Study.php index e69de29..25405f2 100644 --- a/htdocs/lib/Study/Study.php +++ b/htdocs/lib/Study/Study.php @@ -0,0 +1,179 @@ +getQuery() 抓取資料表中所有資訊,並放在此陣列裡 + * @type array + */ + protected $queryResultArray; + + /** + * 從資料庫取得查詢 + * + * @throw \UElearning\Exception\ThemeNoFoundException + * @since 2.0.0 + */ + protected function getQuery(){ + + // 從資料庫查詢 + $db = new Database\DBStudy(); + $info = $db->queryById($this->id); + + // 判斷有沒有這個 + if( $info != null ) { + $this->queryResultArray = $info; + } + else throw new Exception\StudyNoFoundException($this->id); + } + + // ======================================================================== + + /** + * 建構子 + * + * @param int $inputTID 主題ID + * @since 2.0.0 + */ + public function __construct($inputID){ + $this->id = $inputID; + $this->getQuery(); + } + + // ======================================================================== + + /** + * 取得本進出記錄編號 + * + * @return int 進出記錄編號 + * @since 2.0.0 + */ + public function getId(){ + return $this->id; + } + + /** + * 取得本次活動編號 + * + * @return int 學習活動編號 + * @since 2.0.0 + */ + public function getActivityId(){ + return $this->queryResultArray['activity_id']; + } + + /** + * 取得標的編號 + * + * @return int 標的編號 + * @since 2.0.0 + */ + public function getTargetId(){ + return $this->queryResultArray['target_id']; + } + +// /** +// * 取得標的名稱 +// * +// * @return int 標的名稱 +// * @since 2.0.0 +// */ +// public function getTargetName(){ +// return $this->queryResultArray['target_id']; +// } + +// /** +// * 取得此紀錄的使用者ID +// * +// * @return string 使用者ID +// * @since 2.0.0 +// */ +// public function getUserId(){ +// return $this->queryResultArray['target_id']; +// } + + /** + * 是否為實體(實際抵達學習點) + * + * @return bool 是否為實體 + * @since 2.0.0 + */ + public function isEntity(){ + return $this->queryResultArray['is_entity']; + } + + /** + * 取得進入學習點時間 + * + * @return string 進入學習點時間 + * @since 2.0.0 + */ + public function isIn(){ + // TODO: 尚未測試 + if($this->queryResultArray['out_target_time'] == null) { + return true; + } + else { + return false; + } + } + + /** + * 取得進入學習點時間 + * + * @return string 進入學習點時間 + * @since 2.0.0 + */ + public function getInTime(){ + return $this->queryResultArray['in_target_time']; + } + + /** + * 取得離開學習點時間 + * + * @return string 離開學習點時間 + * @since 2.0.0 + */ + public function getOutTime(){ + return $this->queryResultArray['out_target_time']; + } + + /** + * 離開學習點 + * + * @since 2.0.0 + */ + public function toOut() { + $db = new Database\DBStudy(); + $db->toOutTaeget($this->id); + } +} diff --git a/htdocs/lib/Study/StudyManager.php b/htdocs/lib/Study/StudyManager.php new file mode 100644 index 0000000..0abcf12 --- /dev/null +++ b/htdocs/lib/Study/StudyManager.php @@ -0,0 +1,85 @@ +getCurrentInTargetId($activity_id); + } + + /** + * 取得目前已進入的學習點的進出紀錄物件 + * @param string $activity_id 活動編號 + * @return int 進出紀錄編號 + */ + public function getCurrentInStudyId($activity_id) { + + $db = new Database\DBStudy(); + return $db->getCurrentInStudyId($activity_id); + } + + /** + * 進入標的 + * + * @param string $activity_id 活動編號 + * @param string $target_id 標的編號 + * @param string $is_entity 是否為現場學習 + * return int 進出紀錄編號 + */ + public function toInTarget($activity_id, $target_id, $is_entity) { + + // 若沒有任一個點正在學習中 + 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(); + } + } + + /** + * 離開標的 + * + * @param string $activity_id 活動編號 + * @param string $target_id 標的編號 + * @param string $is_entity 是否為現場學習 + * return int 進出紀錄編號 + */ + 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(); + } + } +}