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

my communities and my owned communities done

parent a8efe6f5
<?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');
$leave = param_integer('leave');
if (!$community = get_record('community', 'id', $leave)) {
json_reply(true, get_string('invalidcommunity'));
}
if (!community_user_can_leave($community)) {
json_reply(true, get_string('cannotleavecommunity'));
}
community_remove_user($community->id, $USER->get('id'));
json_reply(false, get_string('leftcommunity'));
?>
<?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');
$owned = param_boolean('owned', 0);
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$prefix = get_config('dbprefix');
$userid = $USER->get('id');
if (empty($owned)) { // just get communities this user is a member of.
$from = 'FROM ' . $prefix . 'community c
WHERE c.owner != ? AND c.id IN
(SELECT cm.community
FROM ' . $prefix . 'community_member cm
WHERE cm.member = ?)';
$count = count_records_sql('SELECT COUNT(*) ' . $from, array($userid, $userid));
$data = get_records_sql_array('SELECT c.id,c.jointype,c.name,c.owner ' . $from,
array($userid, $userid), $offset, $limit);
}
else {
$count = count_records_sql('SELECT COUNT(*) FROM ' . $prefix . 'community c WHERE c.owner = ?',
array($userid));
$datasql = 'SELECT c.id,c.jointype,c.name,c.owner,count(cmr.community) as requestcount
FROM ' . $prefix . 'community c
LEFT JOIN ' . $prefix . 'community_member_request cmr ON cmr.community = c.id
WHERE c.owner = ?
GROUP BY c.id,c.jointype,c.name,c.owner';
$data = get_records_sql_array($datasql,array($userid), $offset, $limit);
}
if (!$data) {
$data = array();
}
print json_encode(array(
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'data' => $data,
));
?>
\ No newline at end of file
......@@ -18,7 +18,7 @@
*
* @package mahara
* @subpackage core
* @author Martyn Smith <martyn@catalyst.net.nz>
* @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
*
......@@ -30,8 +30,51 @@ define('SUBMENUITEM', 'mycommunities');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
$smarty = smarty();
$viewurl = get_config('wwwroot') . 'contacts/communities/view.php?id=';
$leftsuccess = get_string('leftcommunity');
$leftfailed = get_string('leftcommunityfailed');
$javascript = <<<EOF
var communitylist = new TableRenderer(
'communitylist',
'getcommunities.json.php',
[
function (r) {
return TD(null, A({'href': '{$viewurl}' + r.id}, r.name));
},
function (r) {
if (r.jointype == 'controlled') {
return TD(null);
}
return TD(null, A({'href': '', 'onclick': 'leaveCommunity(' + r.id + '); return false;'}, '[X]'));
}
]
);
communitylist.updateOnLoad();
function leaveCommunity(id) {
var pd = {'leave': id}
var d = loadJSONDoc('communityleave.json.php', pd);
d.addCallbacks(function (data) {
if (!data.error) {
$('messagediv').innerHTML = '$leftsuccess';
communitylist.doupdate();
}
if (data.error) {
$('messagediv').innerHTML = '$leftfailed (' + data.error + ')';
}
},
function () {
$('messagediv').innerHTML = '$leftfailed';
watchlist.doupdate();
}
)
}
EOF;
$smarty = smarty(array('tablerenderer'));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->display('contacts/communities/index.tpl');
?>
......@@ -30,7 +30,38 @@ define('SUBMENUITEM', 'myownedcommunities');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
$smarty = smarty();
$viewurl = get_config('wwwroot') . 'contacts/communities/view.php?id=';
$editurl = get_config('wwwroot') . 'contacts/communities/edit.php?id=';
$editstr = get_string('edit');
$javascript = <<<EOF
var communitylist = new TableRenderer(
'communitylist',
'getcommunities.json.php',
[
function (r) {
return TD(null, A({'href': '{$viewurl}' + r.id}, r.name));
},
function (r) {
if (r.requestcount == 0) {
return TD(null);
}
return TD(null, A({'href': '{$editurl}' + r.id + '#pending'}, r.requestcount));
},
function (r) {
return TD(null, A({'href': '{$editurl}' + r.id}, '{$editstr}'));
}
]
);
communitylist.updateOnLoad();
communitylist.owned = 1;
communitylist.statevars.push('owned');
EOF;
$smarty = smarty(array('tablerenderer'));
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->display('contacts/communities/owned.tpl');
......
......@@ -286,4 +286,8 @@ $string['updatewatchlistfailed'] = 'Update of watchlist failed';
$string['view'] = 'view';
$string['View'] = 'View';
// communities
$string['communitymemberrequests'] = 'Pending membership requests';
$string['addcommunity'] = 'Add new community';
?>
<?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
*
*/
defined('INTERNAL') || die();
/**
* is a user allowed to leave a community?
* checks if they're the owner and the membership type
*
* @param object $community (corresponds to db record). if an id is given, record will be fetched.
* @param int $userid (optional, will default to logged in user)
*/
function community_user_can_leave($community, $userid=null) {
if (empty($userid)) {
global $USER;
$userid = $USER->get('id');
}
if (is_numeric($community)) {
if (!$community = get_record('community', 'id', $community)) {
return false;
}
}
if ($community->owner == $userid) {
return false;
}
if ($community->jointype == 'controlled') {
return false;
}
return true;
}
/**
* removes a user from a community
*
* @param int $community id of community
* @param int $user id of user to remove
*/
function community_remove_user($community, $userid) {
delete_records('community_member', 'community', $community, 'member', $userid);
}
?>
\ No newline at end of file
......@@ -9,8 +9,17 @@
<div class="box-cnrs"><span class="cnr-tl"><span class="cnr-tr"><span class="cnr-bl"><span class="cnr-br">
<div class="maincontent">
<h2>{str tag="mycommunities"}</h2>
Content from database here
<table id="communitylist">
<thead>
<tr>
<th>{str tag='name'}</th>
<td></td>
<tr>
</thead>
<tbody>
</tbody>
</table>
<div id="messagediv"></div>
</div>
</span></span></span></span></div>
</div>
......
......@@ -9,8 +9,21 @@
<div class="maincontent">
<h2>{str tag="myownedcommunities"}</h2>
Content from database here
<div class="addcommunitylink">
<a href="{$WWWROOT}/contacts/communities/edit.php?new=1">{str tag='addcommunity'}</a>
</div>
<table id="communitylist">
<thead>
<tr>
<th>{str tag='name'}</th>
<th>{str tag='communitymemberrequests'}</th>
<td></td>
<tr>
</thead>
<tbody>
</tbody>
</table>
<div id="messagediv"></div>
</div>
</span></span></span></span></div>
</div>
......
Supports Markdown
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