AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / include / classes / DAO / TestsQuestionsDAO.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" 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 TestsQuestionsDAO extends DAO {
25         
26         /**
27          * Update an the given field to a given value of an existing record
28          * @access  public
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
34          * @author  Cindy Qi Li
35          */
36         public function UpdateField($questionID, $fieldName, $fieldValue)
37         {
38                 global $addslashes;
39                 
40                 $sql = "UPDATE ".TABLE_PREFIX."tests_questions 
41                            SET ".$fieldName."='".$addslashes($fieldValue)."'
42                          WHERE question_id = ".$questionID;
43                 
44                 return $this->execute($sql);
45         }
46         
47         /**
48          * Delete a row
49          * @access  public
50          * @param   question ID
51          * @return  true, if successful
52          *          false if unsuccessful
53          * @author  Cindy Qi Li
54          */
55         public function Delete($questionID)
56         {
57                 $sql = "DELETE FROM ".TABLE_PREFIX."tests_questions WHERE question_id = ".$questionID;
58                 return $this->execute($sql);
59         }
60
61         /**
62          * Return information by a given question id
63          * @access  public
64          * @param   questionID: category id
65          * @return  the row if successful, otherwise, return false
66          * @author  Cindy Qi Li
67          */
68         public function get($questionID)
69         {
70                 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions 
71                              WHERE question_id=".$questionID;
72                 
73                 $rows = $this->execute($sql);
74                 
75                 if (is_array($rows)) return $rows[0];
76                 else return false;
77         }
78         
79         /**
80          * Return information by an array of question ids
81          * @access  public
82          * @param   questionIDsArray: an array of question ids
83          * @return  the row if successful, otherwise, return false
84          * @author  Cindy Qi Li
85          */
86         public function getByQuestionIDs($questionIDsArray)
87         {
88                 if (!is_array($questionIDsArray) || count($questionIDsArray) == 0) return false;
89                 
90                 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions 
91                              WHERE question_id in (".implode(',', $questionIDsArray). ")";
92                 
93                 return $this->execute($sql);
94         }
95         
96         /**
97          * Return content information by given course id and category id
98          * @access  public
99          * @param   courseID
100          *          categoryID
101          * @return  rows
102          * @author  Cindy Qi Li
103          */
104         public function getByCourseIDAndCategoryID($courseID, $categoryID)
105         {
106                 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions 
107                          WHERE course_id=".$courseID."
108                            AND category_id = ".$categoryID."
109                          ORDER BY question";
110                 
111                 return $this->execute($sql);
112         }
113
114         /**
115          * Return content information by given course id and question type
116          * @access  public
117          * @param   courseID
118          *          type: question type
119          * @return  rows
120          * @author  Cindy Qi Li
121          */
122         public function getByCourseIDAndType($courseID, $type)
123         {
124                 $sql = "SELECT * FROM ".TABLE_PREFIX."tests_questions 
125                          WHERE course_id=".$courseID."
126                            AND type = ".$type;;
127                 
128                 return $this->execute($sql);
129         }
130
131         /**
132          * Validates fields preparing for insert and update
133          * @access  private
134          * @param   $validate_type : new/update. When "new", $ID is course_id. When "update", $ID is category_id
135          *          $title
136          *          $ID
137          * @return  true    if update successfully
138          *          false   if update unsuccessful
139          * @author  Cindy Qi Li
140          */
141         private function isFieldsValid($validate_type, $title, $ID)
142         {
143                 global $msg;
144                 
145                 $missing_fields = array();
146                 /* login name check */
147                 if ($title == '')
148                 {
149                         $missing_fields[] = _AT('title');
150                 }
151
152                 if ($ID == 0)
153                 {
154                         if ($validate_type == 'new') $missing_fields[] = _AT('course_id');
155                         if ($validate_type == 'update') $missing_fields[] = _AT('category_id');
156                 }
157
158                 if ($missing_fields)
159                 {
160                         $missing_fields = implode(', ', $missing_fields);
161                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
162                 }
163                 
164                 if (!$msg->containsErrors())
165                         return true;
166                 else
167                         return false;
168         }
169 }
170 ?>