functions.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace AU\ViewsCounter;
  3. /**
  4. * Increment the views counter for any elgg entity
  5. */
  6. function increment_views_counter($entity_guid) {
  7. $entity = get_entity($entity_guid);
  8. if ($entity) {
  9. $user = elgg_get_logged_in_user_entity();
  10. // If there is no loggedin user then lets updated the common views counter for all not loggedin users that has owner equal zero
  11. $user_guid = ($user) ? ($user->guid) : 0;
  12. $options = array(
  13. 'types' => array($entity->type),
  14. 'guids' => array($entity_guid),
  15. 'annotation_names' => array('views_counter'),
  16. 'annotation_owner_guids' => array($user_guid)
  17. );
  18. if ($entity->getSubtype()) {
  19. $options['subtypes'] = array($entity->getSubtype());
  20. }
  21. $views_counter = elgg_get_annotations($options);
  22. // Update the last view time for this entity to the current time
  23. update_last_view_time($entity_guid, $user_guid);
  24. if ($views_counter) {
  25. return update_annotation($views_counter[0]->id, 'views_counter', $views_counter[0]->value + 1, 'integer', $user_guid, ACCESS_PUBLIC);
  26. } else {
  27. return create_annotation($entity->guid, 'views_counter', 1, 'integer', $user_guid, ACCESS_PUBLIC);
  28. }
  29. }
  30. }
  31. /**
  32. * Update the last view time for this entity to the current time
  33. *
  34. * @param $entity_guid
  35. * @param $user_guid
  36. */
  37. function update_last_view_time($entity_guid, $user_guid) {
  38. $entity = get_entity($entity_guid);
  39. if (!$user = get_entity($user_guid)) {
  40. $user = elgg_get_logged_in_user_entity();
  41. }
  42. if ($entity && $user) {
  43. // Get the last view annotation that has the last view time saved
  44. $options = array(
  45. 'types' => array($entity->type),
  46. 'guids' => array($entity_guid),
  47. 'annotation_owner_guids' => array($user->guid),
  48. 'annotation_names' => array('last_view_time')
  49. );
  50. if ($entity->getSubtype()) {
  51. $options['subtypes'] = array($entity->getSubtype());
  52. }
  53. $last_view_time = elgg_get_annotations($options);
  54. // It should exists only one annotation that has the last view time
  55. $last_view_time = $last_view_time[0];
  56. if ($last_view_time) {
  57. // Update the value of last view time with the current time
  58. return update_annotation($last_view_time->id, 'last_view_time', time(), 'integer', $user->guid, 2);
  59. } else {
  60. // Create one annotation with the current time that means the last view time
  61. return create_annotation($entity->guid, 'last_view_time', time(), 'integer', $user->guid, 2);
  62. }
  63. }
  64. }
  65. /**
  66. * Get the number of views for an elgg entity
  67. *
  68. * @param unknown_type $entity_guid
  69. * @param unknown_type $owner_guid
  70. * @return Ambigous <number, boolean>
  71. */
  72. function get_views_counter($entity_guid, $owner_guid = 0) {
  73. $options = array(
  74. 'guids' => array($entity_guid),
  75. 'annotation_names' => array('views_counter'),
  76. 'annotation_calculation' => 'sum'
  77. );
  78. if ($owner_guid) {
  79. $options['annotation_owner_guid'] = $owner_guid;
  80. }
  81. $views_counter = elgg_get_annotations($options);
  82. return $views_counter ? $views_counter : 0;
  83. }
  84. /**
  85. * Try to set the views counter on the views files based on the pattern followed by elgg for displaying entities
  86. */
  87. function set_views_counter() {
  88. $vc_list = elgg_get_plugin_setting('add_views_counter', PLUGIN_ID);
  89. if ($vc_list) {
  90. $add_views_counter = unserialize($vc_list);
  91. }
  92. else {
  93. $add_views_counter = array();
  94. }
  95. if (is_array($add_views_counter) && count($add_views_counter)) {
  96. foreach ($add_views_counter as $subtype) {
  97. // give plugins an option to use custom views if necessary
  98. $params = array('subtype' => $subtype);
  99. $handled = elgg_trigger_plugin_hook('views_counter', 'set_counter', $params, false);
  100. if ($handled) {
  101. continue; // already taken care of
  102. }
  103. switch ($subtype) {
  104. case '':
  105. break;
  106. case 'user':
  107. elgg_extend_view('profile/details', 'views_counter_pageowner', 490);
  108. break;
  109. case 'group':
  110. elgg_extend_view('groups/profile/layout', 'views_counter_pageowner', 490);
  111. break;
  112. case 'au_set':
  113. elgg_extend_view('page/layouts/au_configurable_widgets', 'views_counter_pageowner', 490);
  114. break;
  115. default:
  116. elgg_extend_view('object/' . $subtype, 'views_counter', 490);
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. /**
  123. * Get the valid types for add views counter plugin
  124. */
  125. function get_valid_types_for_views_counter() {
  126. $statistics = get_entity_statistics();
  127. $valid_types = array('user', 'group');
  128. foreach ($statistics['object'] as $subtype => $counter) {
  129. if ($subtype != 'plugin' && $subtype != '__base__') {
  130. $valid_types[] = $subtype;
  131. }
  132. }
  133. return $valid_types;
  134. }
  135. /**
  136. * Try to add the views counter for an entity based on the settings of the views_counter plugin
  137. *
  138. * @param $entity_guid
  139. */
  140. function add_views_counter($entity_guid) {
  141. static $added_entities;
  142. if (!is_array($added_entities)) {
  143. $added_entities = array();
  144. }
  145. if (in_array($entity_guid, $added_entities)) {
  146. return false; // we've already added this view
  147. }
  148. $entity = get_entity($entity_guid);
  149. if (!$entity) {
  150. return false;
  151. }
  152. // save this guid so we know it's been processed in the future
  153. $added_entities[] = $entity_guid;
  154. // Get the added types for add a views counter
  155. $added_types = unserialize(elgg_get_plugin_setting('add_views_counter', PLUGIN_ID));
  156. // Get the types set up by the admin to not allow the views counter
  157. $removed_types = unserialize(elgg_get_plugin_setting('remove_views_counter', PLUGIN_ID));
  158. // Save the subtype
  159. $subtype = $entity->getSubtype();
  160. $subtype = ($subtype) ? ($subtype) : $entity->type;
  161. // If the entity has a added type then increment the views counter
  162. if (in_array($subtype, $added_types)) {
  163. return increment_views_counter($entity->guid);
  164. } else if (!in_array($subtype, $removed_types)) {
  165. // If the views counter is being added for a subtype that was not set up by the admin then let's set the plugin setting now
  166. if (!in_array($subtype, $added_types)) {
  167. $added_types[] = $subtype;
  168. elgg_set_plugin_setting('add_views_counter', serialize($added_types), PLUGIN_ID);
  169. }
  170. return increment_views_counter($entity->guid);
  171. }
  172. return false;
  173. }
  174. /**
  175. * Return an array of entities ordered by the number of views
  176. *
  177. * @param $options
  178. */
  179. function get_entities_by_views_counter($options) {
  180. $dbprefix = elgg_get_config('dbprefix');
  181. // Select the sum of the views counter returned by the JOIN
  182. $select = 'sum(ms.string) as views_counter';
  183. if (is_array($options['selects'])) {
  184. $options['selects'][] = $select;
  185. } else if ($options['selects']) {
  186. $options['selects'] = array($options['selects'], $select);
  187. } else {
  188. $options['selects'] = array($select);
  189. }
  190. // Get the annotations "views_counter" for each entity
  191. $metastring_id = elgg_get_metastring_id('views_counter');
  192. $join = 'LEFT JOIN ' . $dbprefix . 'annotations a ON a.entity_guid = e.guid AND a.name_id = ' . $metastring_id;
  193. if (is_array($options['joins'])) {
  194. $options['joins'][] = $join;
  195. } else if ($options['joins']) {
  196. $options['joins'] = array($options['joins'], $join);
  197. } else {
  198. $options['joins'] = array($join);
  199. }
  200. // JOIN the value of the annotations. The value of each views counter...
  201. $options['joins'][] = 'LEFT JOIN ' . $dbprefix . 'metastrings ms ON a.entity_guid = e.guid AND a.name_id = ' . $metastring_id . ' AND a.value_id = ms.id';
  202. // Check if the user does not want to list by best average any value different of: 'desc'
  203. if ($options['order_by'] != 'asc') {
  204. $options['order_by'] = ' views_counter desc, e.time_created desc';
  205. } else {
  206. $options['order_by'] = ' views_counter asc, e.time_created desc';
  207. }
  208. // Group the result of JOIN annotations by entity because each entity may have infinite annotations "generic_rate"
  209. $options['group_by'] .= ' e.guid ';
  210. // Let the elgg_get_entities() function make do work for us :)
  211. $entities = elgg_get_entities($options);
  212. return $entities;
  213. }
  214. /**
  215. * Get the class in accordance to the plugin settings
  216. *
  217. */
  218. function get_views_counter_class() {
  219. $remove_css = elgg_get_plugin_setting('remove_css_class', PLUGIN_ID);
  220. if (!$remove_css || $remove_css == 'no') {
  221. $class = 'views_counter';
  222. }
  223. $float = elgg_get_plugin_setting('float_direction', 'views_counter');
  224. if ($float == 'none') {
  225. $float = '';
  226. }
  227. $class .= ' ' . $float;
  228. return $class;
  229. }
  230. /**
  231. * Get the last view time of a user for an entity
  232. *
  233. * @param unknown_type $entity_guid
  234. * @param unknown_type $user_guid
  235. * @return boolean
  236. */
  237. function get_last_view_time($entity_guid, $user_guid) {
  238. $entity = get_entity($entity_guid);
  239. if ($entity) {
  240. $user = get_entity($user_guid);
  241. if (!$user) {
  242. $user = elgg_get_logged_in_user_entity();
  243. }
  244. // Try to get the metadata views counter, if there is views counter then We may know which time the user made the last saw for this this entity
  245. $options = array(
  246. 'guids' => array($entity_guid),
  247. 'types' => array($entity->type),
  248. 'annotation_names' => array('last_view_time'),
  249. 'annotation_owner_guids' => array($user->guid)
  250. );
  251. if ($entity->getSubtype()) {
  252. $options['subtypes'] = array($entity->getSubtype());
  253. }
  254. $last_view_time = elgg_get_annotations($options);
  255. $last_view_time = $last_view_time[0];
  256. if ($last_view_time) {
  257. return $last_view_time->value;
  258. }
  259. }
  260. return false;
  261. }
  262. /**
  263. * Check if the user did see the last update for an entity based on
  264. * the last view time annotation and the updated time for the elgg entity
  265. *
  266. * @param $entity_guid
  267. * @param $user_guid
  268. */
  269. function did_see_last_update($entity_guid, $user_guid) {
  270. $entity = get_entity($entity_guid);
  271. if ($entity) {
  272. $user = get_entity($user_guid);
  273. if (!$user) {
  274. $user = elgg_get_logged_in_user_entity();
  275. }
  276. $last_view_time = get_last_view_time($entity->guid, $user->guid);
  277. $last_view_time = ($last_view_time) ? ($last_view_time) : 0;
  278. if ($entity->time_updated > $last_view_time) {
  279. return true;
  280. } else {
  281. return false;
  282. }
  283. }
  284. return null;
  285. }