AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / DAO / CourseCategoriesDAO.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 "course_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 CourseCategoriesDAO extends DAO {
25
26         /**
27          * Create new row
28          * @access  public
29          * @param   category_name
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($categoryName)
35         {
36                 global $addslashes, $msg;
37
38                 $categoryName = $addslashes(trim($categoryName));
39                 
40                 if ($this->isFieldsValid($categoryName))
41                 {
42                         /* insert into the db */
43                         $sql = "INSERT INTO ".TABLE_PREFIX."course_categories (category_name)
44                                VALUES ('".$categoryName."')";
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 an existing record
64          * @access  public
65          * @param   categoryID
66          *          categoryName
67          * @return  true if successful
68          *          error message array if failed; false if update db failed
69          * @author  Cindy Qi Li
70          */
71         public function Update($categoryID, $categoryName)
72         {
73                 global $addslashes, $msg;
74                 
75                 $categoryName = $addslashes(trim($categoryName));
76                 
77                 if ($this->isFieldsValid($categoryName, $categoryID, 'update')) {
78                         $sql = "UPDATE ".TABLE_PREFIX."course_categories 
79                    SET category_name='".$categoryName."'
80                  WHERE category_id = ".$categoryID;
81                 
82                         return $this->execute($sql);
83                 }
84                 else {
85                         return false;
86                 }
87         }
88         
89         /**
90          * Delete course
91          * @access  public
92          * @param   category ID
93          * @return  true, if successful
94          *          false and add error into global var $msg, if unsuccessful
95          * @author  Cindy Qi Li
96          */
97         public function Delete($categoryID)
98         {
99                 // move the courses that belong to $categoryID to "uncategorized"
100                 $sql = "UPDATE ".TABLE_PREFIX."courses 
101                            SET category_id=".TR_COURSECATEGORY_UNCATEGORIZED."
102                          WHERE category_id = ".$categoryID;
103                 
104                 if ($this->execute($sql))
105                 {
106                         $sql = "DELETE FROM ".TABLE_PREFIX."course_categories WHERE category_id = ".$categoryID;
107                         return $this->execute($sql);
108                 }
109                 else
110                         return false;
111         }
112
113         /**
114          * Return course category information by given category id
115          * @access  public
116          * @param   category id
117          * @return  one row
118          * @author  Cindy Qi Li
119          */
120         public function get($categoryID)
121         {
122                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'course_categories WHERE category_id='.$categoryID;
123                 if ($rows = $this->execute($sql))
124                 {
125                         return $rows[0];
126                 }
127                 else return false;
128         }
129
130         /**
131          * Return all course categories information
132          * @access  public
133          * @param   None
134          * @return  rows
135          * @author  Cindy Qi Li
136          */
137         public function getAll()
138         {
139                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'course_categories ORDER BY category_name';
140                 return $this->execute($sql);
141         }
142
143         /**
144          * Validate fields preparing for insert and update
145          * @access  private
146          * @param   $categoryName: Must have
147          *          $categoryID: optional. only required when $actionType is "update"
148          *          $actionType: optional. Must be one of the values: insert, update. The default value is "insert".
149          * @return  true    if update successfully
150          *          false   if update unsuccessful
151          * @author  Cindy Qi Li
152          */
153         private function isFieldsValid($categoryName, $categoryID = 0, $actionType = "insert")
154         {
155                 global $msg;
156                 
157                 $missing_fields = array();
158                 
159                 if ($categoryName == '')
160                 {
161                         $missing_fields[] = _AT('category_name');
162                 }
163                 if ($actionType == 'update' && intval($categoryID) == 0)
164                 {
165                         $missing_fields[] = _AT('category_id');
166                 }
167                 
168                 if ($missing_fields)
169                 {
170                         $missing_fields = implode(', ', $missing_fields);
171                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
172                 }
173                 
174                 if (!$msg->containsErrors())
175                         return true;
176                 else
177                         return false;
178         }
179 }
180 ?>