AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / DAO / TestsQuestionsAssocDAO.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
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 /************************************************************************/
12
13 /**
14 * DAO for "tests_questions_assoc" table
15 * @access       public
16 * @author       Cindy Qi Li
17 * @package      DAO
18 */
19
20 if (!defined('TR_INCLUDE_PATH')) exit;
21
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23
24 class TestsQuestionsAssocDAO extends DAO {
25
26         /**
27         * Insert a new row
28         * @access  public
29         * @param   test_id, question_id, weight, order
30         * @return  table rows
31         * @author  Cindy Qi Li
32         */
33         function Create($testID, $questionID, $weight, $order)
34         {
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);
39         }
40         
41         /**
42         * Update an existing row
43         * @access  public
44         * @param   test_id, question_id, weight, order
45         * @return  table rows
46         * @author  Cindy Qi Li
47         */
48         function Update($testID, $questionID, $weight, $order)
49         {
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);
54         }
55         
56         /**
57         * Delete a row by test id and question id
58         * @access  public
59         * @param   testID, questionID
60         * @return  true or false
61         * @author  Cindy Qi Li
62         */
63         function Delete($testID, $questionID)
64         {
65             $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc 
66                      WHERE test_id = ".$testID."
67                        AND question_id = ".$questionID;
68             return $this->execute($sql);
69         }
70         
71         /**
72         * Delete rows by question id
73         * @access  public
74         * @param   questionID
75         * @return  true or false
76         * @author  Cindy Qi Li
77         */
78         function DeleteByQuestionID($questionID)
79         {
80             $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc 
81                      WHERE question_id = ".$questionID;
82             return $this->execute($sql);
83         }
84         
85         /**
86         * Delete rows by test id
87         * @access  public
88         * @param   testID
89         * @return  true or false
90         * @author  Cindy Qi Li
91         */
92         function DeleteByTestID($testID)
93         {
94             $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions_assoc 
95                      WHERE test_id = ".$testID;
96             return $this->execute($sql);
97         }
98         
99         /**
100         * Return all associated questions of the given test
101         * @access  public
102         * @param   testID
103         * @return  table rows if successful. false if unsuccessful
104         * @author  Cindy Qi Li
105         */
106         function getByTestID($testID)
107         {
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 
111                      USING (question_id) 
112                      WHERE TQA.test_id=".$testID."
113                      ORDER BY TQA.ordering, TQA.question_id";
114             return $this->execute($sql);
115         }
116
117         /**
118         * Return all associated questions with the weight 0 in the given test
119         * @access  public
120         * @param   testID
121         * @return  table rows if successful. false if unsuccessful
122         * @author  Cindy Qi Li
123         */
124         function getZeroWeightRowsByTestID($testID)
125         {
126             $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions_assoc QA, ".TABLE_PREFIX."tests_questions Q 
127                      WHERE QA.test_id=$testID 
128                        AND QA.weight=0 
129                        AND QA.question_id=Q.question_id 
130                        AND Q.type<>4";
131             return $this->execute($sql);
132         }
133         
134         /**
135         * Return the maximum ordering number in the given test
136         * @access  public
137         * @param   testID
138         * @return  the maximum ordering number
139         * @author  Cindy Qi Li
140         */
141         function getMaxOrderByTestID($testID)
142         {
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'];
146         }
147 }
148 ?>