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

/administrator/components/com_flexicontent/models/stats.php

http://flexicontent.googlecode.com/
PHP | 422 lines | 257 code | 62 blank | 103 comment | 53 complexity | e1edee5aa8b719219970a1b426a97397 MD5 | raw file
Possible License(s): MIT, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @version 1.5 stable $Id: stats.php 1738 2013-08-24 17:42:17Z ggppdk $
  4. * @package Joomla
  5. * @subpackage FLEXIcontent
  6. * @copyright (C) 2009 Emmanuel Danan - www.vistamedia.fr
  7. * @license GNU/GPL v2
  8. *
  9. * FLEXIcontent is a derivative work of the excellent QuickFAQ component
  10. * @copyright (C) 2008 Christoph Lukes
  11. * see www.schlu.net for more information
  12. *
  13. * FLEXIcontent is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. // no direct access
  19. defined('_JEXEC') or die('Restricted access');
  20. jimport('joomla.application.component.model');
  21. /**
  22. * FLEXIcontent Component stats Model
  23. *
  24. * @package Joomla
  25. * @subpackage FLEXIcontent
  26. * @since 1.0
  27. */
  28. class FlexicontentModelStats extends JModelLegacy
  29. {
  30. /**
  31. * Rating resolution
  32. *
  33. * @var object
  34. */
  35. var $_rating_resolution = null;
  36. /**
  37. * Constructor
  38. *
  39. * @since 1.0
  40. */
  41. function __construct()
  42. {
  43. parent::__construct();
  44. $this->getRatingResolution();
  45. }
  46. /**
  47. * Method to get general stats
  48. *
  49. * @access public
  50. * @return array
  51. */
  52. function getGeneralstats()
  53. {
  54. $_items = array();
  55. // Get total nr of items
  56. $query = 'SELECT count(i.id)'
  57. . ' FROM #__content as i'
  58. . ' WHERE sectionid = ' . FLEXI_SECTION
  59. ;
  60. $this->_db->SetQuery($query);
  61. $_items[] = $this->_db->loadResult();
  62. // Get nr of all categories
  63. $query = 'SELECT count(id)'
  64. . ' FROM #__categories'
  65. . ' WHERE section = ' . FLEXI_SECTION
  66. ;
  67. $this->_db->SetQuery($query);
  68. $_items[] = $this->_db->loadResult();
  69. // Get nr of all tags
  70. $query = 'SELECT count(id) FROM #__flexicontent_tags';
  71. $this->_db->SetQuery($query);
  72. $_items[] = $this->_db->loadResult();
  73. // Get nr of all files
  74. $query = 'SELECT count(id) FROM #__flexicontent_files';
  75. $this->_db->SetQuery($query);
  76. $_items[] = $this->_db->loadResult();
  77. return $_items;
  78. }
  79. /**
  80. * Method to get popular data
  81. *
  82. * @access public
  83. * @return array
  84. */
  85. function getPopular()
  86. {
  87. $_df = 100 / $this->_rating_resolution;
  88. $query = 'SELECT (cr.rating_sum / cr.rating_count ) * '.$_df.' AS votes, i.title, i.id, i.hits'
  89. . ' FROM #__content AS i'
  90. . ' LEFT JOIN #__content_rating AS cr ON cr.content_id = i.id'
  91. . ' WHERE i.sectionid = ' . FLEXI_SECTION
  92. . ' ORDER BY i.hits DESC'
  93. . ' LIMIT 5'
  94. ;
  95. $this->_db->SetQuery($query);
  96. $hits = $this->_db->loadObjectList();
  97. return $hits;
  98. }
  99. /**
  100. * Method to get rating data
  101. *
  102. * @access public
  103. * @return array
  104. */
  105. function getRating()
  106. {
  107. $_df = 100 / $this->_rating_resolution;
  108. $query = 'SELECT (cr.rating_sum / cr.rating_count ) * '.$_df.' AS votes, i.title, i.id'
  109. . ' FROM #__content AS i'
  110. . ' INNER JOIN #__content_rating AS cr ON cr.content_id = i.id'
  111. . ' WHERE i.sectionid = ' . FLEXI_SECTION
  112. . ' ORDER BY votes DESC'
  113. . ' LIMIT 5'
  114. ;
  115. $this->_db->SetQuery($query);
  116. $votes = $this->_db->loadObjectList();
  117. return $votes;
  118. }
  119. /**
  120. * Method to get rating data
  121. *
  122. * @access public
  123. * @return array
  124. */
  125. function getWorstRating()
  126. {
  127. $_df = 100 / $this->_rating_resolution;
  128. $query = 'SELECT (cr.rating_sum / cr.rating_count ) * '.$_df.' AS votes, i.title, i.id'
  129. . ' FROM #__content AS i'
  130. . ' INNER JOIN #__content_rating AS cr ON cr.content_id = i.id'
  131. . ' WHERE i.sectionid = ' . FLEXI_SECTION
  132. . ' ORDER BY votes ASC'
  133. . ' LIMIT 5'
  134. ;
  135. $this->_db->SetQuery($query);
  136. $worstvotes = $this->_db->loadObjectList();
  137. return $worstvotes;
  138. }
  139. /**
  140. * Method to get creators data
  141. *
  142. * @access public
  143. * @return array
  144. */
  145. function getCreators()
  146. {
  147. $query = 'SELECT COUNT(*) AS counter, i.created_by AS id, u.name, u.username'
  148. . ' FROM #__content AS i'
  149. . ' LEFT JOIN #__users AS u ON u.id = i.created_by'
  150. . ' WHERE i.sectionid = ' . FLEXI_SECTION
  151. . ' GROUP BY u.name'
  152. . ' ORDER BY counter DESC'
  153. . ' LIMIT 5'
  154. ;
  155. $this->_db->SetQuery($query);
  156. $usercreate = $this->_db->loadObjectList();
  157. return $usercreate;
  158. }
  159. /**
  160. * Method to get editors data
  161. *
  162. * @access public
  163. * @return array
  164. */
  165. function getEditors()
  166. {
  167. $query = 'SELECT COUNT(*) AS counter, i.modified_by AS id, u.name, u.username'
  168. . ' FROM #__content AS i'
  169. . ' LEFT JOIN #__users AS u ON u.id = i.modified_by'
  170. . ' WHERE i.modified_by > 0'
  171. . ' AND i.sectionid = ' . FLEXI_SECTION
  172. . ' GROUP BY u.name'
  173. . ' ORDER BY counter DESC'
  174. . ' LIMIT 5'
  175. ;
  176. $this->_db->SetQuery($query);
  177. $usereditor = $this->_db->loadObjectList();
  178. return $usereditor;
  179. }
  180. /**
  181. * Method to get favourites data
  182. *
  183. * @access public
  184. * @return array
  185. */
  186. function getFavoured()
  187. {
  188. $query = 'SELECT i.title, i.id, COUNT(f.itemid) AS favnr'
  189. . ' FROM #__content AS i'
  190. . ' INNER JOIN #__flexicontent_favourites AS f ON f.itemid = i.id'
  191. . ' WHERE i.sectionid = ' . FLEXI_SECTION
  192. . ' GROUP BY f.itemid'
  193. . ' ORDER BY favnr DESC'
  194. . ' LIMIT 5'
  195. ;
  196. $this->_db->SetQuery($query);
  197. $favnr = $this->_db->loadObjectList();
  198. return $favnr;
  199. }
  200. /**
  201. * Method to get favourites data
  202. * TODO: Clean up this mess
  203. *
  204. * @access public
  205. * @return array
  206. */
  207. function getStatestats()
  208. {
  209. //get states
  210. $query = 'SELECT state'
  211. . ' FROM #__content AS i'
  212. . ' WHERE sectionid = ' . FLEXI_SECTION
  213. ;
  214. $this->_db->SetQuery($query);
  215. $states = $this->_db->loadObjectList();
  216. $total = count($states);
  217. //initialize vars
  218. $collect = array();
  219. $collect['published'] = 0;
  220. $collect['unpublished'] = 0;
  221. $collect['archived'] = 0;
  222. $collect['pending'] = 0;
  223. $collect['open'] = 0;
  224. $collect['progress'] = 0;
  225. //count each states
  226. foreach ($states AS $state) {
  227. if ($state->state == 1) {
  228. $collect['published']++;
  229. } elseif($state->state == 0) {
  230. $collect['unpublished']++;
  231. } elseif($state->state == -1) {
  232. $collect['archived']++;
  233. } elseif($state->state == -3) {
  234. $collect['pending']++;
  235. } elseif($state->state == -4) {
  236. $collect['open']++;
  237. } elseif($state->state == -5) {
  238. $collect['progress']++;
  239. }
  240. }
  241. //get percentage and label
  242. $val = array();
  243. $lab = array();
  244. $i = 0;
  245. foreach ($collect as $key => $proz) {
  246. if ($proz == 0) {
  247. unset($collect[$key]);
  248. continue;
  249. }
  250. $val[] = round($proz / $total * 100);
  251. if ( $key == 'published' ) {
  252. $lab[] = JText::_( 'FLEXI_PUBLISHED' ).' '.$val[$i].' %';
  253. } else if ( $key == 'unpublished' ) {
  254. $lab[] = JText::_( 'FLEXI_UNPUBLISHED' ).' '.$val[$i].' %';
  255. } else if ( $key == 'archived' ) {
  256. $lab[] = JText::_( 'FLEXI_ARCHIVED' ).' '.$val[$i].' %';
  257. } else if ( $key == 'pending' ) {
  258. $lab[] = JText::_( 'FLEXI_PENDING' ).' '.$val[$i].' %';
  259. } else if ( $key == 'open' ) {
  260. $lab[] = JText::_( 'FLEXI_TO_WRITE' ).' '.$val[$i].' %';
  261. } else if ( $key == 'progress' ) {
  262. $lab[] = JText::_( 'FLEXI_IN_PROGRESS' ).' '.$val[$i].' %';
  263. }
  264. $i++;
  265. }
  266. $collect['values'] = implode( ',', $val );
  267. $collect['labels'] = implode('|', $lab);
  268. return $collect;
  269. }
  270. /**
  271. * Method to get votes data
  272. *
  273. * @access public
  274. * @return array
  275. */
  276. function getVotesstats()
  277. {
  278. // Get all votes
  279. $query = 'SELECT cr.rating_sum, cr.rating_count, c.id'
  280. . ' FROM #__content AS c'
  281. . ' LEFT JOIN #__content_rating AS cr ON cr.content_id = c.id'
  282. . ' WHERE c.sectionid = ' . FLEXI_SECTION
  283. ;
  284. $this->_db->SetQuery($query);
  285. $votes = $this->_db->loadObjectList();
  286. $total = count($votes);
  287. //initialize vars
  288. $collect = array();
  289. $collect['020'] = 0;
  290. $collect['040'] = 0;
  291. $collect['060'] = 0;
  292. $collect['080'] = 0;
  293. $collect['100'] = 0;
  294. $collect['novotes'] = 0;
  295. $collect['negative'] = 0;
  296. //count
  297. foreach ($votes as $vote) {
  298. if(!$vote->rating_sum) {
  299. $collect['novotes']++;
  300. continue;
  301. }
  302. //$percentage = round(($vote->rating_sum / $vote->rating_count) * 20);
  303. $percentage = round((($vote->rating_sum / $vote->rating_count) * (100 / $this->_rating_resolution)), 2);
  304. if ($percentage > 0 && $percentage < 20) {
  305. $collect['020']++;
  306. } elseif($percentage >= 20 && $percentage < 40) {
  307. $collect['040']++;
  308. } elseif($percentage >= 40 && $percentage < 60) {
  309. $collect['060']++;
  310. } elseif($percentage >= 60 && $percentage < 80) {
  311. $collect['080']++;
  312. } elseif($percentage >= 80 && $percentage <= 100) {
  313. $collect['100']++;
  314. }
  315. }
  316. //get votes and label
  317. $val = array();
  318. $lab = array();
  319. $i = 0;
  320. foreach ($collect as $key => $value) {
  321. if ($value == 0) {
  322. unset($collect[$key]);
  323. continue;
  324. }
  325. $val[] = $value;
  326. $proz = round($value / $total * 100);
  327. if ( $key == '020' ) {
  328. $lab[] = JText::_( 'FLEXI_VOTES_BEETWEEN_020' ).' '.$proz.' % ('.$val[$i].')';
  329. } else if ( $key == '040' ) {
  330. $lab[] = JText::_( 'FLEXI_VOTES_BEETWEEN_040' ).' '.$proz.' % ('.$val[$i].')';
  331. } else if ( $key == '060' ) {
  332. $lab[] = JText::_( 'FLEXI_VOTES_BEETWEEN_060' ).' '.$proz.' % ('.$val[$i].')';
  333. } else if ( $key == '080' ) {
  334. $lab[] = JText::_( 'FLEXI_VOTES_BEETWEEN_080' ).' '.$proz.' % ('.$val[$i].')';
  335. } else if ( $key == '100' ) {
  336. $lab[] = JText::_( 'FLEXI_VOTES_BEETWEEN_100' ).' '.$proz.' % ('.$val[$i].')';
  337. } else if ( $key == 'novotes' ) {
  338. $lab[] = JText::_( 'FLEXI_NOVOTES' ).' '.$proz.' % ('.$val[$i].')';
  339. }
  340. $i++;
  341. }
  342. $collect['values'] = implode( ',', $val );
  343. $collect['labels'] = implode( '|', $lab );
  344. return $collect;
  345. }
  346. function getRatingResolution()
  347. {
  348. if ($this->_rating_resolution) return $this->_rating_resolution;
  349. $this->_db->setQuery('SELECT * FROM #__flexicontent_fields WHERE field_type="voting"');
  350. $field = $this->_db->loadObject();
  351. $item = JTable::getInstance( $type = 'flexicontent_items', $prefix = '', $config = array() );
  352. //$item->load( $id );
  353. FlexicontentFields::loadFieldConfig($field, $item);
  354. $rating_resolution = (int)$field->parameters->get('rating_resolution', 5);
  355. $rating_resolution = $rating_resolution >= 5 ? $rating_resolution : 5;
  356. $rating_resolution = $rating_resolution <= 100 ? $rating_resolution : 100;
  357. $this->_rating_resolution = $rating_resolution;
  358. }
  359. }
  360. ?>