PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/statistics/eventpergame.php

https://gitlab.com/julienv/joomleague
PHP | 337 lines | 265 code | 42 blank | 30 comment | 23 complexity | 8574f4ce282a5c47335d3449a0e5924a 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. require_once(JLG_PATH_ADMIN.DS.'statistics'.DS.'base.php');
  14. /**
  15. * base class for statistics handling.
  16. *
  17. * @package Joomla
  18. * @subpackage Joomleague
  19. * @since 0.9
  20. */
  21. class JLGStatisticEventPergame extends JLGStatistic {
  22. //also the name of the associated xml file
  23. var $_name = 'eventpergame';
  24. var $_calculated = 1;
  25. var $_showinsinglematchreports = 0;
  26. function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. function getSids()
  31. {
  32. $params = &$this->getParams();
  33. if(!is_array($params->get('event_ids'))) {
  34. $event_ids = explode(',', $params->get('event_ids'));
  35. } else {
  36. $event_ids = $params->get('event_ids');
  37. }
  38. if (!count($event_ids)) {
  39. JError::raiseWarning(0, JText::sprintf('STAT %s/%s WRONG CONFIGURATION', $this->_name, $this->id));
  40. return(array(0));
  41. }
  42. $ids = array();
  43. foreach ($event_ids as $s) {
  44. $ids[] = (int)$s;
  45. }
  46. return $ids;
  47. }
  48. function getQuotedSids()
  49. {
  50. $db = JFactory::getDbo();
  51. $params = $this->getParams();
  52. if(!is_array($params->get('event_ids'))) {
  53. $event_ids = explode(',', $params->get('event_ids'));
  54. } else {
  55. $event_ids = $params->get('event_ids');
  56. }
  57. if (!count($event_ids)) {
  58. JError::raiseWarning(0, JText::sprintf('STAT %s/%s WRONG CONFIGURATION', $this->_name, $this->id));
  59. return(array(0));
  60. }
  61. $db = JFactory::getDbo();
  62. $ids = array();
  63. foreach ($event_ids as $s) {
  64. $ids[] = $db->Quote((int)$s);
  65. }
  66. return $ids;
  67. }
  68. /**
  69. * (non-PHPdoc)
  70. * @see administrator/components/com_joomleague/statistics/JLGStatistic#getPlayerStatsByProject($person_id, $project_id)
  71. */
  72. function getPlayerStatsByProject($person_id, $projectteam_id = 0, $project_id = 0, $sports_type_id = 0)
  73. {
  74. $sids = $this->getSids();
  75. $num = $this->getPlayerStatsByProjectForEvents($person_id, $projectteam_id, $project_id, $sports_type_id, $sids);
  76. $den = $this->getGamesPlayedByPlayer($person_id, $projectteam_id, $project_id, $sports_type_id);
  77. return $this->formatValue($num, $den, $this->getPrecision());
  78. }
  79. /**
  80. * Get players stats
  81. * @param $team_id
  82. * @param $project_id
  83. * @return array
  84. */
  85. function getRosterStats($team_id, $project_id, $position_id)
  86. {
  87. $sids = $this->getSids();
  88. $num = $this->getRosterStatsForEvents($team_id, $project_id, $position_id, $sids);
  89. $den = $this->getGamesPlayedByProjectTeam($team_id, $project_id, $position_id);
  90. $precision = $this->getPrecision();
  91. $res = array();
  92. foreach (array_unique(array_merge(array_keys($num), array_keys($den))) as $person_id)
  93. {
  94. $n = isset($num[$person_id]->value) ? $num[$person_id]->value : 0;
  95. $d = isset($den[$person_id]->value) ? $den[$person_id]->value : 0;
  96. $res[$person_id] = new stdclass();
  97. $res[$person_id]->person_id = $person_id;
  98. $res[$person_id]->value = $this->formatValue($n, $d, $precision);
  99. }
  100. return $res;
  101. }
  102. function getPlayersRanking($project_id, $division_id, $team_id, $limit = 20, $limitstart = 0, $order=null)
  103. {
  104. $sids = $this->getQuotedSids();
  105. $db = JFactory::getDbo();
  106. $query_num = ' SELECT SUM(me.event_sum) AS num, tp.id AS tpid, tp.person_id '
  107. . ' FROM #__joomleague_team_player AS tp '
  108. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  109. . ' INNER JOIN #__joomleague_match_event AS me ON me.teamplayer_id = tp.id '
  110. . ' AND me.event_type_id IN ('. implode(',', $sids) .')'
  111. . ' INNER JOIN #__joomleague_match AS m ON m.id = me.match_id '
  112. . ' AND m.published = 1 '
  113. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  114. ;
  115. if ($division_id != 0)
  116. {
  117. $query_num .= ' AND pt.division_id = '. $db->Quote($division_id);
  118. }
  119. if ($team_id != 0)
  120. {
  121. $query_num .= ' AND pt.team_id = ' . $db->Quote($team_id);
  122. }
  123. $query_num .= ' GROUP BY tp.id ';
  124. $query_den = $this->getGamesPlayedQuery($project_id, $division_id, $team_id);
  125. $query_select_count = ' SELECT COUNT(DISTINCT tp.id) as count';
  126. $query_select_details = ' SELECT (n.num / d.played) AS total, n.person_id, 1 as rank,'
  127. . ' tp.id AS teamplayer_id, tp.person_id, tp.picture AS teamplayerpic,'
  128. . ' p.firstname, p.nickname, p.lastname, p.picture, p.country,'
  129. . ' pt.team_id, pt.picture AS projectteam_picture,'
  130. . ' t.picture AS team_picture, t.name AS team_name, t.short_name AS team_short_name';
  131. $query_core = ' FROM #__joomleague_team_player AS tp'
  132. . ' INNER JOIN ('.$query_num.') AS n ON n.tpid = tp.id'
  133. . ' INNER JOIN ('.$query_den.') AS d ON d.tpid = tp.id'
  134. . ' INNER JOIN #__joomleague_person AS p ON p.id = tp.person_id'
  135. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id'
  136. . ' INNER JOIN #__joomleague_team AS t ON pt.team_id = t.id'
  137. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  138. . ' AND p.published = 1 ';
  139. if ($division_id != 0)
  140. {
  141. $query_core .= ' AND pt.division_id = '. $db->Quote($division_id);
  142. }
  143. if ($team_id != 0)
  144. {
  145. $query_core .= ' AND pt.team_id = ' . $db->Quote($team_id);
  146. }
  147. $query_end_details = ' ORDER BY total '.(!empty($order) ? $order : $this->getParam('ranking_order', 'DESC')).' ';
  148. $res = new stdclass;
  149. $db->setQuery($query_select_count.$query_core);
  150. $res->pagination_total = $db->loadResult();
  151. $db->setQuery($query_select_details.$query_core.$query_end_details, $limitstart, $limit);
  152. $res->ranking = $db->loadObjectList();
  153. if ($res->ranking)
  154. {
  155. $precision = $this->getPrecision();
  156. // get ranks
  157. $previousval = 0;
  158. $currentrank = 1 + $limitstart;
  159. foreach ($res->ranking as $k => $row)
  160. {
  161. if ($row->total == $previousval) {
  162. $res->ranking[$k]->rank = $currentrank;
  163. }
  164. else {
  165. $res->ranking[$k]->rank = $k + 1 + $limitstart;
  166. }
  167. $previousval = $row->total;
  168. $currentrank = $res->ranking[$k]->rank;
  169. $res->ranking[$k]->total = $this->formatValue($res->ranking[$k]->total, 1, $precision);
  170. }
  171. }
  172. return $res;
  173. }
  174. function getTeamsRanking($project_id, $limit = 20, $limitstart = 0)
  175. {
  176. $sids = $this->getQuotedSids();
  177. $db = JFactory::getDbo();
  178. $query_num = ' SELECT SUM(es.event_sum) AS num, pt.id '
  179. . ' FROM #__joomleague_team_player AS tp '
  180. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  181. . ' INNER JOIN #__joomleague_match_event AS es ON es.teamplayer_id = tp.id '
  182. . ' AND es.event_type_id IN ('. implode(',', $sids) .')'
  183. . ' INNER JOIN #__joomleague_match AS m ON m.id = es.match_id '
  184. . ' AND m.published = 1 '
  185. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  186. . ' AND tp.published = 1 '
  187. . ' GROUP BY pt.id '
  188. ;
  189. $query_den = ' SELECT COUNT(m.id) AS value, pt.id '
  190. . ' FROM #__joomleague_project_team AS pt '
  191. . ' INNER JOIN #__joomleague_match AS m ON m.projectteam1_id = pt.id OR m.projectteam2_id = pt.id'
  192. . ' AND m.team1_result IS NOT NULL '
  193. . ' AND m.published = 1 '
  194. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  195. . ' GROUP BY pt.id '
  196. ;
  197. $query = ' SELECT (n.num / d.value) AS total, pt.team_id '
  198. . ' FROM #__joomleague_project_team AS pt '
  199. . ' INNER JOIN ('.$query_num.') AS n ON n.id = pt.id '
  200. . ' INNER JOIN ('.$query_den.') AS d ON d.id = pt.id '
  201. . ' INNER JOIN #__joomleague_team AS t ON pt.team_id = t.id '
  202. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  203. . ' ORDER BY total DESC '
  204. ;
  205. $db->setQuery($query, $limitstart, $limit);
  206. $res = $db->loadObjectList();
  207. if (!empty($res))
  208. {
  209. $precision = $this->getPrecision();
  210. // get ranks
  211. $previousval = 0;
  212. $currentrank = 1 + $limitstart;
  213. foreach ($res as $k => $row)
  214. {
  215. if ($row->total == $previousval) {
  216. $res[$k]->rank = $currentrank;
  217. }
  218. else {
  219. $res[$k]->rank = $k + 1 + $limitstart;
  220. }
  221. $previousval = $row->total;
  222. $currentrank = $res[$k]->rank;
  223. $res[$k]->total = $this->formatValue($res[$k]->total, 1, $precision);
  224. }
  225. }
  226. return $res;
  227. }
  228. function getStaffStats($person_id, $team_id, $project_id)
  229. {
  230. $sids = $this->getQuotedSids();
  231. $db = JFactory::getDbo();
  232. $query = ' SELECT SUM(ms.value) AS value, tp.person_id '
  233. . ' FROM #__joomleague_team_staff AS tp '
  234. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  235. . ' INNER JOIN #__joomleague_match_staff_statistic AS ms ON ms.team_staff_id = tp.id '
  236. . ' AND ms.statistic_id IN ('. implode(',', $sids) .')'
  237. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  238. . ' AND m.published = 1 '
  239. . ' WHERE pt.team_id = '. $db->Quote($team_id)
  240. . ' AND pt.project_id = '. $db->Quote($project_id)
  241. . ' AND tp.person_id = '. $db->Quote($person_id)
  242. . ' GROUP BY tp.id '
  243. ;
  244. $db->setQuery($query);
  245. $num = $db->loadResult();
  246. $query = ' SELECT COUNT(ms.id) AS value, tp.person_id '
  247. . ' FROM #__joomleague_team_staff AS tp '
  248. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  249. . ' INNER JOIN #__joomleague_match_staff AS ms ON ms.team_staff_id = tp.id '
  250. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  251. . ' AND m.published = 1 '
  252. . ' WHERE pt.team_id = '. $db->Quote($team_id)
  253. . ' AND pt.project_id = '. $db->Quote($project_id)
  254. . ' AND tp.person_id = '. $db->Quote($person_id)
  255. . ' GROUP BY tp.id '
  256. ;
  257. $db->setQuery($query);
  258. $den = $db->loadResult();
  259. return $this->formatValue($num, $den, $this->getPrecision());
  260. }
  261. function getHistoryStaffStats($person_id)
  262. {
  263. $sids = $this->getQuotedSids();
  264. $db = JFactory::getDbo();
  265. $query = ' SELECT SUM(ms.value) AS value, tp.person_id '
  266. . ' FROM #__joomleague_team_staff AS tp '
  267. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  268. . ' INNER JOIN #__joomleague_match_staff_statistic AS ms ON ms.team_staff_id = tp.id '
  269. . ' AND ms.statistic_id IN ('. implode(',', $sids) .')'
  270. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  271. . ' AND m.published = 1 '
  272. . ' WHERE tp.person_id = '. $db->Quote($person_id)
  273. . ' GROUP BY tp.id '
  274. ;
  275. $db->setQuery($query);
  276. $num = $db->loadResult();
  277. $query = ' SELECT COUNT(ms.id) AS value, tp.person_id '
  278. . ' FROM #__joomleague_team_staff AS tp '
  279. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  280. . ' INNER JOIN #__joomleague_match_staff AS ms ON ms.team_staff_id = tp.id '
  281. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  282. . ' AND m.published = 1 '
  283. . ' WHERE tp.person_id = '. $db->Quote($person_id)
  284. . ' GROUP BY tp.id '
  285. ;
  286. $db->setQuery($query);
  287. $den = $db->loadResult();
  288. return $this->formatValue($num, $den, $this->getPrecision());
  289. }
  290. function formatValue($num, $den, $precision)
  291. {
  292. $value = (!empty($num) && !empty($den)) ? $num / $den : 0;
  293. return number_format($value, $precision);
  294. }
  295. }