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 /************************************************************************/
14 * DAO for "user_courses" table
20 if (!defined('TR_INCLUDE_PATH')) exit;
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
24 class UserCoursesDAO extends DAO {
32 * @return true if a new row is inserted successfully or the record with (user_id, course_id) already exists
36 public function Create($user_id, $course_id, $role, $last_cid)
38 // check whether the record already exists
39 if (!$this->isExist($user_id, $course_id))
41 /* insert into the db */
42 $sql = "INSERT INTO ".TABLE_PREFIX."user_courses
43 (user_id, course_id, role, last_cid)
49 return $this->execute($sql);
58 * Update last cid based on user id and course id
63 * @return true if successful
67 public function UpdateLastCid($user_id, $course_id, $last_cid)
69 // only save last cid for courses that are on the user's course list
70 if ($this->isExist($user_id, $course_id))
72 $sql = "UPDATE ".TABLE_PREFIX."user_courses
73 SET last_cid = ".$last_cid."
74 WHERE user_id = ".$user_id."
75 AND course_id = ".$course_id;
77 return $this->execute($sql);
85 * @param user id, course id
86 * @return true, if successful, otherwise, return false
89 public function Delete($user_id, $course_id)
91 $sql = "DELETE FROM ".TABLE_PREFIX."user_courses
92 WHERE user_id = ".$user_id." AND course_id = ".$course_id;
93 return $this->execute($sql);
97 * Return course information by given user id & course_id
101 * @return one row from table "user_courses"
102 * @author Cindy Qi Li
104 public function get($user_id, $course_id)
106 $sql = "SELECT * FROM ".TABLE_PREFIX."user_courses
107 WHERE user_id=".$user_id."
108 AND course_id = ".$course_id;
109 if ($rows = $this->execute($sql))
117 * Return course information by given user id
121 * @author Cindy Qi Li
123 public function getByUserID($user_id)
125 $sql = "SELECT * FROM ".TABLE_PREFIX."user_courses uc, ".TABLE_PREFIX."courses c
126 WHERE uc.user_id=".$user_id."
127 AND uc.course_id = c.course_id
129 return $this->execute($sql);
133 * Return course information by given user id
137 * @author Cindy Qi Li
139 public function isExist($user_id, $course_id)
141 $sql = "SELECT * FROM ".TABLE_PREFIX."user_courses
142 WHERE user_id=".$user_id."
143 AND course_id=".$course_id;
144 $rows = $this->execute($sql);
146 return is_array($rows);