PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/statistics/pergame.php

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