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_assoc" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class TestsQuestionsAssocDAO extends DAO {
29 * @param test_id, question_id, weight, order
33 function Create($testID, $questionID, $weight, $order)
35 $sql = "INSERT INTO " . TABLE_PREFIX . "tests_questions_assoc" .
36 "(test_id, question_id, weight, ordering) " .
37 "VALUES ($testID, $questionID, $weight, $order)";
38 return $this->execute($sql);
42 * Update an existing row
44 * @param test_id, question_id, weight, order
48 function Update($testID, $questionID, $weight, $order)
50 $sql = "UPDATE ".TABLE_PREFIX."tests_questions_assoc
51 SET weight=".$weight.", ordering=".$order."
52 WHERE question_id=".$questionID." AND test_id=".$testID;
53 return $this->execute($sql);
57 * Delete a row by test id and question id
59 * @param testID, questionID
60 * @return true or false
63 function Delete($testID, $questionID)
65 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc
66 WHERE test_id = ".$testID."
67 AND question_id = ".$questionID;
68 return $this->execute($sql);
72 * Delete rows by question id
75 * @return true or false
78 function DeleteByQuestionID($questionID)
80 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc
81 WHERE question_id = ".$questionID;
82 return $this->execute($sql);
86 * Delete rows by test id
89 * @return true or false
92 function DeleteByTestID($testID)
94 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc
95 WHERE test_id = ".$testID;
96 return $this->execute($sql);
100 * Return all associated questions of the given test
103 * @return table rows if successful. false if unsuccessful
104 * @author Cindy Qi Li
106 function getByTestID($testID)
108 $sql = "SELECT TQ.*, TQA.test_id, TQA.weight, TQA.ordering
109 FROM ".TABLE_PREFIX."tests_questions TQ
110 INNER JOIN ".TABLE_PREFIX."tests_questions_assoc TQA
112 WHERE TQA.test_id=".$testID."
113 ORDER BY TQA.ordering, TQA.question_id";
114 return $this->execute($sql);
118 * Return all associated questions with the weight 0 in the given test
121 * @return table rows if successful. false if unsuccessful
122 * @author Cindy Qi Li
124 function getZeroWeightRowsByTestID($testID)
126 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions_assoc QA, ".TABLE_PREFIX."tests_questions Q
127 WHERE QA.test_id=$testID
129 AND QA.question_id=Q.question_id
131 return $this->execute($sql);
135 * Return the maximum ordering number in the given test
138 * @return the maximum ordering number
139 * @author Cindy Qi Li
141 function getMaxOrderByTestID($testID)
143 $sql = "SELECT MAX(ordering) AS max_ordering FROM ".TABLE_PREFIX."tests_questions_assoc WHERE test_id=".$testID;
144 $rows = $this->execute($sql);
145 return $rows[0]['max_ordering'];