Commit 555fe0af authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Logged-in users have a chance to join groups when linking to forum pages (#2059)

parent c394e911
......@@ -51,6 +51,7 @@ $group->admins = get_column_sql("SELECT member
$role = group_user_access($group->id);
if (is_logged_in()) {
$afterjoin = param_variable('next', 'view');
if ($role) {
if ($role == 'admin') {
$group->membershiptype = 'admin';
......@@ -67,14 +68,14 @@ if (is_logged_in()) {
else if ($group->jointype == 'invite'
and $invite = get_record('group_member_invite', 'group', $group->id, 'member', $USER->get('id'))) {
$group->membershiptype = 'invite';
$group->invite = group_get_accept_form('invite', $group->id, 'view');
$group->invite = group_get_accept_form('invite', $group->id, $afterjoin);
}
else if ($group->jointype == 'request'
and $request = get_record('group_member_request', 'group', $group->id, 'member', $USER->get('id'))) {
$group->membershiptype = 'request';
}
else if ($group->jointype == 'open') {
$group->groupjoin = group_get_join_form('joingroup', $group->id);
$group->groupjoin = group_get_join_form('joingroup', $group->id, $afterjoin);
}
}
......
......@@ -40,7 +40,7 @@ $group = group_current_group();
$membership = group_user_access($groupid);
if (!$membership && !$group->public) {
throw new AccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
throw new GroupAccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
}
define('TITLE', $group->name . ' - ' . get_string('nameplural', 'interaction.forum'));
......
......@@ -48,18 +48,18 @@ $topic = get_record_sql(
array(0, $USER->get('id'), $USER->get('id'), $topicid)
);
define('GROUP', $topic->groupid);
if (!$topic) {
throw new NotFoundException(get_string('cantfindtopic', 'interaction.forum', $topicid));
}
define('GROUP', $topic->groupid);
$membership = user_can_access_forum((int)$topic->forumid);
$moderator = (bool)($membership & INTERACTION_FORUM_MOD);
if (!$membership
&& !get_field('group', 'public', 'id', $topic->groupid)) {
throw new AccessDeniedException(get_string('cantviewtopic', 'interaction.forum'));
throw new GroupAccessDeniedException(get_string('cantviewtopic', 'interaction.forum'));
}
$topic->canedit = $moderator || user_can_edit_post($topic->poster, $topic->ctime);
......
......@@ -65,7 +65,7 @@ $moderator = (bool)($membership & INTERACTION_FORUM_MOD);
if (!$membership
&& !get_field('group', 'public', 'id', $forum->groupid)) {
throw new AccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
throw new GroupAccessDeniedException(get_string('cantviewforums', 'interaction.forum'));
}
define('TITLE', $forum->groupname . ' - ' . $forum->title);
......
......@@ -151,6 +151,7 @@ $string['group'] = 'group';
$string['Group'] = 'Group';
$string['groups'] = 'groups';
$string['notamember'] = 'You are not a member of this group';
$string['notmembermayjoin'] = 'You must join the group \'%s\' to see this page.';
// friendslist
$string['reasonoptional'] = 'Reason (optional)';
......
......@@ -786,6 +786,35 @@ class AccessDeniedException extends UserException {
}
}
/**
* Exception - Access denied to a group page because the user isn't a member
* Give the user a chance to join then continue
*/
class GroupAccessDeniedException extends AccessDeniedException {
public function render_exception() {
global $USER, $SESSION;
if (defined('GROUP') && $USER->is_logged_in()) {
$roles = $USER->get('grouproles');
log_debug($roles);
if (!isset($roles[GROUP])) {
$group = group_current_group();
if ($group->jointype == 'open'
|| $group->jointype == 'invite' && get_record('group_member_invite', 'group', GROUP, 'member', $USER->get('id'))) {
$SESSION->add_error_msg(get_string('notmembermayjoin', 'group', $group->name));
$next = substr($_SERVER['REQUEST_URI'], strlen(get_mahara_install_subdirectory()) - 1);
redirect(get_config('wwwroot') . 'group/view.php?id=' . GROUP . '&next=' . urlencode($next));
}
if ($group->jointype == 'request' && !get_record('group_member_request', 'group', GROUP, 'member', $USER->get('id'))) {
$SESSION->add_error_msg(get_string('notamember', 'group'));
redirect(get_config('wwwroot') . 'group/requestjoin.php?id=' . GROUP . '&returnto=view');
}
}
}
header("HTTP/1.0 403 Forbidden", true);
return parent::render_exception();
}
}
/**
* Exception - Access totally denied, the user won't be able to access it even if they log in
* as the administrator
......
......@@ -435,7 +435,7 @@ function group_remove_user($groupid, $userid=null) {
/**
* Form for users to join a given group
*/
function group_get_join_form($name, $groupid) {
function group_get_join_form($name, $groupid, $returnto='view') {
return pieform(array(
'name' => $name,
'successcallback' => 'joingroup_submit',
......@@ -448,7 +448,11 @@ function group_get_join_form($name, $groupid) {
'group' => array(
'type' => 'hidden',
'value' => $groupid
)
),
'returnto' => array(
'type' => 'hidden',
'value' => $returnto
),
)
));
}
......@@ -539,7 +543,13 @@ function joingroup_submit(Pieform $form, $values) {
global $SESSION, $USER;
group_add_user($values['group'], $USER->get('id'));
$SESSION->add_ok_msg(get_string('joinedgroup', 'group'));
redirect('/group/view.php?id=' . $values['group']);
if (substr($values['returnto'], 0, 1) == '/') {
$next = $values['returnto'];
}
else {
$next = '/group/view.php?id=' . $values['group'];
}
redirect($next);
}
function group_invite_submit(Pieform $form, $values) {
......@@ -550,7 +560,13 @@ function group_invite_submit(Pieform $form, $values) {
if (isset($values['accept'])) {
group_add_user($values['group'], $USER->get('id'), $inviterecord->role);
$SESSION->add_ok_msg(get_string('groupinviteaccepted', 'group'));
redirect('/group/view.php?id=' . $values['group']);
if (substr($values['returnto'], 0, 1) == '/') {
$next = $values['returnto'];
}
else {
$next = '/group/view.php?id=' . $values['group'];
}
redirect($next);
}
else {
$SESSION->add_ok_msg(get_string('groupinvitedeclined', 'group'));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment