PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/components/com_joomleague/models/nextmatch.php

https://gitlab.com/julienv/joomleague
PHP | 602 lines | 502 code | 57 blank | 43 comment | 34 complexity | e8b38f39a824b5d5f073016a9cf6c466 MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2006-2014 joomleague.at. All rights reserved.
  4. * @license GNU/GPL,see LICENSE.php
  5. * Joomla! is free software. This version may have been modified pursuant
  6. * to the GNU General Public License,and as distributed it includes or
  7. * is derivative of works licensed under the GNU General Public License or
  8. * other free or open source software licenses.
  9. * See COPYRIGHT.php for copyright notices and details.
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined('_JEXEC') or die('Restricted access');
  13. jimport( 'joomla.application.component.model');
  14. require_once( JPATH_COMPONENT.DS . 'helpers' . DS . 'ranking.php' );
  15. require_once( JLG_PATH_SITE . DS . 'models' . DS . 'project.php' );
  16. class JoomleagueModelNextMatch extends JoomleagueModelProject
  17. {
  18. var $project = null;
  19. var $matchid = 0;
  20. var $teamid = 0;
  21. var $projectid = 0;
  22. var $divisionid = 0;
  23. var $ranking = null;
  24. var $teams = null;
  25. /**
  26. * caching match data
  27. * @var object
  28. */
  29. var $_match = null;
  30. function __construct( )
  31. {
  32. parent::__construct( );
  33. $this->projectid = JRequest::getInt( "p", 0 );
  34. $this->divisionid = JRequest::getInt( "division", 0 );
  35. $this->teamid = JRequest::getInt( "tid", 0 );
  36. $this->matchid = JRequest::getInt( "mid", 0 );
  37. $this->getSpecifiedMatch($this->projectid, $this->divisionid, $this->teamid, $this->matchid);
  38. }
  39. function getSpecifiedMatch($projectId, $divisionId, $teamId, $matchId)
  40. {
  41. if (!$this->_match)
  42. {
  43. $config = $this->getTemplateConfig($this->getName());
  44. $expiry_time = $config ? $config['expiry_time'] : 0;
  45. $query = ' SELECT m.*, DATE_FORMAT(m.time_present, "%H:%i") time_present,
  46. pt1.project_id, r.roundcode, p.timezone, pt1.division_id
  47. FROM #__joomleague_match AS m
  48. INNER JOIN #__joomleague_round AS r ON r.id = m.round_id
  49. INNER JOIN #__joomleague_project_team AS pt1 ON pt1.id = m.projectteam1_id
  50. INNER JOIN #__joomleague_project_team AS pt2 ON pt2.id = m.projectteam2_id
  51. INNER JOIN #__joomleague_team AS t1 ON t1.id = pt1.team_id
  52. INNER JOIN #__joomleague_team AS t2 ON t2.id = pt2.team_id
  53. INNER JOIN #__joomleague_project AS p ON p.id = pt1.project_id
  54. WHERE DATE_ADD(m.match_date, INTERVAL '.$this->_db->Quote($expiry_time).' MINUTE) >= NOW()
  55. AND m.cancel=0
  56. AND m.published = 1';
  57. if ($matchId)
  58. {
  59. $query .= ' AND m.id = ' . $this->_db->Quote($matchId);
  60. }
  61. else
  62. {
  63. $query .= ' AND (team1_result is null OR team2_result is null) ';
  64. if ($teamId)
  65. {
  66. $query .= ' AND '
  67. . ' ( '
  68. . ' t1.id = '. $this->_db->Quote($teamId).' OR '
  69. . ' t2.id = '. $this->_db->Quote($teamId)
  70. . ' ) ';
  71. }
  72. else
  73. {
  74. $query .= ' AND (m.projectteam1_id > 0 OR m.projectteam2_id > 0) ';
  75. }
  76. }
  77. if ($projectId)
  78. {
  79. $query .= ' AND p.id = '.$this->_db->Quote($projectId);
  80. }
  81. if ($divisionId)
  82. {
  83. $query .= ' AND pt1.division_id = '.$this->_db->Quote($divisionId);
  84. }
  85. $query .= ' ORDER BY m.match_date';
  86. $this->_db->setQuery($query, 0, 1);
  87. $this->_match = $this->_db->loadObject();
  88. if($this->_match)
  89. {
  90. JoomleagueHelper::convertMatchDateToTimezone($this->_match);
  91. $this->projectid = $this->_match->project_id;
  92. $this->matchid = $this->_match->id;
  93. }
  94. }
  95. return $this->_match;
  96. }
  97. /**
  98. * get match info
  99. * @return object
  100. */
  101. function getMatch()
  102. {
  103. if (empty($this->_match))
  104. {
  105. $query = ' SELECT m.*, DATE_FORMAT(m.time_present, "%H:%i") time_present, pt1.project_id, r.roundcode, p.timezone '
  106. . ' FROM #__joomleague_match AS m '
  107. . ' INNER JOIN #__joomleague_project_team AS pt1 ON pt1.id = m.projectteam1_id '
  108. . ' INNER JOIN #__joomleague_round AS r ON r.id = m.round_id '
  109. . ' INNER JOIN #__joomleague_project AS p ON p.id = r.project_id '
  110. . ' WHERE m.id = '. $this->_db->Quote($this->matchid)
  111. . ' AND m.published = 1';
  112. $this->_db->setQuery($query, 0, 1);
  113. $this->_match = $this->_db->loadObject();
  114. if ($this->_match)
  115. {
  116. JoomleagueHelper::convertMatchDateToTimezone($this->_match);
  117. }
  118. }
  119. return $this->_match;
  120. }
  121. /**
  122. * get match teams details
  123. * @return array
  124. */
  125. function getMatchTeams()
  126. {
  127. if (empty($this->teams))
  128. {
  129. $this->teams = array();
  130. $match = $this->getMatch();
  131. if ( is_null ( $match ) )
  132. {
  133. return null;
  134. }
  135. $team1 = $this->getTeaminfo($match->projectteam1_id);
  136. $team2 = $this->getTeaminfo($match->projectteam2_id);
  137. $this->teams[] = $team1;
  138. $this->teams[] = $team2;
  139. }
  140. return $this->teams;
  141. }
  142. function getReferees()
  143. {
  144. $match = &$this->getMatch();
  145. $query = ' SELECT p.firstname, p.nickname, p.lastname, p.country, pos.name AS position_name, p.id as person_id '
  146. . ' FROM #__joomleague_match_referee AS mr '
  147. . ' LEFT JOIN #__joomleague_project_referee AS pref ON mr.project_referee_id=pref.id '
  148. . ' INNER JOIN #__joomleague_person AS p ON p.id = pref.person_id '
  149. . ' INNER JOIN #__joomleague_project_position ppos ON ppos.id = mr.project_position_id'
  150. . ' INNER JOIN #__joomleague_position AS pos ON pos.id = ppos.position_id '
  151. . ' WHERE mr.match_id = '. $this->_db->Quote($match->id)
  152. . ' AND p.published = 1 '
  153. ;
  154. $this->_db->setQuery($query);
  155. return $this->_db->loadObjectList();
  156. }
  157. function _getRanking()
  158. {
  159. if (empty($this->ranking))
  160. {
  161. $project = $this->getProject();
  162. $division = $this->divisionid;
  163. $ranking = JLGRanking::getInstance($project);
  164. $ranking->setProjectId( $project->id );
  165. $this->ranking = $ranking->getRanking(0, $this->getCurrentRound(), $division);
  166. }
  167. return $this->ranking;
  168. }
  169. function getHomeRanked()
  170. {
  171. $match = &$this->getMatch();
  172. $rankings = &$this->_getRanking();
  173. foreach ($rankings as $ptid => $team)
  174. {
  175. if ($ptid == $match->projectteam1_id) {
  176. return $team;
  177. }
  178. }
  179. return false;
  180. }
  181. function getAwayRanked()
  182. {
  183. $match = &$this->getMatch();
  184. $rankings = &$this->_getRanking();
  185. foreach ($rankings as $ptid => $team)
  186. {
  187. if ($ptid == $match->projectteam2_id) {
  188. return $team;
  189. }
  190. }
  191. return false;
  192. }
  193. function _getHighestHomeWin($teamid)
  194. {
  195. $match = $this->getMatch();
  196. $query = ' SELECT t1.name AS hometeam, '
  197. . ' t2.name AS awayteam, '
  198. . ' team1_result AS homegoals, '
  199. . ' team2_result AS awaygoals, '
  200. . ' pt1.project_id AS pid, '
  201. . ' m.id AS mid '
  202. . ' FROM #__joomleague_match as m '
  203. . ' INNER JOIN #__joomleague_project_team pt1 ON pt1.id = m.projectteam1_id '
  204. . ' INNER JOIN #__joomleague_team t1 ON t1.id = pt1.team_id '
  205. . ' INNER JOIN #__joomleague_project_team pt2 ON pt2.id = m.projectteam2_id '
  206. . ' INNER JOIN #__joomleague_team t2 ON t2.id = pt2.team_id '
  207. . ' WHERE pt1.project_id = ' . $this->_db->Quote($match->project_id)
  208. . ' AND m.published = 1 '
  209. . ' AND m.alt_decision = 0 '
  210. . ' AND t1.id = '. $this->_db->Quote($teamid)
  211. . ' AND (team1_result - team2_result > 0) '
  212. . ' ORDER BY (team1_result - team2_result) DESC '
  213. ;
  214. $this->_db->setQuery($query, 0, 1);
  215. return $this->_db->loadObject();
  216. }
  217. function getHomeHighestHomeWin( )
  218. {
  219. $teams = $this->getMatchTeams();
  220. if ( is_null ( $teams ) )
  221. {
  222. return null;
  223. }
  224. return $this->_getHighestHomeWin( $teams[0]->team_id );
  225. }
  226. function getAwayHighestHomeWin( )
  227. {
  228. $teams = $this->getMatchTeams();
  229. if ( is_null ( $teams ) )
  230. {
  231. return null;
  232. }
  233. return $this->_getHighestHomeWin( $teams[1]->team_id );
  234. }
  235. function _getHighestHomeDef( $teamid )
  236. {
  237. $match = $this->getMatch();
  238. $query = ' SELECT t1.name AS hometeam, '
  239. . ' t2.name AS awayteam, '
  240. . ' team1_result AS homegoals, '
  241. . ' team2_result AS awaygoals, '
  242. . ' pt1.project_id AS pid, '
  243. . ' m.id AS mid '
  244. . ' FROM #__joomleague_match as m '
  245. . ' INNER JOIN #__joomleague_project_team pt1 ON pt1.id = m.projectteam1_id '
  246. . ' INNER JOIN #__joomleague_team t1 ON t1.id = pt1.team_id '
  247. . ' INNER JOIN #__joomleague_project_team pt2 ON pt2.id = m.projectteam2_id '
  248. . ' INNER JOIN #__joomleague_team t2 ON t2.id = pt2.team_id '
  249. . ' WHERE pt1.project_id = ' . $this->_db->Quote($match->project_id)
  250. . ' AND m.published = 1 '
  251. . ' AND m.alt_decision = 0 '
  252. . ' AND t1.id = '. $this->_db->Quote($teamid)
  253. . ' AND (team1_result - team2_result < 0) '
  254. . ' ORDER BY (team1_result - team2_result) ASC '
  255. ;
  256. $this->_db->setQuery($query, 0, 1);
  257. return $this->_db->loadObject();
  258. }
  259. function getHomeHighestHomeDef()
  260. {
  261. $teams = $this->getMatchTeams();
  262. if ( is_null ( $teams ) )
  263. {
  264. return null;
  265. }
  266. return $this->_getHighestHomeDef( $teams[0]->team_id );
  267. }
  268. function getAwayHighestHomeDef()
  269. {
  270. $teams = $this->getMatchTeams();
  271. if ( is_null ( $teams ) )
  272. {
  273. return null;
  274. }
  275. return $this->_getHighestHomeDef( $teams[1]->team_id );
  276. }
  277. function _getHighestAwayWin( $teamid )
  278. {
  279. $match = $this->getMatch();
  280. $query = ' SELECT t1.name AS hometeam, '
  281. . ' t2.name AS awayteam, '
  282. . ' team1_result AS homegoals, '
  283. . ' team2_result AS awaygoals, '
  284. . ' pt1.project_id AS pid, '
  285. . ' m.id AS mid '
  286. . ' FROM #__joomleague_match as m '
  287. . ' INNER JOIN #__joomleague_project_team pt1 ON pt1.id = m.projectteam1_id '
  288. . ' INNER JOIN #__joomleague_team t1 ON t1.id = pt1.team_id '
  289. . ' INNER JOIN #__joomleague_project_team pt2 ON pt2.id = m.projectteam2_id '
  290. . ' INNER JOIN #__joomleague_team t2 ON t2.id = pt2.team_id '
  291. . ' WHERE pt1.project_id = ' . $this->_db->Quote($match->project_id)
  292. . ' AND m.published = 1 '
  293. . ' AND m.alt_decision = 0 '
  294. . ' AND t2.id = '. $this->_db->Quote($teamid)
  295. . ' AND (team2_result - team1_result > 0) '
  296. . ' ORDER BY (team2_result - team1_result) DESC '
  297. ;
  298. $this->_db->setQuery($query, 0, 1);
  299. return $this->_db->loadObject();
  300. }
  301. function getHomeHighestAwayWin( )
  302. {
  303. $teams = $this->getMatchTeams();
  304. if ( is_null ( $teams ) )
  305. {
  306. return null;
  307. }
  308. return $this->_getHighestAwayWin( $teams[0]->team_id );
  309. }
  310. function getAwayHighestAwayWin( )
  311. {
  312. $teams = $this->getMatchTeams();
  313. if ( is_null ( $teams ) )
  314. {
  315. return null;
  316. }
  317. return $this->_getHighestAwayWin( $teams[1]->team_id );
  318. }
  319. function _getHighestAwayDef( $teamid )
  320. {
  321. $match = $this->getMatch();
  322. $query = ' SELECT t1.name AS hometeam, '
  323. . ' t2.name AS awayteam, '
  324. . ' team1_result AS homegoals, '
  325. . ' team2_result AS awaygoals, '
  326. . ' pt1.project_id AS pid, '
  327. . ' m.id AS mid '
  328. . ' FROM #__joomleague_match as m '
  329. . ' INNER JOIN #__joomleague_project_team pt1 ON pt1.id = m.projectteam1_id '
  330. . ' INNER JOIN #__joomleague_team t1 ON t1.id = pt1.team_id '
  331. . ' INNER JOIN #__joomleague_project_team pt2 ON pt2.id = m.projectteam2_id '
  332. . ' INNER JOIN #__joomleague_team t2 ON t2.id = pt2.team_id '
  333. . ' WHERE pt1.project_id = ' . $this->_db->Quote($match->project_id)
  334. . ' AND m.published = 1 '
  335. . ' AND m.alt_decision = 0 '
  336. . ' AND t2.id = '. $this->_db->Quote($teamid)
  337. . ' AND (team1_result - team2_result > 0) '
  338. . ' ORDER BY (team2_result - team1_result) ASC '
  339. ;
  340. $this->_db->setQuery($query, 0, 1);
  341. return $this->_db->loadObject();
  342. }
  343. function getHomeHighestAwayDef()
  344. {
  345. $teams = $this->getMatchTeams();
  346. if ( is_null ( $teams ) )
  347. {
  348. return null;
  349. }
  350. return $this->_getHighestAwayDef( $teams[0]->team_id );
  351. }
  352. function getAwayHighestAwayDef()
  353. {
  354. $teams = $this->getMatchTeams();
  355. if ( is_null ( $teams ) )
  356. {
  357. return null;
  358. }
  359. return $this->_getHighestAwayDef( $teams[1]->team_id );
  360. }
  361. /**
  362. * get all games in all projects for these 2 teams
  363. * @return array
  364. */
  365. function getGames( )
  366. {
  367. $result = array();
  368. $teams = $this->getMatchTeams();
  369. if ( is_null ( $teams ) )
  370. {
  371. return null;
  372. }
  373. $query = ' SELECT m.*, DATE_FORMAT(m.time_present, "%H:%i") time_present, pt1.project_id, p.timezone, '
  374. . ' p.name AS project_name, '
  375. . ' r.id AS roundid, pt1.division_id, '
  376. . ' r.roundcode AS roundcode, '
  377. . ' r.name AS mname, '
  378. . ' p.id AS prid '
  379. . ' FROM #__joomleague_match as m '
  380. . ' INNER JOIN #__joomleague_project_team pt1 ON pt1.id = m.projectteam1_id '
  381. . ' INNER JOIN #__joomleague_project_team pt2 ON pt2.id = m.projectteam2_id '
  382. . ' INNER JOIN #__joomleague_project AS p ON p.id = pt1.project_id '
  383. . ' INNER JOIN #__joomleague_round r ON m.round_id=r.id '
  384. . ' WHERE ((pt1.team_id = '. $teams[0]->team_id .' AND pt2.team_id = '.$teams[1]->team_id .') '
  385. . ' OR (pt1.team_id = '.$teams[1]->team_id .' AND pt2.team_id = '.$teams[0]->team_id .')) '
  386. . ' AND p.published = 1 '
  387. . ' AND m.published = 1 '
  388. . ' AND m.team1_result IS NOT NULL AND m.team2_result IS NOT NULL';
  389. $query .= " GROUP BY m.id ORDER BY p.ordering, m.match_date ASC";
  390. $this->_db->setQuery( $query );
  391. $result = $this->_db->loadObjectList();
  392. if ($result)
  393. {
  394. foreach ($result as $game)
  395. {
  396. JoomleagueHelper::convertMatchDateToTimezone($game);
  397. }
  398. }
  399. return $result;
  400. }
  401. function getTeamsFromMatches( & $games )
  402. {
  403. $teams = Array();
  404. if ( !count( $games ) )
  405. {
  406. return $teams;
  407. }
  408. foreach ( $games as $m )
  409. {
  410. $projectTeamIds[] = $m->projectteam1_id;
  411. $projectTeamIds[] = $m->projectteam2_id;
  412. }
  413. $listProjectTeamId = implode( ",", array_unique( $projectTeamIds ) );
  414. $query = "SELECT t.id, t.name, pt.id as ptid
  415. FROM #__joomleague_project_team AS pt
  416. INNER JOIN #__joomleague_team AS t ON t.id = pt.team_id
  417. WHERE pt.id IN (".$listProjectTeamId.")";
  418. $this->_db->setQuery( $query );
  419. $result = $this->_db->loadObjectList();
  420. foreach ( $result as $r )
  421. {
  422. $teams[$r->ptid] = $r;
  423. }
  424. return $teams;
  425. }
  426. function getPlayground( $pgid )
  427. {
  428. $query = 'SELECT * FROM #__joomleague_playground
  429. WHERE id = '. $this->_db->Quote($pgid);
  430. $this->_db->setQuery($query, 0, 1);
  431. return $this->_db->loadObject();
  432. }
  433. function getMatchText($match_id)
  434. {
  435. $query = "SELECT m.*, t1.name t1name, t2.name t2name, p.timezone
  436. FROM #__joomleague_match AS m
  437. INNER JOIN #__joomleague_project_team AS pt1 ON m.projectteam1_id = pt1.id
  438. INNER JOIN #__joomleague_project_team AS pt2 ON m.projectteam2_id = pt2.id
  439. INNER JOIN #__joomleague_team AS t1 ON pt1.team_id=t1.id
  440. INNER JOIN #__joomleague_team AS t2 ON pt2.team_id=t2.id
  441. INNER JOIN #__joomleague_project AS p ON p.id=pt1.project_id
  442. WHERE m.id = " . $match_id . "
  443. AND m.published = 1
  444. ORDER BY m.match_date, t1.short_name"
  445. ;
  446. $this->_db->setQuery($query);
  447. $matchText = $this->_db->loadObject();
  448. if ($matchText)
  449. {
  450. JoomleagueHelper::convertMatchDateToTimezone($matchText);
  451. }
  452. return $matchText;
  453. }
  454. /**
  455. * Calculates chances between 2 team
  456. * Code is from LMO, all credits go to the LMO developers
  457. * @return array
  458. */
  459. function getChances()
  460. {
  461. $home=$this->getHomeRanked();
  462. $away=$this->getAwayRanked();
  463. if ((($home->cnt_matches)>0) && (($away->cnt_matches)>0))
  464. {
  465. $won1=$home->cnt_won;
  466. $won2=$away->cnt_won;
  467. $loss1=$home->cnt_lost;
  468. $loss2=$away->cnt_lost;
  469. $matches1=$home->cnt_matches;
  470. $matches2=$away->cnt_matches;
  471. $goalsfor1=$home->sum_team1_result;
  472. $goalsfor2=$away->sum_team1_result;
  473. $goalsagainst1=$home->sum_team2_result;
  474. $goalsagainst2=$away->sum_team2_result;
  475. $ax=(100*$won1/$matches1)+(100*$loss2/$matches2);
  476. $bx=(100*$won2/$matches2)+(100*$loss1/$matches1);
  477. $cx=($goalsfor1/$matches1)+($goalsagainst2/$matches2);
  478. $dx=($goalsfor2/$matches2)+($goalsagainst1/$matches1);
  479. $ex=$ax+$bx;
  480. $fx=$cx+$dx;
  481. if (isset($ex) && ($ex>0) && isset($fx) &&($fx>0))
  482. {
  483. $ax=round(10000*$ax/$ex);
  484. $bx=round(10000*$bx/$ex);
  485. $cx=round(10000*$cx/$fx);
  486. $dx=round(10000*$dx/$fx);
  487. $chg1=number_format((($ax+$cx)/200),2,",",".");
  488. $chg2=number_format((($bx+$dx)/200),2,",",".");
  489. $result=array($chg1,$chg2);
  490. return $result;
  491. }
  492. }
  493. }
  494. /**
  495. * get Previous X games of each team
  496. *
  497. * @return array
  498. */
  499. function getPreviousX()
  500. {
  501. if (!$this->_match) {
  502. return false;
  503. }
  504. $games = array();
  505. $games[$this->_match->projectteam1_id] = $this->_getTeamPreviousX($this->_match->roundcode, $this->_match->projectteam1_id);
  506. $games[$this->_match->projectteam2_id] = $this->_getTeamPreviousX($this->_match->roundcode, $this->_match->projectteam2_id);
  507. return $games;
  508. }
  509. /**
  510. * returns last X games
  511. *
  512. * @param int $current_roundcode
  513. * @param int $ptid project team id
  514. * @return array
  515. */
  516. function _getTeamPreviousX($current_roundcode, $ptid)
  517. {
  518. $config = $this->getTemplateConfig('nextmatch');
  519. $nblast = $config['nb_previous'];
  520. $query = ' SELECT m.*, r.project_id, r.id AS roundid, r.roundcode, p.timezone, pt1.division_id '
  521. . ' FROM #__joomleague_match AS m '
  522. . ' INNER JOIN #__joomleague_round AS r ON r.id = m.round_id '
  523. . ' INNER JOIN #__joomleague_project AS p ON p.id = r.project_id '
  524. . ' INNER JOIN #__joomleague_project_team AS pt1 ON p.id = m.projectteam1_id '
  525. . ' WHERE (m.projectteam1_id = ' . $ptid
  526. . ' OR m.projectteam2_id = ' . $ptid.')'
  527. . ' AND r.roundcode < '.$current_roundcode
  528. . ' AND m.published = 1 '
  529. . ' GROUP BY m.id '
  530. . ' ORDER BY r.roundcode DESC '
  531. . ' LIMIT 0, '.$nblast
  532. ;
  533. $this->_db->setQuery($query);
  534. $res = $this->_db->loadObjectList();
  535. if ($res) {
  536. $res = array_reverse($res);
  537. foreach ($res as $game)
  538. {
  539. JoomleagueHelper::convertMatchDateToTimezone($game);
  540. }
  541. }
  542. return $res;
  543. }
  544. }
  545. ?>