PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/statistics/difference.php

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