functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /* functions for au group activity */
  3. // change the group menu to our activity tool
  4. function aga_menu_setup(){
  5. // register new menu item for activities and delete old one
  6. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'aga_owner_block');
  7. }
  8. // generate the tabs
  9. function aga_tabs($handler,$guid,$selected){
  10. // kill standard activity tabs - we will add them back in soon
  11. elgg_register_plugin_hook_handler('register', 'menu:filter', 'aga_kill_activity_tabs',1000);
  12. $group=get_entity($guid);
  13. // we need to create tabs for outgroup and in-group activities
  14. $tab = array(
  15. 'name' => "ingroup",
  16. 'text' => elgg_echo('aga:ingroupactivities'),
  17. 'href' => "$handler/group/{$guid}/ingroup",
  18. 'selected' => $selected=='ingroup'?true:false,
  19. );
  20. elgg_register_menu_item('filter', $tab);
  21. $tab = array(
  22. 'name' => "outgroup",
  23. 'text' => elgg_echo('aga:outgroupactivities'),
  24. 'href' => "$handler/group/{$guid}/outgroup",
  25. 'selected' => $selected=='outgroup'?true:false,
  26. );
  27. elgg_register_menu_item('filter', $tab);
  28. if (($group->canEdit() || $group->aga_members_enable=='yes')&&
  29. (elgg_get_plugin_setting('aga_members','au_group_activity')!='no'|| elgg_is_admin_logged_in())){
  30. $tab = array(
  31. 'name' => "members",
  32. 'text' => elgg_echo('aga:memberactivities'),
  33. 'href' => "$handler/group/{$guid}/members",
  34. 'selected' => $selected=='members'?true:false,
  35. );
  36. elgg_register_menu_item('filter', $tab);
  37. }
  38. if (($group->canEdit() || $group->aga_stats_enable=='yes')&&
  39. (elgg_get_plugin_setting('aga_stats','au_group_activity')!='no'|| elgg_is_admin_logged_in())){
  40. $tab = array(
  41. 'name' => "stats",
  42. 'text' => elgg_echo('aga:groupstats'),
  43. 'href' => "$handler/group/{$guid}/stats",
  44. 'selected' => $selected=='stats'?true:false,
  45. );
  46. elgg_register_menu_item('filter', $tab);
  47. }
  48. }
  49. /**
  50. * main page handler
  51. */
  52. function aga_page_handler($page,$handler) {
  53. if ($page[0] == 'group') {
  54. $guid=$page[1];
  55. $db_prefix = elgg_get_config('dbprefix');
  56. //username
  57. if ($page[3]){
  58. $person=get_user_by_username($page[3]);
  59. if(elgg_instanceof($person,'user')){
  60. $persons=array($person->guid);
  61. $membername = $person->name;
  62. }
  63. }
  64. $group = get_entity($guid);
  65. if (elgg_instanceof($group, 'group')) {
  66. $addtitle="";
  67. switch ($page[2]) {
  68. case 'ingroup' :
  69. aga_tabs($handler,$guid,'ingroup');
  70. $options['joins'] = array("JOIN {$db_prefix}entities e ON e.guid = rv.object_guid");
  71. $options['wheres']= array("e.container_guid = $guid");
  72. $options['selected']='ingroup';
  73. if ($persons){
  74. $options['subject_guids'] = $persons;
  75. $addtitle=" (".elgg_echo('aga:member').": $membername)";
  76. }
  77. $titlepart=elgg_echo('aga:ingroupactivities').$addtitle;
  78. //sort order, used by member stats page
  79. if($page[4]=='asc'){
  80. $options['order_by']='rv.posted ASC';
  81. }
  82. aga_handle_activity_page($group,$page[2],$options,$titlepart);
  83. break;
  84. case 'outgroup':
  85. aga_tabs($handler,$guid,'outgroup');
  86. $id=$group->group_acl;
  87. $members = get_members_of_access_collection($id, TRUE);
  88. if (!$persons){
  89. $options['subject_guids'] = $members;
  90. }else{
  91. $options['subject_guids'] = $persons;
  92. $addtitle=" (".elgg_echo('aga:member').": $membername)";
  93. }
  94. //sort order, used by member stats page
  95. if($page[4]=='asc'){
  96. $options['order_by']='rv.posted ASC';
  97. }
  98. $titlepart=elgg_echo('aga:outgroupactivities').$addtitle;
  99. $options['selected']='outgroup';
  100. aga_handle_activity_page($group,$page[2],$options,$titlepart);
  101. break;
  102. case 'members':
  103. aga_tabs($handler,$guid,'members');
  104. aga_handle_members_page($group,$page[3]);
  105. break;
  106. case 'stats':
  107. aga_tabs($handler,$guid,'stats');
  108. aga_handle_stats_page($group);
  109. break;
  110. default:
  111. return false;
  112. }
  113. return true;
  114. }else{
  115. //this is not an actual group - stop right here
  116. return false;
  117. }
  118. }else{
  119. //it never even claimed to be a group so why are we even here?
  120. return false;
  121. }
  122. }
  123. //generate the activity listing tabs
  124. function aga_handle_activity_page($group,$page,$options,$titlepart){
  125. //checking for filter
  126. $type = preg_replace('[\W]', '', get_input('type', 'all'));
  127. $subtype = preg_replace('[\W]', '', get_input('subtype', ''));
  128. if ($type != 'all') {
  129. $options['type'] = $type;
  130. if ($subtype) {
  131. $options['subtype'] = $subtype;
  132. }
  133. }
  134. if ($subtype) {
  135. $selector = "type=$type&subtype=$subtype";
  136. } else {
  137. $selector = "type=$type";
  138. }
  139. // now start building the page
  140. $content="";
  141. $content .= elgg_view_module('info',elgg_echo('aga:note'),elgg_echo('aga:activitycaution'));
  142. elgg_set_page_owner_guid($group->guid);
  143. $title = $titlepart;
  144. elgg_push_breadcrumb($group->name, $group->getURL());
  145. elgg_push_breadcrumb($title);
  146. // this is where we actually build the list of content
  147. $content.= elgg_view('core/river/filter', array('selector' => $selector));
  148. $options['pagination'] = true;
  149. $results = elgg_list_river($options);
  150. if ($results) {
  151. $content .= $results;
  152. }else{
  153. $content .= '<p>' . elgg_echo('groups:activity:none') .'</p>';
  154. }
  155. $params = array(
  156. 'filter_context' => 'aga',
  157. 'content' => $content,
  158. 'title' => $title,
  159. 'class' => 'elgg-river-layout',
  160. );
  161. $body = elgg_view_layout('content', $params);
  162. echo elgg_view_page($title, $body);
  163. return true;
  164. }
  165. // generate the member stats page
  166. function aga_handle_members_page($group,$member){
  167. //only show if enabled or is admin
  168. if (!(($group->canEdit() || $group->aga_members_enable=='yes')&&
  169. (elgg_get_plugin_setting('aga_members','au_group_activity')!='no'|| elgg_is_admin_logged_in()))){
  170. forward(REFERER);
  171. }
  172. $group=elgg_get_page_owner_entity();
  173. if (elgg_instanceof($group,'group')){
  174. $title = elgg_echo('aga:membertitle', array($group->name));
  175. elgg_push_breadcrumb($group->name, $group->getURL());
  176. elgg_push_breadcrumb(elgg_echo('aga:membertitle'));
  177. $content .= elgg_view_module('info',elgg_echo('aga:note'),elgg_echo('aga:membercaution'));
  178. $db_prefix = elgg_get_config('dbprefix');
  179. //we want to show some stats for each user
  180. elgg_extend_view('user/default','au_group_activity/user_activity_link');
  181. //set up basic options to display members
  182. $options=array(
  183. 'relationship' => 'member',
  184. 'relationship_guid' => $group->guid,
  185. 'inverse_relationship' => true,
  186. 'type' => 'user',
  187. 'limit' => 20,
  188. 'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
  189. 'order_by' => 'u.name ASC',
  190. );
  191. //allow filtering of names
  192. $chosenmembers=get_input('members');
  193. if($chosenmembers){
  194. $options['guids']=$chosenmembers;
  195. }
  196. //form to enter user details
  197. $formbody="";
  198. $formbody.= elgg_view('input/userpicker', array(
  199. 'name' => 'members',
  200. 'internalid' => 'user_picker',
  201. 'value' => $chosenmembers ));
  202. $formbody.= elgg_view('input/hidden', array('name' => 'guid', 'value' => $group->guid));
  203. $formbody.= elgg_view('input/submit', array('value' => elgg_echo('aga:filterbutton')));
  204. $filtercontent.="<div>".elgg_echo('aga:searchmembershelp')."</div>";
  205. $filtercontent.="<div>";
  206. $filtercontent.= elgg_view('input/form', array('body'=>$formbody,'method'=>'POST','action'=>'#'));
  207. $filtercontent.="</div>";
  208. $content.=elgg_view_module('featured',elgg_echo('aga:searchmembers'),$filtercontent);
  209. //list members
  210. $members = elgg_list_entities_from_relationship($options);
  211. $content.=$members;
  212. $params = array(
  213. 'filter_context' => 'aga',
  214. 'content' => $content,
  215. 'title' => $title,
  216. );
  217. $body = elgg_view_layout('content', $params);
  218. echo elgg_view_page($title, $body);
  219. // don't want to persist the stats for each user
  220. elgg_unextend_view('user/default','aga/user_activity_link');
  221. }
  222. }
  223. //generate the stats page
  224. function aga_handle_stats_page($group){
  225. //only show if enabled or is admin
  226. if (!(($group->canEdit() || $group->aga_stats_enable=='yes')&&
  227. (elgg_get_plugin_setting('aga_stats','au_group_activity')!='no'|| elgg_is_admin_logged_in()))){
  228. forward (REFERER);
  229. }
  230. $title=elgg_echo('aga:groupstats',array($group->name));
  231. elgg_push_breadcrumb($group->name, $group->getURL());
  232. elgg_push_breadcrumb(elgg_echo('aga:groupstats'));
  233. $content="";
  234. // Get entity statistics
  235. //first get array of possible objects
  236. $guid=$group->guid;
  237. $types=get_registered_entity_types('object');
  238. $content .= elgg_view_module('info',elgg_echo('aga:note'),elgg_echo('aga:statscaution'));
  239. // generate a table of stats
  240. $even_odd = "";
  241. $statscontent="<table class=\"elgg-table-alt\">";
  242. //member count
  243. $statscontent.="<tr class=\"odd\"><td>".elgg_echo('aga:nummembers')."</td><td>".aga_get_member_count($group)."</td></tr>";
  244. //post count
  245. $statscontent.= "<tr class=\"even\"><td>".elgg_echo('aga:numposts')."</td><td>".aga_get_post_count($group)."</td></tr>";
  246. //count comments and discussion replies
  247. $statscontent.="<tr><td>".elgg_echo('aga:generic_comment')."</td><td>".aga_get_annotation_count($group,'generic_comment')."</td></tr>";
  248. $statscontent.="<tr><td>".elgg_echo('aga:group_topic_post')."</td><td>".aga_get_annotation_count($group,'group_topic_post')."</td></tr>";
  249. //get first and most recent posts
  250. $statscontent.="<tr class=\"odd\"><td>".elgg_echo('aga:first_post')."</td><td>".aga_get_firstlast_post($group,'first')."</td></tr>";
  251. $statscontent.="<tr class=\"even\"><td>".elgg_echo('aga:last_post')."</td><td>".aga_get_firstlast_post($group,'last')."</td></tr>";
  252. // show per-object stats
  253. $statscontent.="<tr><td><h4>".elgg_echo('aga:perobjecttype')."</h4></td><td>".elgg_echo('aga:perobjecttypehelp')."</td></tr>";
  254. foreach($types as $k=>$object){
  255. //This function controls the alternating class
  256. $even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
  257. $count = aga_get_post_count($group,$object);
  258. $objectNice=elgg_echo("item:object:$object");
  259. $statscontent.="<tr class=\"{$even_odd}\">
  260. <td>{$objectNice}</td>
  261. <td><a href=\"{$handler}ingroup?type=object&subtype={$object}\">{$count}</a></td>
  262. </tr> ";
  263. }
  264. /* trying to get things set with group acl but can't make it work yet
  265. $inacl=aga_get_acl_count($group,'in');
  266. $outacl=aga_get_acl_count($group,'out');
  267. $content.="<tr><td>".elgg_echo('aga:accesscount:in')."</td><td>".$inacl."</td></tr>";
  268. $content.="<tr><td>".elgg_echo('aga:accesscount:out')."</td><td>".$outacl."</td></tr>";
  269. */
  270. //end table
  271. $statscontent.="</table>";
  272. $content.=elgg_view_module('aside',elgg_echo('aga:generalstats'),$statscontent);
  273. //show tags
  274. $content .= elgg_view_module('aside',elgg_echo('aga:populartags'),elgg_view_tagcloud(array('container_guid'=>$guid,'limit'=>500,)));
  275. $params = array(
  276. 'filter_context' => 'aga',
  277. 'content' => $content,
  278. 'title' => $title,
  279. );
  280. $body = elgg_view_layout('content', $params);
  281. echo elgg_view_page($title, $body);
  282. }
  283. //get count of
  284. //get number of posts
  285. function aga_get_post_count($group,$subtype,$user){
  286. $options=array(
  287. 'type'=>'object',
  288. 'count' => TRUE,
  289. );
  290. if($group){
  291. $options['container_guid'] = $group->guid;
  292. }
  293. if($subtype){
  294. $options['subtype']=$subtype;
  295. }
  296. if($user){
  297. $options['owner_guids']=array($user->guid);
  298. }
  299. $result=elgg_get_entities($options);
  300. return $result;
  301. }
  302. //get number of posts
  303. function aga_get_annotation_count($group,$annotation_type,$user){
  304. $options=array(
  305. // 'annotation_names'=>array('generic_comment','group_topic_post'),
  306. 'count' => TRUE,
  307. );
  308. if($group){
  309. $options['container_guid'] = $group->guid;
  310. }
  311. if($annotation_type){
  312. $options['annotation_name']=$annotation_type;
  313. }
  314. if($user){
  315. $options['annotation_owner_guids']=array($user->guid);
  316. }
  317. $result=elgg_get_annotations($options);
  318. return $result;
  319. }
  320. //get number of members
  321. function aga_get_member_count($group){
  322. $result=elgg_get_entities_from_relationship(array(
  323. 'relationship' => 'member',
  324. 'relationship_guid' => $group->guid,
  325. 'inverse_relationship' => TRUE,
  326. 'count'=> TRUE,));
  327. return $result;
  328. }
  329. //get first/last created post date
  330. function aga_get_firstlast_post($group=0,$firstlast,$user){
  331. $options=array(
  332. 'type'=>'object',
  333. 'container_guid' => $group->guid,
  334. 'limit' => 1,
  335. );
  336. if ($firstlast=='first'){
  337. $options['order_by']='time_created ASC';
  338. }else{
  339. $options['order_by']='time_created DESC';
  340. }
  341. if ($user){
  342. $options['owner_guid']=$user->guid;
  343. }
  344. if ($post=elgg_get_entities($options)){
  345. $return=gmdate("Y-m-d",$post[0]['time_created']);
  346. }else{
  347. $return="none";
  348. }
  349. return $return;
  350. }
  351. //get number of posts per week
  352. function aga_get_post_count_perweek($group){
  353. }
  354. //get number of posts per ACL permission
  355. function aga_get_acl_count($group){
  356. }
  357. //get number of posts across the site using the group ACL (not working)
  358. function aga_get_group_acl_count($group,$inout){
  359. $options=array(
  360. 'access_id' => $group->group_acl,
  361. 'type' => 'object',
  362. // 'count'=>TRUE,
  363. );
  364. if($inout=='in'){
  365. $options['container_guids']= array($group->guid);
  366. }
  367. $result=elgg_get_entities_from_access_id($options);
  368. return $result;
  369. }
  370. //find an individual's date of joining
  371. function aga_group_join_date($group,$member){
  372. $relationship=elgg_get_entities_from_relationship(array(
  373. 'relationship' => 'member',
  374. 'guids'=> $member->guid,
  375. 'relationship_guid' => $group->guid,
  376. 'inverse_relationship' => TRUE,
  377. 'order_by' => 'r.time_created DESC',
  378. 'limit' => 1));
  379. $result=gmdate("Y-m-d",$relationship[0]['time_created']);
  380. return $result;
  381. }
  382. //get the earliest joining (probably when created)
  383. function aga_group_first_join_date($group){
  384. $result=elgg_get_entities_from_relationship(array(
  385. 'relationship' => 'member',
  386. 'relationship_guid' => $group->guid,
  387. 'inverse_relationship' => TRUE,
  388. 'order_by' => 'r.time_created ASC',
  389. 'limit' => 1));
  390. return $result;
  391. }
  392. //get most recent join date
  393. function aga_group_last_join_date($group,$member){
  394. $result=elgg_get_entities_from_relationship(array(
  395. 'relationship' => 'member',
  396. 'relationship_guid' => $group->guid,
  397. 'inverse_relationship' => TRUE,
  398. 'order_by' => 'r.time_created DESC',
  399. 'limit' => 1));
  400. return $result;
  401. }