4 * Community invite plugin
5 * Allows people to invite their mutual friends to communities
8 // Set up menu items etc
9 function communityinvite_pagesetup() {
18 $page_owner = $profile_id;
19 $usertype = user_type($page_owner);
20 $username= user_info('username', $page_owner);
22 if ($usertype == "community") {
24 if (defined("context") && context
== "profile") {
26 if (run("permissions:check", "profile")) {
29 $PAGE->menu_sub
[] = array( 'name' => 'community:edit',
30 'html' => a_href("{$CFG->wwwroot}mod/communityinvite/index.php?profile_id=$page_owner" ,
31 __gettext("Invite friends to community")));
39 // Get IDs of community members as a string
40 function communityinvite_notin($userid) {
43 if ($friends = get_records_sql("select ident from {$CFG->friends} where friend = {$userid}")) {
44 foreach($friends as $friend) {
45 $notin .= "," . $friend->ident
;
51 // Get usernames of mutual friends
52 function communityinvite_mutualfriends($userid = -2, $notin = "-1") {
53 global $CFG, $messages;
55 $userid = $_SESSION['userid'];
58 $notin = communityinvite_notin($userid);
61 $mutualfriends = get_records_sql("select * from {$CFG->prefix}users where ident not in ({$notin}) and ident in (select owner as ident from {$CFG->prefix}friends where friend = {$userid} and owner in (select friend as owner from {$CFG->prefix}friends where owner = {$userid}))");
62 return $mutualfriends;
69 function communityinvite_actions() {
70 global $CFG, $messages, $page_owner;
72 $action = optional_param("action");
73 if (isloggedin() && run("permissions:check", "profile") && !empty($action)) {
77 case "community:friends:invite":
78 $people = optional_param("people");
79 if (!empty($people)) {
80 communityinvite_doinvite($page_owner, $people);
82 $messages[] = __gettext("Sorry - we couldn't find anyone to invite.");
84 $_SESSION['messages'] = $messages;
85 header("Location: {$CFG->wwwroot}mod/communityinvite/index.php?profile_id={$page_owner}");
96 function communityinvite_doinvite($community, $people) {
97 global $CFG,$messages;
100 if ($mutualfriends = communityinvite_mutualfriends($_SESSION['userid'])) {
102 $emailarray = array();
103 foreach($mutualfriends as $mutual) {
104 $emailarray[trim(strtolower($mutual->username
))] = $mutual->ident
;
107 $communityrow = get_record_sql("select * from {$CFG->prefix}users where ident = {$community}");
108 $peoplearray = explode(",",$people);
110 $title = sprintf(__gettext("%s has invited you to join a community"),$_SESSION['name']);
111 $message = sprintf(__gettext("%s has invited you to join the following community: %s. To do so, visit the following profile:\n\n\t%s\n\nYou can join the community from this screen."),$_SESSION['name'],$communityrow->name
,$CFG->wwwroot
. $communityrow->username
);
113 if (!empty($peoplearray)) {
114 foreach($peoplearray as $person) {
115 $person = trim(strtolower($person));
116 if (!empty($emailarray[$person])) {
117 notify_user($emailarray[$person], $title, $message);
120 $messages[] = sprintf(__gettext("Your friends were invited to join the %s community."),$communityrow->name
);