From 90e7063e45d7a049c5ba2e47e59a9abc8fbe7728 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sun, 15 Aug 2004 12:38:29 +0000 Subject: [PATCH] Editing teacher can now subscribe users to a forum, see http://moodle.org/mod/forum/discuss.php?d=11089 --- lang/en/forum.php | 2 + mod/forum/lib.php | 19 +++ mod/forum/subscriber.html | 80 ++++++++++ mod/forum/subscribers.php | 300 ++++++++++++++++++++++++++++---------- 4 files changed, 327 insertions(+), 74 deletions(-) create mode 100644 mod/forum/subscriber.html diff --git a/lang/en/forum.php b/lang/en/forum.php index 828a5387155..8ef3c042006 100644 --- a/lang/en/forum.php +++ b/lang/en/forum.php @@ -43,6 +43,7 @@ $string['editing'] = 'Editing'; $string['emptymessage'] = 'Something was wrong with your post. Perhaps you left it blank, or the attachment was too big. Your changes have NOT been saved.'; $string['everyonecanchoose'] = 'Everyone can choose to be subscribed'; $string['everyoneissubscribed'] = 'Everyone is subscribed to this forum'; +$string['existingsubscribers'] = 'Existing subscribers'; $string['forcesubscribe'] = 'Force everyone to be subscribed'; $string['forcesubscribeq'] = 'Force everyone to be subscribed?'; $string['forum'] = 'Forum'; @@ -101,6 +102,7 @@ $string['postrating3'] = 'Mostly Connected Knowing'; $string['posts'] = 'Posts'; $string['posttoforum'] = 'Post to forum'; $string['postupdated'] = 'Your post was updated'; +$string['potentialsubscribers'] = 'Potential subscribers'; $string['processingpost'] = 'Processing post $a'; $string['processingdigest'] = 'Processing email digest for user $a'; $string['prune'] = 'Split'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c11d7dc92e4..dd3fa71958a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2809,4 +2809,23 @@ function forum_change_discussionid($postid, $discussionid) { return true; } +function forum_update_subscriptions_button($courseid, $forumid) { +// Prints the editing button on subscribers page + global $CFG, $USER; + + if (isteacheredit($courseid)) { + if (!empty($USER->subscriptionsediting)) { + $string = get_string("turneditingoff"); + $edit = "off"; + } else { + $string = get_string("turneditingon"); + $edit = "on"; + } + return "
framename\" method=\"get\" action=\"$CFG->wwwroot/mod/forum/subscribers.php\">". + "". + "". + "
"; + } +} + ?> diff --git a/mod/forum/subscriber.html b/mod/forum/subscriber.html new file mode 100644 index 00000000000..1a67a85cf22 --- /dev/null +++ b/mod/forum/subscriber.html @@ -0,0 +1,80 @@ + +
+ + + + + + + + + + + + + +
+ + + +
+ +
+ +
+ +
+
+ +
+ + + '."\n"; + } + ?> +
+
+ + diff --git a/mod/forum/subscribers.php b/mod/forum/subscribers.php index 6f1c8cb3472..a398dfc9ad3 100644 --- a/mod/forum/subscribers.php +++ b/mod/forum/subscribers.php @@ -7,6 +7,8 @@ optional_variable($subscribe, ''); // 'all' or 'none' optional_variable($unsubscribe, ''); // a single user id optional_variable($group); // change of group + + optional_variable($edit); // Turn editing on and off if (! $forum = get_record("forum", "id", $id)) { error("Forum ID is incorrect"); @@ -29,6 +31,18 @@ unset($SESSION->fromdiscussion); add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id); + + if (isteacheredit($course->id)) { + if (isset($_GET['edit'])) { + if($edit == "on") { + $USER->subscriptionsediting = true; + } else { + $USER->subscriptionsediting = false; + } + } + } else { + $USER->subscriptionsediting = false; + } $strunsubscribeshort = get_string("unsubscribeshort", "forum"); $strsubscribeall = get_string("subscribeall", "forum"); @@ -38,102 +52,240 @@ if ($course->category) { $navigation = "id\">$course->shortname -> - id\">$strforums -> - id\">$forum->name -> $strsubscribers"; + id\">$strforums -> + id\">$forum->name -> $strsubscribers"; } else { $navigation = "id\">$strforums -> - id\">$forum->name -> $strsubscribers"; + id\">$forum->name -> $strsubscribers"; } - print_header("$course->shortname: $strsubscribers", "$course->fullname", "$navigation"); - + print_header("$course->shortname: $strsubscribers", "$course->fullname", "$navigation", + "", "", true, forum_update_subscriptions_button($course->id, $id)); + if (empty($USER->subscriptionsediting)) { /// Display an overview of subscribers + /// Check to see if groups are being used in this forum - if ($groupmode = groupmode($course, $cm)) { // Groups are being used - $currentgroup = setup_and_print_groups($course, $groupmode, "subscribers.php?id=$forum->id"); - } else { - $currentgroup = false; - } - - if ($subscribe == 'all') { - if ($forum->type == 'teacher') { - $users = get_course_teachers($course->id); - } elseif ($currentgroup) { - $users = get_group_users($currentgroup); + if ($groupmode = groupmode($course, $cm)) { // Groups are being used + $currentgroup = setup_and_print_groups($course, $groupmode, "subscribers.php?id=$forum->id"); } else { - $users = get_course_users($course->id); + $currentgroup = false; } - if ($users) { - foreach ($users as $user) { - forum_subscribe($user->id, $forum->id); + + if ($subscribe == 'all') { + if ($forum->type == 'teacher') { + $users = get_course_teachers($course->id); + } elseif ($currentgroup) { + $users = get_group_users($currentgroup); + } else { + $users = get_course_users($course->id); } - } - } else if ($subscribe == 'none') { - if ($currentgroup) { - if ($users = get_group_users($currentgroup)) { + if ($users) { foreach ($users as $user) { - forum_unsubscribe($user->id, $forum->id); + forum_subscribe($user->id, $forum->id); } } + } else if ($subscribe == 'none') { + if ($currentgroup) { + if ($users = get_group_users($currentgroup)) { + foreach ($users as $user) { + forum_unsubscribe($user->id, $forum->id); + } + } + } else { + delete_records("forum_subscriptions", "forum", $forum->id); + } + } + + if ($unsubscribe) { + if ($user = get_record('user', 'id', $unsubscribe)) { + forum_unsubscribe($user->id, $forum->id); + $info->name = fullname($user); + $info->forum = $forum->name; + notify(get_string("nownotsubscribed", "forum", $info)); + } + } + + if (! $users = forum_subscribed_users($course, $forum, $currentgroup) ) { + + if (!$forum->forcesubscribe) { + echo '
'; + $options['id'] = $forum->id; + $options['subscribe'] = 'all'; + print_single_button('subscribers.php', $options, $strsubscribeall); + echo '
'; + } + + print_heading(get_string("nosubscribers", "forum")); + } else { - delete_records("forum_subscriptions", "forum", $forum->id); + + if (!$forum->forcesubscribe) { + echo ''; + echo ''; + echo ''; + echo '
'; + $options['id'] = $forum->id; + $options['subscribe'] = 'all'; + print_single_button('subscribers.php', $options, $strsubscribeall); + echo ''; + $options['subscribe'] = 'none'; + print_single_button('subscribers.php', $options, $strsubscribenone); + echo '
'; + } + + print_heading(get_string("subscribersto","forum", "'$forum->name'")); + + echo ''; + foreach ($users as $user) { + echo ""; + } + echo "
"; + print_user_picture($user->id, $course->id, $user->picture); + echo "cellcontent\">"; + echo "$user->firstname $user->lastname"; + echo "cellcontent\">"; + echo "$user->email"; + echo ""; + echo "id&unsubscribe=$user->id\">$strunsubscribeshort"; + echo "
"; + } + + print_footer($course); + exit; + } + +/// We are in editing mode. + + if (!isteacheredit($course->id)) { + error("You must be an editing teacher in this course, or an admin"); + } + + $strexistingsubscribers = get_string("existingsubscribers", 'forum'); + $strpotentialsubscribers = get_string("potentialsubscribers", 'forum'); + $straddsubscriber = get_string("addsubscriber", 'forum'); + $strremovesubscriber = get_string("removesubscriber", 'forum'); + $strsearch = get_string("search"); + $strsearchresults = get_string("searchresults"); + $strshowall = get_string("showall"); + $strsubscribers = get_string("subscribers", "forum"); + $strforums = get_string("forums", "forum"); + + if ($frm = data_submitted()) { + +/// A form was submitted so process the input + + if (!empty($frm->add) and !empty($frm->addselect)) { + + foreach ($frm->addselect as $addsubscriber) { + if (! forum_subscribe($addsubscriber, $id)) { + error("Could not add subscriber with id $addsubscriber to this forum!"); + } + } + } else if (!empty($frm->remove) and !empty($frm->removeselect)) { + foreach ($frm->removeselect as $removesubscriber) { + if (! forum_unsubscribe($removesubscriber, $id)) { + error("Could not remove subscriber with id $removesubscriber from this forum!"); + } + } + } else if (!empty($frm->showall)) { + unset($frm->searchtext); + $frm->previoussearch = 0; } } - if ($unsubscribe) { - if ($user = get_record('user', 'id', $unsubscribe)) { - forum_unsubscribe($user->id, $forum->id); - $info->name = fullname($user); - $info->forum = $forum->name; - notify(get_string("nownotsubscribed", "forum", $info)); - } + $previoussearch = (!empty($frm->search) or ($frm->previoussearch == 1)) ; + +/// Get all existing subscribers for this forum. + if (!$subscribers = forum_subscribed_users($course, $forum)) { + $subscribers = array(); } + + $subscriberarray = array(); + foreach ($subscribers as $subscriber) { + $subscriberarray[] = $subscriber->id; + } + $subscriberlist = implode(',', $subscriberarray); + + unset($subscriberarray); - if (! $users = forum_subscribed_users($course, $forum, $currentgroup) ) { - - if (!$forum->forcesubscribe) { - echo '
'; - $options['id'] = $forum->id; - $options['subscribe'] = 'all'; - print_single_button('subscribers.php', $options, $strsubscribeall); - echo '
'; - } - - print_heading(get_string("nosubscribers", "forum")); - +/// Get search results excluding any users already subscribed + switch ($CFG->dbtype) { + case "mysql": + $fullname = " CONCAT(u.firstname,\" \",u.lastname) "; + $LIKE = "LIKE"; + break; + case "postgres7": + $fullname = " u.firstname||' '||u.lastname "; + $LIKE = "ILIKE"; + break; + default: + $fullname = " u.firstname||\" \"||u.lastname "; + $LIKE = "ILIKE"; + } + if (!empty($subscriberlist)) { + $except = " AND u.id NOT IN ($subscriberlist) "; } else { + $except = ''; + } + if (!empty($frm->searchtext) and $previoussearch) { + $searchusers = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_students s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + AND ($fullname $LIKE '%$frm->searchtext%' OR u.email $LIKE '%$frm->searchtext%') + $except + UNION + SELECT u.id, u.firstname, u.lastname, u.email + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_teachers s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + AND ($fullname $LIKE '%$frm->searchtext%' OR u.email $LIKE '%$frm->searchtext%') + $except + ORDER BY u.firstname ASC, u.lastname ASC"); - if (!$forum->forcesubscribe) { - echo ''; - echo ''; - echo ''; - echo '
'; - $options['id'] = $forum->id; - $options['subscribe'] = 'all'; - print_single_button('subscribers.php', $options, $strsubscribeall); - echo ''; - $options['subscribe'] = 'none'; - print_single_button('subscribers.php', $options, $strsubscribenone); - echo '
'; + $usercount = count_records_sql("SELECT COUNT(*) + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_students s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + $except") + + count_records_sql("SELECT COUNT(*) + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_teachers s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + $except"); + } + +/// If no search results then get potential subscribers for this forum excluding users already subscribed + if (empty($searchusers)) { + if (!$users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_students s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + $except + UNION + SELECT u.id, u.firstname, u.lastname, u.email + FROM {$CFG->prefix}user u, + {$CFG->prefix}user_teachers s + WHERE s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0' + $except + ORDER BY u.firstname ASC, u.lastname ASC")) { + $users = array(); } - - print_heading(get_string("subscribersto","forum", "'$forum->name'")); - - echo ''; - foreach ($users as $user) { - echo ""; - } - echo "
"; - print_user_picture($user->id, $course->id, $user->picture); - echo "cellcontent\">"; - echo "$user->firstname $user->lastname"; - echo "cellcontent\">"; - echo "$user->email"; - echo ""; - echo "id&unsubscribe=$user->id\">$strunsubscribeshort"; - echo "
"; + $usercount = count($users); } - print_footer($course); + + + + $searchtext = (isset($frm->searchtext)) ? $frm->searchtext : ""; + $previoussearch = ($previoussearch) ? '1' : '0'; + + print_simple_box_start("center", "", "$THEME->cellheading"); + + include('subscriber.html'); + + print_simple_box_end(); + + print_footer(); ?>