Commit 03999ba0 authored by Penny Leach's avatar Penny Leach
Browse files

added more stuff to the community lib and updated view user to use it

parent 9c1b9a85
......@@ -89,15 +89,24 @@ function get_member_communities($userid=0, $offset=0, $limit=0) {
* all communities the user owns
*
* @param int userid (optional, defaults to $USER id)
* @param string $jointype (optional), will filter by jointype.
* @return array of community db rows
*/
function get_owned_communities($userid=0) {
function get_owned_communities($userid=0, $jointype=null) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
return get_records_sql_array('SELECT c.* FROM ' . $prefix . 'community c
WHERE c.owner = ?', array($userid));
$sql = 'SELECT c.* FROM ' . $prefix . 'community c
WHERE c.owner = ?';
$values = array($userid);
if (!empty($jointype)) {
$sql .= ' AND jointype = ?';
$values[] = $jointype;
}
return get_records_sql_array($sql, $values);
}
/**
......@@ -135,5 +144,59 @@ function get_requested_communities($userid=0) {
WHERE cmr.member = ?', array($userid));
}
/**
* all communities this user is associated with at all
* either member, invited or requested.
*
* @param int $userid (optional, defaults to $USER id)
* @return array of community db rows (with type=member|invite|request)
*/
function get_associated_communities($userid=0) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
$sql = "SELECT c.*, a.type FROM " . $prefix . "community c JOIN (
SELECT cm.community, 'invite' AS type
FROM " . $prefix . "community_member_invite cm WHERE cm.member = ?
UNION
SELECT cm.community, 'request' AS type
FROM " . $prefix . "community_member_request cm WHERE cm.member = ?
UNION
SELECT cm.community, 'member' AS type
FROM " . $prefix . "community_member cm WHERE cm.member = ?
) AS a ON a.community = c.id";
return get_records_sql_assoc($sql, array($userid, $userid, $userid));
}
/**
* gets communities the user is a tutor in
*
* @param int $userid (optional, defaults to $USER id)
* @param string $jointype (optional, will filter by jointype
*/
function get_tutor_communities($userid=0, $jointype=null) {
$userid = optional_userid($userid);
$prefix = get_config('dbprefix');
$sql = 'SELECT c.*, cm.ctime
FROM ' . $prefix . 'community c
JOIN ' . $prefix . 'community_member cm ON cm.community = c.id
WHERE c.owner != ? AND cm.member = ? AND cm.tutor = ? ';
$values = array($userid, $userid, 1);
if (!empty($jointype)) {
$sql .= ' AND c.jointype = ? ';
$values[] = $jointype;
}
return get_records_sql_array($sql, $values);
}
?>
\ No newline at end of file
......@@ -26,6 +26,7 @@
define('INTERNAL', 1);
require(dirname(dirname(__FILE__)).'/init.php');
require_once('community.php');
$userid = param_integer('id','');
global $USER;
......@@ -82,15 +83,15 @@ else {
}
$smarty = smarty();
$userassoccommunities = get_associated_communities($userid);
// Get the logged in user's "invite only" communities
// @todo: check if user is already a community member.
if ($communities = @get_records_select_array('community',
'owner = ' . $loggedinid . "AND jointype = 'invite'",
null, 'name', 'id,name')) {
if ($communities = get_owned_communities($loggedinid, 'invite')) {
$invitelist = array();
foreach ($communities as $community) {
if (array_key_exists($community->id, $userassoccommunities)) {
continue;
}
$invitelist[$community->id] = $community->name;
}
require_once('pieforms/pieform.php');
......@@ -116,14 +117,12 @@ if ($communities = @get_records_select_array('community',
$prefix = get_config('dbprefix');
// Get the "controlled membership" communities in which the logged in user is a tutor
// @todo: check if user is already a community member.
if ($communities = @get_records_sql_array('SELECT c.id, c.name
FROM ' . $prefix . 'community c
JOIN ' . $prefix . 'community_member cm ON c.id = cm.community
WHERE cm.member = ' . $loggedinid . " AND cm.tutor = 1 AND c.jointype = 'controlled'",'')) {
if ($communities = get_tutor_communities($loggedinid, 'controlled')) {
$controlledlist = array();
foreach ($communities as $community) {
if (array_key_exists($community->id, $userassoccommunities)) {
continue;
}
$controlledlist[$community->id] = $community->name;
}
require_once('pieforms/pieform.php');
......@@ -163,6 +162,12 @@ function addmember_submit($values) {
$data->tutor = 0;
try {
insert_record('community_member', $data, 'community,member');
activity_occured('maharamessage',
array('users' => array($values['id']),
'subject' => get_string('addedtocommunitysubject'),
'message' => get_string('addedtocommunitymessage'),
'url' => get_config('wwwroot')
. 'contacts/communities/view.php?id=' . $values['community']));
}
catch (SQLException $e) {
json_reply('local', get_string('adduserfailed'));
......
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