/modules/event/classes/model/event.php

https://github.com/kodeplay/kodelearn · PHP · 143 lines · 105 code · 17 blank · 21 comment · 11 complexity · c79ba916cd4b37f7c50f715243b94b46 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Model_Event extends ORM {
  3. protected $_has_many = array(
  4. 'lectures' => array(
  5. 'model' => 'lecture',
  6. 'through' => 'lectures_events',
  7. ),
  8. );
  9. protected $_belongs_to = array(
  10. 'room' => array(
  11. 'model' => 'room',
  12. 'foreign_key' => 'room_id'
  13. )
  14. );
  15. public function validator($data) {
  16. return Validation::factory($data)
  17. ->rule('date', 'date')
  18. ->rule('date', 'not_empty')
  19. ->rule('from', 'not_empty')
  20. ->rule('to', 'not_empty');
  21. }
  22. /**
  23. * Method to get the teacher associated with this event
  24. * In case- no teacher is associated, then return 0
  25. * @return mixed (null|Model_User)
  26. */
  27. public function associated_teacher() {
  28. $event = Event_Abstract::factory($this->eventtype, $this->id);
  29. if (method_exists($event, 'associated_teacher')) {
  30. return $event->associated_teacher();
  31. }
  32. return null;
  33. }
  34. public function is_cancelled() {
  35. return (bool) $this->cancel;
  36. }
  37. /**
  38. * Method to get all the events in the specified month of the year
  39. * excluding the cancelled events
  40. * @param int $month 0 < $month < 12
  41. * @param int $year > 1970 due to unix time stamp
  42. * @return Database_MySQL_Result
  43. */
  44. public static function monthly_events($month, $year, $event_type = "") {
  45. // first date of the month
  46. $first = mktime(0, 0, 0, $month, 1, $year);
  47. // last date of the month
  48. $last = mktime(0, 0, 0, $month+1, 1, $year);
  49. // find out the padding days from previous and next months and also consider them
  50. $padding_days = array(
  51. 'prev' => (int)date('N', $first)%7,
  52. 'next' => 7 - (int) date('N', $last)
  53. );
  54. $first = $first - $padding_days['prev'] * (60*60*24);
  55. $last = $last + $padding_days['next'] * (60*60*24);
  56. $user = Acl::instance()->relevant_user();
  57. if ($user instanceof Model_User) {
  58. $courses = $user->courses->find_all()->as_array(null, 'id');
  59. $courses[] = 0;
  60. $event = ORM::factory('event')
  61. ->where('eventstart', 'BETWEEN', array($first, $last))
  62. ->where('events.course_id', 'IN', DB::expr('(' . implode(", ", $courses) . ')'))
  63. ->where('events.cancel', ' = ', 0);
  64. if($event_type != "") {
  65. $event->where('events.eventtype', '=', $event_type);
  66. }
  67. $events = $event->find_all();
  68. } else {
  69. $event = ORM::factory('event')
  70. ->where('eventstart', 'BETWEEN', array($first, $last))
  71. ->where('events.cancel', ' = ', 0);
  72. if($event_type != "") {
  73. $event->where('events.eventtype', '=', $event_type);
  74. }
  75. $events = $event->find_all();
  76. }
  77. return $events;
  78. }
  79. /**
  80. * Method to get all the events happening on a date
  81. * including the cancelled events and indicate if its cancelled
  82. * @param date format: 'YYYY-mm-dd'
  83. * @return Database_MySQL_Result
  84. */
  85. public static function daily_events($date) {
  86. $user = Acl::instance()->relevant_user();
  87. if ($user instanceof Model_User) {
  88. $courses = $user->courses->find_all()->as_array(null, 'id');
  89. $courses[] = 0;
  90. $event = ORM::factory('event')
  91. ->where(DB::expr('DATE(FROM_UNIXTIME(eventstart))'), ' = ', $date)
  92. ->where('events.course_id', 'IN', DB::expr('(' . implode(", ", $courses) . ')'))
  93. ->order_by('eventstart', 'ASC')
  94. ->find_all();
  95. } else {
  96. $event = ORM::factory('event')
  97. ->where(DB::expr('DATE(FROM_UNIXTIME(eventstart))'), ' = ', $date)
  98. ->order_by('eventstart', 'ASC')
  99. ->find_all();
  100. }
  101. return $event;
  102. }
  103. public function get_Attendance(){
  104. $user = Auth::instance()->get_user();
  105. $attendance = ORM::factory('attendance');
  106. $attendance->where('attendances.user_id','=',$user->id)
  107. ->where('attendances.event_id','=',$this->id) ;
  108. $attendances = $attendance->find();
  109. return $attendances;
  110. }
  111. public function get_conflict_event(){
  112. $sql = 'SELECT `events`.* FROM `events` WHERE ((`events`.`eventstart` BETWEEN :from AND :to OR `events`.`eventend` BETWEEN :from AND :to) OR (:from BETWEEN `events`.`eventstart` AND `events`.`eventend` OR :to BETWEEN `events`.`eventstart` AND `events`.`eventend`)) AND `events`.cancel = 0';
  113. $sql .= ' AND `events`.id != ' . (int) $this->id;
  114. $query = DB::query(Database::SELECT, $sql);
  115. $query->parameters(array(
  116. ':from' => $this->eventstart,
  117. ':to' => $this->eventend,
  118. ));
  119. $result = $query->execute()->as_array(NULL, 'id');
  120. if($result){
  121. return ORM::factory('event', $result[0]);
  122. } else {
  123. return false;
  124. }
  125. }
  126. }