diff --git a/htdocs/lib/Database/DBUser.php b/htdocs/lib/Database/DBUser.php index a422bee..f5d9aa9 100644 --- a/htdocs/lib/Database/DBUser.php +++ b/htdocs/lib/Database/DBUser.php @@ -174,12 +174,17 @@ class DBUser extends Database { if( count($queryResultAll) >= 1 ) { $queryResult = $queryResultAll[0]; + if($queryResult['UEnabled'] != '0') { + $output_enable = true; + } + else { $output_enable = false; } + $result = array( 'user_id' => $queryResult['UID'], 'password' => $queryResult['UPassword'], 'group_id' => $queryResult['GID'], 'class_id' => $queryResult['CID'], - 'enable' => $queryResult['UEnabled'], + 'enable' => $output_enable, 'build_time' => $queryResult['UBuild_Time'], 'learnStyle_mode' => $queryResult['LMode'], 'material_mode' => $queryResult['MMode'], @@ -233,12 +238,18 @@ class DBUser extends Database { // 製作回傳結果陣列 $result = array(); foreach($queryResultAll as $key => $thisResult) { + + if($thisResult['UEnabled'] != '0') { + $output_enable = true; + } + else { $output_enable = false; } + array_push($result, array( 'user_id' => $thisResult['UID'], 'password' => $thisResult['UPassword'], 'group_id' => $thisResult['GID'], 'class_id' => $thisResult['CID'], - 'enable' => $thisResult['UEnabled'], + 'enable' => $output_enable, 'build_time' => $thisResult['UBuild_Time'], 'learnStyle_mode' => $thisResult['LMode'], 'material_mode' => $thisResult['MMode'], @@ -366,13 +377,23 @@ class DBUser extends Database { // 如果有查到一筆以上 if( count($queryResultAll) >= 1 ) { $thisResult = $queryResultAll[0]; + + // 轉換成boolean + if($thisResult['GAuth_Admin'] != '0') { + $output_auth_admin = true; + } else { $output_auth_admin = false; } + + if($thisResult['GAuth_ClientAdmin'] != '0') { + $output_auth_clientAdmin = true; + } else { $output_auth_clientAdmin = false; } + // 製作回傳結果陣列 $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'] + 'auth_admin' => $output_auth_admin, + 'auth_clientAdmin' => $output_auth_clientAdmin ); return $result; } @@ -411,13 +432,24 @@ class DBUser extends Database { // 製作回傳結果陣列 $result = array(); foreach($queryResultAll as $key => $thisResult) { + + // 轉換成boolean + if($thisResult['GAuth_Admin'] != '0') { + $output_auth_admin = true; + } else { $output_auth_admin = false; } + + if($thisResult['GAuth_ClientAdmin'] != '0') { + $output_auth_clientAdmin = true; + } else { $output_auth_clientAdmin = false; } + + // 插入一筆資料 array_push($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']) + 'auth_admin' => $output_auth_admin, + 'auth_clientAdmin' => $output_auth_clientAdmin) ); } return $result; @@ -436,7 +468,7 @@ class DBUser extends Database { * $db = new Database\DBUser(); * $db->changeGroupData('student', 'name', '學生'); * - * @param string $uId 使用者名稱 + * @param string $gId 群組ID * @param string $field 欄位名稱 * @param string $value 內容 */ @@ -464,4 +496,175 @@ class DBUser extends Database { $query->execute(); } + // ======================================================================== + + /** + * 插入班級資料 + * + * @param string $cId 班級ID + * @param string $name 班級顯示名稱 + * @param string $memo 備註 + * @return int 剛剛新增的ID + */ + public function insertClassGroup($cId, $name, $memo) { + + // 紀錄使用者帳號進資料庫 + $sqlString = "INSERT INTO ".$this->table('CGroup'). + " (`CID`, `CName`, `CMemo`, `CBuild_Time`) + VALUES ( :id , :name, :memo , NOW() )"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $cId); + $query->bindParam(":name", $name); + $query->bindParam(":memo", $memo); + $query->execute(); + + // 取得剛剛加入的ID + $sqlString = "SELECT LAST_INSERT_ID()"; + $query = $this->connDB->query($sqlString); + $queryResult = $query->fetch(); + return $queryResult[0]; + } + + /** + * 移除一個班級 + * @param string $cId + */ + public function deleteClassGroup($cId) { + + $sqlString = "DELETE FROM ".$this->table('CGroup'). + " WHERE `CID` = :id "; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(":id", $cId); + $query->execute(); + } + + /** + * 查詢一個班級資料 + * + * @return array 班級資料陣列,格式為: + * + * array( 'class_id' => <班級ID>, + * 'name' => <班級顯示名稱>, + * 'memo' => <備註>, + * 'build_time' => <建立時間> + * ); + * + */ + public function queryClassGroup($cId) { + + $sqlString = "SELECT * FROM ".$this->table('CGroup'). + " WHERE `CID` = :cid"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(':cid', $cId); + $query->execute(); + + $queryResultAll = $query->fetchAll(); + // 如果有查到一筆以上 + if( count($queryResultAll) >= 1 ) { + $thisResult = $queryResultAll[0]; + // 製作回傳結果陣列 + $result = array('class_id' => $thisResult['CID'], + 'name' => $thisResult['CName'], + 'memo' => $thisResult['CMemo'], + 'build_time' => $thisResult['CBuild_Time'] + ); + return $result; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 查詢所有的班級資料 + * + * @return array 班級資料陣列,格式為: + * + * array( + * array( + * 'class_id' => <班級ID>, + * 'name' => <班級顯示名稱>, + * 'memo' => <備註>, + * 'build_time' => <建立時間> + * ) + * ); + * + */ + public function queryAllClassGroup() { + + $sqlString = "SELECT * FROM ".$this->table('CGroup'); + + $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( 'class_id' => $thisResult['CID'], + 'name' => $thisResult['CName'], + 'memo' => $thisResult['CMemo'], + 'build_time' => $thisResult['CBuild_Time']) + ); + } + return $result; + } + // 若都沒查到的話 + else { + return null; + } + } + + /** + * 修改一個群組的資料內容 + * + * 範例: + * + * $db = new Database\DBUser(); + * $db->changeClassGroupData(2, 'name', '五年一班'); + * + * @param string $cId 班級ID + * @param string $field 欄位名稱 + * @param string $value 內容 + */ + public function changeClassGroupData($cId, $field, $value) { + + $sqlField = null; + switch($field) { + case 'class_id': $sqlField = 'CID'; break; + case 'name': $sqlField = 'CName'; break; + case 'memo': $sqlField = 'CMemo'; break; + default: $sqlField = $field; break; + } + + + $sqlString = "UPDATE ".$this->table('CGroup'). + " SET `".$sqlField."` = :value". + " WHERE `CID` = :cid"; + + $query = $this->connDB->prepare($sqlString); + $query->bindParam(':cid', $cId); + $query->bindParam(':value', $value); + $query->execute(); + } + + /** + * 設定自動編號的起始值 + * @param int $num 自動編號起始值 + */ + public function setClassGroupIDAutoIncrement($num) { + + // TODO: 不帶值的話,以最後編號為起頭 + $sqlString = "ALTER TABLE ".$this->table('CGroup'). + " AUTO_INCREMENT = $num"; + + $this->connDB->exec($sqlString); + } } \ No newline at end of file diff --git a/htdocs/lib/User/ClassGroup.php b/htdocs/lib/User/ClassGroup.php index 46830d9..52aaf56 100644 --- a/htdocs/lib/User/ClassGroup.php +++ b/htdocs/lib/User/ClassGroup.php @@ -1,7 +1,167 @@ getName(); + * $group->setName('測試用'); + * echo $group->getName(); + * } + * catch (User\Exception\ClassNoFoundException $e) { + * echo 'No Found class: '. $e->getGroupId(); + * } + * + * @version 2.0.0 + * @package UElearning + * @subpackage User + */ +class ClassGroup { + + /** + * 群組ID + * @type int + */ + protected $cId; + + // ======================================================================== + + /** + * 查詢到此帳號的所有資訊的結果 + * + * 由 $this->getQuery() 抓取資料表中所有資訊,並放在此陣列裡 + * + * @type array + */ + protected $queryResultArray; + + /** + * 從資料庫取得此群組查詢 + * + * @throw UElearning\User\Exception\ClassNoFoundException + * @since 2.0.0 + */ + protected function getQuery(){ + + // 從資料庫查詢群組 + $db = new Database\DBUser(); + $groupInfo = $db->queryClassGroup($this->cId); + + // 判斷有沒有這個群組 + if( $groupInfo != null ) { + $this->queryResultArray = $groupInfo; + } + else throw new Exception\ClassNoFoundException($this->cId); + } + + /** + * 從資料庫更新此群組設定 + * + * @since 2.0.0 + */ + protected function setUpdate($field, $value){ + + // 將新設定寫進資料庫裡 + $db = new Database\DBUser(); + $db->changeClassGroupData($this->cId, $field, $value); + $this->getQuery(); + } + + // ======================================================================== + + /** + * 建構子 + * + * @param int $inputCID 班級ID + * @since 2.0.0 + */ + public function __construct($inputCID){ + $this->cId = $inputCID; + $this->getQuery(); + } + + // ======================================================================== + + /** + * 取得群組ID + * + * @return int 班級ID + * @since 2.0.0 + */ + public function getID(){ + return $this->cId; + } + + // ------------------------------------------------------------------------ + + /** + * 取得帳號建立時間 + * + * @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); + } + +} \ No newline at end of file diff --git a/htdocs/lib/User/ClassGroupAdmin.php b/htdocs/lib/User/ClassGroupAdmin.php index 2ba6220..fe472d5 100644 --- a/htdocs/lib/User/ClassGroupAdmin.php +++ b/htdocs/lib/User/ClassGroupAdmin.php @@ -4,4 +4,142 @@ */ 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; + +/** + * 管理班級群組的操作 + * + * @author Yuan Chiu + * @version 2.0.0 + * @package UElearning + * @subpackage User + */ +class ClassGroupAdmin { + + /** + * 建立班級 + * + * 建立班級群組範例: + * + * try { + * $groupAdmin = new User\ClassGroupAdmin(); + * $newId = null; + * $newId = $groupAdmin->create( + * array( 'name' => '學生', + * 'memo' => null + * )); + * echo '你剛建立:'.$newId; + * } + * // 若已有重複班級ID + * catch (User\Exception\ClassIdExistException $e) { + * echo 'Is exist class: ', $e->getGroupId(); + * } + * + * @param array $classGroupArray 班級群組資訊陣列,格式為: + * array( 'class_id' => 12, + * 'name' => '學生', + * 'memo' => null ) + * @return int 剛剛新增進去的ID + * @throw UElearning\User\Exception\ClassIdExistException + * @since 2.0.0 + */ + public function create($classGroupArray) { + + // 檢查有無填寫 + if(isset($classGroupArray)) { + + // 若此id已存在 + if( isset($classGroupArray['class_id']) && + $this->isExist($classGroupArray['class_id']) ) { + + throw new Exception\ClassIdExistException( + $classGroupArray['class_id'] ); + } + // 沒有問題 + else { + // 處理未帶入的資料 + if( !isset($classGroupArray['class_id']) ){ + $classGroupArray['class_id'] = null; + } + + // 處理未帶入的資料 + if( !isset($classGroupArray['name']) ){ + $classGroupArray['name'] = null; + } + // 處理未帶入的資料 + if( !isset($classGroupArray['memo']) ){ + $classGroupArray['memo'] = null; + } + + // 新增一筆使用者資料進資料庫 + $db = new Database\DBUser(); + $id = $db->insertClassGroup( + $classGroupArray['class_id'], + $classGroupArray['name'], + $classGroupArray['memo'] + ); + + // 回傳剛剛新增的ID + return $id; + } + } + else throw Exception\NoDataException(); + } + + /** + * 是否已有相同名稱的班級ID + * + * @param int $class_id 班級ID + * @return bool 已有相同的班級ID + * @since 2.0.0 + */ + public function isExist($class_id) { + + $db = new Database\DBUser(); + $info = $db->queryClassGroup($class_id); + + if( $info != null ) return true; + else return false; + } + + /** + * 移除此班級 + * + * 範例: + * + * try { + * $groupAdmin = new User\ClassGroupAdmin(); + * $groupAdmin->remove(2); + * + * } + * catch (User\Exception\ClassNoFoundException $e) { + * echo 'No Found class: ', $e->getGroupId(); + * } + * + * @param int $class_id 班級ID + * @throw UElearning\User\Exception\ClassNoFoundException + * @since 2.0.0 + */ + public function remove($class_id) { + + // 若有此使用者 + if($this->isExist($class_id)) { + + // TODO: 檢查所有關聯的資料,確認是否可以移除 + + // 移除資料庫中的使用者 + $db = new Database\DBUser(); + $db->deleteClassGroup($class_id); + } + // 若沒有這位使用者 + else { + throw new Exception\ClassNoFoundException($class_id); + } + + } + +} \ No newline at end of file diff --git a/htdocs/lib/User/Exception.php b/htdocs/lib/User/Exception.php index f9a294a..1359a91 100644 --- a/htdocs/lib/User/Exception.php +++ b/htdocs/lib/User/Exception.php @@ -42,6 +42,8 @@ abstract class UserException extends \UnexpectedValueException { /** * 沒有找到此帳號 * @since 2.0.0 + * @package UElearning + * @subpackage User */ class UserNoFoundException extends UserException { /** @@ -56,6 +58,8 @@ class UserNoFoundException extends UserException { /** * 使用者登入密碼錯誤 * @since 2.0.0 + * @package UElearning + * @subpackage User */ class UserPasswordErrException extends UserException { /** @@ -70,6 +74,8 @@ class UserPasswordErrException extends UserException { /** * 此帳號未啟用 * @since 2.0.0 + * @package UElearning + * @subpackage User */ class UserNoActivatedException extends UserException { /** @@ -85,6 +91,8 @@ class UserNoActivatedException extends UserException { /** * 已有重複的使用者名稱 * @since 2.0.0 + * @package UElearning + * @subpackage User */ class UserIdExistException extends UserException { /** @@ -134,6 +142,8 @@ abstract class GroupException extends \UnexpectedValueException { /** * 已有重複的使用者群組ID * @since 2.0.0 + * @package UElearning + * @subpackage User */ class GroupIdExistException extends GroupException { /** @@ -148,6 +158,8 @@ class GroupIdExistException extends GroupException { /** * 沒有找到此使用者群組ID * @since 2.0.0 + * @package UElearning + * @subpackage User */ class GroupNoFoundException extends GroupException { /** @@ -162,7 +174,41 @@ class GroupNoFoundException extends GroupException { // ============================================================================ /** - * 使用者群組例外 + * 已有重複的使用者群組ID + * @since 2.0.0 + * @package UElearning + * @subpackage User + */ +class ClassIdExistException extends GroupException { + /** + * 已有重複的使用者名稱 + * @param string $groupId 輸入的使用者群組ID + */ + public function __construct($groupId) { + parent::__construct($groupId, 'ClassId: "'.$groupId.'" is exist.'); + } +} + +/** + * 沒有找到此使用者群組ID + * @since 2.0.0 + * @package UElearning + * @subpackage User + */ +class ClassNoFoundException extends GroupException { + /** + * 沒有找到此帳號 + * @param string $groupId 輸入的使用者群組ID + */ + public function __construct($groupId) { + parent::__construct($groupId, 'Class Group: "'.$groupId.'" is no found.'); + } +} + +// ============================================================================ + +/** + * 沒有此權限例外 * @since 2.0.0 * @package UElearning * @subpackage User diff --git a/htdocs/lib/User/User.php b/htdocs/lib/User/User.php index 3b63e94..47955b7 100644 --- a/htdocs/lib/User/User.php +++ b/htdocs/lib/User/User.php @@ -276,8 +276,7 @@ class User { * @since 2.0.0 */ public function isEnable(){ - if($this->queryResultArray['enable'] == 0) return false; - else return true; + return $this->queryResultArray['enable']; } /** diff --git a/htdocs/lib/User/UserGroupAdmin.php b/htdocs/lib/User/UserGroupAdmin.php index 09ed50a..c443c39 100644 --- a/htdocs/lib/User/UserGroupAdmin.php +++ b/htdocs/lib/User/UserGroupAdmin.php @@ -23,7 +23,7 @@ class UserGroupAdmin { /** * 建立群組 * - * 建立使用者範例: + * 建立使用者群組範例: * * try { * $groupAdmin = new User\UserGroupAdmin(); @@ -36,12 +36,12 @@ class UserGroupAdmin { * )); * * } - * // 若已有重複帳號名稱 + * // 若已有重複群組ID * catch (User\Exception\GroupIdExistException $e) { * echo 'Is exist group: ', $e->getGroupId(); * } * - * @param array $groupArray 使用者資訊陣列,格式為: + * @param array $groupArray 使用者群組資訊陣列,格式為: * array( 'group_id' => 'student', * 'name' => '學生', * 'memo' => null, // (optional) 預設為null diff --git a/tests/Database/DBUserTest.php b/tests/Database/DBUserTest.php index 23b35ed..66eee31 100644 --- a/tests/Database/DBUserTest.php +++ b/tests/Database/DBUserTest.php @@ -129,7 +129,7 @@ class DBUserTest extends \PHPUnit_Framework_TestCase 'harf-line-learn', 1, '元兒~', 'Yuan Chiu', 'chyuaner@gmail.com', null), - array('eee_unittest', 'qqqssss', 'admin', null, 1, + array('eee_unittest', 'qqqssss', 'admin', null, 0, 'harf-line-learn', '1', 'sss', 'Yuan Chiu', 'chyuanesr@gmail.com', null) ); @@ -205,8 +205,12 @@ class DBUserTest extends \PHPUnit_Framework_TestCase */ public function groupDataProvider(){ return array( - array('testG_a', '測試用群組a', null, '1', '0'), - array('testG_b', '測試用群組b', 'testhahaha Groups', '0', '1') + array('testG_a', '測試用群組a', null, 1, 0), + array('testG_b', '測試用群組b', 'testhahaha Groups', 0, 1) ); } + + // ======================================================================== + + // TODO: ClassGroup Test } \ No newline at end of file diff --git a/tests/User/ClassGroupAdminTest.php.bak b/tests/User/ClassGroupAdminTest.php.bak new file mode 100644 index 0000000..23daa9e --- /dev/null +++ b/tests/User/ClassGroupAdminTest.php.bak @@ -0,0 +1,92 @@ + + */ +namespace UElearning; + +require_once UELEARNING_LIB_ROOT.'/User/ClassGroupAdmin.php'; +use UElearning\User\ClassGroupAdmin; + +class ClassGroupAdminTest extends \PHPUnit_Framework_TestCase +{ + + protected $classId; + + protected function setUp(){ + $this->classId = array(); + } + + /** + * 測試建立群組 + * + * @dataProvider groupDataProvider + */ + public function testCreateGroup($cId, $name, $memo){ + + try { + $groupAdmin = new User\ClassGroupAdmin(); + $newId = null; + $newId = $groupAdmin->create( + array( 'class_id' => $cId, + 'name' => $name, + 'memo' => $memo + )); + + array_push($this->classId, $newId); + print_r($this->classId); + } + // 若已有重複帳號名稱 + catch (User\Exception\ClassIdExistException $e) { + throw $e; + } + + } + + /** + * 測試查詢群組 + * + * @dataProvider groupDataProvider + */ + public function testCheckExist($cId){ + + if(isset($cid)) { + $groupAdmin = new User\ClassGroupAdmin(); + // 比對資料是否吻合 + $this->assertEquals($groupAdmin->isExist($cId), true); + } + } + + /** + * 測試移除使用者 + * @depends testCreateGroup + */ + public function testDeleteGroup() { + print_r($this->classId); + foreach($this->classId as $thisId) { + + try { + $groupAdmin = new User\ClassGroupAdmin(); + $groupAdmin->remove($thisId); + + $this->assertEquals($groupAdmin->isExist($thisId), false); + } + catch (User\Exception\ClassNoFoundException $e) { + throw $e; + } + } + } + + /** + * 測試時要填的資料 + */ + public function groupDataProvider(){ + return array( + array(null, '測試用群組a', null), + array(2859, '測試用群組b', 'testhahaha Groups') + ); + } + +} \ No newline at end of file