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