GraphTypes.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/data/Graph.php');
  16. /**
  17. * Each subclass within this file extends the parent Graph object and
  18. * correlate to individual graph data on the statistics pages.
  19. *
  20. * GraphTopArtists represents the Top Artists displayed on the user
  21. * statistics page. */
  22. class GraphTopArtists extends Graph {
  23. public $artists, $artists_data;
  24. public $number_of_artists;
  25. public $begin;
  26. /**
  27. * @param string $user The current user to build the information on.
  28. * @param int $num The number of artists to be included in the search.
  29. * @param int $begin Only count scrobbles with a timestamp higher than this.
  30. **/
  31. function __construct($user, $num = 20, $begin = null) {
  32. parent::__construct($user, 'bar_horiz');
  33. $this->number_of_artists = $num;
  34. $this->begin = $begin;
  35. $this->buildGraphData();
  36. }
  37. /**
  38. * Parses the data internally into a format expected by the plotting
  39. * JS libraries.
  40. *
  41. * Arrays are reversed to the expectation of order in the current (jqPlot)
  42. * plotting utility.
  43. **/
  44. private function buildGraphData() {
  45. $tmp = $this->user->getTopArtists($this->number_of_artists, 0, False, $this->begin);
  46. if (!empty($tmp)) {
  47. foreach ($tmp as $root => $node) {
  48. $tmp = '<a href="'.$node['artisturl'].'">';
  49. $tmp .= htmlentities($node['artist'], ENT_QUOTES, 'UTF-8').'</a>';
  50. $artists[] = $tmp;
  51. $artists_data[] = $node['freq'];
  52. }
  53. $this->setMaxX($artists_data[0]);
  54. $artists = array_reverse($artists);
  55. $artists_data = array_reverse($artists_data);
  56. $this->artists = $this->buildJsSingleArray($artists);
  57. $this->data[0][0] = $artists_data;
  58. $this->artists_data = $this->buildJsDataArray(true);
  59. }
  60. }
  61. }
  62. /**
  63. * Represents the Top Tracks data on the user statistic page.
  64. **/
  65. class GraphTopTracks extends Graph {
  66. public $tracks, $tracks_data;
  67. public $number_of_tracks;
  68. public $begin;
  69. /**
  70. * @param string $user The current user to build the information on.
  71. * @param int $num The number of tracks to be included in the search, 20 by default.
  72. * @param int $begin Only count scrobbles with a timestamp higher than this.
  73. **/
  74. function __construct($user, $num = 20, $begin = null) {
  75. parent::__construct($user, 'bar_horiz');
  76. $this->number_of_tracks = $num;
  77. $this->begin = $begin;
  78. $this->buildGraphData();
  79. }
  80. /**
  81. * Parses the data internally into a format expected by the plotting
  82. * JS libraries.
  83. *
  84. * Arrays are reversed to the expectation of order in the current (jqPlot)
  85. * plotting utility.
  86. **/
  87. private function buildGraphData() {
  88. $this->data_buffer = $this->user->getTopTracks($this->number_of_tracks, 0, False, $this->begin);
  89. $tracks = array();
  90. $listings = array();
  91. foreach($this->data_buffer as $key => $entry) {
  92. $tmp_line = '<a href="'.$entry['artisturl'].'">';
  93. $tmp_line .= htmlentities($entry['artist'], ENT_QUOTES, 'UTF-8').'</a>';
  94. $tmp_line .= ' - <a href="'.$entry['trackurl'].'">';
  95. $tmp_line .= htmlentities($entry['track'], ENT_QUOTES, 'UTF-8').'</a>';
  96. $listings[] = $entry['freq'];
  97. $tracks[] = $tmp_line;
  98. }
  99. $this->setMaxX($listings[0]);
  100. $tracks = array_reverse($tracks);
  101. $listings = array_reverse($listings);
  102. $this->tracks = $this->buildJsSingleArray($tracks);
  103. $this->data[0][0] = $listings;
  104. $this->tracks_data = $this->buildJsDataArray(true);
  105. }
  106. }
  107. /**
  108. * Represents the Plays By Days line graph data on the user statistic page.
  109. **/
  110. class GraphPlaysByDays extends Graph {
  111. public $plays_by_days;
  112. public $number_of_days;
  113. /**
  114. * @param $user - the current user to build the information on.
  115. * @param $num - the number of days worth of tracks to be included in
  116. * the search, 20 by default.
  117. **/
  118. function __construct($user, $num = 20) {
  119. parent::__construct($user, 'line');
  120. $this->number_of_days = $num;
  121. $this->buildGraphData();
  122. }
  123. /**
  124. * Parses the data internally into a format expected by the plotting
  125. * JS libraries.
  126. *
  127. * Currently does not delegate the construction of the JS array to parent
  128. * object, however it should do this. Tokenisation required in parent.
  129. *
  130. * @todo: tokenise build JS array functions and refactor accordingly.
  131. **/
  132. private function buildGraphData() {
  133. $this->data_buffer = Statistic::generatePlayByDays('Scrobbles',
  134. $this->number_of_days, $this->user->uniqueid, 300);
  135. $date_line = '[';
  136. $prev_date == null;
  137. if (!empty($this->data_buffer)) {
  138. foreach ($this->data_buffer as $key => $entry) {
  139. $curr_date = DateTime::createFromFormat("Y-m-d", $entry['date']);
  140. if($prev_date == null) {
  141. $prev_date = DateTime::createFromFormat("Y-m-d", $entry['date']);
  142. }
  143. while($prev_date > $curr_date) {
  144. $date_line .= '[\'' . $prev_date->format("Y-m-d") . '\', 0],';
  145. $prev_date->sub(new DateInterval("P1D"));
  146. }
  147. $prev_date->sub(new DateInterval("P1D"));
  148. $date_line .= '[\'' . $entry['date'] . '\', ' . $entry['count'] . '],';
  149. }
  150. $this->plays_by_days = rtrim($date_line, ',');
  151. $this->plays_by_days .= ']';
  152. } else {
  153. $this->plays_by_days = '[]';
  154. }
  155. }
  156. }
  157. class GraphTrackPerformance extends Graph {}