Add Hall & Area Class

This commit is contained in:
Yuan Chiu 2014-10-14 23:57:12 +08:00
parent 82e99814ff
commit a2abffc5fd
6 changed files with 667 additions and 6 deletions

View File

@ -13,9 +13,9 @@ require_once UELEARNING_LIB_ROOT.'/Database/Database.php';
require_once UELEARNING_LIB_ROOT.'/Database/Exception.php';
/**
* 使用者帳號資料表
* 學習標的資料表
*
* 對資料庫中的使用者資料表進行操作。
* 此檔案針對學習標的,以及學習標的的區域、廳等的資料表進行操作。
*
*
* @author Yuan Chiu <chyuaner@gmail.com>
@ -83,7 +83,7 @@ class DBTarget extends Database {
* echo '<pre>'; print_r($targetInfo); echo '</pre>';
*
*
* @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 '<pre>'; print_r($areaInfo); echo '</pre>';
*
*
* @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 '<pre>'; print_r($hallInfo); echo '</pre>';
*
*
* @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();
//}
}

183
htdocs/lib/Target/Area.php Normal file
View File

@ -0,0 +1,183 @@
<?php
/**
* 此位使用者的相關操作
*/
namespace UElearning\Target;
require_once UELEARNING_LIB_ROOT.'/Database/DBTarget.php';
require_once UELEARNING_LIB_ROOT.'/Target/Exception.php';
use UElearning\Database;
use UElearning\Exception;
/**
* 區域專用類別
*
* 一個物件即代表一塊區域
*
* 範例:
*
* try{
* $target = new Target\Area(3);
* echo $target->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'];
}
}

View File

@ -5,6 +5,8 @@
namespace UElearning\Exception;
// TODO: 將以下類別濃縮
/**
* 沒有找到此標的
* @since 2.0.0
@ -35,3 +37,65 @@ class TargetNoFoundException extends \UnexpectedValueException {
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
*/
public function getId() {
return $this->id;
}
}

151
htdocs/lib/Target/Hall.php Normal file
View File

@ -0,0 +1,151 @@
<?php
/**
* 廳專用類別
*/
namespace UElearning\Target;
require_once UELEARNING_LIB_ROOT.'/Database/DBTarget.php';
require_once UELEARNING_LIB_ROOT.'/Target/Exception.php';
use UElearning\Database;
use UElearning\Exception;
/**
* 廳專用類別
*
* 一個物件即代表一塊廳
*
* 使用範例:
*
* try{
* $target = new Target\Hall(1);
* echo $target->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'];
}
}

View File

@ -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;
}

0
htdocs/script/here Normal file
View File