Commit 41fcb358 authored by Penny Leach's avatar Penny Leach
Browse files

finished (I think) community/view

parent 56ff83d9
......@@ -43,6 +43,8 @@ $data = array();
if (!$membership = user_can_access_community($id)) {
community_json_empty();
}
$community = get_record('community', 'id', $id);
$prefix = get_config('prefix');
$dbnow = db_format_timestamp(time());
......@@ -90,6 +92,7 @@ switch ($type) {
community_json_empty();
}
$sql = str_replace('community_member', 'community_member_request', $sql);
$sql = str_replace(',c.tutor', ',1 AS request, c.reason', $sql);
$count = count_records('community_member_request', 'community', $id);
$data = get_records_sql_array($sql, array($id), $offset, $limit);
}
......@@ -98,45 +101,58 @@ switch ($type) {
}
foreach ($data as $d) {
$d->displayname = display_name($d);
if (!empty($d->tutor) && $membership == COMMUNITY_MEMBERSHIP_MEMBER) {
$d->displayname .= ' (' . get_string('tutor') . ')';
}
}
break;
case 'membercontrol':
foreach ($_REQUEST as $k => $v) {
if (preg_match('/member-(\d+)/', $k, $m)) {
$user = $m[1];
log_debug("user $user v $v");
$changed = false;
try {
switch ($v) {
case 'nonmember':
delete_records('usr_watchlist_community', 'usr', $user, 'community', $id);
// get all the views in this user's watchlist associated with this community.
$views = get_column_sql('SELECT v.id
FROM ' . $prefix . 'view v JOIN ' . $prefix . 'usr_watchlist_view va on va.view = v.id
JOIN ' . $prefix . 'view_access_community c ON c.view = v.id');
// @todo this is probably a retarded way to do it and should be changed later.
foreach ($views as $view) {
db_begin();
delete_record('usr_watchlist_view', 'view', $view, 'usr', $user);
if (can_view_view($view, $user)) {
db_rollback();
case 'remove':
community_remove_user($id, $user);
$changed = true;
break;
case 'member':
case 'tutor':
if ($cm = get_record('community_member', 'member', $user, 'community', $id)) {
// already a member so just set the flag
if ($v == 'member' && $cm->tutor == 1) {
$changed = true;
set_field('community_member', 'tutor', 0, 'member', $user, 'community', $id);
}
else {
db_commit();
else if ($v == 'tutor' && $cm->tutor == 0) {
$changed = true;
set_field('community_member', 'tutor', 1, 'member', $user, 'community', $id);
}
// else not changed.
}
else {
community_add_member($id, $user);
delete_records('community_member_request', 'member', $user, 'community', $id);
$changed = true;
$v = 'added' . $v; // for the string for notify
}
delete_records('community_member', 'member', $user, 'community', $id);
break;
case 'member':
set_field('community_member', 'tutor', 0, 'member', $user, 'community', $id);
break;
case 'tutor':
set_field('community_member', 'tutor', 1, 'member', $user, 'community', $id);
case 'declinerequest':
delete_records('community_member_request', 'member', $user, 'community', $id);
break;
}
}
catch (SQLException $e) {
json_reply(true, get_string('memberchangefailed'));
}
require_once('activity.php');
activity_occurred('maharamessage',
array('users' => array($user),
'subject' => get_string('communitymembershipchangesubject', 'mahara', $community->name),
'message' => get_string('communitymembershipchangemessage' . $v),
'url' => get_config('wwwroot') . 'contacts/communities/view.php?id=' . $id));
}
}
json_reply(false, get_string('memberchangesuccess'));
......
......@@ -32,6 +32,7 @@ require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('community.php');
$id = param_integer('id');
$joincontrol = param_alpha('joincontrol', null);
if (!$community = get_record('community', 'id', $id)) {
throw new CommunityNotFoundException("Couldn't find community with id $id");
......@@ -40,26 +41,110 @@ $community->ownername = display_name(get_record('usr', 'id', $community->owner))
$membership = user_can_access_community($id);
$invited = get_record('community_member_invite', 'community', $id, 'member', $USER->get('id'));
$requested = get_record('community_member_request', 'community', $id, 'member', $USER->get('id'));
if (!empty($joincontrol)) {
// leave, join, acceptinvite, request
switch ($joincontrol) {
case 'leave':
// make sure they're a member and can leave
if ($membership == COMMUNITY_MEMBERSHIP_MEMBER && $community->jointype != 'controlled') {
community_remove_member($id, $USER->get('id'));
$SESSION->add_ok_msg(get_string('leftcommunity'));
}
else {
$SESSION->add_error_msg(get_string('couldnotleavecommunity'));
}
break;
case 'join':
if (empty($membership) && $community->jointype == 'open') {
community_add_member($id, $USER->get('id'));
$SESSION->add_ok_msg(get_string('joinedcommunity'));
}
else {
$SESSION->add_error_msg(get_string('couldnotjoincommunity'));
}
break;
case 'acceptinvite':
case 'declineinvite':
if (!$request = get_record('community_member_invite', 'member', $USER->get('id'), 'community', $id)) {
$SESSION->add_error_msg(get_string('communitynotinvited'));
break;
}
if ($joincontrol == 'acceptinvite') {
community_add_member($id, $USER->get('id'));
$message = get_string('communityinviteaccepted');
}
else {
$message = get_string('communityinvitedeclined');
}
delete_records('community_member_invite', 'member', $USER->get('id'), 'community', $id);
$SESSION->add_ok_msg($message);
break;
case 'request':
if (empty($membership) && $community->jointype == 'request'
&& !record_exists('community_member_request', 'community', $id, 'member', $USER->get('id'))) {
$cmr = new StdClass;
$cmr->reason = param_variable('reason', null);
$cmr->community = $id;
$cmr->member = $USER->get('id');
$cmr->ctime = db_format_timestamp(time());
$owner = get_record('usr', 'id', $community->owner);
insert_record('community_member_request', $cmr);
if (empty($cmr->reason)) {
$message = get_string('communityrequestmessage', 'mahara',
display_name($USER, $owner), $community->name);
}
else {
$message = get_string('communityrequestmessagereason', 'mahara',
display_name($USER, $owner), $community->name, $cmr->reason);
}
require_once('activity.php');
activity_occurred('maharamessage',
array('users' => array($community->owner),
'subject' => get_string('communityrequestsubject'),
'message' => $message,
'url' => get_config('wwwroot') . 'contacts/communities/view.php?id=' . $id));
$SESSION->add_ok_msg(get_string('communityrequestsent'));
}
else {
$SESSION->add_error_msg(get_string('couldnotrequestcommunity'));
}
break;
}
// redirect, stuff will have changed
redirect(get_config('wwwroot') . 'contacts/communities/view.php?id=' . $id);
exit;
}
$invited = get_record('community_member_invite', 'community', $id, 'member', $USER->get('id'));
$requested = get_record('community_member_request', 'community', $id, 'member', $USER->get('id'));
$userview = get_config('wwwroot') . 'user/view.php?id=';
$viewview = get_config('wwwroot') . 'view/view.php?id=';
$viewview = get_config('wwwroot') . 'view/view.php?view=';
$commview = get_config('wwwroot') . 'contacts/communities/view.php';
$releaseviewstr = get_string('releaseview');
$tutorstr = get_string('tutor');
$memberstr = get_string('member');
$removestr = get_string('remove');
// strings that are used in the js
$releaseviewstr = get_string('releaseview');
$tutorstr = get_string('tutor');
$memberstr = get_string('member');
$removestr = get_string('remove');
$declinestr = get_string('declinerequest');
$updatefailedstr = get_string('updatefailed');
$requeststr = get_string('sendrequest');
$reasonstr = get_string('reason');
$tutor = ($membership < COMMUNITY_MEMBERSHIP_MEMBER);
$controlled = ($community->jointype == 'controlled');
$admin = ($membership == COMMUNITY_MEMBERSHIP_ADMIN);
$canremove = (int)(($tutor && $controlled) || $admin);
$canpromote = (int)$tutor;
// all the permissions stuff
$tutor = (int)($membership < COMMUNITY_MEMBERSHIP_MEMBER);
$controlled = (int)($community->jointype == 'controlled');
$admin = (int)($membership == COMMUNITY_MEMBERSHIP_ADMIN);
$canremove = (int)(($tutor && $controlled) || $admin);
$canpromote = (int)$tutor;
$canleave = ($membership == COMMUNITY_MEMBERSHIP_MEMBER && $community->jointype != 'controlled');
$canrequestjoin = (empty($membership) && empty($invited) && empty($requested) && $community->jointype == 'request');
$canjoin = (empty($membership) && $community->jointype == 'open');
$javascript = <<<EOF
$javascript = '';
if ($membership) {
$javascript .= <<<EOF
viewlist = new TableRenderer(
'viewlist',
'view.json.php',
......@@ -71,7 +156,7 @@ viewlist = new TableRenderer(
return TD(null, A({'href': '{$userview}' + r.owner}, r.ownername));
},
function (r,d) {
if (r.submittedto && {$tutor}) {
if (r.submittedto && {$tutor} == 1) {
return TD(null, A({'href': '', 'onclick': 'return releaseView(' + r.id + ');'}, '{$releaseviewstr}'));
}
return TD(null);
......@@ -97,7 +182,7 @@ memberlist = new TableRenderer(
EOF;
if ($tutor) {
$javascript .= <<<EOF
'reason',
function (r) {
var options = new Array();
var tutor = OPTION({'value': 'tutor'}, '{$tutorstr}');
......@@ -105,16 +190,23 @@ if ($tutor) {
if (r.tutor == 1) {
tutor.selected = true;
}
else {
else if (r.request != 1) {
member.selected = true;
}
options.push(tutor);
options.push(member);
if (r.request) {
var nonmember = OPTION({'value': 'declinerequest'}, '{$declinestr}');
nonmember.selected = true;
options.push(nonmember);
}
EOF;
if (($controlled && $tutor) || $admin) {
$javascript .= <<<EOF
var remove = OPTION({'value': 'nonmember'}, '{$removestr}');
options.push(remove);
if (!r.request) {
var remove = OPTION({'value': 'remove'}, '{$removestr}');
options.push(remove);
}
EOF;
}
$javascript .= <<<EOF
......@@ -126,7 +218,6 @@ EOF;
$javascript .= <<<EOF
]
);
memberlist.type = 'members';
memberlist.id = $id;
memberlist.pending = 0;
......@@ -135,8 +226,18 @@ memberlist.statevars.push('pending');
memberlist.statevars.push('id');
memberlist.updateOnLoad();
function memberControl(id, type) {
return false;
addLoadEvent(function () { hideElement($('pendingreasonheader')); });
function switchPending() {
var pending = $('pendingselect').options[$('pendingselect').selectedIndex].value;
if (pending == 0) {
hideElement($('pendingreasonheader'));
}
else {
showElement($('pendingreasonheader'));
}
memberlist.pending = pending;
memberlist.doupdate();
}
function releaseView(id) {
......@@ -161,6 +262,9 @@ function updateMembership() {
var d = loadJSONDoc('view.json.php', pd);
d.addCallbacks(function (data) {
$('messagediv').innerHTML = data.message;
if (memberlist.pending == 1) {
memberlist.offset = 0;
}
memberlist.doupdate();
},
function () {
......@@ -169,13 +273,30 @@ function updateMembership() {
}
EOF;
}// end of membership only javascript (tablerenderers etc)
$javascript .= <<<EOF
function joinRequestControl() {
var form = P({'id': 'joinrequestextras'},
'{$reasonstr}: ',
FORM({'method': 'post', 'action': '{$commview}'},
INPUT({'type': 'hidden', 'name': 'id', 'value': {$id}}),
INPUT({'type': 'hidden', 'name': 'joincontrol', 'value': 'request'}),
INPUT({'type': 'text', 'name': 'reason'}),
INPUT({'type': 'submit', 'value': '{$requeststr}'})));
insertSiblingNodesAfter('joinrequest', form);
return false;
}
EOF;
$smarty = smarty(array('tablerenderer'));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign('member', $membership);
$smarty->assign('tutor', $tutor);
$smarty->assign('canjoin', empty($membership) && $community->joinmode == 'open');
$smarty->assign('canrequestjoin', empty($membership) && empty($invited) && empty($requested) && $community->joinmode == 'request');
$smarty->assign('canleave', $membership == COMMUNITY_MEMBERSHIP_MEMBER && $community->jointype != 'controlled');
$smarty->assign('canjoin', $canjoin);
$smarty->assign('canrequestjoin', $canrequestjoin);
$smarty->assign('canleave', $canleave);
$smarty->assign('canacceptinvite', $invited);
$smarty->assign('community', $community);
$smarty->display('contacts/communities/view.tpl');
......
......@@ -49,9 +49,12 @@ $string['lastpage'] = 'Last page';
$string['accept'] = 'Accept';
$string['reject'] = 'Reject';
$string['sendrequest'] = 'Send request';
$string['reason'] = 'Reason';
$string['mainmenu'] = 'Main menu';
$string['updatefailed'] = 'Update failed';
$string['declinerequest'] = 'Decline request';
// auth
$string['accessforbiddentoadminsection'] = 'You are forbidden from accessing the administration section';
......@@ -367,6 +370,36 @@ $string['memberchangesuccess'] = 'Membership status changed successfully';
$string['viewreleasedsubject'] = 'Your view has been released';
$string['viewreleasedmessage'] = 'The view that you submitted to community %s has been released back to you by %s';
$string['viewreleasedsuccess'] = 'View was released successfully';
$string['communitymembershipchangedsubject'] = 'Community membership: %s';
$string['communitymembershipchangedmessagetutor'] = 'You have been promoted to a tutor in this community';
$string['communitymembershipchangedmessagemember'] = 'You have been demoted from a tutor in this community';
$string['communitymembershipchangedmessageremove'] = 'You have been removed from this community';
$string['communitymembershipchangedmessagedeclinerequest'] = 'Your request to join this community has been declined';
$string['communitymembershipchangedmessageaddedtutor'] = 'You have been added as a tutor in this community';
$string['communitymembershipchangedmessageaddedmember'] = 'You have been added as a member in this community';
$string['leavecommunity'] = 'Leave this community';
$string['joincommunity'] = 'Join this community';
$string['requestjoincommunity'] = 'Request to join this community';
$string['communityhaveinvite'] = 'You have been invited to join this community';
$string['communitynotinvited'] = 'You have not been invited to join this community';
$string['communityinviteaccepted'] = 'Invite accepted successfully! You are now a community member';
$string['communityinvitedeclined'] = 'Invite declined successfully!';
$string['acceptinvitecommunity'] = 'Accept';
$string['declineinvitecommunity'] = 'Decline';
$string['leftcommunity'] = 'You have now left this community';
$string['couldnotleavecommunity'] = 'You cannot leave this community';
$string['joinedcommunity'] = 'You are now a community member';
$string['couldnotjoincommunity'] = 'You cannot join this community';
$string['communityrequestsent'] = 'Community membership request sent';
$string['couldnotrequestcommunity'] = 'Could not send community membership request';
$string['communityjointypeopen'] = 'Membership to this community is open';
$string['communityjointypecontrolled'] = 'Membership to this community is controlled. You cannot join this community';
$string['communityjointypeinvite'] = 'Membership to this community is invite only';
$string['communityjointyperequest'] = 'Membership to this community is by request only';
$string['communityrequestsubject'] = 'New community membership request';
$string['communityrequestmessage'] = '%s has sent a membership request to join the community %s';
$string['communityrequestmessagereason'] = '%s has sent a membership request to join the community %s with the reason %s.';
// friendslist
$string['reasonoptional'] = 'Reason (optional)';
......
......@@ -259,4 +259,54 @@ function user_can_access_community($community, $user=null) {
return COMMUNITY_MEMBERSHIP_MEMBER;
}
/**
* helper function to remove a user from a community
* also deletes the community from their watchlist,
* and deletes any views they can only access through this community
* from their watchlist.
*
* @param int $communityid
* @param int $userid
*/
function community_remove_member($communityid, $userid) {
$prefix = get_config('dbprefix');
delete_records('usr_watchlist_community', 'usr', $userid, 'community', $communityid);
// get all the views in this user's watchlist associated with this community.
$views = get_column_sql('SELECT v.id
FROM ' . $prefix . 'view v JOIN ' . $prefix . 'usr_watchlist_view va on va.view = v.id
JOIN ' . $prefix . 'view_access_community c ON c.view = v.id');
// @todo this is probably a retarded way to do it and should be changed later.
foreach ($views as $view) {
db_begin();
delete_record('usr_watchlist_view', 'view', $view, 'usr', $userid);
if (can_view_view($view, $userid)) {
db_rollback();
}
else {
db_commit();
}
}
delete_records('community_member', 'member', $userid, 'community', $communityid);
}
/**
* function to add a member to a community
* doesn't do any jointype checking, that should be handled by the caller
*
* @param int $communityid
* @param int $userid
*/
function community_add_member($communityid, $userid) {
$cm = new StdClass;
$cm->member = $userid;
$cm->community = $communityid;
$cm->ctime = db_format_timestamp(time());
$cm->tutor = 0;
insert_record('community_member', $cm);
}
?>
\ No newline at end of file
......@@ -10,11 +10,14 @@
<div class="maincontent">
<h2>{$community->name}</h2>
<p>{str tag='owner'}: {$community->ownername}</p>
{assign var="jointype" value=$community->jointype}
{assign var="joinstr" value=communityjointype$jointype}
<p>{str tag=$joinstr}</p>
{if $community->description} <p>{$community->description}</p> {/if}
{if $canleave} <p><a href="" onClick="return memberControl({$community->id}, 'leave');">{str tag='leavecommunity'}</a></p>
{elseif $canrequestjoin} <p><a href="" onClick="return memberControl({$community->id}, 'request');">{str tag='requestjoincommunity'}</a></p>
{elseif $canjoin} <p><a href="" onClick="return memberControl({$community->id}, 'join');">{str tag='joincommunity'}</a></p>
{elseif $canacceptinvite} <p><a href="" onClick="return memberControl({$community->id}, 'invite');">{str tag='invitedjoincommunity'}</a></p>{/if}
{if $canleave} <p><a href="view.php?id={$community->id}&amp;joincontrol=leave">{str tag='leavecommunity'}</a></p>
{elseif $canrequestjoin} <p id="joinrequest"><a href="" onClick="return joinRequestControl();">{str tag='requestjoincommunity'}</a></p>
{elseif $canjoin} <p><a href="view.php?id={$community->id}&amp;joincontrol=join"">{str tag='joincommunity'}</a></p>
{elseif $canacceptinvite} <p>{str tag='communityhaveinvite'} <a href="view.php?id={$community->id}&amp;joincontrol=acceptinvite">{str tag='acceptinvitecommunity'}</a> | <a href="view.php?id={$community->id}&amp;joincontrol=declineinvite">{str tag='declineinvitecommunity'}</a></p>{/if}
<div id="messagediv"></div>
{if $member}
<div class="communityviews">
......@@ -42,7 +45,7 @@
<h5>{str tag='members'}</h5>
{if $tutor}
<form>
<select name="pending" onChange="memberlist.pending=this.options[this.selectedIndex].value;memberlist.doupdate();">
<select id="pendingselect" name="pending" onChange="switchPending();">
<option value="0">{str tag='members'}</option>
<option value="1">{str tag='memberrequests'}</option>
</select>
......@@ -52,12 +55,15 @@
<thead>
<tr>
<th>{str tag='name'}</th>
<th id="pendingreasonheader">{str tag='reason'}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input type="button" value="{str tag='updatemembership'}" onClick="return updateMembership();" />
{if $tutor}
<input type="button" value="{str tag='updatemembership'}" onClick="return updateMembership();" />
{/if}
</div>
{/if}
</div>
......
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