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 "tests_questions_categories" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class TestsQuestionsCategoriesDAO extends DAO {
29 * @param course_id, title
30 * @return category id, if successful
31 * false and add error into global var $msg, if unsuccessful
34 public function Create($courseID, $title)
38 $courseID = intval($courseID);
39 $title = $addslashes(trim($title));
41 if ($this->isFieldsValid('new', $title, $courseID))
43 $sql = "INSERT INTO ".TABLE_PREFIX."tests_questions_categories (course_id, title)
44 VALUES (".$courseID.", '".$title."')";
46 if (!$this->execute($sql))
48 $msg->addError('DB_NOT_UPDATED');
53 return mysql_insert_id();
63 * Update the title of an existing category
67 * @return user id, if successful
68 * false and add error into global var $msg, if unsuccessful
71 public function Update($categoryID, $title)
75 $title = $addslashes(trim($title));
77 if ($this->isFieldsValid('update', $title, $categoryID))
79 /* insert into the db */
80 $sql = "UPDATE ".TABLE_PREFIX."tests_questions_categories
81 SET title = '".$title."'
82 WHERE category_id = ".$categoryID;
84 return $this->execute($sql);
92 * @return true, if successful
93 * false if unsuccessful
96 public function Delete($categoryID)
98 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_categories WHERE category_id = ".$categoryID;
99 return $this->execute($sql);
103 * Return content information by given category id
105 * @param categoryID: category id
106 * @return the row if successful, otherwise, return false
107 * @author Cindy Qi Li
109 public function get($categoryID)
111 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions_categories
112 WHERE category_id=".$categoryID;
114 $rows = $this->execute($sql);
116 if (is_array($rows)) return $rows[0];
121 * Return content information by given content id
123 * @param courseID: course id
125 * @author Cindy Qi Li
127 public function getByCourseID($courseID)
129 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions_categories
130 WHERE course_id=".$courseID."
133 return $this->execute($sql);
137 * Validates fields preparing for insert and update
139 * @param $validate_type : new/update. When "new", $ID is course_id. When "update", $ID is category_id
142 * @return true if update successfully
143 * false if update unsuccessful
144 * @author Cindy Qi Li
146 private function isFieldsValid($validate_type, $title, $ID)
150 $missing_fields = array();
151 /* login name check */
154 $missing_fields[] = _AT('title');
159 if ($validate_type == 'new') $missing_fields[] = _AT('course_id');
160 if ($validate_type == 'update') $missing_fields[] = _AT('category_id');
165 $missing_fields = implode(', ', $missing_fields);
166 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
169 if (!$msg->containsErrors())