/app/models/event.php

https://github.com/recrea/academic · PHP · 206 lines · 178 code · 20 blank · 8 comment · 15 complexity · 796acaafa8973bdf2fbaa72c524dc721 MD5 · raw file

  1. <?php
  2. require_once('models/academic_model.php');
  3. class Event extends AcademicModel {
  4. var $name = "Event";
  5. var $hasOne = "AttendanceRegister";
  6. var $belongsTo = array(
  7. 'Group' => array(
  8. 'className' => 'Group'
  9. ),
  10. 'Activity' => array(
  11. 'className' => 'Activity'
  12. ),
  13. 'Classroom' => array(
  14. 'className' => 'Classroom'
  15. ),
  16. 'Teacher' => array(
  17. 'className' => 'User',
  18. 'conditions' => array("(Teacher.type = 'Profesor' OR Teacher.type = 'Administrador')")
  19. ),
  20. 'Teacher_2' => array(
  21. 'className' => 'User',
  22. 'conditions' => array("(Teacher.type = 'Profesor' OR Teacher.type = 'Administrador')")
  23. ),
  24. 'Parent' => array(
  25. 'className' => 'Event',
  26. 'foreignKey' => 'parent_id'
  27. )
  28. );
  29. var $hasMany = array(
  30. 'Events' => array(
  31. 'className' => 'Event',
  32. 'foreignKey' => 'parent_id'
  33. )
  34. );
  35. var $validate = array(
  36. 'classroom_id' => array(
  37. 'notEmpty' => array(
  38. 'rule' => 'notEmpty',
  39. 'required' => true,
  40. 'message' => 'Debe especificar un aula para este evento'
  41. )
  42. ),
  43. 'initial_hour' => array(
  44. 'notEmpty' => array(
  45. 'rule' => 'notEmpty',
  46. 'required' => true,
  47. 'message' => 'Debe especificar una fecha de inicio para este evento'
  48. ),
  49. 'eventDontOverlap' => array(
  50. 'rule' => array('eventDontOverlap')
  51. ),
  52. 'eventDurationDontExceedActivityDuration' => array(
  53. 'rule' => array('eventDurationDontExceedActivityDuration')
  54. )
  55. ),
  56. 'final_hour' => array(
  57. 'notEmpty' => array(
  58. 'rule' => 'notEmpty',
  59. 'required' => true,
  60. 'message' => 'Debe especificar una fecha de inicio para este evento'
  61. ),
  62. ),
  63. 'teacher_id' => array(
  64. 'notEmpty' => array(
  65. 'rule' => 'notEmpty',
  66. 'required' => true,
  67. 'message' => 'Debe especificar un profesor'
  68. )
  69. )
  70. );
  71. function eventDontOverlap($initial_hour){
  72. $initial_hour = $this->data['Event']['initial_hour'];
  73. $final_hour = $this->data['Event']['final_hour'];
  74. $classroom_id = $this->data['Event']['classroom_id'];
  75. $query = "SELECT Event.id FROM events Event WHERE ((Event.initial_hour <= '{$initial_hour}' AND Event.final_hour > '{$initial_hour}') OR (Event.initial_hour < '{$final_hour}' AND Event.final_hour >= '{$final_hour}') OR (Event.initial_hour >= '{$initial_hour}' AND Event.final_hour <= '{$final_hour}')) AND Event.classroom_id = {$classroom_id}";
  76. if ((isset($this->data['Event']['id'])) && ($this->data['Event']['id'] > 0))
  77. $query .= " AND Event.id <> {$this->data['Event']['id']}";
  78. $events_count = $this->query($query);
  79. if (count($events_count) > 0) {
  80. $this->id = $events_count[0]['Event']['id'];
  81. return false;
  82. }
  83. else
  84. return (count($events_count) == 0);
  85. }
  86. function eventDurationDontExceedActivityDuration($initial_hour){
  87. $activity = $this->Activity->find('first', array('conditions' => array('Activity.id' => $this->data['Event']['activity_id'])));
  88. $query = "SELECT activity_id, group_id, sum(duration) as scheduled from events Event WHERE activity_id = {$activity['Activity']['id']} AND group_id = {$this->data['Event']['group_id']}";
  89. if (isset($this->data['Event']['id']))
  90. $query .= " AND Event.id <> {$this->data['Event']['id']}";
  91. $query .= " group by activity_id, group_id";
  92. $duration = $this->query($query);
  93. if ((isset($duration[0])) && (isset($duration[0][0]['scheduled'])) && ($duration[0][0]['scheduled'] != null) )
  94. $duration = $duration[0][0]['scheduled'];
  95. else
  96. $duration = 0;
  97. if ( ($duration + $this->data['Event']['duration']) > $activity['Activity']['duration']){
  98. $this->id = -1;
  99. return false;
  100. }
  101. else
  102. return true;
  103. }
  104. function beforeValidate(){
  105. if (!empty($this->data['Event']['initial_hour'])) {
  106. $initial_hour = date_create($this->data['Event']['initial_hour']);
  107. $this->data['Event']['initial_hour'] = $initial_hour->format('Y-m-d H:i:s');
  108. }
  109. if (!empty($this->data['Event']['final_hour'])){
  110. $final_hour = date_create($this->data['Event']['final_hour']);
  111. $this->data['Event']['final_hour'] = $final_hour->format('Y-m-d H:i:s');
  112. }
  113. if ((!empty($this->data['Event']['initial_hour'])) && (!empty($this->data['Event']['final_hour'])))
  114. $this->data['Event']['duration'] = $this->_get_event_duration($initial_hour, $final_hour);
  115. return true;
  116. }
  117. function _get_event_duration($initial_hour, $final_hour) {
  118. // Hour, minute, second, month, day, year
  119. $initial_timestamp = $this->_get_timestamp($initial_hour);
  120. $final_timestamp = $this->_get_timestamp($final_hour);
  121. return ($final_timestamp - $initial_timestamp) / 3600.0;
  122. }
  123. function _get_timestamp($date){
  124. $date_components = split("-", $date->format('Y-m-d-H-i-s'));
  125. return mktime($date_components[3],$date_components[4],$date_components[5], $date_components[1], $date_components[2], $date_components[0]);
  126. }
  127. function findRegisteredStudents($id = null) {
  128. $event = $this->find('first', array('conditions' => array('Event.id' => $id), 'recursive' => -1));
  129. return $this->AttendanceRegister->Student->find('all', array(
  130. 'joins' => array(
  131. array(
  132. 'table' => 'registrations',
  133. 'alias' => 'Registration',
  134. 'type' => 'INNER',
  135. 'conditions' => array('Registration.student_id = Student.id'),
  136. ),
  137. ),
  138. 'conditions' => array(
  139. 'Registration.group_id' => $event['Event']['group_id'],
  140. 'Registration.activity_id' => $event['Event']['activity_id'],
  141. ),
  142. 'fields' => array('Student.id', 'Student.first_name', 'Student.last_name'),
  143. 'recursive' => -1,
  144. 'order' => array('Student.last_name', 'Student.first_name'),
  145. ));
  146. }
  147. /**
  148. * Finds all events on a given date
  149. *
  150. * @param date Date when events take place
  151. * @return Array of events
  152. * @since 2013-03-10
  153. */
  154. function findAllByDate($date = '') {
  155. if (empty($date)) {
  156. return array();
  157. }
  158. $this->Behaviors->attach('Containable');
  159. $this->unbindModel(array(
  160. 'belongsTo' => array('Activity')
  161. ));
  162. $this->bindModel(array(
  163. 'hasOne' => array(
  164. 'Activity' => array(
  165. 'foreignKey' => false,
  166. 'conditions' => array('Activity.id = Event.activity_id')
  167. ),
  168. 'Subject' => array(
  169. 'foreignKey' => false,
  170. 'conditions' => array('Subject.id = Activity.subject_id')
  171. )
  172. )
  173. ));
  174. return $this->find('all', array(
  175. 'fields' => array(
  176. 'Event.initial_hour', 'Event.final_hour',
  177. 'Teacher.first_name', 'Teacher.last_name',
  178. 'Classroom.name', 'Subject.name'
  179. ),
  180. 'contain' => array('Teacher', 'Classroom', 'Activity', 'Subject'),
  181. 'conditions' => array('Event.initial_hour >= ' => $date . ' 00:00:00', 'Event.final_hour <=' => $date . ' 23:59:59'),
  182. 'order' => array('Event.initial_hour'),
  183. ));
  184. }
  185. }
  186. ?>