/models/CourseProfile.php

https://github.com/ashwanthkumar/webnaplo · PHP · 337 lines · 175 code · 48 blank · 114 comment · 50 complexity · cc79aa2bc818a170b948eaf83b70233c MD5 · raw file

  1. <?php
  2. /**
  3. * Course Profile Model class
  4. *
  5. * @since 1.1
  6. *
  7. * @Changes:
  8. *
  9. * 1. Updated the model to suite the new class model
  10. **/
  11. class CourseProfile {
  12. public $idcourse_profile;
  13. public $name;
  14. public $course_id;
  15. public $staff_id;
  16. public $syllabus;
  17. public $enable_confirm;
  18. /**
  19. * Save the current instance of the object to the datastore
  20. *
  21. * @param $db PDOObject
  22. *
  23. * @return 1 if successful, else FALSE on error
  24. **/
  25. public function save($db) {
  26. $r = $db->insert("course_profile", array(
  27. "name" => $this->name,
  28. "course_id" => $this->course_id,
  29. "syllabus" => $this->syllabus,
  30. "staff_id" => $this->staff_id
  31. ));
  32. if(is_object($r) && get_class($r) == "PDOException") return FALSE;
  33. $this->idcourse_profile = $db->lastInsertId();
  34. $r1 = LockUnLock::addCourseProfile($this->idcourse_profile, $db);
  35. if(is_object($r1) && get_class($r1) == "PDOException") return $r1;
  36. return $r;
  37. }
  38. /**
  39. * Updates the current instance of the object in the datastore
  40. *
  41. * @param $db PDOObject
  42. *
  43. * @return 1 if successful, else PDOException
  44. **/
  45. public function update($db) {
  46. return $db->update("course_profile", array(
  47. "name" => $this->name,
  48. "course_id" => $this->course_id,
  49. "syllabus" => $this->syllabus,
  50. "staff_id" => $this->staff_id,
  51. "enable_confirm" => $this->enable_confirm
  52. ), "idcourse_profile = :cid", array(":cid" => $this->idcourse_profile));
  53. }
  54. /**
  55. * Delete an instance of the Course Profile object in the datastore
  56. *
  57. * @param $cpid Course Profile ID
  58. * @param $db PDOObject
  59. **/
  60. public static function Delete($cpid, $db) {
  61. return $db->delete("course_profile", "idcourse_profile = :cc", array(":cc" => $cpid));
  62. }
  63. /**
  64. * Delete the course profile with Staff ID Check to ensure staff can delete only their course profiles
  65. *
  66. * @param $cpid Value or Array of Course Profile IDs to delete
  67. * @param $staffid Staff ID (PK of Staff) who is the owner of these Course profiles
  68. *
  69. * @return TRUE When the $cpid is an array and it has executed fine
  70. * 1 If the $cpid is a value, and it has executed fine.
  71. * FALSE In case of an error
  72. **/
  73. public static function DeleteByStaff($cpid, $staffid, $db) {
  74. if(is_array($cpid)) {
  75. // Loop over all the array values and delete them one by one
  76. foreach($cpid as $cid) {
  77. $return = $db->delete("course_profile", "idcourse_profile = :cc and staff_id = :sid", array(":cc" => $cid, ":sid" => $staffid));
  78. // Have to find a better way to do this
  79. if(is_object($return) && get_class($return) == "PDOException") return FALSE;
  80. }
  81. return TRUE;
  82. } else {
  83. $r = $db->delete("course_profile", "idcourse_profile = :cc and staff_id = :sid", array(":cc" => $cpid, ":sid" => $staffid));
  84. if(!(is_object($r) && get_class($r) == "PDOException")) return 1;
  85. else return FALSE;
  86. }
  87. }
  88. /**
  89. * Load and update the instance of the object in the datastore from the $_POST array
  90. *
  91. * @param $cp $_POST Array value
  92. * @param $db PDOObject
  93. *
  94. * @return 1 If successful, else
  95. * PDOException Object
  96. **/
  97. public static function LoadAndUpdate($cp, $db, &$course_object = null) {
  98. extract($cp);
  99. $cprofile = new CourseProfile;
  100. $cprofile->idcourse_profile = $idcourse_profile;
  101. $cprofile->name = $name;
  102. $cprofile->course_id = $course_id;
  103. $cprofile->staff_id = $staff_id;
  104. if(isset($syllabus)) $cprofile->syllabus = $syllabus;
  105. $r = $cprofile->update($db);
  106. $course_object = $cprofile;
  107. return $r;
  108. }
  109. /**
  110. * Load and Save the current instance of the object. Generally to be used while creating the Model Object in the system.
  111. *
  112. * @param $cp Array of Model properties
  113. *
  114. * @return 1 If successful, PDOException Object
  115. **/
  116. public static function LoadAndSave($cp, $db, &$course_object = null) {
  117. extract($cp);
  118. // Check if the Staff has already created a Course Profile for the Course
  119. $staff_cps = $db->select("course_profile", "course_id = :cid and staff_id = :sid", array(":cid" => $course_id, ":sid" => $staff_id));
  120. if(count($staff_cps) > 0) {
  121. // Course Profile already exist for the given course for the staff
  122. return false;
  123. }
  124. $cprofile = new CourseProfile;
  125. $cprofile->name = $name;
  126. $cprofile->course_id = $course_id;
  127. $cprofile->staff_id = $staff_id;
  128. if(isset($syllabus)) $cprofile->syllabus = $syllabus;
  129. $r = $cprofile->save($db);
  130. $course_object = $cprofile;
  131. return $r;
  132. }
  133. /**
  134. * Add the students to the current course profile
  135. *
  136. * @param $student Student(s) registernumber to add
  137. * @return number of students successfully added
  138. **/
  139. public function addStudent($students, $db, &$added_reg_nos = null) {
  140. $added_reg_nos = array();
  141. if(is_array($students)) {
  142. $numberOfStudentsInserted = 0;
  143. /**
  144. * @TODO This method of batch inserting the data is not sercure and a good practice.
  145. * Need to use Transactioons to maintain consistency in the datastore.
  146. **/
  147. // Array of registeration numbers to add
  148. foreach($students as $student) {
  149. $r = $db->insert("cp_has_student", array(
  150. "cp_id" => $this->idcourse_profile,
  151. "idstudent" => $student
  152. ));
  153. if(is_object($r) && get_class($r) == "PDOException") {
  154. error_log($r->getMessage());
  155. continue; // Need to use transactions to revert the datastore
  156. }
  157. // Now Create a record in cia_marks table
  158. $cia_marks = new CIAMarks;
  159. $cia_marks->assignment = NULL;
  160. $cia_marks->mark_1 = NULL;
  161. $cia_marks->mark_2 = NULL;
  162. $cia_marks->mark_3 = NULL;
  163. $cia_marks->cp_id = $this->idcourse_profile;
  164. $cia_marks->student_id = $student;
  165. $cia_marks_status = $cia_marks->save($db);
  166. if(is_object($cia_marks_status) && get_class($cia_marks_status) == "PDOException") {
  167. error_log($cia_marks_status->getMessage());
  168. continue; // Need to use transactions to revert the datastore
  169. }
  170. $numberOfStudentsInserted++;
  171. $added_reg_nos[] = $student;
  172. }
  173. // Return the number of students added to the system
  174. return $numberOfStudentsInserted;
  175. } else {
  176. // Add a single student to the course profile
  177. $r = $db->insert("cp_has_student", array(
  178. "cp_id" => $this->idcourse_profile,
  179. "idstudent" => $students
  180. ));
  181. // Make sure the insert was successful
  182. if((is_object($r) && get_class($r) == "PDOException")) {
  183. trigger_error($cia_marks_status->getMessage());
  184. return 0;
  185. }
  186. // Now Create a record in cia_marks table
  187. $cia_marks = new CIAMarks;
  188. $cia_marks->assignment = NULL;
  189. $cia_marks->mark_1 = NULL;
  190. $cia_marks->mark_2 = NULL;
  191. $cia_marks->mark_3 = NULL;
  192. $cia_marks->cp_id = $this->course_profile;
  193. $cia_marks->student_id = $student;
  194. $cia_marks_status = $cia_marks->save($db);
  195. if(is_object($cia_marks_status) && get_class($cia_marks_status) == "PDOException") {
  196. trigger_error($cia_marks_status->getMessage());
  197. return 0; // Need to use transactions to revert the datastore
  198. }
  199. $added_reg_nos[] = $students;
  200. return 1;
  201. }
  202. }
  203. /**
  204. * Remove the students from the current course profile
  205. *
  206. * @param $students Student(s) registernumber to delete
  207. * @return Number of students deleted
  208. **/
  209. public function removeStudent($students, $db) {
  210. if(is_array($students)) {
  211. $numberOfStudentsInserted = 0;
  212. // Array of registeration numbers to add
  213. foreach($students as $student) {
  214. $r = $db->delete("cp_has_student", "cp_id = :cpid and idstudent = :sid",array(
  215. ":cpid" => $this->idcourse_profile,
  216. ":sid" => $student
  217. ));
  218. if((is_object($r) && get_class($r) == "PDOException")) continue;
  219. $delete_status = CIAMarks::Delete($student, $this->idcourse_profile, $db);
  220. if((is_object($delete_status) && get_class($delete_status) == "PDOException")) continue;
  221. $numberOfStudentsInserted++;
  222. }
  223. // Return the number of students added to the system
  224. return $numberOfStudentsInserted;
  225. } else {
  226. // Add a single student to the course profile
  227. $r = $db->delete("cp_has_student", "cp_id = :cpid and idstudent = :sid",array(
  228. ":cpid" => $this->idcourse_profile,
  229. ":sid" => $students
  230. ));
  231. if((is_object($r) && get_class($r) == "PDOException")) return 0;
  232. $r = CIAMarks::Delete($students, $this->idcourse_profile, $db);
  233. if((is_object($r) && get_class($r) == "PDOException")) return 0;
  234. // Make sure the insert was successful
  235. return 1;
  236. }
  237. }
  238. /**
  239. * Get all the students attached with the current Course Profile
  240. *
  241. * @param $db PDOObject
  242. *
  243. * @return Array of Students with idstudent, name, cpid
  244. **/
  245. public function getStudents($db) {
  246. $list = $db->run("select s.idstudent as idstudent, s.name as name, cp.idcourse_profile as cpid from student s, course_profile cp, cp_has_student chs where chs.idstudent = s.idstudent and chs.cp_id = cp.idcourse_profile and cp.idcourse_profile = :cpid order by s.idstudent", array(":cpid" => $this->idcourse_profile));
  247. /**
  248. * @TODO May be we can get the register numbers and get the Student Object using the Student::load() and return array of Students objects.
  249. * Wouldn't that be more interactive to show more details in the UI?
  250. **/
  251. // Return only the students register number
  252. return $list;
  253. }
  254. /**
  255. * Load the current instance of the object from the datastore
  256. *
  257. * @param $cpid Course Profile ID
  258. * @param $db PDOObject
  259. *
  260. * @return An instance of CourseProfile
  261. **/
  262. public static function load($cpid, $db) {
  263. $r = $db->select("course_profile", "idcourse_profile = :cpid", array(":cpid" => $cpid));
  264. if((is_object($r) && get_class($r) == "PDOException")) return FALSE;
  265. else {
  266. extract($r[0]);
  267. $cprofile = new CourseProfile;
  268. $cprofile->idcourse_profile = $idcourse_profile;
  269. $cprofile->name = $name;
  270. $cprofile->course_id = $course_id;
  271. $cprofile->staff_id = $staff_id;
  272. $cprofile->syllabus = $syllabus;
  273. $cprofile->enable_confirm = $enable_confirm;
  274. return $cprofile;
  275. }
  276. }
  277. /**
  278. * Search the Model entities in the datastore
  279. *
  280. * @param $db PDOObject
  281. * @param $condition Search Condition
  282. * @param $bind Array of bound values used in $condition
  283. *
  284. * @return Array of Model entities matching the $condition
  285. **/
  286. public static function search($db, $condition = '1=1', $bind = array()) {
  287. return $db->select("course_profile", $condition, $bind);
  288. }
  289. }