/class/Absence.php

https://github.com/westernmagic/schades · PHP · 327 lines · 138 code · 30 blank · 159 comment · 22 complexity · afb2b3bf5a09a8ccb2db8c00c0c14a00 MD5 · raw file

  1. <?php
  2. /*
  3. This file is part of schades.
  4. schades is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. schades is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with schades. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file Absence.php
  17. *
  18. * @author Michal Sudwoj <mswoj61@gmail.com>
  19. * @copyright Michal Sudwoj
  20. * @link http://www.sourceforge.com/projects/schades/
  21. * @licence http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  22. * @version 0.8
  23. */
  24. require_once( 'include.php' ) ;
  25. /**
  26. * @brief Absence class
  27. */
  28. class Absence extends SuperClass {
  29. /**
  30. * @brief int
  31. */
  32. private $id ;
  33. /**
  34. * @brief string
  35. */
  36. private $type ;
  37. /**
  38. * @brief Date
  39. */
  40. private $date ;
  41. /**
  42. * @brief int
  43. */
  44. private $week ;
  45. /**
  46. * @brief int
  47. */
  48. private $day ;
  49. /**
  50. * @brief int
  51. */
  52. private $lesson ;
  53. /**
  54. * @brief Constructor
  55. *
  56. * @param int $id
  57. */
  58. public function __construct ( $id ) {
  59. $this->id = $id ;
  60. }
  61. /**
  62. * @brief Initialize basic variables
  63. */
  64. private function getData() {
  65. $query = "SELECT " . DB::name( 'abs_abs' , 'date' ) . " , " . DB::name( 'abs_abs' , 'week' ) . " , " . DB::name( 'abs_abs' , 'day' ) . " , " . DB::name( 'abs_abs' , 'lesson' ) . " , " . DB::name( 'abs_abs' , 'abs_type' ) ;
  66. $query .= " FROM " . DB::name( 'abs_abs' ) ;
  67. $query .= " WHERE " . DB::name( 'abs_abs' , 'abs_id' ) . " = " . DB::escape( $this->getId() ) . " ; " ;
  68. if( $resultset = DB::query( $query ) ) {
  69. if( DB::count( $resultset ) == 1 ) {
  70. $row = DB::fetch( $resultset ) ;
  71. $this->date = new Date( $row['date'] ) ;
  72. if( $row['week'] >= 1 and $row['week'] <= 52 and $row['day'] >= 1 and $row['day'] <= 5 ) {
  73. $this->week = $row['week'] ;
  74. $this->day = $row['day'] ;
  75. } elseif ( strtotime( $row['date'] ) >= strtotime( '000-00-00' ) ) {
  76. $this->week = $this->getDate()->getWeek() ;
  77. $this->day = $this->getDate()->getDay() ;
  78. } else {
  79. throw new ErrorException( 'Absence data error' , 802 , 1 , 'Absence.php' , 39 ) ;
  80. }
  81. $this->lesson = $row['lesson'] ;
  82. $this->type = $row['abs_type'] ;
  83. }
  84. DB::free( $resultset ) ;
  85. }
  86. }
  87. /**
  88. * @return int $id
  89. */
  90. public function getId() {
  91. return $this->id ;
  92. }
  93. /**
  94. * @return string $type
  95. */
  96. public function getType() {
  97. if( !isset( $this->type ) ) {
  98. $this->getData() ;
  99. }
  100. return $this->type ;
  101. }
  102. /**
  103. * @return Date $date
  104. */
  105. public function getDate() {
  106. if( !isset( $this->date ) ) {
  107. $this->getData() ;
  108. }
  109. return $this->date ;
  110. }
  111. /**
  112. * @return int $week
  113. */
  114. public function getWeek() {
  115. if( !isset( $this->week ) ) {
  116. $this->getData() ;
  117. }
  118. return $this->week ;
  119. }
  120. /**
  121. * @return int $day
  122. */
  123. public function getDay() {
  124. if( !isset( $this->day ) ) {
  125. $this->getData() ;
  126. }
  127. return $this->day ;
  128. }
  129. /**
  130. * @return int $lesson
  131. */
  132. public function getLesson() {
  133. if( !isset( $this->lesson ) ) {
  134. $this->getData() ;
  135. }
  136. return $this->lesson ;
  137. }
  138. /**
  139. * @brief Sets the type
  140. *
  141. * @deprecated never used
  142. * @param string $var type
  143. * @return bool
  144. * @deprecated doe not log!
  145. */
  146. public function setType( $var ) {
  147. $query = "UPDATE " . DB::name( 'abs_abs' ) ;
  148. $query .= " SET " . DB::name( 'abs_abs' , 'abs_type' ) . " = " . DB::escape( $var ) ;
  149. $query .= " WHERE " . DB::name( 'abs_abs' , 'abs_id' ) . " = " . DB::escape( $this->id ) . ' ;' ;
  150. if( DB::query( $query ) ) {
  151. $this->type = $var ;
  152. return true ;
  153. } else {
  154. return false ;
  155. }
  156. }
  157. /**
  158. * @param int $week
  159. * @param int $day
  160. * @param int @lesson
  161. * @param int $value
  162. * @param int duration
  163. * @return Absence Excuse dummy absence
  164. */
  165. public static function AbsExcuse( $week , $day , $lesson , $value , $duration ) {
  166. $excuse = new Absence( 0 ) ;
  167. $excuse->week = $week ;
  168. $excuse->day = $day ;
  169. $excuse->lesson = $lesson ;
  170. $excuse->type = 'e#' . $value . '#' . $duration ;
  171. return $excuse ;
  172. }
  173. /**
  174. * @brief Inserts / updates an Absence
  175. *
  176. * @param int $ad
  177. * @param int $week
  178. * @param int $day
  179. * @param int $lesson
  180. * @param string $type
  181. * @param int $exec
  182. * @return Absence
  183. */
  184. public static function newAbsence( $ad , $week , $day , $lesson , $type , $exec ) {
  185. /*
  186. $query = 'INSERT INTO ' . DB::name( 'abs_abs' ) ;
  187. $query .= ' SET' ;
  188. $query .= ' ' . DB::name( 'abs_abs' , 'abs_id' ) . ' = DEFAULT ,' ;
  189. $query .= ' ' . DB::name( 'abs_abs' , 'ad_id' ) . ' = ' . DB::escape( $ad ) . ' ,' ;
  190. $query .= ' ' . DB::name( 'abs_abs' , 'week' ) . ' = ' . DB::escape( $week ) . ' ,' ;
  191. $query .= ' ' . DB::name( 'abs_abs' , 'day' ) . ' = ' . DB::escape( $day ) . ' ,' ;
  192. $query .= ' ' . DB::name( 'abs_abs' , 'lesson' ) . ' = ' . DB::escape( $lesson ) . ' ,' ;
  193. $query .= ' ' . DB::name( 'abs_abs' , 'abs_type' ) . ' = "' . DB::escape( $type ) . '"' ;
  194. $query .= ' ON DUPLICATE KEY UPDATE' ;
  195. $query .= ' ' . DB::name( 'abs_abs' , 'abs_type' ) . ' = "' . DB::escape( $type ) . '" ;' ;
  196. if( DB::query( $query ) ) {
  197. $id = DB::insertId() ;
  198. if( $id === false ) {
  199. return false ;
  200. } else{
  201. if( $id == 0 ) {
  202. $query = 'SELECT ' . DB::name( 'abs_abs' , 'abs_id' ) ;
  203. $query .= ' FROM ' . DB::name( 'abs_abs' ) ;
  204. $query .= ' WHERE ' . DB::name( 'abs_abs' , 'ad_id' ) . ' = ' . DB::escape( $ad ) ;
  205. $query .= ' AND ' . DB::name( 'abs_abs' , 'week' ) . ' = ' . DB::escape( $week ) ;
  206. $query .= ' AND ' . DB::name( 'abs_abs' , 'day' ) . ' = ' . DB::escape( $day ) ;
  207. $query .= ' AND ' . DB::name( 'abs_abs' , 'lesson' ) . ' = ' . DB::escape( $lesson ) . ' ;' ;
  208. if( $resultset = DB::query( $query ) ) {
  209. $row = DB::fetch( $resultset ) ;
  210. DB::free( $resultset ) ;
  211. $id = $row['abs_id'] ;
  212. }
  213. }
  214. $query = 'INSERT INTO ' . DB::name( 'abs_log_abs' ) ;
  215. $query .= ' SET' ;
  216. $query .= ' ' . DB::name( 'abs_log_abs' , 'log_abs_id' ) . ' = DEFAULT ,' ;
  217. $query .= ' ' . DB::name( 'abs_log_abs' , 'abs_id' ) . ' = ' . DB::escape( $id ) . ' ,' ;
  218. $query .= ' ' . DB::name( 'abs_log_abs' , 'after') . ' = "' . DB::escape( $type ) . '" ,' ;
  219. $query .= ' ' . DB::name( 'abs_log_abs' , 'executor_id' ) . ' = ' . DB::escape( $exec ) . ' ,' ;
  220. $query .= ' ' . DB::name( 'abs_log_abs' , 'date_time' ) . ' = DEFAULT ;' ;
  221. */
  222. $query = 'SELECT set_abs_in( ' . DB::escape( $ad ) . ' , '
  223. . DB::escape( $week ) . ' , '
  224. . DB::escape( $day ) . ' , '
  225. . DB::escape( $lesson ) . ' , '
  226. . "'" . DB::escape( $type ) . "' , "
  227. . DB::escape( $exec ) . ' ) as id' ;
  228. if( $resultset = DB::query( $query ) ) {
  229. if( DB::count( $resultset ) == 1 ) {
  230. $row = DB::fetch( $resultset ) ;
  231. return new Absence( $row[ 'id' ] ) ;
  232. }
  233. } else {
  234. throw new ErrorException( 'Absence logging error.' , 891 , 1 , 'Absence.php' , 135 ) ;
  235. }
  236. //}
  237. //} else {
  238. // throw new ErrorException( 'Absence inserting error.' , 801 , 1 , 'Absence.php' , 139 ) ;
  239. //}
  240. return false ;
  241. }
  242. /**
  243. * @brief Filters arrays of Absence according to $week and $day
  244. *
  245. * @deprecated use Person::getAbs() instead
  246. * @param array $abs Array of Absence
  247. * @param int|NULL $week optional, default NULL
  248. * @param int|NULL $day optional, default NULL
  249. * @return array Filtered array of Absence
  250. */
  251. public static function filter_abs( array $abs , $week = NULL , $day = NULL ) {
  252. if( $week !== NULL ) {
  253. $temp = array() ;
  254. foreach( $abs as $absence ) {
  255. if( $absence->getDate()->getWeek() == $week ) {
  256. $temp[ $absence->getDate()->getDay() * 12 + $absence->getLesson() ] = $absence ;
  257. }
  258. unset( $absence ) ;
  259. }
  260. $abs = $temp ;
  261. unset( $temp ) ;
  262. if( $day !== NULL ) {
  263. $temp = array() ;
  264. foreach( $abs as $absence ) {
  265. if( $absence->getDate()->getDay() == $day ) {
  266. $temp[ $absence->getLesson() ] = $absence ;
  267. }
  268. unset( $absence ) ;
  269. }
  270. $abs = $temp ;
  271. unset( $temp ) ;
  272. }
  273. }
  274. return $abs ;
  275. }
  276. /**
  277. * @brief Transforms an array of Absence into an array of string types
  278. *
  279. * @param array $abs Array of Absence
  280. * @return array Array of transformed string types
  281. */
  282. public static function abs_type( $abs ) {
  283. $result = array() ;
  284. foreach( $abs as $key => $value ) {
  285. $result[ $key ] = $value->getType() ;
  286. }
  287. return $result ;
  288. }
  289. }
  290. ?>