* @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 (Exception\ClassIdExistException $e) { * echo 'Is exist class: ', $e->getGroupId(); * } * * @param array $classGroupArray 班級群組資訊陣列,格式為: * array( 'class_id' => 12, * 'name' => '學生', * 'memo' => null ) * @return int 剛剛新增進去的ID * @throw UElearning\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 (Exception\ClassNoFoundException $e) { * echo 'No Found class: ', $e->getGroupId(); * } * * @param int $class_id 班級ID * @throw UElearning\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); } } }