LiveLog.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. class Application_Model_LiveLog
  3. {
  4. public static function GetLiveShowDuration($p_keepData=false)
  5. {
  6. try {
  7. $sql = "SELECT * FROM CC_LIVE_LOG"
  8. ." WHERE state = :state"
  9. ." and (start_time >= (now() - INTERVAL '1 day'))"
  10. ." ORDER BY id";
  11. $rows = Application_Common_Database::prepareAndExecute($sql, array(':state'=>'L'),
  12. Application_Common_Database::ALL);
  13. /* Check if last log has end time.
  14. * If not, set end time to current time
  15. */
  16. if ($rows != null) {
  17. $last_row = self::UpdateLastLogEndTime(array_pop($rows));
  18. array_push($rows, $last_row);
  19. $skip = false;
  20. } else {
  21. $sql = "SELECT * FROM CC_LIVE_LOG"
  22. ." WHERE state = :state"
  23. ." ORDER BY id";
  24. $rows = Application_Common_Database::prepareAndExecute($sql, array(':state'=>'L'),
  25. Application_Common_Database::ALL);
  26. if ($rows != null) {
  27. $last_row = self::UpdateLastLogEndTime(array_pop($rows));
  28. array_push($rows, $last_row);
  29. foreach ($rows as $row) {
  30. $sql_delete = "DELETE FROM CC_LIVE_LOG"
  31. ." WHERE id = :id";
  32. Application_Common_Database::prepareAndExecute($sql_delete, array(':id'=>$row['id']),
  33. Application_Common_Database::EXECUTE);
  34. }
  35. }
  36. $skip = true;
  37. }
  38. $hours = 0;
  39. $minutes = 0;
  40. $seconds = 0;
  41. if (!$skip) {
  42. foreach ($rows as $row) {
  43. $end = new DateTime($row['end_time']);
  44. $start = new DateTime($row['start_time']);
  45. $duration = $start->diff($end);
  46. $duration = $duration->format("%H:%i:%s");
  47. $intervals = explode(":", $duration);
  48. for ($i = 0; $i < sizeof($intervals); $i++) {
  49. if (!isset($intervals[$i])) {
  50. $intervals[$i] = 0;
  51. }
  52. }
  53. // Trim milliseconds (DateInterval does not support)
  54. $sec = explode(".", $intervals[2]);
  55. if (isset($sec[0])) {
  56. $intervals[2] = $sec[0];
  57. }
  58. $seconds += $intervals[2];
  59. if ($seconds / 60 >= 1) {
  60. $minutes += 1;
  61. $seconds -= 60;
  62. }
  63. $minutes += $intervals[1];
  64. if ($minutes / 60 >= 1) {
  65. $hours += 1;
  66. $minutes -= 60;
  67. }
  68. $hours += $intervals[0];
  69. if (!$p_keepData) {
  70. // Delete data we just used to start a new log history
  71. $sql_delete = "DELETE FROM CC_LIVE_LOG"
  72. ." WHERE id = :id";
  73. Application_Common_Database::prepareAndExecute($sql_delete, array(':id'=>$row['id']),
  74. Application_Common_Database::EXECUTE);
  75. }
  76. }
  77. //Trim milliseconds
  78. $seconds = explode(".", $seconds);
  79. if (isset($seconds[0])) {
  80. $minutes = (double) (($hours*60)+$minutes . "." . $seconds[0]);
  81. } else {
  82. $minutes = (double) (($hours*60)+$minutes);
  83. }
  84. }
  85. return $minutes;
  86. } catch (Exception $e) {
  87. header('HTTP/1.0 503 Service Unavailable');
  88. Logging::info("GetLiveShowDuration - Could not connect to database.");
  89. exit;
  90. }
  91. }
  92. public static function GetScheduledDuration($p_keepData=false)
  93. {
  94. try {
  95. $sql_get_logs = "SELECT * FROM CC_LIVE_LOG"
  96. ." WHERE state = :state"
  97. ." and (start_time >= (now() - INTERVAL '1 day'))"
  98. ." ORDER BY id";
  99. $rows = Application_Common_Database::prepareAndExecute($sql_get_logs, array(':state'=>'S'),
  100. Application_Common_Database::ALL);
  101. /* Check if last log has end time.
  102. * If not, set end time to current time
  103. */
  104. if ($rows != null) {
  105. $last_row = self::UpdateLastLogEndTime(array_pop($rows));
  106. array_push($rows, $last_row);
  107. $skip = false;
  108. } else {
  109. $sql = "SELECT * FROM CC_LIVE_LOG"
  110. ." WHERE state = :state"
  111. ." ORDER BY id";
  112. $rows = Application_Common_Database::prepareAndExecute($sql, array(':state'=>'S'),
  113. Application_Common_Database::ALL);
  114. if ($rows != null) {
  115. $last_row = self::UpdateLastLogEndTime(array_pop($rows));
  116. array_push($rows, $last_row);
  117. foreach ($rows as $row) {
  118. $sql_delete = "DELETE FROM CC_LIVE_LOG"
  119. ." WHERE id = :id";
  120. Application_Common_Database::prepareAndExecute($sql_delete, array(':id'=>$row['id']),
  121. Application_Common_Database::EXECUTE);
  122. }
  123. }
  124. $skip = true;
  125. }
  126. $hours = 0;
  127. $minutes = 0;
  128. $seconds = 0;
  129. if (!$skip) {
  130. /* Get all shows and tracks from cc_schedule that played
  131. * during a scheduled state
  132. */
  133. foreach ($rows as $row) {
  134. $sql_get_tracks = "SELECT * FROM cc_schedule"
  135. ." WHERE starts >= :starts1"
  136. ." AND starts < :starts2"
  137. ." AND file_id IS NOT NULL"
  138. ." AND media_item_played IS TRUE";
  139. $params = array(
  140. ':starts1'=>$row['start_time'],
  141. ':starts2'=>$row['end_time']
  142. );
  143. $tracks = Application_Common_Database::prepareAndExecute($sql_get_tracks, $params,
  144. Application_Common_Database::ALL);
  145. foreach ($tracks as $track) {
  146. if ($track['ends'] > $row['end_time']) {
  147. $scheduled_ends = new DateTime($row['end_time']);
  148. $track_ends = new DateTime($track['ends']);
  149. $extra_time = $scheduled_ends->diff($track_ends);
  150. /* Get difference between clip_length
  151. * and the extra time. We need to subtract
  152. * this difference from the track's
  153. * clip length.
  154. */
  155. $clip_length = $track['clip_length'];
  156. //Convert clip_length into seconds
  157. $clip_length_intervals = explode(":", $clip_length);
  158. for ($i = 0; $i < sizeof($clip_length_intervals); $i++) {
  159. if (!isset($clip_length_intervals[$i])) {
  160. $clip_length_intervals[$i] = 0;
  161. }
  162. }
  163. $clip_length_seconds = $clip_length_intervals[0]*3600 + $clip_length_intervals[1]*60 + $clip_length_intervals[2];
  164. $extra_time = $extra_time->format("%H:%i:%s");
  165. //Convert extra_time into seconds;
  166. $extra_time_intervals = explode(":", $extra_time);
  167. for ($i = 0; $i < sizeof($extra_time_intervals); $i++) {
  168. if (!isset($extra_time_intervals[$i])) {
  169. $extra_time_intervals[$i] = 0;
  170. }
  171. }
  172. $extra_time_seconds = $extra_time_intervals[0]*3600 + $extra_time_intervals[1]*60 + $extra_time_intervals[2];
  173. $clip_length_seconds -= $extra_time_seconds;
  174. //Convert new clip_length into "H-i-s" format
  175. $clip_length_arr = array();
  176. if ($clip_length_seconds / 3600 >= 1) {
  177. array_push($clip_length_arr, str_pad(floor($clip_length_seconds / 3600), 2, "0", STR_PAD_LEFT));
  178. $clip_length_seconds -= floor($clip_length_seconds / 3600);
  179. } else {
  180. array_push($clip_length_arr, "00");
  181. }
  182. if ($clip_length_seconds / 60 >= 1) {
  183. array_push($clip_length_arr, str_pad(floor($clip_length_seconds / 60), 2, "0", STR_PAD_LEFT));
  184. $clip_length_seconds -= floor($clip_length_seconds / 60);
  185. } else {
  186. array_push($clip_length_arr, "00");
  187. }
  188. array_push($clip_length_arr, str_pad($clip_length_seconds, 2, "0", STR_PAD_LEFT));
  189. $clip_length = implode(":", $clip_length_arr);
  190. } else {
  191. $clip_length = $track['clip_length'];
  192. }
  193. $intervals = explode(":", $clip_length);
  194. for ($i = 0; $i < sizeof($intervals); $i++) {
  195. if (!isset($intervals[$i])) {
  196. $intervals[$i] = 0;
  197. }
  198. }
  199. // Trim milliseconds (DateInteral does not support)
  200. $sec = explode(".", $intervals[2]);
  201. if (isset($sec[0])) {
  202. $intervals[2] = $sec[0];
  203. }
  204. $seconds += $intervals[2];
  205. if ($seconds / 60 >= 1) {
  206. $minutes += 1;
  207. $seconds -= 60;
  208. }
  209. $minutes += $intervals[1];
  210. if ($minutes / 60 >= 1) {
  211. $hours += 1;
  212. $minutes -= 60;
  213. }
  214. $hours += $intervals[0];
  215. }
  216. if (!$p_keepData) {
  217. //Delete row because we do not need data anymore
  218. $sql_delete = "DELETE FROM CC_LIVE_LOG"
  219. ." WHERE id = :id";
  220. Application_Common_Database::prepareAndExecute($sql_delete, array(':id'=>$row['id']),
  221. Application_Common_Database::EXECUTE);
  222. }
  223. }
  224. $seconds = explode(".", $seconds);
  225. if (isset($seconds[0])) {
  226. $minutes = (double) (($hours*60)+$minutes . "." . $seconds[0]);
  227. } else {
  228. $minutes = (double) (($hours*60)+$minutes);
  229. }
  230. }
  231. return $minutes;
  232. } catch (Exception $e) {
  233. header('HTTP/1.0 503 Service Unavailable');
  234. Logging::info("GetScheduledDuration - Could not connect to database.");
  235. exit;
  236. }
  237. }
  238. public static function UpdateLastLogEndTime($log)
  239. {
  240. if ($log['end_time'] == null) {
  241. $current_time = new DateTime("now", new DateTimeZone('UTC'));
  242. $log['end_time'] = $current_time;
  243. $log['end_time'] = $log['end_time']->format("Y-m-d H:i:s");
  244. self::SetEndTime($log['state'], $current_time, true);
  245. self::SetNewLogTime($log['state'], $current_time);
  246. }
  247. return $log;
  248. }
  249. public static function SetNewLogTime($state, $dateTime)
  250. {
  251. try {
  252. $scheduled = Application_Model_Preference::GetSourceSwitchStatus('scheduled_play');
  253. if ($state == 'L' && $scheduled == 'on') {
  254. self::SetEndTime('S', $dateTime);
  255. }
  256. /* Only insert new state if last log
  257. * has ended
  258. */
  259. $sql_select = "SELECT max(id) from CC_LIVE_LOG"
  260. ." WHERE (state= :state1 and end_time is NULL) or (state= :state2 and end_time is NULL)";
  261. $params = array(
  262. ":state1"=> 'L',
  263. ":state2"=> 'S'
  264. );
  265. $id = Application_Common_Database::prepareAndExecute($sql_select, $params,
  266. Application_Common_Database::COLUMN);
  267. if ($id == null) {
  268. $sql_insert = "INSERT INTO CC_LIVE_LOG (state, start_time)"
  269. ." VALUES (:state, :start)";
  270. $params = array(
  271. ':state'=>$state,
  272. ':start'=>$dateTime->format("Y-m-d H:i:s")
  273. );
  274. Application_Common_Database::prepareAndExecute($sql_insert, $params,
  275. Application_Common_Database::EXECUTE);
  276. if ($state == "S") {
  277. // if scheduled play source is getting broadcasted
  278. Application_Model_Schedule::UpdateBrodcastedStatus($dateTime, 1);
  279. }
  280. }
  281. } catch (Exception $e) {
  282. header('HTTP/1.0 503 Service Unavailable');
  283. Logging::info("SetNewLogTime - Could not connect to database.");
  284. exit;
  285. }
  286. }
  287. public static function SetEndTime($state, $dateTime, $override=false)
  288. {
  289. try {
  290. $dj_live = Application_Model_Preference::GetSourceSwitchStatus('live_dj');
  291. $master_live = Application_Model_Preference::GetSourceSwitchStatus('master_dj');
  292. if (($dj_live=='off' && $master_live=='off') || $state == 'S' || $override) {
  293. $sql = "SELECT id, state from cc_live_log"
  294. ." where id in (select max(id) from cc_live_log)";
  295. $row = Application_Common_Database::prepareAndExecute($sql, array(),
  296. Application_Common_Database::SINGLE);
  297. /* Only set end time if state recevied ($state)
  298. * is the last row in cc_live_log
  299. */
  300. if ($row['state'] == $state) {
  301. $update_sql = "UPDATE CC_LIVE_LOG"
  302. ." SET end_time = :end"
  303. ." WHERE id = :id";
  304. $params = array(
  305. ':end'=>$dateTime->format("Y-m-d H:i:s"),
  306. ':id'=>$row['id']
  307. );
  308. Application_Common_Database::prepareAndExecute($update_sql, $params,
  309. Application_Common_Database::EXECUTE);
  310. }
  311. //If live broadcasting is off, turn scheduled play on
  312. $scheduled = Application_Model_Preference::GetSourceSwitchStatus('scheduled_play');
  313. if ($state == 'L' && $scheduled=='on' && !$override) {
  314. self::SetNewLogTime('S', $dateTime);
  315. }
  316. }
  317. } catch (Exception $e) {
  318. header('HTTP/1.0 503 Service Unavailable');
  319. Logging::info("SetEndTime - Could not connect to database.");
  320. exit;
  321. }
  322. }
  323. }