diff --git a/htdocs/lib/Database/DBUser.php b/htdocs/lib/Database/DBUser.php index af6d4f7..a422bee 100644 --- a/htdocs/lib/Database/DBUser.php +++ b/htdocs/lib/Database/DBUser.php @@ -314,8 +314,8 @@ class DBUser extends Database { // 紀錄使用者帳號進資料庫 $sqlString = "INSERT INTO ".$this->table('AGroup'). - " (`GID`, `GName`, `GMemo`, `GAuth_Admin`, `GAuth_ClientAdmin`) - VALUES ( :id , :name, :memo , :auth_admin , :auth_clientAdmin )"; + " (`GID`, `GName`, `GMemo`, `GBuild_Time`, `GAuth_Admin`, `GAuth_ClientAdmin`) + VALUES ( :id , :name, :memo , NOW(), :auth_admin , :auth_clientAdmin )"; $query = $this->connDB->prepare($sqlString); $query->bindParam(":id", $gId); @@ -370,6 +370,7 @@ class DBUser extends Database { $result = array('group_id' => $thisResult['GID'], 'name' => $thisResult['GName'], 'memo' => $thisResult['GMemo'], + 'build_time' => $thisResult['GBuild_Time'], 'auth_admin' => $thisResult['GAuth_Admin'], 'auth_clientAdmin' => $thisResult['GAuth_ClientAdmin'] ); @@ -414,6 +415,7 @@ class DBUser extends Database { array( 'group_id' => $thisResult['GID'], 'name' => $thisResult['GName'], 'memo' => $thisResult['GMemo'], + 'build_time' => $thisResult['GBuild_Time'], 'auth_admin' => $thisResult['GAuth_Admin'], 'auth_clientAdmin' => $thisResult['GAuth_ClientAdmin']) ); @@ -426,5 +428,40 @@ class DBUser extends Database { } } + /** + * 修改一個群組的資料內容 + * + * 範例: + * + * $db = new Database\DBUser(); + * $db->changeGroupData('student', 'name', '學生'); + * + * @param string $uId 使用者名稱 + * @param string $field 欄位名稱 + * @param string $value 內容 + */ + public function changeGroupData($gId, $field, $value) { + // UPDATE `UElearning`.`chu__User` SET `UMemo` = '測試者' WHERE `chu__User`.`UID` = 'yuan'; + + $sqlField = null; + switch($field) { + case 'group_id': $sqlField = 'UID'; break; + case 'name': $sqlField = 'GName'; break; + case 'memo': $sqlField = 'GMemo'; break; + case 'auth_admin': $sqlField = 'GAuth_Admin'; break; + case 'auth_clientAdmin': $sqlField = 'GAuth_ClientAdmin'; break; + default: $sqlField = $field; break; + } + + + $sqlString = "UPDATE ".$this->table('AGroup'). + " SET `".$sqlField."` = :value". + " WHERE `GID` = :gid"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(':gid', $gId); + $query->bindParam(':value', $value); + $query->execute(); + } } \ No newline at end of file diff --git a/htdocs/lib/User/User.php b/htdocs/lib/User/User.php index 7e5d6f7..e466d11 100644 --- a/htdocs/lib/User/User.php +++ b/htdocs/lib/User/User.php @@ -83,8 +83,6 @@ class User { $this->getQuery(); } - - // ======================================================================== /** @@ -166,7 +164,7 @@ class User { } /** - * 取得所在群組顯式名稱 + * 取得所在群組顯示名稱 * * @return string 群組名稱 * @since 2.0.0 diff --git a/htdocs/lib/User/UserGroup.php b/htdocs/lib/User/UserGroup.php index 857b05f..f17f32b 100644 --- a/htdocs/lib/User/UserGroup.php +++ b/htdocs/lib/User/UserGroup.php @@ -4,4 +4,190 @@ */ namespace UElearning\User; -// TODO: write this code \ No newline at end of file + +require_once UELEARNING_LIB_ROOT.'/Database/DBUser.php'; +require_once UELEARNING_LIB_ROOT.'/User/Exception.php'; +require_once UELEARNING_LIB_ROOT.'/Exception.php'; +use UElearning\Database; + +/** + * 使用者群組類別 + * + * 一個物件即代表這一個使用者群組 + * + * 範例: + * + * try { + * $group = new User\UserGroup('testG'); + * echo $group->getName(); + * $group->setName('測試用'); + * echo $group->getName(); + * } + * catch (User\Exception\GroupNoFoundException $e) { + * echo 'No Found group: '. $e->getGroupId(); + * } + * + * @version 2.0.0 + * @package UElearning + * @subpackage User + */ +class UserGroup { + + /** + * 群組ID + * @type string + */ + protected $gId; + + // ======================================================================== + + /** + * 查詢到此帳號的所有資訊的結果 + * + * 由 $this->getQuery() 抓取資料表中所有資訊,並放在此陣列裡 + * @type array + */ + protected $queryResultArray; + + /** + * 從資料庫取得此群組查詢 + * + * @throw UElearning\User\Exception\GroupNoFoundException + * @since 2.0.0 + */ + protected function getQuery(){ + // 從資料庫查詢群組 + $db = new Database\DBUser(); + $groupInfo = $db->queryGroup($this->gId); + + // 判斷有沒有這個群組 + if( $groupInfo != null ) { + $this->queryResultArray = $groupInfo; + } + else throw new Exception\GroupNoFoundException($this->gId); + } + + /** + * 從資料庫更新此群組設定 + * + * @since 2.0.0 + */ + protected function setUpdate($field, $value){ + /// 將新設定寫進資料庫裡 + $db = new Database\DBUser(); + $db->changeGroupData($this->gId, $field, $value); + $this->getQuery(); + } + + // ======================================================================== + + /** + * 建構子 + * + * @param string $inputGID 群組ID + * @since 2.0.0 + */ + public function __construct($inputGID){ + $this->gId = $inputGID; + $this->getQuery(); + } + + // ======================================================================== + + /** + * 取得群組ID + * + * @return string 群組ID + * @since 2.0.0 + */ + public function getID(){ + return $this->gId; + } + + // ------------------------------------------------------------------------ + + /** + * 取得帳號建立時間 + * + * @return string 建立時間 + * @since 2.0.0 + */ + public function getCreateTime(){ + return $this->queryResultArray['build_time']; + } + // ======================================================================== + + /** + * 取得群組顯示名稱 + * + * @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 getMemo(){ + return $this->queryResultArray['memo']; + } + + /** + * 修改帳號備註資訊 + * + * @param string $input 新的帳號備註資訊 + * @since 2.0.0 + */ + public function setMemo($input){ + $this->setUpdate('memo', $input); + } + + // ======================================================================== + + /** + * 取得權限清單 + * + * @return array 權限清單 + */ + public function getPermissionList() { + // TODO: 取得權限清單 + + } + + /** + * 是否擁有此權限 + * + * @param string $permissionName 權限名稱 + * @return bool 是否擁有 + */ + public function havePermission($permissionName) { + // TODO: 是否擁有此權限 + } + + /** + * 設定擁有此權限 + * + * @param string $permissionName 權限名稱 + * @return bool 是否擁有 + */ + public function setPermission($permissionName, $setBool) { + // TODO: 設定擁有此權限 + } + +} \ No newline at end of file diff --git a/tests/Database/DBUserTest.php b/tests/Database/DBUserTest.php index ca065e3..23b35ed 100644 --- a/tests/Database/DBUserTest.php +++ b/tests/Database/DBUserTest.php @@ -164,6 +164,33 @@ class DBUserTest extends \PHPUnit_Framework_TestCase $this->assertEquals($info['auth_clientAdmin'], $auth_clientAdmin); } + /** + * 測試修改群組 + * + * @dataProvider groupDataProvider + */ + public function testChangeGroup($gId, $name, $memo, $auth_admin, $auth_clientAdmin){ + $afterData = 'sfisjojjoij'; + + // 記下更改前的資料 + $info = $this->db->queryGroup($gId); + $beforeData = $info['memo']; + + // 更改資料 + $this->db->changeGroupData($gId, 'memo', $afterData); + + // 檢查更改後的結果 + $info = $this->db->queryGroup($gId); + $this->assertEquals($info['memo'], $afterData); + + // 改回來 + $this->db->changeGroupData($gId, 'memo', $beforeData); + + // 檢查有沒有改回來 + $info = $this->db->queryGroup($gId); + $this->assertEquals($info['memo'], $beforeData); + } + /** * 測試移除使用者 *