From 20bbd0c2bc5113863eda34cd2a921238c1d5322a Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Mon, 29 Dec 2014 04:08:47 +0800 Subject: [PATCH] =?UTF-8?q?Add=20Target:=20=E5=8F=96=E5=BE=97=E6=95=99?= =?UTF-8?q?=E6=9D=90=E8=B7=AF=E5=BE=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/lib/Database/DBMaterial.php | 153 +++++++++++++++++++++++++++++ htdocs/lib/Target/Target.php | 32 ++++++ 2 files changed, 185 insertions(+) create mode 100644 htdocs/lib/Database/DBMaterial.php diff --git a/htdocs/lib/Database/DBMaterial.php b/htdocs/lib/Database/DBMaterial.php new file mode 100644 index 0000000..9d3f0a0 --- /dev/null +++ b/htdocs/lib/Database/DBMaterial.php @@ -0,0 +1,153 @@ + + * @version 2.0.0 + * @package UElearning + * @subpackage Database + */ +class DBMaterial extends Database { + + /** + * 內部使用的查詢動作 + * @param string $where 查詢語法 + * @return array 查詢結果陣列 + */ + protected function queryMaterialByWhere($where) { + + $sqlString = "SELECT * FROM `".$this->table('Material')."` ". + "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['MEntity'] != '0') { + $output_entiry = true; + } + else { $output_entiry = false; } + + array_push($result, + array( 'material_id' => $thisResult['MID'], + 'target_id' => $thisResult['TID'], + 'is_entity' => $output_entiry, + 'mode' => $thisResult['MMode'], + 'url' => $thisResult['MUrl'] + )); + } + return $result; + } + // 若都沒查到的話 + else { + return null; + } + } + + + /** + * 查詢一個標的資料 + * + * + * 範例: + * + * require_once __DIR__.'/../config.php'; + * require_once UELEARNING_LIB_ROOT.'/Database/DBMaterial.php'; + * use UElearning\Database; + * + * $db = new Database\DBMaterial(); + * $materialInfo = $db->queryMaterial(1); + * echo '
'; print_r($materialInfo); echo '
'; + * + * + * @param int $mId 教材ID + * @return array 教材資料陣列,格式為: + * array( + * 'material_id' => <教材ID>, + * 'target_id' => <標的ID>, + * 'is_entity' => <是否為實體教材>, + * 'mode' => <教材類型>, + * 'url' => <此標的教材路徑> + * ); + * + */ + public function queryMaterial($mId) { + + $queryResultAll = $this->queryMaterialByWhere("`MID`=".$this->connDB->quote($mId)); + + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + return $queryResultAll[0]; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 查詢此標的內所教材的資料 + * + * @param int $tID 標的ID + * @return array 教材資料陣列,格式為: + * + * array( + * array( + * 'material_id' => <教材ID>, + * 'target_id' => <標的ID>, + * 'is_entity' => <是否為實體教材>, + * 'mode' => <教材類型>, + * 'url' => <此標的教材路徑> + * ); + * ); + * + */ + public function queryAllMaterialByTargetId($tID) { + + return $this->queryMaterialByWhere("`TID`=".$this->connDB->quote($tID)); + } + + /** + * 查詢所有教材資料 + * + * @return array 教材資料陣列,格式為: + * + * array( + * array( + * 'material_id' => <教材ID>, + * 'target_id' => <標的ID>, + * 'is_entity' => <是否為實體教材>, + * 'mode' => <教材類型>, + * 'url' => <此標的教材路徑> + * ); + * ); + * + */ + public function queryAllMaterial() { + + return $this->queryMaterialByWhere("1"); + } + +} diff --git a/htdocs/lib/Target/Target.php b/htdocs/lib/Target/Target.php index 74d6c40..c7b8f76 100644 --- a/htdocs/lib/Target/Target.php +++ b/htdocs/lib/Target/Target.php @@ -6,6 +6,7 @@ namespace UElearning\Target; require_once UELEARNING_LIB_ROOT.'/Database/DBTarget.php'; +require_once UELEARNING_LIB_ROOT.'/Database/DBMaterial.php'; require_once UELEARNING_LIB_ROOT.'/Target/Exception.php'; use UElearning\Database; use UElearning\Exception; @@ -184,6 +185,37 @@ class Target { return $this->queryResultArray['map_url']; } + /** + * 取得標的的教材路徑 + * + * @param bool $isEntity 是否為實體教材 + * @param string $mode 教材種類 + * @return string 教材檔案路徑 + * @since 2.0.0 + */ + public function getMaterialUrl($isEntity, $mode){ + + $db = new Database\DBMaterial(); + $query = $db->queryAllMaterialByTargetId($this->tId); + + foreach($query as $thisData) { + + if($thisData['is_entity'] != 0) { + $thisEntiry = true; + } + else { $thisEntiry = false; } + + if($thisEntiry==$isEntity && $thisData['mode'] == $mode) { + return $thisData['url']; + break; + } + } + + return null; + } + + // ======================================================================== + /** * 取得預估的學習時間 *