9f4056069543ddd9827e673edf09a54d5a4cb27b
[acontent.git] / docs / include / classes / DAO / TestsDAO.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 "tests" 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 require_once(TR_INCLUDE_PATH. 'classes/Utility.class.php');
24
25 class TestsDAO extends DAO {
26
27         /**
28          * Create a new row
29          * @access  public
30          * @param   
31          * @return  true, if successful
32          *          false and add error into global var $msg, if unsuccessful
33          * @author  Cindy Qi Li
34          */
35         public function Create($course_id, $title, $description)
36         {
37                 global $addslashes;
38                 
39                 $title = Utility::validateLength($addslashes(trim($title)), 100);
40                 $description = $addslashes(trim($description));
41
42                 if ($this->isFieldsValid($title))
43                 {
44                         $sql = "INSERT INTO ".TABLE_PREFIX."tests " .
45                                "(course_id, title, description)" .
46                                "VALUES ($course_id, '$title', '$description')";
47         
48                         if (!$this->execute($sql))
49                         {
50                                 $msg->addError('DB_NOT_UPDATED');
51                                 return false;
52                         }
53                         else
54                         {
55                                 return mysql_insert_id();
56                         }
57                 }
58                 else
59                 {
60                         return false;
61                 }
62         }
63
64         /**
65          * Update an existing user record
66          * @access  public
67          * @param   testID, title, description
68          * @return  user id, if successful
69          *          false and add error into global var $msg, if unsuccessful
70          * @author  Cindy Qi Li
71          */
72         public function Update($testID, $title, $description)
73         {
74                 global $addslashes;
75                 
76                 $title = Utility::validateLength($addslashes(trim($title)), 100);
77                 $description = $addslashes(trim($description));
78
79                 if ($this->isFieldsValid($title))
80                 {
81                         $sql = "UPDATE ".TABLE_PREFIX."tests " . 
82                                "SET title='$title', 
83                                     description='$description' 
84                                 WHERE test_id=$testID";
85                         
86                         return $this->execute($sql);
87                 }
88         }
89         
90         /**
91          * Delete content
92          * @access  public
93          * @param   test ID
94          * @return  true, if successful
95          *          false and add error into global var $msg, if unsuccessful
96          * @author  Cindy Qi Li
97          */
98         public function Delete($testID)
99         {
100                 $sql = "DELETE FROM ".TABLE_PREFIX."tests WHERE test_id = ".$testID;
101                 return $this->execute($sql);
102         }
103
104         /**
105          * Return content information by given content id
106          * @access  public
107          * @param   test id
108          * @return  test row
109          * @author  Cindy Qi Li
110          */
111         public function get($testID)
112         {
113                 $sql = 'SELECT * FROM '.TABLE_PREFIX.'tests WHERE test_id='.$testID;
114                 if ($rows = $this->execute($sql))
115                 {
116                         return $rows[0];
117                 }
118                 else return false;
119         }
120
121         /**
122          * Return max ordering based on given course id and content parent id 
123          * @access  public
124          * @param   course_id
125          * @return  test rows
126          * @author  Cindy Qi Li
127          */
128         public function getByCourseID($courseID)
129         {
130                 $sql = "SELECT * 
131                           FROM ".TABLE_PREFIX."tests 
132                          WHERE course_id=$courseID";
133                 return $this->execute($sql);
134         }
135
136         /**
137          * Validates fields preparing for insert and update
138          * @access  private
139          * @param   $title
140          *          $random
141          *          $num_questions
142          *          $pass_score_checkbox
143          *          $passscore
144          *          $passpercent
145          * @return  true    if update successfully
146          *          false   if update unsuccessful
147          * @author  Cindy Qi Li
148          */
149         private function isFieldsValid($title)
150         {
151                 global $msg;
152                 
153                 $missing_fields = array();
154                 
155                 if ($title == '') {
156                         $missing_fields[] = _AT('title');
157                 }
158         
159                 if ($missing_fields) {
160                         $missing_fields = implode(', ', $missing_fields);
161                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
162                 }
163         
164                 if (!$msg->containsErrors()) return true;
165                 else return false;
166         }
167 }
168 ?>