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

some more helper functions for communities and updated the my communities

stuff to use them
parent cbb0e979
......@@ -26,6 +26,7 @@
define('INTERNAL', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('community.php');
$owned = param_boolean('owned', 0);
$limit = param_integer('limit', 10);
......@@ -35,15 +36,8 @@ $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);
$data = get_member_communities($userid, $offset, $limit);
$count = count_records('community_member', 'member', $userid);
}
else {
......
......@@ -35,10 +35,8 @@ defined('INTERNAL') || die();
* @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');
}
$userid = optional_userid($userid);
if (is_numeric($community)) {
if (!$community = get_record('community', 'id', $community)) {
......@@ -62,12 +60,80 @@ function community_user_can_leave($community, $userid=null) {
* @param int $community id of community
* @param int $user id of user to remove
*/
function community_remove_user($community, $userid) {
function community_remove_user($community, $userid) {
db_begin();
delete_records('community_member', 'community', $community, 'member', $userid);
delete_records('usr_watchlist_community', 'community', $community, 'usr', $userid);
db_commit();
}
/**
* all communities the user is a member of
*
* @param int userid (optional, defaults to $USER id)
* @return array of community db rows
*/
function get_member_communities($userid=0, $offset=0, $limit=0) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
return get_records_sql_array('SELECT c.*, cm.ctime, cm.tutor
FROM ' . $prefix . 'community c
JOIN ' . $prefix . 'community_member cm ON cm.community = c.id
WHERE c.owner != ? AND cm.member = ?', array($userid, $userid), $offset, $limit);
}
/**
* all communities the user owns
*
* @param int userid (optional, defaults to $USER id)
* @return array of community db rows
*/
function get_owned_communities($userid=0) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
return get_records_sql_array('SELECT c.* FROM ' . $prefix . 'community c
WHERE c.owner = ?', array($userid));
}
/**
* all communities the user has pending invites to
*
* @param int userid (optional, defaults to $USER id)
* @return array of community db rows
*/
function get_invited_communities($userid=0) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
return get_records_sql_array('SELECT c.*, cmi.ctime, cmi.reason
FROM ' . $prefix . 'community c
JOIN ' . $prefix . 'community_member_invite cmi ON cmi.community = c.id
WHERE cmi.member = ?)', array($userid));
}
/**
* all communities the user has pending requests for
*
* @param int $userid (optional, defaults to $USER id)
* @return array of community db rows
*/
function get_requested_communities($userid=0) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
return get_records_sql_array('SELECT c.*, cmr.ctime, cmr.reason
FROM ' . $prefix . 'community c
JOIN ' . $prefix . 'community_member_request cmr ON cmr.community = c.id
WHERE cmr.member = ?', array($userid));
}
?>
\ No newline at end of file
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