2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
13 if (!defined('TR_INCLUDE_PATH')) exit;
15 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
17 class ContentDAO extends DAO {
23 * @return user id, if successful
24 * false and add error into global var $msg, if unsuccessful
27 public function Create($course_id, $content_parent_id, $ordering, $revision, $formatting, $keywords,
28 $content_path, $title, $text, $head, $use_customized_head, $test_message,
31 global $addslashes, $msg;
33 if ($this->isFieldsValid('create', $course_id, $title))
35 /* insert into the db */
36 $sql = "INSERT INTO ".TABLE_PREFIX."content
52 VALUES (".$course_id.",
53 ".$content_parent_id.",
58 '".$addslashes($keywords)."',
60 '".$addslashes($title)."',
61 '".$addslashes($text)."',
62 '".$addslashes($head)."',
63 ".$use_customized_head.",
64 '".$addslashes($test_message)."',
67 if (!$this->execute($sql))
69 $msg->addError('DB_NOT_UPDATED');
74 $cid = mysql_insert_id();
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");
91 * Update an existing content record
93 * @param userID: user ID (1 [admin] or 2 [user])
97 * first_name: first name
98 * last_name: last name
100 * @return true, if successful
101 * false and add error into global var $msg, if unsuccessful
102 * @author Cindy Qi Li
104 public function Update($content_id, $title, $text, $keywords, $formatting,
105 $head, $use_customized_head, $test_message)
107 global $addslashes, $msg;
109 if ($this->isFieldsValid('update', $content_id, $title))
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;
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");
129 $msg->addError('DB_NOT_UPDATED');
139 * Update one field of an existing content record
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
148 public function UpdateField($contentID, $fieldName, $fieldValue)
152 $sql = "UPDATE ".TABLE_PREFIX."content
153 SET ".$fieldName."='".$addslashes($fieldValue)."'
154 WHERE content_id = ".$contentID;
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");
163 $msg->addError('DB_NOT_UPDATED');
172 * @return true, if successful
173 * false and add error into global var $msg, if unsuccessful
174 * @author Cindy Qi Li
176 public function Delete($contentID)
180 require_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
181 $a4a = new A4a($contentID);
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);
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);
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");
202 $msg->addError('DB_NOT_UPDATED');
208 * Return content information by given content id
211 * @return content row
212 * @author Cindy Qi Li
214 public function get($contentID)
216 $sql = 'SELECT * FROM '.TABLE_PREFIX.'content WHERE content_id='.$contentID;
217 if ($rows = $this->execute($sql))
225 * Return all content rows by given course id
228 * @return content rows
229 * @author Cindy Qi Li
231 public function getContentByCourseID($courseID)
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";
239 return $this->execute($sql);
243 * Return max ordering based on given course id and content parent id
245 * @param course_id, content_parent_id
246 * @return max ordering: int
247 * @author Cindy Qi Li
249 public function getMaxOrdering($course_id, $content_parent_id)
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']);
259 * Validate fields preparing for insert and update
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
269 private function isFieldsValid($action_type, $row_id, $title)
273 $missing_fields = array();
275 if (intval($row_id) == 0)
277 if ($action_type == 'create') $missing_fields[] = _AT('course_id');
278 if ($action_type == 'update') $missing_fields[] = _AT('content_id');
282 $missing_fields[] = _AT('title');
287 $missing_fields = implode(', ', $missing_fields);
288 $msg->addError(array('EMPTY_FIELDS', $missing_fields));
291 if (!$msg->containsErrors())