aboutsummaryrefslogtreecommitdiff
path: root/mod/groups/lib
diff options
context:
space:
mode:
Diffstat (limited to 'mod/groups/lib')
-rw-r--r--mod/groups/lib/discussion.php235
-rw-r--r--mod/groups/lib/groups.php590
2 files changed, 825 insertions, 0 deletions
diff --git a/mod/groups/lib/discussion.php b/mod/groups/lib/discussion.php
new file mode 100644
index 000000000..874e21b2d
--- /dev/null
+++ b/mod/groups/lib/discussion.php
@@ -0,0 +1,235 @@
+<?php
+/**
+ * Discussion function library
+ */
+
+/**
+ * List all discussion topics
+ */
+function discussion_handle_all_page() {
+
+ elgg_pop_breadcrumb();
+ elgg_push_breadcrumb(elgg_echo('discussion'));
+
+ $content = elgg_list_entities(array(
+ 'type' => 'object',
+ 'subtype' => 'groupforumtopic',
+ 'order_by' => 'e.last_action desc',
+ 'limit' => 20,
+ 'full_view' => false,
+ ));
+
+ $params = array(
+ 'content' => $content,
+ 'title' => elgg_echo('discussion:latest'),
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * List discussion topics in a group
+ *
+ * @param int $guid Group entity GUID
+ */
+function discussion_handle_list_page($guid) {
+
+ elgg_set_page_owner_guid($guid);
+
+ $group = get_entity($guid);
+ if (!elgg_instanceof($group, 'group')) {
+ forward('', '404');
+ }
+ elgg_push_breadcrumb($group->name);
+
+ elgg_register_title_button();
+
+ group_gatekeeper();
+
+ $title = elgg_echo('item:object:groupforumtopic');
+
+ $options = array(
+ 'type' => 'object',
+ 'subtype' => 'groupforumtopic',
+ 'limit' => 20,
+ 'order_by' => 'e.last_action desc',
+ 'container_guid' => $guid,
+ 'full_view' => false,
+ );
+ $content = elgg_list_entities($options);
+ if (!$content) {
+ $content = elgg_echo('discussion:none');
+ }
+
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Edit or add a discussion topic
+ *
+ * @param string $type 'add' or 'edit'
+ * @param int $guid GUID of group or topic
+ */
+function discussion_handle_edit_page($type, $guid) {
+ gatekeeper();
+
+ if ($type == 'add') {
+ $group = get_entity($guid);
+ if (!$group) {
+ register_error(elgg_echo('group:notfound'));
+ forward();
+ }
+
+ // make sure user has permissions to add a topic to container
+ if (!$group->canWriteToContainer(0, 'object', 'groupforumtopic')) {
+ register_error(elgg_echo('groups:permissions:error'));
+ forward($group->getURL());
+ }
+
+ $title = elgg_echo('groups:addtopic');
+
+ elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
+ elgg_push_breadcrumb($title);
+
+ $body_vars = discussion_prepare_form_vars();
+ $content = elgg_view_form('discussion/save', array(), $body_vars);
+ } else {
+ $topic = get_entity($guid);
+ if (!$topic || !$topic->canEdit()) {
+ register_error(elgg_echo('discussion:topic:notfound'));
+ forward();
+ }
+ $group = $topic->getContainerEntity();
+ if (!$group) {
+ register_error(elgg_echo('group:notfound'));
+ forward();
+ }
+
+ $title = elgg_echo('groups:edittopic');
+
+ elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
+ elgg_push_breadcrumb($topic->title, $topic->getURL());
+ elgg_push_breadcrumb($title);
+
+ $body_vars = discussion_prepare_form_vars($topic);
+ $content = elgg_view_form('discussion/save', array(), $body_vars);
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * View a discussion topic
+ *
+ * @param int $guid GUID of topic
+ */
+function discussion_handle_view_page($guid) {
+ // We now have RSS on topics
+ global $autofeed;
+ $autofeed = true;
+
+ $topic = get_entity($guid);
+ if (!$topic) {
+ register_error(elgg_echo('noaccess'));
+ $_SESSION['last_forward_from'] = current_page_url();
+ forward('');
+ }
+
+ $group = $topic->getContainerEntity();
+ if (!$group) {
+ register_error(elgg_echo('group:notfound'));
+ forward();
+ }
+
+ elgg_set_page_owner_guid($group->getGUID());
+
+ group_gatekeeper();
+
+ elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
+ elgg_push_breadcrumb($topic->title);
+
+ $content = elgg_view_entity($topic, array('full_view' => true));
+ if ($topic->status == 'closed') {
+ $content .= elgg_view('discussion/replies', array(
+ 'entity' => $topic,
+ 'show_add_form' => false,
+ ));
+ $content .= elgg_view('discussion/closed');
+ } elseif ($group->canWriteToContainer(0, 'object', 'groupforumtopic') || elgg_is_admin_logged_in()) {
+ $content .= elgg_view('discussion/replies', array(
+ 'entity' => $topic,
+ 'show_add_form' => true,
+ ));
+ } else {
+ $content .= elgg_view('discussion/replies', array(
+ 'entity' => $topic,
+ 'show_add_form' => false,
+ ));
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $topic->title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($topic->title, $body);
+}
+
+/**
+ * Prepare discussion topic form variables
+ *
+ * @param ElggObject $topic Topic object if editing
+ * @return array
+ */
+function discussion_prepare_form_vars($topic = NULL) {
+ // input names => defaults
+ $values = array(
+ 'title' => '',
+ 'description' => '',
+ 'status' => '',
+ 'access_id' => ACCESS_DEFAULT,
+ 'tags' => '',
+ 'container_guid' => elgg_get_page_owner_guid(),
+ 'guid' => null,
+ 'entity' => $topic,
+ );
+
+ if ($topic) {
+ foreach (array_keys($values) as $field) {
+ if (isset($topic->$field)) {
+ $values[$field] = $topic->$field;
+ }
+ }
+ }
+
+ if (elgg_is_sticky_form('topic')) {
+ $sticky_values = elgg_get_sticky_values('topic');
+ foreach ($sticky_values as $key => $value) {
+ $values[$key] = $value;
+ }
+ }
+
+ elgg_clear_sticky_form('topic');
+
+ return $values;
+}
diff --git a/mod/groups/lib/groups.php b/mod/groups/lib/groups.php
new file mode 100644
index 000000000..d5bec1862
--- /dev/null
+++ b/mod/groups/lib/groups.php
@@ -0,0 +1,590 @@
+<?php
+/**
+ * Groups function library
+ */
+
+/**
+ * List all groups
+ */
+function groups_handle_all_page() {
+
+ // all groups doesn't get link to self
+ elgg_pop_breadcrumb();
+ elgg_push_breadcrumb(elgg_echo('groups'));
+
+ if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
+ elgg_register_title_button();
+ }
+
+ $selected_tab = get_input('filter', 'newest');
+
+ switch ($selected_tab) {
+ case 'popular':
+ $content = elgg_list_entities_from_relationship_count(array(
+ 'type' => 'group',
+ 'relationship' => 'member',
+ 'inverse_relationship' => false,
+ 'full_view' => false,
+ ));
+ if (!$content) {
+ $content = elgg_echo('groups:none');
+ }
+ break;
+ case 'discussion':
+ $content = elgg_list_entities(array(
+ 'type' => 'object',
+ 'subtype' => 'groupforumtopic',
+ 'order_by' => 'e.last_action desc',
+ 'limit' => 40,
+ 'full_view' => false,
+ ));
+ if (!$content) {
+ $content = elgg_echo('discussion:none');
+ }
+ break;
+ case 'newest':
+ default:
+ $content = elgg_list_entities(array(
+ 'type' => 'group',
+ 'full_view' => false,
+ ));
+ if (!$content) {
+ $content = elgg_echo('groups:none');
+ }
+ break;
+ }
+
+ $filter = elgg_view('groups/group_sort_menu', array('selected' => $selected_tab));
+
+ $sidebar = elgg_view('groups/sidebar/find');
+ $sidebar .= elgg_view('groups/sidebar/featured');
+
+ $params = array(
+ 'content' => $content,
+ 'sidebar' => $sidebar,
+ 'filter' => $filter,
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page(elgg_echo('groups:all'), $body);
+}
+
+function groups_search_page() {
+ elgg_push_breadcrumb(elgg_echo('search'));
+
+ $tag = get_input("tag");
+ $display_query = _elgg_get_display_query($tag);
+ $title = elgg_echo('groups:search:title', array($display_query));
+
+ // groups plugin saves tags as "interests" - see groups_fields_setup() in start.php
+ $params = array(
+ 'metadata_name' => 'interests',
+ 'metadata_value' => $tag,
+ 'type' => 'group',
+ 'full_view' => FALSE,
+ );
+ $content = elgg_list_entities_from_metadata($params);
+ if (!$content) {
+ $content = elgg_echo('groups:search:none');
+ }
+
+ $sidebar = elgg_view('groups/sidebar/find');
+ $sidebar .= elgg_view('groups/sidebar/featured');
+
+ $params = array(
+ 'content' => $content,
+ 'sidebar' => $sidebar,
+ 'filter' => false,
+ 'title' => $title,
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * List owned groups
+ */
+function groups_handle_owned_page() {
+
+ $page_owner = elgg_get_page_owner_entity();
+
+ if ($page_owner->guid == elgg_get_logged_in_user_guid()) {
+ $title = elgg_echo('groups:owned');
+ } else {
+ $title = elgg_echo('groups:owned:user', array($page_owner->name));
+ }
+ elgg_push_breadcrumb($title);
+
+ if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
+ elgg_register_title_button();
+ }
+
+ $content = elgg_list_entities(array(
+ 'type' => 'group',
+ 'owner_guid' => elgg_get_page_owner_guid(),
+ 'full_view' => false,
+ ));
+ if (!$content) {
+ $content = elgg_echo('groups:none');
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * List groups the user is memober of
+ */
+function groups_handle_mine_page() {
+
+ $page_owner = elgg_get_page_owner_entity();
+
+ if ($page_owner->guid == elgg_get_logged_in_user_guid()) {
+ $title = elgg_echo('groups:yours');
+ } else {
+ $title = elgg_echo('groups:user', array($page_owner->name));
+ }
+ elgg_push_breadcrumb($title);
+
+ if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
+ elgg_register_title_button();
+ }
+
+ $content = elgg_list_entities_from_relationship(array(
+ 'type' => 'group',
+ 'relationship' => 'member',
+ 'relationship_guid' => elgg_get_page_owner_guid(),
+ 'inverse_relationship' => false,
+ 'full_view' => false,
+ ));
+ if (!$content) {
+ $content = elgg_echo('groups:none');
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Create or edit a group
+ *
+ * @param string $page
+ * @param int $guid
+ */
+function groups_handle_edit_page($page, $guid = 0) {
+ gatekeeper();
+
+ if ($page == 'add') {
+ elgg_set_page_owner_guid(elgg_get_logged_in_user_guid());
+ $title = elgg_echo('groups:add');
+ elgg_push_breadcrumb($title);
+ if (elgg_get_plugin_setting('limited_groups', 'groups') != 'yes' || elgg_is_admin_logged_in()) {
+ $content = elgg_view('groups/edit');
+ } else {
+ $content = elgg_echo('groups:cantcreate');
+ }
+ } else {
+ $title = elgg_echo("groups:edit");
+ $group = get_entity($guid);
+
+ if ($group && $group->canEdit()) {
+ elgg_set_page_owner_guid($group->getGUID());
+ elgg_push_breadcrumb($group->name, $group->getURL());
+ elgg_push_breadcrumb($title);
+ $content = elgg_view("groups/edit", array('entity' => $group));
+ } else {
+ $content = elgg_echo('groups:noaccess');
+ }
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Group invitations for a user
+ */
+function groups_handle_invitations_page() {
+ gatekeeper();
+
+ $user = elgg_get_page_owner_entity();
+
+ $title = elgg_echo('groups:invitations');
+ elgg_push_breadcrumb($title);
+
+ // @todo temporary workaround for exts #287.
+ $invitations = groups_get_invited_groups(elgg_get_logged_in_user_guid());
+ $content = elgg_view('groups/invitationrequests', array('invitations' => $invitations));
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Group profile page
+ *
+ * @param int $guid Group entity GUID
+ */
+function groups_handle_profile_page($guid) {
+ elgg_set_page_owner_guid($guid);
+
+ // turn this into a core function
+ global $autofeed;
+ $autofeed = true;
+
+ elgg_push_context('group_profile');
+
+ $group = get_entity($guid);
+ if (!elgg_instanceof($group, 'group')) {
+ forward('', '404');
+ }
+
+ elgg_push_breadcrumb($group->name);
+
+ groups_register_profile_buttons($group);
+
+ $content = elgg_view('groups/profile/layout', array('entity' => $group));
+ $sidebar = '';
+
+ if (group_gatekeeper(false)) {
+ if (elgg_is_active_plugin('search')) {
+ $sidebar .= elgg_view('groups/sidebar/search', array('entity' => $group));
+ }
+ $sidebar .= elgg_view('groups/sidebar/members', array('entity' => $group));
+
+ $subscribed = false;
+ if (elgg_is_active_plugin('notifications')) {
+ global $NOTIFICATION_HANDLERS;
+
+ foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
+ $relationship = check_entity_relationship(elgg_get_logged_in_user_guid(),
+ 'notify' . $method, $guid);
+
+ if ($relationship) {
+ $subscribed = true;
+ break;
+ }
+ }
+ }
+
+ $sidebar .= elgg_view('groups/sidebar/my_status', array(
+ 'entity' => $group,
+ 'subscribed' => $subscribed
+ ));
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'sidebar' => $sidebar,
+ 'title' => $group->name,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($group->name, $body);
+}
+
+/**
+ * Group activity page
+ *
+ * @param int $guid Group entity GUID
+ */
+function groups_handle_activity_page($guid) {
+
+ elgg_set_page_owner_guid($guid);
+
+ $group = get_entity($guid);
+ if (!$group || !elgg_instanceof($group, 'group')) {
+ forward();
+ }
+
+ group_gatekeeper();
+
+ $title = elgg_echo('groups:activity');
+
+ elgg_push_breadcrumb($group->name, $group->getURL());
+ elgg_push_breadcrumb($title);
+
+ $db_prefix = elgg_get_config('dbprefix');
+
+ $content = elgg_list_river(array(
+ 'joins' => array("JOIN {$db_prefix}entities e ON e.guid = rv.object_guid"),
+ 'wheres' => array("e.container_guid = $guid")
+ ));
+ if (!$content) {
+ $content = '<p>' . elgg_echo('groups:activity:none') . '</p>';
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Group members page
+ *
+ * @param int $guid Group entity GUID
+ */
+function groups_handle_members_page($guid) {
+
+ elgg_set_page_owner_guid($guid);
+
+ $group = get_entity($guid);
+ if (!$group || !elgg_instanceof($group, 'group')) {
+ forward();
+ }
+
+ group_gatekeeper();
+
+ $title = elgg_echo('groups:members:title', array($group->name));
+
+ elgg_push_breadcrumb($group->name, $group->getURL());
+ elgg_push_breadcrumb(elgg_echo('groups:members'));
+
+ $db_prefix = elgg_get_config('dbprefix');
+ $content = elgg_list_entities_from_relationship(array(
+ 'relationship' => 'member',
+ 'relationship_guid' => $group->guid,
+ 'inverse_relationship' => true,
+ 'type' => 'user',
+ 'limit' => 20,
+ 'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
+ 'order_by' => 'u.name ASC',
+ ));
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Invite users to a group
+ *
+ * @param int $guid Group entity GUID
+ */
+function groups_handle_invite_page($guid) {
+ gatekeeper();
+
+ elgg_set_page_owner_guid($guid);
+
+ $group = get_entity($guid);
+
+ $title = elgg_echo('groups:invite:title');
+
+ elgg_push_breadcrumb($group->name, $group->getURL());
+ elgg_push_breadcrumb(elgg_echo('groups:invite'));
+
+ if ($group && $group->canEdit()) {
+ $content = elgg_view_form('groups/invite', array(
+ 'id' => 'invite_to_group',
+ 'class' => 'elgg-form-alt mtm',
+ ), array(
+ 'entity' => $group,
+ ));
+ } else {
+ $content .= elgg_echo('groups:noaccess');
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Manage requests to join a group
+ *
+ * @param int $guid Group entity GUID
+ */
+function groups_handle_requests_page($guid) {
+
+ gatekeeper();
+
+ elgg_set_page_owner_guid($guid);
+
+ $group = get_entity($guid);
+
+ $title = elgg_echo('groups:membershiprequests');
+
+ if ($group && $group->canEdit()) {
+ elgg_push_breadcrumb($group->name, $group->getURL());
+ elgg_push_breadcrumb($title);
+
+ $requests = elgg_get_entities_from_relationship(array(
+ 'type' => 'user',
+ 'relationship' => 'membership_request',
+ 'relationship_guid' => $guid,
+ 'inverse_relationship' => true,
+ 'limit' => 0,
+ ));
+ $content = elgg_view('groups/membershiprequests', array(
+ 'requests' => $requests,
+ 'entity' => $group,
+ ));
+
+ } else {
+ $content = elgg_echo("groups:noaccess");
+ }
+
+ $params = array(
+ 'content' => $content,
+ 'title' => $title,
+ 'filter' => '',
+ );
+ $body = elgg_view_layout('content', $params);
+
+ echo elgg_view_page($title, $body);
+}
+
+/**
+ * Registers the buttons for title area of the group profile page
+ *
+ * @param ElggGroup $group
+ */
+function groups_register_profile_buttons($group) {
+
+ $actions = array();
+
+ // group owners
+ if ($group->canEdit()) {
+ // edit and invite
+ $url = elgg_get_site_url() . "groups/edit/{$group->getGUID()}";
+ $actions[$url] = 'groups:edit';
+ $url = elgg_get_site_url() . "groups/invite/{$group->getGUID()}";
+ $actions[$url] = 'groups:invite';
+ }
+
+ // group members
+ if ($group->isMember(elgg_get_logged_in_user_entity())) {
+ if ($group->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
+ // leave
+ $url = elgg_get_site_url() . "action/groups/leave?group_guid={$group->getGUID()}";
+ $url = elgg_add_action_tokens_to_url($url);
+ $actions[$url] = 'groups:leave';
+ }
+ } elseif (elgg_is_logged_in()) {
+ // join - admins can always join.
+ $url = elgg_get_site_url() . "action/groups/join?group_guid={$group->getGUID()}";
+ $url = elgg_add_action_tokens_to_url($url);
+ if ($group->isPublicMembership() || $group->canEdit()) {
+ $actions[$url] = 'groups:join';
+ } else {
+ // request membership
+ $actions[$url] = 'groups:joinrequest';
+ }
+ }
+
+ if ($actions) {
+ foreach ($actions as $url => $text) {
+ elgg_register_menu_item('title', array(
+ 'name' => $text,
+ 'href' => $url,
+ 'text' => elgg_echo($text),
+ 'link_class' => 'elgg-button elgg-button-action',
+ ));
+ }
+ }
+}
+
+/**
+ * Prepares variables for the group edit form view.
+ *
+ * @param mixed $group ElggGroup or null. If a group, uses values from the group.
+ * @return array
+ */
+function groups_prepare_form_vars($group = null) {
+ $values = array(
+ 'name' => '',
+ 'membership' => ACCESS_PUBLIC,
+ 'vis' => ACCESS_PUBLIC,
+ 'guid' => null,
+ 'entity' => null
+ );
+
+ // handle customizable profile fields
+ $fields = elgg_get_config('group');
+
+ if ($fields) {
+ foreach ($fields as $name => $type) {
+ $values[$name] = '';
+ }
+ }
+
+ // handle tool options
+ $tools = elgg_get_config('group_tool_options');
+ if ($tools) {
+ foreach ($tools as $group_option) {
+ $option_name = $group_option->name . "_enable";
+ $values[$option_name] = $group_option->default_on ? 'yes' : 'no';
+ }
+ }
+
+ // get current group settings
+ if ($group) {
+ foreach (array_keys($values) as $field) {
+ if (isset($group->$field)) {
+ $values[$field] = $group->$field;
+ }
+ }
+
+ if ($group->access_id != ACCESS_PUBLIC && $group->access_id != ACCESS_LOGGED_IN) {
+ // group only access - this is done to handle access not created when group is created
+ $values['vis'] = ACCESS_PRIVATE;
+ } else {
+ $values['vis'] = $group->access_id;
+ }
+
+ $values['entity'] = $group;
+ }
+
+ // get any sticky form settings
+ if (elgg_is_sticky_form('groups')) {
+ $sticky_values = elgg_get_sticky_values('groups');
+ foreach ($sticky_values as $key => $value) {
+ $values[$key] = $value;
+ }
+ }
+
+ elgg_clear_sticky_form('groups');
+
+ return $values;
+}