From a2abffc5fd9ebed61602f311af33a75212de4a30 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Tue, 14 Oct 2014 23:57:12 +0800 Subject: [PATCH] Add Hall & Area Class --- htdocs/lib/Database/DBTarget.php | 271 ++++++++++++++++++++++++++++++- htdocs/lib/Target/Area.php | 183 +++++++++++++++++++++ htdocs/lib/Target/Exception.php | 64 ++++++++ htdocs/lib/Target/Hall.php | 151 +++++++++++++++++ htdocs/lib/Target/Target.php | 4 +- htdocs/script/here | 0 6 files changed, 667 insertions(+), 6 deletions(-) create mode 100644 htdocs/lib/Target/Area.php create mode 100644 htdocs/lib/Target/Hall.php create mode 100644 htdocs/script/here diff --git a/htdocs/lib/Database/DBTarget.php b/htdocs/lib/Database/DBTarget.php index d465cb1..a446f68 100644 --- a/htdocs/lib/Database/DBTarget.php +++ b/htdocs/lib/Database/DBTarget.php @@ -13,9 +13,9 @@ require_once UELEARNING_LIB_ROOT.'/Database/Database.php'; require_once UELEARNING_LIB_ROOT.'/Database/Exception.php'; /** - * 使用者帳號資料表 + * 學習標的資料表 * - * 對資料庫中的使用者資料表進行操作。 + * 此檔案針對學習標的,以及學習標的的區域、廳等的資料表進行操作。 * * * @author Yuan Chiu @@ -83,7 +83,7 @@ class DBTarget extends Database { * echo '
'; print_r($targetInfo); echo '
'; * * - * @param string $tId 標的ID + * @param int $tId 標的ID * @return array 標的資料陣列,格式為: * array( * 'target_id' => <標的ID>, @@ -138,7 +138,7 @@ class DBTarget extends Database { */ public function queryAllTarget() { - return $this->queryTargetByWhere("1"); + return $this->queryAreaByWhere("1"); } /** @@ -174,4 +174,267 @@ class DBTarget extends Database { $query->bindParam(':value', $value); $query->execute(); } + + // ======================================================================== + + private function queryAreaByWhere($where) { + + $sqlString = "SELECT * FROM `".$this->table('Area')."`". + "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( 'area_id' => $thisResult['AID'], + 'hall_id' => $thisResult['HID'], + 'floor' => $thisResult['AFloor'], + 'area_number' => $thisResult['ANum'], + 'name' => $thisResult['AName'], + 'map_url' => $thisResult['AMapID'], + 'introduction' => $thisResult['AIntroduction'] + )); + } + 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(); + * + * $areaInfo = $db->queryArea(4); + * echo '
'; print_r($areaInfo); echo '
'; + * + * + * @param int $aId 區域ID + * @return array 區域資料陣列,格式為: + * array( + * 'area_id' => <區域ID>, + * 'hall_id' => <區域所在的廳ID>, + * 'floor' => <區域所在的樓層>, + * 'number' => <地圖上的區域編號>, + * 'name' => <區域名稱>, + * 'map_url' => <地圖路徑>, + * 'introduction' => <區域簡介> + * ); + * + */ + public function queryArea($aId) { + + $queryResultAll = $this->queryAreaByWhere("`AID`=".$this->connDB->quote($aId)); + + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + return $queryResultAll[0]; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 查詢所有區域資料 + * + * @return array 區域資料陣列,格式為: + * + * array( + * array( + * 'area_id' => <區域ID>, + * 'hall_id' => <區域所在的廳ID>, + * 'floor' => <區域所在的樓層>, + * 'number' => <地圖上的區域編號>, + * 'name' => <區域名稱>, + * 'map_url' => <地圖路徑>, + * 'introduction' => <區域簡介> + * ) + * ); + * + */ + public function queryAllArea() { + + return $this->queryAreaByWhere("1"); + } + + ///** + // * 修改一個標的資訊 + // * + // * @param int $tId 標的編號 + // * @param string $field 欄位名稱 + // * @param string $value 內容 + // */ + //function changeTargetData($tId, $field, $value) { + ////TODO: 待修成Area + // $sqlField = null; + // switch($field) { + // case 'area_id': $sqlField = 'AID'; break; + // case 'hall_id': $sqlField = 'HID'; break; + // case 'floor': $sqlField = 'AFloor'; 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(); + //} + + // ======================================================================== + + private function queryHallByWhere($where) { + + $sqlString = "SELECT * FROM `".$this->table('Hall')."`". + "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( 'hall_id' => $thisResult['HID'], + 'name' => $thisResult['HName'], + 'map_url' => $thisResult['HMapID'], + 'introduction' => $thisResult['HIntroduction'] + )); + } + 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(); + * + * $hallInfo = $db->queryHall(1); + * echo '
'; print_r($hallInfo); echo '
'; + * + * + * @param int $hId 廳ID + * @return array 區域資料陣列,格式為: + * array( + * 'hall_id' => <廳的ID>, + * 'name' => <廳名稱>, + * 'map_url' => <地圖路徑>, + * 'introduction' => <區域簡介> + * ); + * + */ + public function queryHall($hId) { + + $queryResultAll = $this->queryHallByWhere("`HID`=".$this->connDB->quote($hId)); + + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + return $queryResultAll[0]; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 查詢所有區域資料 + * + * @return array 區域資料陣列,格式為: + * + * array( + * array( + * 'hall_id' => <廳的廳ID>, + * 'name' => <廳名稱>, + * 'map_url' => <地圖路徑>, + * 'introduction' => <區域簡介> + * ); + * + */ + public function queryAllHall() { + + return $this->queryHallByWhere("1"); + } + + ///** + // * 修改一個標的資訊 + // * + // * @param int $tId 標的編號 + // * @param string $field 欄位名稱 + // * @param string $value 內容 + // */ + //function changeTargetData($tId, $field, $value) { + ////TODO: 待修成Area + // $sqlField = null; + // switch($field) { + // case 'area_id': $sqlField = 'AID'; break; + // case 'hall_id': $sqlField = 'HID'; break; + // case 'floor': $sqlField = 'AFloor'; 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(); + //} + } \ No newline at end of file diff --git a/htdocs/lib/Target/Area.php b/htdocs/lib/Target/Area.php new file mode 100644 index 0000000..04a7964 --- /dev/null +++ b/htdocs/lib/Target/Area.php @@ -0,0 +1,183 @@ +getId(); + * echo $target->getHallId(); + * echo $target->getFloor(); + * echo $target->getNumber(); + * echo $target->getName(); + * echo $target->getMapUrl(); + * + * } + * catch (Exception\AreaNoFoundException $e) { + * echo 'No Found area: '. $e->getId(); + * } + * + * @version 2.0.0 + * @package UElearning + * @subpackage Target + */ +class Area { + + /** + * 標的ID + * @type int + */ + protected $aId; + + // ------------------------------------------------------------------------ + + /** + * 查詢到此標的的所有資訊的結果 + * + * 由 $this->getQuery() 抓取資料表中所有資訊,並放在此陣列裡 + * @type array + */ + protected $queryResultArray; + + /** + * 從資料庫取得此標的查詢 + * + * @throw UElearning\Exception\AreaNoFoundException + * @since 2.0.0 + */ + protected function getQuery(){ + // 從資料庫查詢 + $db = new Database\DBTarget(); + $areaInfo = $db->queryArea($this->aId); + + // 判斷有沒有這個 + if( $areaInfo != null ) { + $this->queryResultArray = $areaInfo; + } + else throw new Exception\AreaNoFoundException($this->aId); + } + + /** + * 從資料庫更新此標的設定 + * + * @since 2.0.0 + */ + protected function setUpdate($field, $value){ + // 將新設定寫進資料庫裡 + //$db = new Database\DBTarget(); + //$db->changeTargetData($this->tId, $field, $value); + //$this->getQuery(); + } + + // ======================================================================== + + /** + * 建構子 + * + * @param int $inputAID 區域ID + * @since 2.0.0 + */ + public function __construct($inputAID){ + $this->aId = $inputAID; + $this->getQuery(); + } + + // ======================================================================== + + /** + * 取得區域ID + * + * @return int 區域ID + * @since 2.0.0 + */ + public function getId(){ + return $this->aId; + } + + /** + * 取得區域所在的廳ID + * + * @return int 區域所在的廳ID + * @since 2.0.0 + */ + public function getHallId(){ + return $this->queryResultArray['hall_id']; + } + + /** + * 取得區域所在樓層 + * + * @return int 區域所在樓層 + * @since 2.0.0 + */ + public function getFloor(){ + return $this->queryResultArray['floor']; + } + + /** + * 取得區域地圖上的編號 + * + * @return int 地圖上的區域編號 + * @since 2.0.0 + */ + public function getNumber(){ + return $this->queryResultArray['area_number']; + } + + /** + * 取得區域名稱 + * + * @return string 區域名稱 + * @since 2.0.0 + */ + public function getName(){ + return $this->queryResultArray['name']; + } + + ///** + // * 設定區域名稱 + // * + // * @param string $name 區域名稱 + // * @since 2.0.0 + // */ + //public function setName($name){ + // $this->setUpdate('name', $name); + //} + + // ======================================================================== + + /** + * 取得區域的地圖圖片檔路徑 + * + * @return string 地圖圖片檔路徑 + * @since 2.0.0 + */ + public function getMapUrl(){ + return $this->queryResultArray['map_url']; + } + + /** + * 取得區域簡介 + * + * @return string 區域簡介 + * @since 2.0.0 + */ + public function getIntroduction(){ + return $this->queryResultArray['introduction']; + } + +} \ No newline at end of file diff --git a/htdocs/lib/Target/Exception.php b/htdocs/lib/Target/Exception.php index 195a167..edd986e 100644 --- a/htdocs/lib/Target/Exception.php +++ b/htdocs/lib/Target/Exception.php @@ -5,6 +5,8 @@ namespace UElearning\Exception; +// TODO: 將以下類別濃縮 + /** * 沒有找到此標的 * @since 2.0.0 @@ -27,6 +29,68 @@ class TargetNoFoundException extends \UnexpectedValueException { parent::__construct('No Target: '.$this->id); } + /** + * 取得輸入的標的ID + * @return int 標的ID + */ + public function getId() { + return $this->id; + } +} + +/** + * 沒有找到此區域 + * @since 2.0.0 + * @package UElearning + * @subpackage Target + */ +class AreaNoFoundException 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; + } +} + +/** + * 沒有找到此廳 + * @since 2.0.0 + * @package UElearning + * @subpackage Target + */ +class HallNoFoundException 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 diff --git a/htdocs/lib/Target/Hall.php b/htdocs/lib/Target/Hall.php new file mode 100644 index 0000000..1245fb2 --- /dev/null +++ b/htdocs/lib/Target/Hall.php @@ -0,0 +1,151 @@ +getId(); + * echo $target->getName(); + * echo $target->getMapUrl(); + * echo $target->getIntroduction(); + * + * } + * catch (Exception\HallNoFoundException $e) { + * echo 'No Found area: '. $e->getId(); + * } + * + * @version 2.0.0 + * @package UElearning + * @subpackage Target + */ +class Hall { + + /** + * 廳ID + * @type int + */ + protected $hId; + + // ------------------------------------------------------------------------ + + /** + * 查詢到此廳的所有資訊的結果 + * + * 由 $this->getQuery() 抓取資料表中所有資訊,並放在此陣列裡 + * @type array + */ + protected $queryResultArray; + + /** + * 從資料庫取得此廳查詢 + * + * @throw UElearning\Exception\HallNoFoundException + * @since 2.0.0 + */ + protected function getQuery(){ + // 從資料庫查詢 + $db = new Database\DBTarget(); + $hallInfo = $db->queryHall($this->hId); + + // 判斷有沒有這個 + if( $hallInfo != null ) { + $this->queryResultArray = $hallInfo; + } + else throw new Exception\HallNoFoundException($this->hId); + } + + /** + * 從資料庫更新此標的設定 + * + * @since 2.0.0 + */ + protected function setUpdate($field, $value){ + // 將新設定寫進資料庫裡 + //$db = new Database\DBTarget(); + //$db->changeTargetData($this->tId, $field, $value); + //$this->getQuery(); + } + + // ======================================================================== + + /** + * 建構子 + * + * @param int $inputAID 區域ID + * @since 2.0.0 + */ + public function __construct($inputHID){ + $this->hId = $inputHID; + $this->getQuery(); + } + + // ======================================================================== + + /** + * 取得廳ID + * + * @return int 廳ID + * @since 2.0.0 + */ + public function getId(){ + return $this->hId; + } + + /** + * 取得廳名稱 + * + * @return string 廳名稱 + * @since 2.0.0 + */ + public function getName(){ + return $this->queryResultArray['name']; + } + + ///** + // * 設定區域名稱 + // * + // * @param string $name 區域名稱 + // * @since 2.0.0 + // */ + //public function setName($name){ + // $this->setUpdate('name', $name); + //} + + // ======================================================================== + + /** + * 取得區域的地圖圖片檔路徑 + * + * @return string 地圖圖片檔路徑 + * @since 2.0.0 + */ + public function getMapUrl(){ + return $this->queryResultArray['map_url']; + } + + /** + * 取得區域簡介 + * + * @return string 區域簡介 + * @since 2.0.0 + */ + public function getIntroduction(){ + return $this->queryResultArray['introduction']; + } + +} \ No newline at end of file diff --git a/htdocs/lib/Target/Target.php b/htdocs/lib/Target/Target.php index 08035b3..515c820 100644 --- a/htdocs/lib/Target/Target.php +++ b/htdocs/lib/Target/Target.php @@ -72,11 +72,11 @@ class Target { * @since 2.0.0 */ protected function getQuery(){ - // 從資料庫查詢使用者 + // 從資料庫查詢此標的 $db = new Database\DBTarget(); $targetInfo = $db->queryTarget($this->tId); - // 判斷有沒有這位使用者 + // 判斷有沒有這個標的 if( $targetInfo != null ) { $this->queryResultArray = $targetInfo; } diff --git a/htdocs/script/here b/htdocs/script/here new file mode 100644 index 0000000..e69de29