moved code up one level to eliminate the docs subdirectory
[acontent.git] / include / classes / DAO / UserCoursesDAO.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 "user_courses" 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 UserCoursesDAO extends DAO {
25
26         /**
27          * Create a new row
28          * @access  public
29          * @param   user_id
30          *          course_id
31          *          last_cid
32          * @return  true if a new row is inserted successfully or the record with (user_id, course_id) already exists 
33          *          otherwise, false
34          * @author  Cindy Qi Li
35          */
36         public function Create($user_id, $course_id, $role, $last_cid)
37         {
38                 // check whether the record already exists
39                 if (!$this->isExist($user_id, $course_id))
40                 {
41                         /* insert into the db */
42                         $sql = "INSERT INTO ".TABLE_PREFIX."user_courses
43                                       (user_id, course_id, role, last_cid)
44                                VALUES (".$user_id.",
45                                        ".$course_id.",
46                                        ".$role.",
47                                        ".$last_cid.")";
48
49                         return $this->execute($sql);
50                 }
51                 else
52                 {
53                         return true;
54                 }
55         }
56
57         /**
58          * Update last cid based on user id and course id
59          * @access  public
60          * @param   user_id
61          *          course_id
62          *          last_cid
63          * @return  true if successful 
64          *          otherwise, false
65          * @author  Cindy Qi Li
66          */
67         public function UpdateLastCid($user_id, $course_id, $last_cid)
68         {
69                 // only save last cid for courses that are on the user's course list
70                 if ($this->isExist($user_id, $course_id))
71                 {
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;
76         
77                         return $this->execute($sql);
78                 }
79                 else return true;
80         }
81
82         /**
83          * Delete a record
84          * @access  public
85          * @param   user id, course id
86          * @return  true, if successful, otherwise, return false
87          * @author  Cindy Qi Li
88          */
89         public function Delete($user_id, $course_id)
90         {
91                 $sql = "DELETE FROM ".TABLE_PREFIX."user_courses 
92                          WHERE user_id = ".$user_id." AND course_id = ".$course_id;
93                 return $this->execute($sql);
94         }
95
96         /**
97          * Return course information by given user id & course_id
98          * @access  public
99          * @param   user id
100          *          course_id
101          * @return  one row from table "user_courses"
102          * @author  Cindy Qi Li
103          */
104         public function get($user_id, $course_id)
105         {
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))
110                 {
111                         return $rows[0];
112                 }
113                 else return false;
114         }
115
116         /**
117          * Return course information by given user id
118          * @access  public
119          * @param   user id
120          * @return  course row
121          * @author  Cindy Qi Li
122          */
123         public function getByUserID($user_id)
124         {
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
128                          ORDER BY c.title";
129                 return $this->execute($sql);
130         }
131
132         /**
133          * Return course information by given user id
134          * @access  public
135          * @param   user id
136          * @return  course row
137          * @author  Cindy Qi Li
138          */
139         public function isExist($user_id, $course_id)
140         {
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);
145                 
146                 return is_array($rows);
147         }
148
149 }
150 ?>