Guest User

Untitled

a guest
Jun 21st, 2018
310
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class StatsData extends CodonData {
  3. /**
  4. * Return the total from a table given the conditions specified
  5. * Also handle any caching for said query
  6. *
  7. * @param array $params See function for parameters
  8. * @return int Total number
  9. */
  10. public static function getTotalForCol($params) {
  11.  
  12. $params = array_merge(array(
  13. 'table' => '',
  14. 'column' => '',
  15. 'airline_code' => '', // optional
  16. 'where' => array(), // optional
  17. 'func' => 'COUNT', //optional
  18. ), $params
  19. );
  20.  
  21. if($params['table'] == '' || $params['table'] == '') {
  22. return false;
  23. }
  24.  
  25. if($params['func'] == '') {
  26. $params['func'] = 'COUNT';
  27. }
  28.  
  29. if(!is_array($params['where'])) {
  30. $params['where'] = array();
  31. }
  32.  
  33. if(!empty($params['airline_code'])) {
  34. $params['airline_code'] = strtoupper($params['airline_code']);
  35. $params['where']['code'] = $params['airline_code'];
  36. }
  37.  
  38. $mixed = substr(md5(implode('', $params)), 0, 8);
  39. $key = 'total_'.$mixed;
  40.  
  41. $total = CodonCache::read($key);
  42.  
  43. if($total === false) {
  44.  
  45. $params['column'] = trim($params['column']);
  46. if($params['column'] != '*') {
  47. $params['column'] = '`'.$params['column'].'`';
  48. }
  49.  
  50. $sql="SELECT ".$params['func']."(".$params['column'].") as `total` "
  51. ."FROM ".TABLE_PREFIX.$params['table'];
  52.  
  53. $sql .= DB::build_where($params['where']);
  54. $total = DB::get_row($sql);
  55.  
  56. if(!$total) {
  57. $total = 0;
  58. } else {
  59. $total = $total->total;
  60. }
  61.  
  62. CodonCache::write($key, $total, '15minute');
  63. }
  64.  
  65. return $total;
  66. }
  67.  
  68. /**
  69. * Get the date of the very first PIREP, use this as the "start date"
  70. * of the VA
  71. *
  72. * @return array, PIREP ID and submit date
  73. */
  74. public static function getStartDate() {
  75.  
  76. $start_date = CodonCache::read('start_date');
  77.  
  78. if ($start_date === false) {
  79. $sql = 'SELECT `pirepid`, `submitdate`
  80. FROM ' . TABLE_PREFIX . 'pireps
  81. ORDER BY `submitdate` ASC
  82. LIMIT 1';
  83.  
  84. $start_date = DB::get_row($sql);
  85. CodonCache::write('start_date', $start_date, 'long');
  86. }
  87.  
  88. return $start_date;
  89. }
  90.  
  91. /**
  92. * Get all of the months since the VA started
  93. */
  94. public static function getMonthsSinceStart() {
  95.  
  96. $months_list = CodonCache::read('months_since_start');
  97.  
  98. if ($months_list === false) {
  99. $date = self::GetStartDate();
  100.  
  101. if (!$date) {
  102. $startdate = time();
  103. } else {
  104. $startdate = $date->submitdate;
  105. }
  106.  
  107. $months_list = self::getMonthsSinceDate($startdate);
  108. CodonCache::write('months_since_start', $months_list, 'long');
  109. }
  110.  
  111. return $months_list;
  112. }
  113.  
  114. /**
  115. * Get years since the VA started
  116. */
  117. public static function getYearsSinceStart() {
  118.  
  119. $key = 'years_since_start';
  120. $years_start = CodonCache::read($key);
  121.  
  122. if ($years_start === false) {
  123. $date = self::getStartDate();
  124.  
  125. if (!$date) {
  126. $startdate = 'Today';
  127. } else {
  128. $startdate = $date->submitdate;
  129. }
  130.  
  131. $start = strtotime($startdate);
  132. $end = date('Y');
  133. do {
  134. $year = date('Y', $start); # Get the months
  135. $years[$year] = $start; # Set the timestamp
  136. $start = strtotime('+1 Year', $start);
  137.  
  138. } while ($year < $end);
  139.  
  140. $years_start = array_reverse($years, true);
  141. CodonCache::write($key, $years_start, 'long');
  142. }
  143.  
  144. return $years_start;
  145. }
  146.  
  147. /**
  148. * Get all of the months since a certain date
  149. */
  150. public static function getMonthsSinceDate($start) {
  151.  
  152. $key_month = date('MY', $start);
  153. $key = 'months_since_' . $key_month;
  154. $months = CodonCache::read($key);
  155.  
  156. if ($months === false) {
  157. if (!is_numeric($start)) {
  158. $start = strtotime($start);
  159. }
  160.  
  161. $end = date('Ym');
  162.  
  163. do {
  164. # Get the months
  165. $month = date('M Y', $start);
  166. $months[$month] = $start; # Set the timestamp
  167. $start = strtotime('+1 month +1 day', strtotime($month));
  168.  
  169. # Convert to YYYYMM to compare
  170. $check = intval(date('Ym', $start));
  171.  
  172. } while ($check <= $end);
  173.  
  174. CodonCache::write($key, $months, 'long');
  175. }
  176.  
  177. return $months;
  178. }
  179.  
  180. /**
  181. * Get all the months within a certain range
  182. * Pass timestamp, or textual date
  183. */
  184. public static function getMonthsInRange($start, $end) {
  185.  
  186. $key = "months_in_{$start}_{$end}";
  187. $months = CodonCache::read($key);
  188.  
  189. if ($months === false) {
  190.  
  191. if (!is_numeric($start)) {
  192. $start = strtotime($start);
  193. }
  194.  
  195. if (!is_numeric($end)) {
  196. $end = strtotime($end);
  197. }
  198.  
  199. $end = intval(date('Ym', $end));
  200.  
  201. /* Loop through, adding one month to $start each time
  202. */
  203. do {
  204. $month = date('M Y', $start); # Get the month
  205. $months[$month] = $start; # Set the timestamp
  206. $start = strtotime('+1 month +1 day', strtotime($month));
  207. //$start += (SECONDS_PER_DAY * 25); # Move it up a month
  208.  
  209. $check = intval(date('Ym', $start));
  210.  
  211. } while ($check <= $end);
  212.  
  213. CodonCache::write($key, $months, 'long');
  214. }
  215.  
  216. return $months;
  217. }
  218.  
  219. /**
  220. * StatsData::updateTotalHours()
  221. *
  222. * @return
  223. */
  224. public static function updateTotalHours() {
  225.  
  226. $sql = "SELECT SUM(TIME_TO_SEC(flighttime_stamp)) AS `total`
  227. FROM `".TABLE_PREFIX."pireps`
  228. WHERE `accepted`=".PIREP_ACCEPTED;
  229.  
  230. $totaltime = DB::get_row($sql);
  231. if(!$totaltime) {
  232. $totaltime = '00:00:00';
  233. } else {
  234. $totaltime = Util::secondsToTime($totaltime->total);
  235. }
  236.  
  237. SettingsData::SaveSetting('TOTAL_HOURS', $totaltime);
  238. }
  239.  
  240. /**
  241. * Get the total number of hours flown by pilots
  242. */
  243. public static function TotalHours() {
  244. return SettingsData::GetSettingValue('TOTAL_HOURS');
  245. }
  246.  
  247. /**
  248. * Get the top routes
  249. */
  250. public static function TopRoutes($airline_code = '') {
  251.  
  252. $key = 'top_routes';
  253. if ($airline_code != '') {
  254. $key .= '_' . $airline_code;
  255. }
  256.  
  257. $top_routes = CodonCache::read($key);
  258. if ($top_routes === false) {
  259. $sql = 'SELECT * FROM `' . TABLE_PREFIX . 'schedules`';
  260.  
  261. if ($airline_code != '') {
  262. $sql .= " WHERE `code`='{$airline_code}'";
  263. }
  264.  
  265. $sql = $sql . ' ORDER BY `timesflown` DESC LIMIT 10';
  266.  
  267. $top_routes = DB::get_results($sql);
  268. CodonCache::write($key, $top_routes, 'medium');
  269. }
  270. return $top_routes;
  271. }
  272.  
  273. /**
  274. * StatsData::UsersOnline()
  275. *
  276. * @param string $minutes
  277. * @return
  278. */
  279. public static function UsersOnline($minutes = '') {
  280.  
  281. $key = 'users_online';
  282. $users_online = CodonCache::read($key);
  283.  
  284. if ($users_online === false) {
  285. if ($minutes == '') $minutes = Config::Get('USERS_ONLINE_TIME');
  286.  
  287. $sql = "SELECT p.*
  288. FROM " . TABLE_PREFIX . "pilots p, " . TABLE_PREFIX . "sessions s
  289. WHERE s.pilotid = p.pilotid
  290. AND DATE_SUB(NOW(), INTERVAL {$minutes} MINUTE) <= s.`logintime`";
  291.  
  292. $users_online = DB::get_results($sql);
  293.  
  294. # Check if it's blank, then return an empty array
  295. if (!$users_online) $users_online = array();
  296.  
  297. CodonCache::write($key, $users_online, 'short');
  298. }
  299.  
  300. return $users_online;
  301. }
  302.  
  303. /**
  304. * StatsData::GuestsOnline()
  305. *
  306. * @param string $minutes
  307. * @return
  308. */
  309. public static function GuestsOnline($minutes = '') {
  310.  
  311. $key = 'guests_online';
  312. $guests_online = CodonCache::read($key);
  313.  
  314. if ($guests_online === false) {
  315. if ($minutes == '') $minutes = Config::Get('USERS_ONLINE_TIME');
  316.  
  317. $sql = "SELECT s.*
  318. FROM " . TABLE_PREFIX . "sessions s
  319. WHERE s.pilotid = 0
  320. AND DATE_SUB(NOW(), INTERVAL {$minutes} MINUTE) <= s.`logintime`";
  321.  
  322. $guests_online = DB::get_results($sql);
  323.  
  324. if (!$guests_online) $guests_online = array();
  325.  
  326. CodonCache::write($key, $guests_online, 'short');
  327. }
  328.  
  329. return $guests_online;
  330. }
  331.  
  332. /**
  333. * Get the current aircraft usage
  334. */
  335. public static function AircraftUsage() {
  336.  
  337. $key = 'stats_aircraft_usage';
  338.  
  339. $aircraft_usage = CodonCache::read($key);
  340. if ($aircraft_usage === false) {
  341. //SEC_TO_TIME(SUM(p.flighttime*60*60)) AS totaltime,
  342. $sql = 'SELECT a.*, a.name AS aircraft,
  343. COUNT(p.pirepid) AS routesflown,
  344. SUM(p.distance) AS distance,
  345. SEC_TO_TIME(SUM(TIME_TO_SEC(p.flighttime_stamp))) as totaltime,
  346. AVG(p.distance) AS averagedistance,
  347. AVG(p.flighttime) as averagetime
  348. FROM ' . TABLE_PREFIX . 'aircraft a
  349. INNER JOIN ' . TABLE_PREFIX . 'pireps p ON (p.aircraft = a.id)
  350. GROUP BY a.registration';
  351.  
  352. $aircraft_usage = DB::get_results($sql);
  353. CodonCache::write($key, $aircraft_usage, 'short');
  354. }
  355.  
  356. return $aircraft_usage;
  357. }
  358.  
  359. /**
  360. * Show pie chart for all of the aircraft flown
  361. * by a certain pilot. Outputs image, unless $ret == true,
  362. * then it returns the URL.
  363. */
  364. public static function AircraftFlownGraph($ret = false) {
  365.  
  366. //Select aircraft types
  367. $sql = 'SELECT a.name AS aircraft, COUNT(p.aircraft) AS count
  368. FROM ' . TABLE_PREFIX . 'pireps p, ' . TABLE_PREFIX . 'aircraft a
  369. WHERE p.aircraft = a.id
  370. GROUP BY a.name';
  371.  
  372. $stats = DB::get_results($sql);
  373.  
  374. if (!$stats) {
  375. return;
  376. }
  377.  
  378. $data = '';
  379. $labels = '';
  380. foreach ($stats as $stat) {
  381. if ($stat->aircraft == '') continue;
  382.  
  383. $data .= $stat->count . ',';
  384. $labels .= $stat->aircraft . '|';
  385. }
  386.  
  387. // remove that final lone char
  388. $data = substr($data, 0, strlen($data) - 1);
  389. $labels = substr($labels, 0, strlen($labels) - 1);
  390.  
  391. $chart = new googleChart($data, 'pie');
  392. $chart->dimensions = '350x200';
  393. $chart->setLabels($labels);
  394.  
  395. if ($ret == true) return $chart->draw(false);
  396. else echo '<img src="' . $chart->draw(false) . '" />';
  397. }
  398.  
  399. /**
  400. * StatsData::PilotAircraftFlownCounts()
  401. *
  402. * @param mixed $pilotid
  403. * @return
  404. */
  405. public static function PilotAircraftFlownCounts($pilotid) {
  406.  
  407. $key = 'ac_flown_counts_' . $pilotid;
  408.  
  409. $counts = CodonCache::read($key);
  410. if ($counts === false) {
  411. //Select aircraft types
  412. $sql = 'SELECT a.name AS aircraft, COUNT(p.aircraft) AS count, SUM(p.flighttime) AS hours
  413. FROM ' . TABLE_PREFIX . 'pireps p, ' . TABLE_PREFIX . 'aircraft a
  414. WHERE p.aircraft = a.id AND p.pilotid=' . intval($pilotid) . '
  415. GROUP BY a.name';
  416.  
  417. $counts = DB::get_results($sql);
  418. CodonCache::write($key, $counts, 'medium');
  419. }
  420.  
  421. return $counts;
  422. }
  423.  
  424. /**
  425. * Show pie chart for all of the aircraft flown
  426. * by a certain pilot. Outputs image, unless $ret == true,
  427. * then it returns the URL.
  428. *
  429. * @param int $pilotid The ID of the pilot
  430. * @param bool $ret Return the URL, or display it in an IMG tag
  431. */
  432. public static function PilotAircraftFlownGraph($pilotid, $ret = false) {
  433.  
  434. $stats = self::PilotAircraftFlownCounts($pilotid);
  435.  
  436. if (!$stats) {
  437. return;
  438. }
  439.  
  440. $data = '';
  441. $labels = '';
  442. foreach ($stats as $stat) {
  443. if ($stat->aircraft == '') continue;
  444.  
  445. $data .= $stat->count . ',';
  446. $labels .= $stat->aircraft . '|';
  447. }
  448.  
  449. // remove that final lone char
  450. $data = substr($data, 0, strlen($data) - 1);
  451. $labels = substr($labels, 0, strlen($labels) - 1);
  452.  
  453. $chart = new GoogleChart($data, 'pie');
  454. $chart->dimensions = '350x200';
  455. $chart->setLabels($labels);
  456.  
  457.  
  458. $url = $chart->draw(false);
  459. unset($chart);
  460.  
  461. if ($ret == true) return $url;
  462. else echo '<img src="' . $url . '" alt="" />';
  463. }
  464.  
  465. /* These contributed by simpilot from phpVMS forums
  466. */
  467.  
  468. /**
  469. * Get the total number of pilots
  470. */
  471. public static function PilotCount($airline_code = '') {
  472.  
  473. return self::getTotalForCol(array(
  474. 'table' => 'pilots',
  475. 'column' => '*',
  476. 'airline_code' => $airline_code,
  477. )
  478. );
  479.  
  480. }
  481.  
  482. /**
  483. * Get the total number of flights flown
  484. */
  485. public static function TotalFlights($airline_code = '') {
  486.  
  487. return self::getTotalForCol(array(
  488. 'table' => 'pireps',
  489. 'column' => '*',
  490. 'airline_code' => $airline_code,
  491. 'where' => array('accepted' => PIREP_ACCEPTED),
  492. 'func' => 'COUNT',
  493. )
  494. );
  495.  
  496. }
  497.  
  498. /**
  499. * Return the total number of passengers carried
  500. *
  501. * @param string $airline_code Airline code specifically to call for, optional
  502. * @return int
  503. *
  504. */
  505. public static function TotalPaxCarried($airline_code = '') {
  506.  
  507. return self::getTotalForCol(array(
  508. 'table' => 'pireps',
  509. 'column' => 'load',
  510. 'airline_code' => $airline_code,
  511. 'where' => array(
  512. 'accepted' => PIREP_ACCEPTED,
  513. 'flighttype' => 'P',
  514. ),
  515. 'func' => 'SUM',
  516. )
  517. );
  518. }
  519.  
  520.  
  521. /**
  522. * Return the number of flights flown today
  523. *
  524. * @return int Total number of flights
  525. *
  526. */
  527. public static function TotalFlightsToday($airline_code = '') {
  528.  
  529. return self::getTotalForCol(array(
  530. 'table' => 'pireps',
  531. 'column' => '*',
  532. 'airline_code' => $airline_code,
  533. 'where' => array('DATE(`submitdate`) = CURDATE()')
  534. )
  535. );
  536.  
  537. }
  538.  
  539. /**
  540. * Total amount of fuel burned among all accepted PIREPS
  541. *
  542. * @return float In units specified in config
  543. *
  544. */
  545. public static function TotalFuelBurned($airline_code = '') {
  546.  
  547. return self::getTotalForCol(array(
  548. 'table' => 'pireps',
  549. 'column' => 'fuelused',
  550. 'airline_code' => $airline_code,
  551. 'where' => array('accepted' => PIREP_ACCEPTED),
  552. 'func' => 'SUM',
  553. )
  554. );
  555.  
  556. }
  557.  
  558. /**
  559. * Get the total miles/km flown
  560. *
  561. * @return float Total distance flown in units in config
  562. *
  563. */
  564. public static function TotalMilesFlown($airline_code = '') {
  565. return self::TotalDistanceFlown($airline_code);
  566. }
  567.  
  568. /**
  569. * Get the total miles/km flown
  570. *
  571. * @return float Total distance flown in units in config
  572. *
  573. */
  574. public static function TotalDistanceFlown($airline_code = '') {
  575. return self::getTotalForCol(array(
  576. 'table' => 'pireps',
  577. 'column' => 'distance',
  578. 'airline_code' => $airline_code,
  579. 'where' => array('accepted' => PIREP_ACCEPTED),
  580. 'func' => 'SUM',
  581. )
  582. );
  583. }
  584.  
  585.  
  586. /**
  587. * Return the total number of aircraft in the fleet
  588. *
  589. * @return int Total
  590. *
  591. */
  592. public static function TotalAircraftInFleet($airline_code = '') {
  593.  
  594. return self::getTotalForCol(array(
  595. 'table' => 'aircraft',
  596. 'column' => 'id',
  597. 'airline_code' => $airline_code,
  598. ));
  599.  
  600. }
  601.  
  602.  
  603. /**
  604. * Return the total number of news stories
  605. *
  606. * @return int Total
  607. *
  608. */
  609. public static function TotalNewsItems() {
  610.  
  611. return self::getTotalForCol(array(
  612. 'table' => 'news',
  613. 'column' => 'id',
  614. )
  615. );
  616.  
  617. }
  618.  
  619.  
  620. /**
  621. * Return the total number of schedules in the system
  622. *
  623. * @return int $airline_code Total number
  624. *
  625. */
  626. public static function TotalSchedules($airline_code = '') {
  627.  
  628. return self::getTotalForCol(array(
  629. 'table' => 'schedules',
  630. 'column' => 'id',
  631. 'airline_code' => $airline_code,
  632. )
  633. );
  634.  
  635. }
  636.  
  637.  
  638.  
  639.  
  640. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×