2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 * DAO for "course_categories" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class CourseCategoriesDAO extends DAO {
29 * @param category_name
30 * @return category id, if successful
31 * false and add error into global var $msg, if unsuccessful
34 public function Create($categoryName)
36 global $addslashes, $msg;
38 $categoryName = $addslashes(trim($categoryName));
40 if ($this->isFieldsValid($categoryName))
42 /* insert into the db */
43 $sql = "INSERT INTO ".TABLE_PREFIX."course_categories (category_name)
44 VALUES ('".$categoryName."')";
46 if (!$this->execute($sql))
48 $msg->addError('DB_NOT_UPDATED');
53 return mysql_insert_id();
63 * Update an existing record
67 * @return true if successful
68 * error message array if failed; false if update db failed
71 public function Update($categoryID, $categoryName)
73 global $addslashes, $msg;
75 $categoryName = $addslashes(trim($categoryName));
77 if ($this->isFieldsValid($categoryName, $categoryID, 'update')) {
78 $sql = "UPDATE ".TABLE_PREFIX."course_categories
79 SET category_name='".$categoryName."'
80 WHERE category_id = ".$categoryID;
82 return $this->execute($sql);
93 * @return true, if successful
94 * false and add error into global var $msg, if unsuccessful
97 public function Delete($categoryID)
99 // move the courses that belong to $categoryID to "uncategorized"
100 $sql = "UPDATE ".TABLE_PREFIX."courses
101 SET category_id=".TR_COURSECATEGORY_UNCATEGORIZED."
102 WHERE category_id = ".$categoryID;
104 if ($this->execute($sql))
106 $sql = "DELETE FROM ".TABLE_PREFIX."course_categories WHERE category_id = ".$categoryID;
107 return $this->execute($sql);
114 * Return course category information by given category id
118 * @author Cindy Qi Li
120 public function get($categoryID)
122 $sql = 'SELECT * FROM '.TABLE_PREFIX.'course_categories WHERE category_id='.$categoryID;
123 if ($rows = $this->execute($sql))
131 * Return all course categories information
135 * @author Cindy Qi Li
137 public function getAll()
139 $sql = 'SELECT * FROM '.TABLE_PREFIX.'course_categories ORDER BY category_name';
140 return $this->execute($sql);
144 * Validate fields preparing for insert and update
146 * @param $categoryName: Must have
147 * $categoryID: optional. only required when $actionType is "update"
148 * $actionType: optional. Must be one of the values: insert, update. The default value is "insert".
149 * @return true if update successfully
150 * false if update unsuccessful
151 * @author Cindy Qi Li
153 private function isFieldsValid($categoryName, $categoryID = 0, $actionType = "insert")
157 $missing_fields = array();
159 if ($categoryName == '')
161 $missing_fields[] = _AT('category_name');
163 if ($actionType == 'update' && intval($categoryID) == 0)
165 $missing_fields[] = _AT('category_id');
170 $missing_fields = implode(', ', $missing_fields);
171 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
174 if (!$msg->containsErrors())