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; + } + + // ======================================================================== + /** * 取得預估的學習時間 *