MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / forum / subscribers.php
blob853d6a738c1e03e2b58c0843848cd70ab3d37d20
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 $navlinks = array();
40 $navlinks[] = array('name' => $strforums, 'link' => "index.php?id=$course->id", 'type' => 'activity');
41 $navlinks[] = array('name' => format_string($forum->name), 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
42 $navlinks[] = array('name' => $strsubscribers, 'link' => '', 'type' => 'title');
44 $navigation = build_navigation($navlinks);
46 if (has_capability('mod/forum:managesubscriptions', $context)) {
47 print_header_simple("$strsubscribers", "", $navigation,
48 "", "", true, forum_update_subscriptions_button($course->id, $id));
49 if ($edit != -1) {
50 $USER->subscriptionsediting = $edit;
52 } else {
53 print_header_simple("$strsubscribers", "", $navigation, "", "", true, '');
54 unset($USER->subscriptionsediting);
57 /// Check to see if groups are being used in this forum
58 groups_print_activity_menu($cm, "subscribers.php?id=$forum->id");
59 $currentgroup = groups_get_activity_group($cm);
60 $groupmode = groups_get_activity_groupmode($cm);
62 if (empty($USER->subscriptionsediting)) { /// Display an overview of subscribers
64 if (! $users = forum_subscribed_users($course, $forum, $currentgroup) ) {
66 print_heading(get_string("nosubscribers", "forum"));
68 } else {
70 print_heading(get_string("subscribersto","forum", "'".format_string($forum->name)."'"));
72 echo '<table align="center" cellpadding="5" cellspacing="5">';
73 foreach ($users as $user) {
74 echo '<tr><td>';
75 print_user_picture($user->id, $course->id, $user->picture);
76 echo '</td><td>';
77 echo fullname($user);
78 echo '</td><td>';
79 echo $user->email;
80 echo '</td></tr>';
82 echo "</table>";
85 print_footer($course);
86 exit;
89 /// We are in editing mode.
91 $strexistingsubscribers = get_string("existingsubscribers", 'forum');
92 $strpotentialsubscribers = get_string("potentialsubscribers", 'forum');
93 $straddsubscriber = get_string("addsubscriber", 'forum');
94 $strremovesubscriber = get_string("removesubscriber", 'forum');
95 $strsearch = get_string("search");
96 $strsearchresults = get_string("searchresults");
97 $strshowall = get_string("showall");
98 $strsubscribers = get_string("subscribers", "forum");
99 $strforums = get_string("forums", "forum");
101 if ($frm = data_submitted()) {
103 /// A form was submitted so process the input
105 if (!empty($frm->add) and !empty($frm->addselect)) {
107 foreach ($frm->addselect as $addsubscriber) {
108 if (! forum_subscribe($addsubscriber, $id)) {
109 error("Could not add subscriber with id $addsubscriber to this forum!");
112 } else if (!empty($frm->remove) and !empty($frm->removeselect)) {
113 foreach ($frm->removeselect as $removesubscriber) {
114 if (! forum_unsubscribe($removesubscriber, $id)) {
115 error("Could not remove subscriber with id $removesubscriber from this forum!");
118 } else if (!empty($frm->showall)) {
119 unset($frm->searchtext);
120 $frm->previoussearch = 0;
124 $previoussearch = (!empty($frm->search) or (!empty($frm->previoussearch) && $frm->previoussearch == 1)) ;
126 /// Get all existing subscribers for this forum.
127 if (!$subscribers = forum_subscribed_users($course, $forum, $currentgroup)) {
128 $subscribers = array();
131 $subscriberarray = array();
132 foreach ($subscribers as $subscriber) {
133 $subscriberarray[] = $subscriber->id;
135 $subscriberlist = implode(',', $subscriberarray);
137 unset($subscriberarray);
139 /// Get search results excluding any users already subscribed
141 if (!empty($frm->searchtext) and $previoussearch) {
142 $searchusers = search_users($course->id, $currentgroup, $frm->searchtext, 'firstname ASC, lastname ASC', $subscriberlist);
145 /// If no search results then get potential subscribers for this forum excluding users already subscribed
146 if (empty($searchusers)) {
147 if ($currentgroup) {
148 $users = get_group_users($currentgroup, 'firstname ASC, lastname ASC', $subscriberlist);
149 } else {
150 $users = get_course_users($course->id, 'firstname ASC, lastname ASC', $subscriberlist);
152 if (!$users) {
153 $users = array();
158 $searchtext = (isset($frm->searchtext)) ? $frm->searchtext : "";
159 $previoussearch = ($previoussearch) ? '1' : '0';
161 print_simple_box_start('center');
163 include('subscriber.html');
165 print_simple_box_end();
167 print_footer($course);