PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/statistics/percentage.php

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