AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / DAO / ContentDAO.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 if (!defined('TR_INCLUDE_PATH')) exit;
14
15 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
16
17 class ContentDAO extends DAO {
18
19         /**
20          * Create new content
21          * @access  public
22          * @param   
23          * @return  user id, if successful
24          *          false and add error into global var $msg, if unsuccessful
25          * @author  Cindy Qi Li
26          */
27         public function Create($course_id, $content_parent_id, $ordering, $revision, $formatting, $keywords, 
28                                $content_path, $title, $text, $head, $use_customized_head, $test_message, 
29                                $content_type)
30         {
31                 global $addslashes, $msg;
32
33                 if ($this->isFieldsValid('create', $course_id, $title))
34                 {
35                         /* insert into the db */
36                         $sql = "INSERT INTO ".TABLE_PREFIX."content
37                                       (course_id,
38                                        content_parent_id,
39                                        ordering,
40                                        last_modified,
41                                        revision,
42                                        formatting,
43                                        keywords,
44                                        content_path,
45                                        title,
46                                        text,
47                                        head,
48                                        use_customized_head,
49                                        test_message,
50                                        content_type
51                                        )
52                                VALUES (".$course_id.",
53                                        ".$content_parent_id.",
54                                        ".$ordering.",
55                                        now(), 
56                                        ".$revision.",
57                                        ".$formatting.",
58                                        '".$addslashes($keywords)."',
59                                        '".$content_path."', 
60                                        '".$addslashes($title)."',
61                                        '".$addslashes($text)."',
62                                        '".$addslashes($head)."',
63                                        ".$use_customized_head.",
64                                        '".$addslashes($test_message)."',
65                                        ".$content_type.")";
66
67                         if (!$this->execute($sql))
68                         {
69                                 $msg->addError('DB_NOT_UPDATED');
70                                 return false;
71                         }
72                         else
73                         {
74                                 $cid = mysql_insert_id();
75                                 
76                                 // update the courses.modified_date to the current timestamp
77                                 include_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
78                                 $coursesDAO = new CoursesDAO();
79                                 $coursesDAO->updateModifiedDate($cid, "content_id");
80                                 
81                                 return $cid;
82                         }
83                 }
84                 else
85                 {
86                         return false;
87                 }
88         }
89
90         /**
91          * Update an existing content record
92          * @access  public
93          * @param   userID: user ID (1 [admin] or 2 [user])
94          *          login: login name
95          *          pwd: password
96          *          email: email
97          *          first_name: first name
98          *          last_name: last name
99          *          status
100          * @return  true, if successful
101          *          false and add error into global var $msg, if unsuccessful
102          * @author  Cindy Qi Li
103          */
104         public function Update($content_id, $title, $text, $keywords, $formatting, 
105                              $head, $use_customized_head, $test_message)
106         {
107                 global $addslashes, $msg;
108
109                 if ($this->isFieldsValid('update', $content_id, $title))
110                 {
111                         /* insert into the db */
112                         $sql = "UPDATE ".TABLE_PREFIX."content
113                                    SET title = '".$addslashes($title)."',
114                                        text = '".$addslashes($text)."',
115                                        keywords = '".$addslashes($keywords)."',
116                                        formatting = '".$formatting."',
117                                        head = '".$addslashes($head)."',
118                                        use_customized_head = ".$use_customized_head.",
119                                        test_message = '".$addslashes($test_message)."'
120                                  WHERE content_id = ".$content_id;
121
122                         if ($this->execute($sql)) {
123                                 // update the courses.modified_date to the current timestamp
124                                 include_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
125                                 $coursesDAO = new CoursesDAO();
126                                 $coursesDAO->updateModifiedDate($content_id, "content_id");
127                                 return true;
128                         } else {
129                                 $msg->addError('DB_NOT_UPDATED');
130                                 return false;
131                         }
132                 }
133                 else {
134                         return false;
135                 }
136         }
137
138         /**
139          * Update one field of an existing content record
140          * @access  public
141          * @param   contentID
142          *          fieldName: the name of the table field to update
143          *          fieldValue: the value to update
144          * @return  true if successful
145          *          error message array if failed; false if update db failed
146          * @author  Cindy Qi Li
147          */
148         public function UpdateField($contentID, $fieldName, $fieldValue)
149         {
150                 global $addslashes;
151                 
152                 $sql = "UPDATE ".TABLE_PREFIX."content 
153                            SET ".$fieldName."='".$addslashes($fieldValue)."'
154                          WHERE content_id = ".$contentID;
155                 
156                 if ($this->execute($sql)) {
157                         // update the courses.modified_date to the current timestamp
158                         include_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
159                         $coursesDAO = new CoursesDAO();
160                         $coursesDAO->updateModifiedDate($contentID, "content_id");
161                         return true;
162                 } else {
163                         $msg->addError('DB_NOT_UPDATED');
164                         return false;
165                 }
166         }
167         
168         /**
169          * Delete content
170          * @access  public
171          * @param   content ID
172          * @return  true, if successful
173          *          false and add error into global var $msg, if unsuccessful
174          * @author  Cindy Qi Li
175          */
176         public function Delete($contentID)
177         {
178                 global $msg;
179                 
180                 require_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
181                 $a4a = new A4a($contentID);
182                 $a4a->deleteA4a();
183                 
184                 // delete the content tests association
185                 include_once(TR_INCLUDE_PATH.'classes/DAO/ContentTestsAssocDAO.class.php');
186                 $contentTestsAssocDAO = new ContentTestsAssocDAO();
187                 $contentTestsAssocDAO->DeleteByContentID($contentID);
188                 
189                 // delete the content forums association
190                 include_once(TR_INCLUDE_PATH.'classes/DAO/ContentForumsAssocDAO.class.php');
191                 $contentForumsAssocDAO = new ContentForumsAssocDAO();
192                 $contentForumsAssocDAO->DeleteByContentID($contentID);
193                 
194                 $sql = "DELETE FROM ".TABLE_PREFIX."content WHERE content_id = ".$contentID;
195                 if ($this->execute($sql)) {
196                         // update the courses.modified_date to the current timestamp
197                         include_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
198                         $coursesDAO = new CoursesDAO();
199                         $coursesDAO->updateModifiedDate($contentID, "content_id");
200                         return true;
201                 } else {
202                         $msg->addError('DB_NOT_UPDATED');
203                         return false;
204                 }
205         }
206
207         /**
208          * Return content information by given content id
209          * @access  public
210          * @param   content id
211          * @return  content row
212          * @author  Cindy Qi Li
213          */
214         public function get($contentID)
215         {
216                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'content WHERE content_id='.$contentID;
217                 if ($rows = $this->execute($sql))
218                 {
219                         return $rows[0];
220                 }
221                 else return false;
222         }
223
224         /**
225          * Return all content rows by given course id
226          * @access  public
227          * @param   course id
228          * @return  content rows
229          * @author  Cindy Qi Li
230          */
231         public function getContentByCourseID($courseID)
232         {
233                 $sql = "SELECT *, 
234                                UNIX_TIMESTAMP(last_modified) AS u_ts 
235                           FROM ".TABLE_PREFIX."content 
236                          WHERE course_id=$courseID 
237                          ORDER BY content_parent_id, ordering";
238                 
239                 return $this->execute($sql);
240         }
241
242         /**
243          * Return max ordering based on given course id and content parent id 
244          * @access  public
245          * @param   course_id, content_parent_id
246          * @return  max ordering: int
247          * @author  Cindy Qi Li
248          */
249         public function getMaxOrdering($course_id, $content_parent_id)
250         {
251                 $sql = "SELECT MAX(ordering) AS ordering FROM ".TABLE_PREFIX."content 
252                          WHERE course_id=".$course_id." 
253                            AND content_parent_id=".$content_parent_id;
254                 $rows = $this->execute($sql);
255                 return intval($rows[0]['ordering']);
256         }
257
258         /**
259          * Validate fields preparing for insert and update
260          * @access  private
261          * @param   $action_type: "create" or "update"
262          *          $row_id: when action_type is "create", row_id is course_id
263          *                   when action_type is "update", row_id is content_id
264          *          $title: content title
265          * @return  true    if update successfully
266          *          false   if update unsuccessful
267          * @author  Cindy Qi Li
268          */
269         private function isFieldsValid($action_type, $row_id, $title)
270         {
271                 global $msg;
272                 
273                 $missing_fields = array();
274                 
275                 if (intval($row_id) == 0)
276                 {
277                         if ($action_type == 'create') $missing_fields[] = _AT('course_id');
278                         if ($action_type == 'update') $missing_fields[] = _AT('content_id');
279                 }
280                 if ($title == '')
281                 {
282                         $missing_fields[] = _AT('title');
283                 }
284                 
285                 if ($missing_fields)
286                 {
287                         $missing_fields = implode(', ', $missing_fields);
288                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
289                 }
290                 
291                 if (!$msg->containsErrors())
292                         return true;
293                 else
294                         return false;
295         }
296
297 }
298 ?>