Add Target Class
This commit is contained in:
parent
8a80c47bd9
commit
82e99814ff
@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* 學習標的資料表
|
||||
*
|
||||
* 此檔案針對學習標的,以及學習標的的區域、廳等的資料庫查詢用。
|
||||
*/
|
||||
|
||||
namespace UElearning\Database;
|
||||
|
||||
use UElearning\Exception;
|
||||
|
||||
require_once UELEARNING_LIB_ROOT.'/Database/Database.php';
|
||||
require_once UELEARNING_LIB_ROOT.'/Database/Exception.php';
|
||||
|
||||
/**
|
||||
* 使用者帳號資料表
|
||||
*
|
||||
* 對資料庫中的使用者資料表進行操作。
|
||||
*
|
||||
*
|
||||
* @author Yuan Chiu <chyuaner@gmail.com>
|
||||
* @version 2.0.0
|
||||
* @package UElearning
|
||||
* @subpackage Database
|
||||
*/
|
||||
class DBTarget extends Database {
|
||||
|
||||
private function queryTargetByWhere($where) {
|
||||
|
||||
$sqlString = "SELECT `TID`, Target.`AID`, Area.`HID`, ".
|
||||
"`TNum`, `TName`, `TMapID`, `TLearnTime`, `PLj`, `Mj`, `S`, `Fi` ".
|
||||
"FROM `".$this->table('Target')."` as Target ".
|
||||
"LEFT JOIN `".$this->table('Area')."` as Area ".
|
||||
"ON Area.`AID` = Target.`AID` ".
|
||||
"WHERE ".$where;
|
||||
|
||||
$query = $this->connDB->prepare($sqlString);
|
||||
$query->execute();
|
||||
|
||||
$queryResultAll = $query->fetchAll();
|
||||
// 如果有查到一筆以上
|
||||
if( count($queryResultAll) >= 1 ) {
|
||||
// 製作回傳結果陣列
|
||||
$result = array();
|
||||
foreach($queryResultAll as $key => $thisResult) {
|
||||
|
||||
array_push($result,
|
||||
array( 'target_id' => $thisResult['TID'],
|
||||
'area_id' => $thisResult['AID'],
|
||||
'hall_id' => $thisResult['HID'],
|
||||
'target_number' => $thisResult['TNum'],
|
||||
'name' => $thisResult['TName'],
|
||||
'map_url' => $thisResult['TMapID'],
|
||||
'learn_time' => $thisResult['TLearnTime'],
|
||||
'PLj' => $thisResult['PLj'],
|
||||
'Mj' => $thisResult['Mj'],
|
||||
'S' => $thisResult['S'],
|
||||
'Fi' => $thisResult['Fi']
|
||||
));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
// 若都沒查到的話
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查詢一個標的資料
|
||||
*
|
||||
*
|
||||
* 範例:
|
||||
*
|
||||
* require_once __DIR__.'/../config.php';
|
||||
* require_once UELEARNING_LIB_ROOT.'/Database/DBTarget.php';
|
||||
* use UElearning\Database;
|
||||
*
|
||||
* $db = new Database\DBTarget();
|
||||
*
|
||||
* $targetInfo = $db->queryTarget(4);
|
||||
* echo '<pre>'; print_r($targetInfo); echo '</pre>';
|
||||
*
|
||||
*
|
||||
* @param string $tId 標的ID
|
||||
* @return array 標的資料陣列,格式為:
|
||||
* array(
|
||||
* 'target_id' => <標的ID>,
|
||||
* 'area_id' => <標的所在的區域ID>,
|
||||
* 'hall_id' => <標的所在的廳ID>,
|
||||
* 'target_number' => <地圖上的標的編號>,
|
||||
* 'name' => <標的名稱>,
|
||||
* 'map_url' => <地圖路徑>,
|
||||
* 'learn_time' => <預估的學習時間>,
|
||||
* 'PLj' => <學習標的的人數限制>,
|
||||
* 'Mj' => <目前人數>,
|
||||
* 'S' => <學習標的飽和率上限>,
|
||||
* 'Fi' => <學習標的滿額指標>
|
||||
* );
|
||||
*
|
||||
*/
|
||||
public function queryTarget($tId) {
|
||||
|
||||
$queryResultAll = $this->queryTargetByWhere("`TID`=".$this->connDB->quote($tId));
|
||||
|
||||
// 如果有查到一筆以上
|
||||
if( count($queryResultAll) >= 1 ) {
|
||||
return $queryResultAll[0];
|
||||
}
|
||||
// 若都沒查到的話
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查詢所有標的資料
|
||||
*
|
||||
* @return array 標的資料陣列,格式為:
|
||||
*
|
||||
* array(
|
||||
* array(
|
||||
* 'target_id' => <標的ID>,
|
||||
* 'area_id' => <標的所在的區域ID>,
|
||||
* 'hall_id' => <標的所在的廳ID>,
|
||||
* 'target_number' => <地圖上的標的編號>,
|
||||
* 'name' => <標的名稱>,
|
||||
* 'map_url' => <地圖路徑>,
|
||||
* 'learn_time' => <預估的學習時間>,
|
||||
* 'PLj' => <學習標的的人數限制>,
|
||||
* 'Mj' => <目前人數>,
|
||||
* 'S' => <學習標的飽和率上限>,
|
||||
* 'Fi' => <學習標的滿額指標>
|
||||
* )
|
||||
* );
|
||||
*
|
||||
*/
|
||||
public function queryAllTarget() {
|
||||
|
||||
return $this->queryTargetByWhere("1");
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改一個標的資訊
|
||||
*
|
||||
* @param int $tId 標的編號
|
||||
* @param string $field 欄位名稱
|
||||
* @param string $value 內容
|
||||
*/
|
||||
function changeTargetData($tId, $field, $value) {
|
||||
$sqlField = null;
|
||||
switch($field) {
|
||||
case 'area_id': $sqlField = 'AID'; break;
|
||||
case 'hall_id': $sqlField = 'HID'; break;
|
||||
case 'target_number': $sqlField = 'TNum'; break;
|
||||
case 'name': $sqlField = 'TName'; break;
|
||||
case 'map_url': $sqlField = 'TMapID'; break;
|
||||
case 'learn_time': $sqlField = 'TLearnTime'; break;
|
||||
case 'PLj': $sqlField = 'PLj'; break;
|
||||
case 'Mj': $sqlField = 'Mj'; break;
|
||||
case 'S': $sqlField = 'S'; break;
|
||||
case 'Fi': $sqlField = 'Fi'; break;
|
||||
default: $sqlField = $field; break;
|
||||
}
|
||||
|
||||
|
||||
$sqlString = "UPDATE ".$this->table('Target').
|
||||
" SET `".$sqlField."` = :value".
|
||||
" WHERE `TID` = :tid";
|
||||
|
||||
$query = $this->connDB->prepare($sqlString);
|
||||
$query->bindParam(':tid', $tId);
|
||||
$query->bindParam(':value', $value);
|
||||
$query->execute();
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* 使用者帳號群組相關例外類別檔案
|
||||
*/
|
||||
|
||||
namespace UElearning\Exception;
|
||||
|
||||
/**
|
||||
* 沒有找到此標的
|
||||
* @since 2.0.0
|
||||
* @package UElearning
|
||||
* @subpackage Target
|
||||
*/
|
||||
class TargetNoFoundException extends \UnexpectedValueException {
|
||||
/**
|
||||
* 指定的標的ID
|
||||
* @type int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* 使用者帳號例外
|
||||
* @param int $id 輸入的標的ID
|
||||
*/
|
||||
public function __construct($id) {
|
||||
$this->id = $id;
|
||||
parent::__construct('No Target: '.$this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得輸入的標的ID
|
||||
* @return int 標的ID
|
||||
*/
|
||||
public function getId() {
|
||||
return $this->id;
|
||||
}
|
||||
}
|
@ -5,8 +5,8 @@
|
||||
|
||||
namespace UElearning\Target;
|
||||
|
||||
require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php';
|
||||
require_once UELEARNING_LIB_ROOT.'/Exception.php';
|
||||
require_once UELEARNING_LIB_ROOT.'/Database/DBTarget.php';
|
||||
require_once UELEARNING_LIB_ROOT.'/Target/Exception.php';
|
||||
use UElearning\Database;
|
||||
use UElearning\Exception;
|
||||
|
||||
@ -15,6 +15,34 @@ use UElearning\Exception;
|
||||
*
|
||||
* 一個物件即代表一個標的
|
||||
*
|
||||
* 使用範例:
|
||||
*
|
||||
* require_once __DIR__.'/../config.php';
|
||||
* require_once UELEARNING_LIB_ROOT.'/Target/Target.php';
|
||||
* use UElearning\Target;
|
||||
* use UElearning\Exception;
|
||||
*
|
||||
* try{
|
||||
* $target = new Target\Target(3);
|
||||
* echo $target->getId();
|
||||
* echo $target->getAreaId();
|
||||
* echo $target->getHallId();
|
||||
* echo $target->getNumber();
|
||||
* echo $target->getName();
|
||||
* echo $target->getMapUrl();
|
||||
* echo $target->getLearnTime();
|
||||
* echo $target->getPLj();
|
||||
* echo $target->getMj();
|
||||
* echo $target->isFullPeople();
|
||||
* echo $target->getVacancyPeople();
|
||||
* echo $target->getS();
|
||||
* echo $target->getFi();
|
||||
*
|
||||
* }
|
||||
* catch (Exception\TargetNoFoundException $e) {
|
||||
* echo 'No Found target: '. $e->getId();
|
||||
* }
|
||||
*
|
||||
* @version 2.0.0
|
||||
* @package UElearning
|
||||
* @subpackage Target
|
||||
@ -40,20 +68,19 @@ class Target {
|
||||
/**
|
||||
* 從資料庫取得此標的查詢
|
||||
*
|
||||
* @throw UElearning\Exception\UserNoFoundException
|
||||
* @throw UElearning\Exception\TargetNoFoundException
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected function getQuery(){
|
||||
// TODO: getQuery
|
||||
// // 從資料庫查詢使用者
|
||||
// $db = new Database\DBUser();
|
||||
// $userInfo = $db->queryUser($this->uId);
|
||||
//
|
||||
// // 判斷有沒有這位使用者
|
||||
// if( $userInfo != null ) {
|
||||
// $this->queryResultArray = $userInfo;
|
||||
// }
|
||||
// else throw new Exception\UserNoFoundException($this->uId);
|
||||
// 從資料庫查詢使用者
|
||||
$db = new Database\DBTarget();
|
||||
$targetInfo = $db->queryTarget($this->tId);
|
||||
|
||||
// 判斷有沒有這位使用者
|
||||
if( $targetInfo != null ) {
|
||||
$this->queryResultArray = $targetInfo;
|
||||
}
|
||||
else throw new Exception\TargetNoFoundException($this->tId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,11 +89,10 @@ class Target {
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected function setUpdate($field, $value){
|
||||
// TODO: setUpdate
|
||||
// /// 將新設定寫進資料庫裡
|
||||
// $db = new Database\DBUser();
|
||||
// $db->changeUserData($this->uId, $field, $value);
|
||||
// $this->getQuery();
|
||||
// 將新設定寫進資料庫裡
|
||||
$db = new Database\DBTarget();
|
||||
$db->changeTargetData($this->tId, $field, $value);
|
||||
$this->getQuery();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
@ -122,7 +148,7 @@ class Target {
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function getNumber(){
|
||||
return $this->queryResultArray['number'];
|
||||
return $this->queryResultArray['target_number'];
|
||||
}
|
||||
// ========================================================================
|
||||
|
||||
@ -200,6 +226,31 @@ class Target {
|
||||
return $this->queryResultArray['Mj'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 設定學習標的目前人數
|
||||
*
|
||||
* @param int $number 學習標的目前人數
|
||||
* @since 2.0.0
|
||||
*/
|
||||
function setMj($number){
|
||||
$this->setUpdate('Mj', $number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加學習標的目前人數
|
||||
*
|
||||
* 若要減少可直接在參數內帶入負值
|
||||
*
|
||||
* @param int $number 學習標的目前人數調整值
|
||||
* @return int 學習標的目前人數
|
||||
* @since 2.0.0
|
||||
*/
|
||||
function addMj($number){
|
||||
$setedNum = $this->queryResultArray['Mj']+$number;
|
||||
if($setedNum < 0) $setedNum = 0;
|
||||
$this->setUpdate('Mj', $setedNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得學習標的目前人數
|
||||
*
|
||||
@ -216,22 +267,20 @@ class Target {
|
||||
* @param int $number 學習標的目前人數
|
||||
* @since 2.0.0
|
||||
*/
|
||||
function setMj($number){
|
||||
//return $this->queryResultArray['Mj'];
|
||||
public function setCurrentPeople($number){
|
||||
$this->setMj($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 設定學習標的目前人數
|
||||
* 取得此標的還剩下人可容納
|
||||
*
|
||||
* @param int $number 學習標的目前人數
|
||||
* @return int 此標的還剩下人可容納
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function setCurrentPeople($number){
|
||||
//return $this->getMj();
|
||||
public function getVacancyPeople() {
|
||||
return $this->getPLj() - $this->getMj();
|
||||
}
|
||||
|
||||
// TODO: 加人數、減人數
|
||||
|
||||
/**
|
||||
* 目前此標的人數是否已滿
|
||||
*
|
||||
@ -239,7 +288,8 @@ class Target {
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function isFullPeople(){
|
||||
// TODO: isFull
|
||||
if($this->getPLj()-$this->getMj() <= 0) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user