MDL-10234
[moodle-linuxchix.git] / mod / forum / subscribers.php
blob0109fd3056eada4ae1f83a642f6276fb82e56717
1 <?php // $Id$
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id',PARAM_INT); // forum
7 $group = optional_param('group',0,PARAM_INT); // change of group
8 $edit = optional_param('edit',-1,PARAM_BOOL); // Turn editing on and off
10 if (! $forum = get_record("forum", "id", $id)) {
11 error("Forum ID is incorrect");
14 if (! $course = get_record("course", "id", $forum->course)) {
15 error("Could not find this course!");
18 if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
19 $cm->id = 0;
22 require_login($course->id, false, $cm);
24 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
26 if (!has_capability('mod/forum:viewsubscribers', $context)) {
27 error('You do not have the permission to view forum subscribers');
30 unset($SESSION->fromdiscussion);
32 add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id);
34 $strsubscribeall = get_string("subscribeall", "forum");
35 $strsubscribenone = get_string("subscribenone", "forum");
36 $strsubscribers = get_string("subscribers", "forum");
37 $strforums = get_string("forums", "forum");
39 $crumbs[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
40 $crumbs[] = array('name' => format_string($forum->name), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
41 $crumbs[] = array('name' => $strsubscribers, 'link' => '', 'type' => 'title');
43 $navigation = build_navigation($crumbs);
45 if (has_capability('mod/forum:managesubscriptions', $context)) {
46 print_header_simple("$strsubscribers", "", $navigation,
47 "", "", true, forum_update_subscriptions_button($course->id, $id));
48 if ($edit != -1) {
49 $USER->subscriptionsediting = $edit;
51 } else {
52 print_header_simple("$strsubscribers", "", $navigation, "", "", true, '');
53 unset($USER->subscriptionsediting);
56 /// Check to see if groups are being used in this forum
57 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
58 $currentgroup = setup_and_print_groups($course, $groupmode, "subscribers.php?id=$forum->id");
59 } else {
60 $currentgroup = false;
63 if (empty($USER->subscriptionsediting)) { /// Display an overview of subscribers
65 if (! $users = forum_subscribed_users($course, $forum, $currentgroup) ) {
67 print_heading(get_string("nosubscribers", "forum"));
69 } else {
71 print_heading(get_string("subscribersto","forum", "'".format_string($forum->name)."'"));
73 echo '<table align="center" cellpadding="5" cellspacing="5">';
74 foreach ($users as $user) {
75 echo '<tr><td>';
76 print_user_picture($user->id, $course->id, $user->picture);
77 echo '</td><td>';
78 echo fullname($user);
79 echo '</td><td>';
80 echo $user->email;
81 echo '</td></tr>';
83 echo "</table>";
86 print_footer($course);
87 exit;
90 /// We are in editing mode.
92 $strexistingsubscribers = get_string("existingsubscribers", 'forum');
93 $strpotentialsubscribers = get_string("potentialsubscribers", 'forum');
94 $straddsubscriber = get_string("addsubscriber", 'forum');
95 $strremovesubscriber = get_string("removesubscriber", 'forum');
96 $strsearch = get_string("search");
97 $strsearchresults = get_string("searchresults");
98 $strshowall = get_string("showall");
99 $strsubscribers = get_string("subscribers", "forum");
100 $strforums = get_string("forums", "forum");
102 if ($frm = data_submitted()) {
104 /// A form was submitted so process the input
106 if (!empty($frm->add) and !empty($frm->addselect)) {
108 foreach ($frm->addselect as $addsubscriber) {
109 if (! forum_subscribe($addsubscriber, $id)) {
110 error("Could not add subscriber with id $addsubscriber to this forum!");
113 } else if (!empty($frm->remove) and !empty($frm->removeselect)) {
114 foreach ($frm->removeselect as $removesubscriber) {
115 if (! forum_unsubscribe($removesubscriber, $id)) {
116 error("Could not remove subscriber with id $removesubscriber from this forum!");
119 } else if (!empty($frm->showall)) {
120 unset($frm->searchtext);
121 $frm->previoussearch = 0;
125 $previoussearch = (!empty($frm->search) or (!empty($frm->previoussearch) && $frm->previoussearch == 1)) ;
127 /// Get all existing subscribers for this forum.
128 if (!$subscribers = forum_subscribed_users($course, $forum, $currentgroup)) {
129 $subscribers = array();
132 $subscriberarray = array();
133 foreach ($subscribers as $subscriber) {
134 $subscriberarray[] = $subscriber->id;
136 $subscriberlist = implode(',', $subscriberarray);
138 unset($subscriberarray);
140 /// Get search results excluding any users already subscribed
142 if (!empty($frm->searchtext) and $previoussearch) {
143 $searchusers = search_users($course->id, $currentgroup, $frm->searchtext, 'firstname ASC, lastname ASC', $subscriberlist);
146 /// If no search results then get potential subscribers for this forum excluding users already subscribed
147 if (empty($searchusers)) {
148 if ($currentgroup) {
149 $users = get_group_users($currentgroup, 'firstname ASC, lastname ASC', $subscriberlist);
150 } else {
151 $users = get_course_users($course->id, 'firstname ASC, lastname ASC', $subscriberlist);
153 if (!$users) {
154 $users = array();
159 $searchtext = (isset($frm->searchtext)) ? $frm->searchtext : "";
160 $previoussearch = ($previoussearch) ? '1' : '0';
162 print_simple_box_start('center');
164 include('subscriber.html');
166 print_simple_box_end();
168 print_footer($course);