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

/com_joomleague/administrator/components/com_joomleague/statistics/complexsumpergame.php

https://gitlab.com/julienv/joomleague
PHP | 456 lines | 359 code | 58 blank | 39 comment | 41 complexity | 3f6b899daf232141f4db0e73e6bfd1c1 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 JLGStatisticComplexsumpergame extends JLGStatistic {
  22. //also the name of the associated xml file
  23. var $_name = 'complexsumpergame';
  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('stat_ids'))) {
  34. $stat_ids = explode(',', $params->get('stat_ids'));
  35. } else {
  36. $stat_ids = $params->get('stat_ids');
  37. }
  38. if (!count($stat_ids)) {
  39. JError::raiseWarning(0, JText::sprintf('STAT %s/%s WRONG CONFIGURATION', $this->_name, $this->id));
  40. return(array(0));
  41. }
  42. $sids = array();
  43. foreach ($stat_ids as $s) {
  44. $sids[] = (int)$s;
  45. }
  46. return $sids;
  47. }
  48. function getFactors()
  49. {
  50. $params = &$this->getParams();
  51. $factors = explode(',', $params->get('factors'));
  52. $stat_ids = $this->getSids();
  53. if (count($stat_ids) != count($factors)) {
  54. JError::raiseWarning(0, JText::sprintf('STAT %s/%s WRONG CONFIGURATION - BAD FACTORS COUNT', $this->_name, $this->id));
  55. return(array(0));
  56. }
  57. $sids = array();
  58. foreach ($factors as $s) {
  59. $sids[] = (float)$s;
  60. }
  61. return $sids;
  62. }
  63. function getQuotedSids()
  64. {
  65. $params = &$this->getParams();
  66. if(!is_array($params->get('stat_ids'))) {
  67. $stat_ids = explode(',', $params->get('stat_ids'));
  68. } else {
  69. $stat_ids = $params->get('stat_ids');
  70. }
  71. if (!count($stat_ids)) {
  72. JError::raiseWarning(0, JText::sprintf('STAT %s/%s WRONG CONFIGURATION', $this->_name, $this->id));
  73. return(array(0));
  74. }
  75. $db = JFactory::getDbo();
  76. $sids = array();
  77. foreach ($stat_ids as $s) {
  78. $sids[] = $db->Quote($s);
  79. }
  80. return $sids;
  81. }
  82. function getPlayerStatsByProject($person_id, $projectteam_id = 0, $project_id = 0, $sports_type_id = 0)
  83. {
  84. $sids = $this->getSids();
  85. $factors = $this->getFactors();
  86. $num = $this->getPlayerStatsByProjectForIds($person_id, $projectteam_id, $project_id, $sports_type_id, $sids, $factors);
  87. $den = $this->getGamesPlayedByPlayer($person_id, $projectteam_id, $project_id, $sports_type_id);
  88. return $this->formatValue($num, $den, $this->getPrecision());
  89. }
  90. /**
  91. * Get players stats
  92. * @param $team_id
  93. * @param $project_id
  94. * @param $position_id
  95. * @return array
  96. */
  97. function getRosterStats($team_id, $project_id, $position_id)
  98. {
  99. $sids = $this->getSids();
  100. $factors = $this->getFactors();
  101. $num = $this->getRosterStatsForIds($team_id, $project_id, $position_id, $sids, $factors);
  102. $den = $this->getGamesPlayedByProjectTeam($team_id, $project_id, $position_id);
  103. $precision = $this->getPrecision();
  104. $res = array();
  105. foreach (array_unique(array_merge(array_keys($num), array_keys($den))) as $person_id)
  106. {
  107. $res[$person_id] = new stdclass();
  108. $res[$person_id]->person_id = $person_id;
  109. $n = isset($num[$person_id]->value) ? $num[$person_id]->value : 0;
  110. $d = isset($den[$person_id]->value) ? $den[$person_id]->value : 0;
  111. $res[$person_id]->value = $this->formatValue($n, $d, $precision);
  112. }
  113. return $res;
  114. }
  115. function getPlayersRanking($project_id, $division_id, $team_id, $limit = 20, $limitstart = 0, $order = null)
  116. {
  117. $sids = $this->getSids();
  118. $sqids = $this->getQuotedSids();
  119. $factors = $this->getFactors();
  120. $db = JFactory::getDbo();
  121. // get all stats
  122. $query = ' SELECT ms.value, ms.statistic_id, tp.id AS tpid'
  123. . ' FROM #__joomleague_match_statistic AS ms'
  124. . ' INNER JOIN #__joomleague_team_player AS tp ON ms.teamplayer_id = tp.id'
  125. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id'
  126. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id'
  127. . ' WHERE pt.project_id = '. $db->Quote($project_id);
  128. if ($division_id != 0)
  129. {
  130. $query .= ' AND pt.division_id = '. $db->Quote($division_id);
  131. }
  132. if ($team_id != 0)
  133. {
  134. $query .= ' AND pt.team_id = ' . $db->Quote($team_id);
  135. }
  136. $query .= ' AND ms.statistic_id IN ('. implode(',', $sqids) .')'
  137. . ' AND m.published = 1';
  138. $db->setQuery($query);
  139. $stats = $db->loadObjectList();
  140. $query = $this->getGamesPlayedQuery($project_id, $division_id, $team_id);
  141. $db->setQuery($query);
  142. $gp = $db->loadObjectList('tpid');
  143. // now calculate per player
  144. $players = array();
  145. // first, the numerator complex sum
  146. foreach ($stats as $stat)
  147. {
  148. $key = array_search($stat->statistic_id, $sids);
  149. if ($key !== FALSE) {
  150. @$players[$stat->tpid] += $factors[$key]*$stat->value;
  151. }
  152. }
  153. // then divide by games played
  154. foreach ($players as $k => $value)
  155. {
  156. if (isset($gp[$k]) && $gp[$k]->played) {
  157. $players[$k] = $players[$k] / $gp[$k]->played;
  158. }
  159. else {
  160. unset($players[$k]);
  161. }
  162. }
  163. // now we reorder
  164. $order = (!empty($order) ? $order : $this->getParam('ranking_order', 'DESC'));
  165. if ($order == 'ASC') {
  166. asort($players);
  167. }
  168. else {
  169. arsort($players);
  170. }
  171. $res = new stdclass;
  172. $res->pagination_total = count($players);
  173. $players = array_slice($players, $limitstart, $limit, true);
  174. $ids = array_keys($players);
  175. $query = ' SELECT tp.id AS teamplayer_id, tp.person_id, tp.picture AS teamplayerpic,'
  176. . ' p.firstname, p.nickname, p.lastname, p.picture, p.country,'
  177. . ' pt.team_id, pt.picture AS projectteam_picture, t.picture AS team_picture,'
  178. . ' t.name AS team_name, t.short_name AS team_short_name'
  179. . ' FROM #__joomleague_team_player AS tp'
  180. . ' INNER JOIN #__joomleague_person AS p ON p.id = tp.person_id'
  181. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id'
  182. . ' INNER JOIN #__joomleague_team AS t ON pt.team_id = t.id'
  183. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  184. . ' AND p.published = 1'
  185. . ' AND tp.id IN ('. implode(',', $ids) .')';
  186. if ($division_id != 0)
  187. {
  188. $query .= ' AND pt.division_id = '. $db->Quote($division_id);
  189. }
  190. if ($team_id != 0)
  191. {
  192. $query .= ' AND pt.team_id = ' . $db->Quote($team_id);
  193. }
  194. $db->setQuery($query);
  195. $details = $db->loadObjectList('teamplayer_id');
  196. $res->ranking = array();
  197. if (!empty($details))
  198. {
  199. $precision = $this->getPrecision();
  200. // get ranks
  201. $previousval = 0;
  202. $currentrank = 1 + $limitstart;
  203. $i = 0;
  204. foreach ($players as $k => $value)
  205. {
  206. $res->ranking[$i] = $details[$k];
  207. $res->ranking[$i]->total = $value;
  208. if ($value == $previousval) {
  209. $res->ranking[$i]->rank = $currentrank;
  210. }
  211. else {
  212. $res->ranking[$i]->rank = $i + 1 + $limitstart;
  213. }
  214. $previousval = $value;
  215. $currentrank = $res->ranking[$i]->rank;
  216. $res->ranking[$i]->total = $this->formatValue($res->ranking[$i]->total, 1, $precision);
  217. $i++;
  218. }
  219. }
  220. return $res;
  221. }
  222. function getTeamsRanking($project_id, $limit = 20, $limitstart = 0, $order=null)
  223. {
  224. $sids = $this->getSids();
  225. $sqids = $this->getQuotedSids();
  226. $factors = $this->getFactors();
  227. $db = JFactory::getDbo();
  228. // team games
  229. $query = ' SELECT COUNT(m.id) AS value, pt.team_id '
  230. . ' FROM #__joomleague_project_team AS pt '
  231. . ' INNER JOIN #__joomleague_match AS m ON m.projectteam1_id = pt.id OR m.projectteam2_id = pt.id'
  232. . ' AND m.published = 1 '
  233. . ' AND m.team1_result IS NOT NULL '
  234. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  235. . ' GROUP BY pt.id '
  236. ;
  237. $db->setQuery($query);
  238. $gp = $db->loadObjectList('team_id');
  239. // get all stats
  240. $query = ' SELECT ms.value, ms.statistic_id, pt.team_id '
  241. . ' FROM #__joomleague_match_statistic AS ms '
  242. . ' INNER JOIN #__joomleague_team_player AS tp ON ms.teamplayer_id = tp.id '
  243. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  244. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  245. . ' WHERE pt.project_id = '. $db->Quote($project_id)
  246. . ' AND ms.statistic_id IN ('. implode(',', $sqids) .')'
  247. . ' AND tp.published = 1 '
  248. . ' AND m.published = 1 '
  249. ;
  250. $db->setQuery($query);
  251. $stats = $db->loadObjectList();
  252. // now calculate per team
  253. $teams = array();
  254. foreach ($stats as $stat)
  255. {
  256. $key = array_search($stat->statistic_id, $sids);
  257. if ($key !== FALSE) {
  258. @$teams[$stat->team_id] += $factors[$key]*$stat->value;
  259. }
  260. }
  261. // then divide by games played
  262. foreach ($teams as $k => $value)
  263. {
  264. if (isset($gp[$k]) && $gp[$k]->value) {
  265. $teams[$k] = $teams[$k] / $gp[$k]->value;
  266. }
  267. else {
  268. unset($teams[$k]);
  269. }
  270. }
  271. // now we reorder
  272. $order = (!empty($order) ? $order : $this->getParam('ranking_order', 'DESC'));
  273. if ($order == 'ASC') {
  274. asort($teams);
  275. }
  276. else {
  277. arsort($teams);
  278. }
  279. $teams = array_slice($teams, $limitstart, $limit, true);
  280. $res = array();
  281. foreach ($teams as $id => $value)
  282. {
  283. $obj = new stdclass;
  284. $obj->team_id = $id;
  285. $obj->total = $value;
  286. $res[] = $obj;
  287. }
  288. if (!empty($res))
  289. {
  290. $precision = $this->getPrecision();
  291. // get ranks
  292. $previousval = 0;
  293. $currentrank = 1 + $limitstart;
  294. foreach ($res as $k => $row)
  295. {
  296. if ($row->total == $previousval) {
  297. $res[$k]->rank = $currentrank;
  298. }
  299. else {
  300. $res[$k]->rank = $k + 1 + $limitstart;
  301. }
  302. $previousval = $row->total;
  303. $currentrank = $res[$k]->rank;
  304. $res[$k]->total = $this->formatValue($res[$k]->total, 1, $precision);
  305. }
  306. }
  307. return $res;
  308. }
  309. function getStaffStats($person_id, $team_id, $project_id)
  310. {
  311. $sids = $this->getSids();
  312. $sqids = $this->getQuotedSids();
  313. $factors = $this->getFactors();
  314. $db = JFactory::getDbo();
  315. $query = ' SELECT ms.value, ms.statistic_id '
  316. . ' FROM #__joomleague_team_staff AS tp '
  317. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  318. . ' INNER JOIN #__joomleague_match_staff_statistic AS ms ON ms.team_staff_id = tp.id '
  319. . ' AND ms.statistic_id IN ('. implode(',', $sqids) .')'
  320. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  321. . ' AND m.published = 1 '
  322. . ' WHERE pt.team_id = '. $db->Quote($team_id)
  323. . ' AND pt.project_id = '. $db->Quote($project_id)
  324. . ' AND tp.person_id = '. $db->Quote($person_id)
  325. ;
  326. $db->setQuery($query);
  327. $stats = $db->loadObjectList();
  328. $num = 0;
  329. foreach ($stats as $stat)
  330. {
  331. $key = array_search($stat->statistic_id, $sids);
  332. if ($key !== FALSE) {
  333. $num += $factors[$key]*$stat->value;
  334. }
  335. }
  336. //games
  337. $query = ' SELECT COUNT(ms.id) AS value, tp.person_id '
  338. . ' FROM #__joomleague_team_staff AS tp '
  339. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  340. . ' INNER JOIN #__joomleague_match_staff AS ms ON ms.team_staff_id = tp.id '
  341. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  342. . ' AND m.published = 1 '
  343. . ' WHERE pt.team_id = '. $db->Quote($team_id)
  344. . ' AND pt.project_id = '. $db->Quote($project_id)
  345. . ' AND tp.person_id = '. $db->Quote($person_id)
  346. . ' GROUP BY tp.id '
  347. ;
  348. $db->setQuery($query);
  349. $den = $db->loadResult();
  350. return $this->formatValue($num, $den, $this->getPrecision());;
  351. }
  352. function getHistoryStaffStats($person_id)
  353. {
  354. $sids = $this->getSids();
  355. $sqids = $this->getQuotedSids();
  356. $factors = $this->getFactors();
  357. $db = JFactory::getDbo();
  358. $query = ' SELECT ms.value AS value, ms.statistic_id '
  359. . ' FROM #__joomleague_team_staff AS tp '
  360. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  361. . ' INNER JOIN #__joomleague_project AS p ON p.id = pt.project_id '
  362. . ' INNER JOIN #__joomleague_match_staff_statistic AS ms ON ms.team_staff_id = tp.id '
  363. . ' AND ms.statistic_id IN ('. implode(',', $sqids) .')'
  364. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  365. . ' AND m.published = 1 '
  366. . ' WHERE tp.person_id = '. $db->Quote($person_id)
  367. . ' AND p.published = 1 '
  368. ;
  369. $db->setQuery($query);
  370. $stats = $db->loadObjectList();
  371. $num = 0;
  372. foreach ($stats as $stat)
  373. {
  374. $key = array_search($stat->statistic_id, $sids);
  375. if ($key !== FALSE) {
  376. $num += $factors[$key]*$stat->value;
  377. }
  378. }
  379. //games
  380. $query = ' SELECT COUNT(ms.id) AS value, tp.person_id '
  381. . ' FROM #__joomleague_team_staff AS tp '
  382. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  383. . ' INNER JOIN #__joomleague_match_staff AS ms ON ms.team_staff_id = tp.id '
  384. . ' INNER JOIN #__joomleague_match AS m ON m.id = ms.match_id '
  385. . ' AND m.published = 1 '
  386. . ' WHERE tp.person_id = '. $db->Quote($person_id)
  387. . ' GROUP BY tp.id '
  388. ;
  389. $db->setQuery($query);
  390. $den = $db->loadResult();
  391. return $this->formatValue($num, $den, $this->getPrecision());;
  392. }
  393. function formatValue($num, $den, $precision)
  394. {
  395. $value = (!empty($num) && !empty($den)) ? $num / $den : 0;
  396. return number_format($value, $precision);
  397. }
  398. }