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" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class TestsQuestionsDAO extends DAO {
27 * Update an the given field to a given value of an existing record
29 * @param questionID: question ID
30 * fieldName: the name of the table field to update
31 * fieldValue: the value to update
32 * @return true if successful
33 * error message array if failed; false if update db failed
36 public function UpdateField($questionID, $fieldName, $fieldValue)
40 $sql = "UPDATE ".TABLE_PREFIX."tests_questions
41 SET ".$fieldName."='".$addslashes($fieldValue)."'
42 WHERE question_id = ".$questionID;
44 return $this->execute($sql);
51 * @return true, if successful
52 * false if unsuccessful
55 public function Delete($questionID)
57 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions WHERE question_id = ".$questionID;
58 return $this->execute($sql);
62 * Return information by a given question id
64 * @param questionID: category id
65 * @return the row if successful, otherwise, return false
68 public function get($questionID)
70 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions
71 WHERE question_id=".$questionID;
73 $rows = $this->execute($sql);
75 if (is_array($rows)) return $rows[0];
80 * Return information by an array of question ids
82 * @param questionIDsArray: an array of question ids
83 * @return the row if successful, otherwise, return false
86 public function getByQuestionIDs($questionIDsArray)
88 if (!is_array($questionIDsArray) || count($questionIDsArray) == 0) return false;
90 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions
91 WHERE question_id in (".implode(',', $questionIDsArray). ")";
93 return $this->execute($sql);
97 * Return content information by given course id and category id
102 * @author Cindy Qi Li
104 public function getByCourseIDAndCategoryID($courseID, $categoryID)
106 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions
107 WHERE course_id=".$courseID."
108 AND category_id = ".$categoryID."
111 return $this->execute($sql);
115 * Return content information by given course id and question type
118 * type: question type
120 * @author Cindy Qi Li
122 public function getByCourseIDAndType($courseID, $type)
124 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions
125 WHERE course_id=".$courseID."
128 return $this->execute($sql);
132 * Validates fields preparing for insert and update
134 * @param $validate_type : new/update. When "new", $ID is course_id. When "update", $ID is category_id
137 * @return true if update successfully
138 * false if update unsuccessful
139 * @author Cindy Qi Li
141 private function isFieldsValid($validate_type, $title, $ID)
145 $missing_fields = array();
146 /* login name check */
149 $missing_fields[] = _AT('title');
154 if ($validate_type == 'new') $missing_fields[] = _AT('course_id');
155 if ($validate_type == 'update') $missing_fields[] = _AT('category_id');
160 $missing_fields = implode(', ', $missing_fields);
161 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
164 if (!$msg->containsErrors())