Commit be52ab95 authored by Penny Leach's avatar Penny Leach
Browse files

most of view community is now done

parent 30c8cea8
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage core
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('community.php');
$id = param_integer('id');
$pending = param_boolean('pending', 0); // for memberlist
$submitted = param_boolean('submitted', 0); // for viewlist
$type = param_alpha('type');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$count = 0;
$data = array();
if (!$membership = user_can_access_community($id)) {
community_json_empty();
}
$prefix = get_config('prefix');
$dbnow = db_format_timestamp(time());
switch ($type) {
case 'views':
$where = 'WHERE a.community = ?
AND ( v.startdate IS NULL OR v.startdate < ? )
AND ( v.stopdate IS NULL OR v.stopdate < ? )
AND ( a.startdate IS NULL OR a.startdate < ? )
AND ( a.stopdate IS NULL OR a.stopdate < ? )';
$count = count_records_sql('SELECT COUNT(v.id) FROM ' . $prefix . 'view_access_community a
JOIN ' . $prefix . 'view v ON a.view = v.id ' . $where,
array($id, $dbnow, $dbnow, $dbnow, $dbnow));
$data = get_records_sql_array('SELECT v.*,u.firstname,u.lastname, u.preferredname,u.id AS usr
FROM ' . $prefix . 'view_access_community a JOIN ' . $prefix . 'view v ON a.view = v.id
JOIN ' . $prefix.'usr u ON v.owner = u.id ' . $where,
array($id, $dbnow, $dbnow, $dbnow, $dbnow),
$offset, $limit);
if (empty($data)) {
$data = array();
}
foreach ($data as $d) {
$tmp = clone($d);
$tmp->id = $tmp->usr;
$d->ownername = display_name($tmp);
}
break;
case 'members':
$sql = 'SELECT u.*,c.tutor
FROM ' . $prefix . 'usr u JOIN ' . $prefix . 'community_member c
ON c.member = u.id
WHERE c.community = ?';
if (empty($pending)) { // default behaviour - actual members
$count = count_records('community_member', 'community', $id);
$data = get_records_sql_array($sql, array($id), $offset, $limit);
}
else {
if ($membership == COMMUNITY_MEMBERSHIP_MEMBER) {
community_json_empty();
}
$sql = str_replace('community_member', 'community_member_request', $sql);
$count = count_records('community_member_request', 'community', $id);
$data = get_records_sql_array($sql, array($id), $offset, $limit);
}
if (empty($data)) {
$data = array();
}
foreach ($data as $d) {
$d->displayname = display_name($d);
}
break;
case 'control':
foreach ($_REQUEST as $k => $v) {
if (preg_match('/member-(\d+)/', $k, $m)) {
$user = $m[1];
log_debug("user $user v $v");
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();
}
else {
db_commit();
}
}
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);
break;
}
}
catch (SQLException $e) {
json_reply(true, get_string('memberchangefailed'));
}
}
}
json_reply(false, get_string('memberchangesuccess'));
break;
}
if (!$data) {
$data = array();
}
print json_encode(array(
'count' => $count,
'data' => $data,
'limit' => $limit,
'offset' => $offset,
'id' => $id,
'type' => $type,
'pending' => $pending,
'submitted' => $submitted)
);
function community_json_empty() {
global $limit, $offset, $id, $type, $pending, $submitted;
print json_encode(array(
'count' => 0 ,
'data' => array(),
'limit' => $limit,
'offset' => $offset,
'id' => $id,
'type' => $type,
'pending' => $pending,
'submitted' => $submitted)
);
exit;
}
?>
\ No newline at end of file
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage core
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('MENUITEM', 'mycontacts');
define('SUBMENUITEM', 'mycommunities');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('community.php');
$id = param_integer('id');
if (!$community = get_record('community', 'id', $id)) {
throw new CommunityNotFoundException("Couldn't find community with id $id");
}
$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'));
$userview = get_config('wwwroot') . 'user/view.php?id=';
$viewview = get_config('wwwroot') . 'view/view.php?id=';
$releaseviewstr = get_string('releaseview');
$tutorstr = get_string('tutor');
$memberstr = get_string('member');
$removestr = get_string('remove');
$tutor = ($membership < COMMUNITY_MEMBERSHIP_MEMBER);
$controlled = ($community->jointype == 'controlled');
$admin = ($membership == COMMUNITY_MEMBERSHIP_ADMIN);
$canremove = (int)(($tutor && $controlled) || $admin);
$canpromote = (int)$tutor;
$javascript = <<<EOF
viewlist = new TableRenderer(
'viewlist',
'view.json.php',
[
function (r) {
return TD(null, A({'href': '{$viewview}' + r.id}, r.title));
},
function (r) {
return TD(null, A({'href': '{$userview}' + r.owner}, r.ownername));
},
function (r,d) {
if (r.submittedto && {$tutor}) {
return TD(null, A({'href': '', 'onclick': 'releaseView(' + r.id + ');'}, '{$releaseviewstr}'));
}
return TD(null);
}
]
);
viewlist.type = 'views';
viewlist.submitted = 0;
viewlist.id = $id;
viewlist.statevars.push('type');
viewlist.statevars.push('id');
viewlist.statevars.push('submitted');
viewlist.updateOnLoad();
memberlist = new TableRenderer(
'memberlist',
'view.json.php',
[
function (r) {
return TD(null, A({'href': '{$userview}' + r.id}, r.displayname));
},
EOF;
if ($tutor) {
$javascript .= <<<EOF
function (r) {
var options = new Array();
var tutor = OPTION({'value': 'tutor'}, '{$tutorstr}');
var member = OPTION({'value': 'member'}, '{$memberstr}');
if (r.tutor == 1) {
tutor.selected = true;
}
else {
member.selected = true;
}
options.push(tutor);
options.push(member);
EOF;
if (($controlled && $tutor) || $admin) {
$javascript .= <<<EOF
var remove = OPTION({'value': 'nonmember'}, '{$removestr}');
options.push(remove);
EOF;
}
$javascript .= <<<EOF
return TD(null, SELECT({'name': 'member-' + r.id, 'class': 'member'}, options));
}
EOF;
}
$javascript .= <<<EOF
]
);
memberlist.type = 'members';
memberlist.id = $id;
memberlist.pending = 0;
memberlist.statevars.push('type');
memberlist.statevars.push('pending');
memberlist.statevars.push('id');
memberlist.updateOnLoad();
function memberControl(id, type) {
return false;
}
function releaseView(id) {
return false;
}
function updateMembership() {
var pd = {'type': 'control', 'id': '{$community->id}'};
var e = getElementsByTagAndClassName(null, 'member');
for (s in e) {
pd[e[s].name] = e[s].options[e[s].selectedIndex].value;
}
debugObject(pd);
var d = loadJSONDoc('view.json.php', pd);
d.addCallbacks(function (data) {
$('messagediv').innerHTML = data.message;
memberlist.doupdate();
},
function () {
$('messagediv').innerHTML = data.message;
}
)
}
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('canacceptinvite', $invited);
$smarty->assign('community', $community);
$smarty->display('contacts/communities/view.tpl');
?>
\ No newline at end of file
......@@ -352,6 +352,17 @@ $string['addedtocommunitysubject'] = 'You were added to a community';
$string['addedtocommunitymessage'] = '%s has added you to a community, \'%s\'. Click on the link below to see the community';
$string['adduserfailed'] = 'Failed to add the user';
$string['useradded'] = 'User added';
$string['owner'] = 'Owner';
$string['members'] = 'Members';
$string['memberrequests'] = 'Membership requests';
$string['submittedviews'] = 'Submitted views';
$string['releaseview'] = 'Release view';
$string['tutor'] = 'Tutor';
$string['member'] = 'Member';
$string['remove'] = 'Remove';
$string['updatemembership'] = 'Update membership';
$string['memberchangefailed'] = 'Failed to update some membership information';
$string['memberchangesuccess'] = 'Membership status changed successfully';
// friendslist
$string['reasonoptional'] = 'Reason (optional)';
......
......@@ -197,6 +197,66 @@ function get_tutor_communities($userid=0, $jointype=null) {
}
// constants for community membership type
define('COMMUNITY_MEMBERSHIP_ADMIN', 1);
define('COMMUNITY_MEMBERSHIP_STAFF', 2);
define('COMMUNITY_MEMBERSHIP_OWNER', 3);
define('COMMUNITY_MEMBERSHIP_TUTOR', 4);
define('COMMUNITY_MEMBERSHIP_MEMBER', 5);
/**
* Can a user access a given community?
*
* @param mixed $community id of community or db record (object)
* @param mixed $user optional (object or id), defaults to logged in user
*
* @returns constant access level or FALSE
*/
function user_can_access_community($community, $user=null) {
if (empty($userid)) {
global $USER;
$user = $USER;
}
else if (is_int($user)) {
$user = get_user($user);
}
else if (is_object($user) && !$user instanceof User) {
$user = get_user($user->get('id'));
}
if (!$user instanceof User) {
throw new InvalidArgumentException("not useful user arg given to user_can_access_community: $user");
}
if (is_int($community)) {
$community = get_record('community', 'id', $community);
}
if (!is_object($community)) {
throw new InvalidArgumentException("not useful community arg given to user_can_access_community: $community");
}
if ($user->get('admin')) {
return COMMUNITY_MEMBERSHIP_ADMIN;
}
if ($user->get('staff')) {
return COMMUNITY_MEMBERSHIP_STAFF;
}
if ($community->owner == $user->get('id')) {
return COMMUNITY_MEMBERSHIP_OWNER;
}
if (!$membership = get_record('community_member', 'community', $community->id, 'member', $user->get('id'))) {
return false;
}
if ($membership->tutor) {
return COMMUNITY_MEMBERSHIP_TUTOR;
}
return COMMUNITY_MEMBERSHIP_MEMBER;
}
?>
\ No newline at end of file
......@@ -615,6 +615,11 @@ class ViewNotFoundException extends UserException {}
*/
class UserNotFoundException extends UserException {}
/**
* Exception - community not found
*/
class CommunityNotFoundException extends UserException {}
/**
* Exception - anything to do with template parsing
*/
......
{include file="header.tpl"}
<div id="column-right">
{include file="adminmenu.tpl"}
</div>
<div id="column-left">
<div class="content">
<div class="box-cnrs"><span class="cnr-tl"><span class="cnr-tr"><span class="cnr-bl"><span class="cnr-br">
<div class="maincontent">
<h2>{$community->name}</h2>
<p>{str tag='owner'}: {$community->ownername}</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 $member}
<div class="communityviews">
<h5>{str tag='views'}</h5>
{if $tutor}
<form>
<select name="submitted" onChange="viewlist.submitted=this.options[this.selectedIndex].value;viewlist.doupdate();">
<option value="0">{str tag='allviews'}</option>
<option value="1">{str tag='submittedviews'}</option>
</select>
</form>
{/if}
<table id="viewlist">
<thead>
<tr>
<th>{str tag='name'}</th>
<th>{str tag='owner'}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="communitymembers">
<h5>{str tag='members'}</h5>
{if $tutor}
<form>
<select name="pending" onChange="memberlist.pending=this.options[this.selectedIndex].value;memberlist.doupdate();">
<option value="0">{str tag='members'}</option>
<option value="1">{str tag='memberrequests'}</option>
</select>
</form>
{/if}
<table id="memberlist">
<thead>
<tr>
<th>{str tag='name'}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<input type="button" value="{str tag='updatemembership'}" onClick="return updateMembership();" />
<div id="messagediv"></div>
</div>
{/if}
</div>
</span></span></span></span></div>
</div>
</div>
{include file="footer.tpl"}
......@@ -40,9 +40,9 @@ $viewid = param_integer('viewid');
$prefix = get_config('dbprefix');
if (!$data = get_records_sql_array('SELECT va.accesstype AS type, va.startdate, va.stopdate
FROM ' . $prefix . 'view_access va
LEFT JOIN ' . $prefix . 'view v ON (va.view = v.id)
WHERE va.view = ?
FROM ' . $prefix . 'view v
LEFT JOIN ' . $prefix . 'view_access va ON (va.view = v.id)
WHERE v.id = ?
AND v.owner = ?
ORDER BY va.accesstype', array($viewid, $USER->get('id')))) {
$SESSION->add_error_msg(get_string('canteditdontown', 'view'));
......
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