From: Sam Hemelryk Date: Thu, 10 Dec 2009 02:11:28 +0000 (+0000) Subject: mod-forum MDL-17550 Converted subscribers.php to use the new user_selector control X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c23c90093ec7c83e5f41b4504b968a918db16efb;p=moodle.git mod-forum MDL-17550 Converted subscribers.php to use the new user_selector control At the same time I also converted weblib.php::check_theme_arrows to use UTF8 arrows rather than entities. --- diff --git a/lib/weblib.php b/lib/weblib.php index d175fcc6d7..e993a751b7 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1926,8 +1926,8 @@ function check_theme_arrows() { if (!isset($THEME->rarrow) and !isset($THEME->larrow)) { // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8... // Also OK in Win 9x/2K/IE 5.x - $THEME->rarrow = '►'; - $THEME->larrow = '◄'; + $THEME->rarrow = '►'; // ► + $THEME->larrow = '◄'; // ◄ if (empty($_SERVER['HTTP_USER_AGENT'])) { $uagent = ''; } else { @@ -1937,12 +1937,12 @@ function check_theme_arrows() { || false !== strpos($uagent, 'Mac')) { // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari. // Not broken in Mac/IE 5, Mac/Netscape 7 (?). - $THEME->rarrow = '▶'; - $THEME->larrow = '◀'; + $THEME->rarrow = '▶'; // ▶ + $THEME->larrow = '◀'; // ◀ } elseif (false !== strpos($uagent, 'Konqueror')) { - $THEME->rarrow = '→'; - $THEME->larrow = '←'; + $THEME->rarrow = '→'; // → + $THEME->larrow = '←'; // ← } elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET']) && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) { diff --git a/mod/forum/forum.css b/mod/forum/forum.css new file mode 100644 index 0000000000..4e2dde0fc2 --- /dev/null +++ b/mod/forum/forum.css @@ -0,0 +1,26 @@ +#mod-forum-subscribers .subscriberdiv, +#mod-forum-subscribers .subscribertable { + width:100%; + background-color:#EEEEEE; + border:1px solid #BBBBBB; +} +#mod-forum-subscribers .subscriberdiv, +#mod-forum-subscribers .subscribertable tr td { + vertical-align:top; + padding:0.2em 0.3em; +} +#mod-forum-subscribers .subscribertable tr td.existing { + width:42%; +} +#mod-forum-subscribers .subscribertable tr td.actions { + width:16%; + padding-top:3em; +} +#mod-forum-subscribers .subscribertable tr td.actions .actionbutton { + margin:0.3em 0; + padding:0.5em 0; + width:100%; +} +#mod-forum-subscribers .subscribertable tr td.potential { + width:42%; +} \ No newline at end of file diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 5daec0e17e..4406e729bc 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -16,7 +16,7 @@ // along with Moodle. If not, see . /** - * @package mod-forum + * @package forum * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -26,6 +26,7 @@ require_once($CFG->libdir.'/filelib.php'); require_once($CFG->libdir.'/eventslib.php'); require_once($CFG->libdir.'/portfoliolib.php'); require_once($CFG->libdir . '/completionlib.php'); +require_once($CFG->dirroot.'/user/selector/lib.php'); /// CONSTANTS /////////////////////////////////////////////////////////// @@ -7964,7 +7965,7 @@ function forum_get_extra_capabilities() { } /** - * @package mod-forum + * @package forum * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -8499,3 +8500,123 @@ function forum_extend_settings_navigation($settingsnav, $module=null) { return $forumkey; } + +abstract class forum_subscriber_selector_base extends user_selector_base { + + protected $forumid = null; + protected $context = null; + protected $currentgroup = null; + + public function __construct($name, $options) { + parent::__construct($name, $options); + if (isset($options['context'])) { + $this->context = $options['context']; + } + if (isset($options['currentgroup'])) { + $this->currentgroup = $options['currentgroup']; + } + if (isset($options['forumid'])) { + $this->forumid = $options['forumid']; + } + } + + protected function get_options() { + global $CFG; + $options = parent::get_options(); + $options['file'] = substr(__FILE__, strlen($CFG->dirroot.'/')); + $options['context'] = $this->context; + $options['currentgroup'] = $this->currentgroup; + $options['forumid'] = $this->forumid; + return $options; + } + +} + +class forum_potential_subscriber_selector extends forum_subscriber_selector_base { + + protected $forcesubscribed = false; + protected $existingsubscribers = array(); + + public function __construct($name, $options) { + parent::__construct($name, $options); + if (isset($options['forcesubscribed'])) { + $this->forcesubscribed=true; + } + } + + protected function get_options() { + $options = parent::get_options(); + if ($this->forcesubscribed===true) { + $options['forcesubscribed']=1; + } + return $options; + } + + public function find_users($search) { + global $DB; + + $availableusers = forum_get_potential_subscribers($this->context, $this->currentgroup, $this->required_fields_sql('u'), 'firstname ASC, lastname ASC'); + + if (empty($availableusers)) { + $availableusers = array(); + } else if ($search) { + $search = strtolower($search); + foreach ($availableusers as $key=>$user) { + if (stripos($user->firstname, $search) === false && stripos($user->lastname, $search) === false) { + unset($availableusers[$key]); + } + } + } + + // Unset any existing subscribers + if (count($this->existingsubscribers)>0 && !$this->forcesubscribed) { + foreach ($this->existingsubscribers as $group) { + foreach ($group as $user) { + if (array_key_exists($user->id, $availableusers)) { + unset($availableusers[$user->id]); + } + } + } + } + + if ($this->forcesubscribed) { + return array(get_string("existingsubscribers", 'forum') => $availableusers); + } else { + return array(get_string("potentialsubscribers", 'forum') => $availableusers); + } + } + + public function set_existing_subscribers(array $users) { + $this->existingsubscribers = $users; + } + + public function set_force_subscribed($setting=true) { + $this->forcesubscribed = true; + } +} + +class forum_existing_subscriber_selector extends forum_subscriber_selector_base { + + public function find_users($search) { + global $DB; + list($wherecondition, $params) = $this->search_sql($search, 'u'); + + $fields = 'SELECT ' . $this->required_fields_sql('u'); + $from = ' FROM {user} u LEFT JOIN {forum_subscriptions} s ON s.userid=u.id'; + $wherecondition .= ' AND s.forum=?'; + $params[] = $this->forumid; + $order = ' ORDER BY lastname ASC, firstname ASC'; + + if ($this->currentgroup) { + $from .= ", {groups_members} gm "; + $wherecondition .= " AND gm.groupid = ? AND u.id = gm.userid"; + $params[] = $this->currentgroup; + } + if (!$subscribers = $DB->get_records_sql($fields.$from.' WHERE '.$wherecondition.$order, $params)) { + $subscribers = array(); + } + + return array(get_string("existingsubscribers", 'forum') => $subscribers); + } + +} \ No newline at end of file diff --git a/mod/forum/renderer.php b/mod/forum/renderer.php new file mode 100644 index 0000000000..c1f4ade4e6 --- /dev/null +++ b/mod/forum/renderer.php @@ -0,0 +1,136 @@ +. + +/** + * This file contains a custom renderer class used by the forum module. + * + * @package forum + * @copyright 2009 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * A custom renderer class that extends the moodle_renderer_base and + * is used by the forum module. + * + * @package forum + * @copyright 2009 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + **/ +class moodle_mod_forum_renderer extends moodle_renderer_base { + + /** + * A reference to the current general renderer probably {@see moodle_core_renderer} + * @var moodle_renderer_base + */ + protected $output; + + /** + * Contructor method, calls the parent constructor + * @param moodle_page $page + * @param moodle_renderer_base $output Probably moodle_core_renderer + */ + public function __construct($page, $output) { + parent::__construct($page); + $this->output = $output; + } + + /** + * This method is used to generate HTML for a subscriber selection form that + * uses two user_selector controls + * + * @param user_selector_base $existinguc + * @param user_selector_base $potentialuc + * @return string + */ + public function subscriber_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) { + $output = ''; + $formattributes = array(); + $formattributes['id'] = 'subscriberform'; + $formattributes['action'] = ''; + $formattributes['method'] = 'post'; + $output .= $this->output_start_tag('form', $formattributes); + $output .= $this->output_empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey())); + + $existingcell = new html_table_cell(); + $existingcell->text = $existinguc->display(true); + $existingcell->set_classes(array('existing')); + $actioncell = new html_table_cell(); + $actioncell->text = $this->output_start_tag('div', array()); + $actioncell->text .= $this->output_empty_tag('input', array('type'=>'submit', 'name'=>'subscribe', 'value'=>$this->page->theme->larrow.' '.get_string('add'), 'class'=>'actionbutton')); + $actioncell->text .= $this->output_empty_tag('br', array()); + $actioncell->text .= $this->output_empty_tag('input', array('type'=>'submit', 'name'=>'unsubscribe', 'value'=>$this->page->theme->rarrow.' '.get_string('remove'), 'class'=>'actionbutton')); + $actioncell->text .= $this->output_end_tag('div', array()); + $actioncell->set_classes(array('actions')); + $potentialcell = new html_table_cell(); + $potentialcell->text = $potentialuc->display(true); + $potentialcell->set_classes(array('potential')); + + $table = new html_table(); + $table->set_classes(array('subscribertable','boxaligncenter')); + $table->data = array(html_table_row::make(array($existingcell, $actioncell, $potentialcell))); + $output .= $this->output->table($table); + + $output .= $this->output_end_tag('form'); + return $output; + } + + /** + * This function generates HTML to display a subscriber overview, primarily used on + * the subscribers page if editing was turned off + * + * @param array $users + * @param object $forum + * @param object $course + * @return string + */ + public function subscriber_overview($users, $forum , $course) { + $output = ''; + if (!$users || !is_array($users) || count($users)===0) { + $output .= $this->output->heading(get_string("nosubscribers", "forum")); + } else { + $output .= $this->output->heading(get_string("subscribersto","forum", "'".format_string($forum->name)."'")); + $table = new html_table(); + $table->cellpadding = 5; + $table->cellspacing = 5; + $table->tablealign = 'center'; + $table->data = array(); + foreach ($users as $user) { + $table->data[] = array($this->output->user_picture(moodle_user_picture::make($user, $course->id)), fullname($user), $user->email); + } + $output .= $this->output->table($table); + } + return $output; + } + + /** + * This is used to display a control containing all of the subscribed users so that + * it can be searched + * + * @param user_selector_base $existingusers + * @return string + */ + public function subscribed_users(user_selector_base $existingusers) { + $output = $this->output->box_start('subscriberdiv boxaligncenter'); + $output .= $this->output_tag('p', array(), get_string('forcessubscribe', 'forum')); + $output .= $existingusers->display(true); + $output .= $this->output->box_end(); + return $output; + } + + +} \ No newline at end of file diff --git a/mod/forum/subscriber.html b/mod/forum/subscriber.html deleted file mode 100644 index 5ea4b0fedb..0000000000 --- a/mod/forum/subscriber.html +++ /dev/null @@ -1,71 +0,0 @@ - -
- - - - - - - - - - - - - -
- - - -
- -
- -
- -
-
- -
- - - '."\n"; - } - ?> -
-
- - diff --git a/mod/forum/subscribers.php b/mod/forum/subscribers.php index f3e50e72a6..477c4db004 100644 --- a/mod/forum/subscribers.php +++ b/mod/forum/subscribers.php @@ -1,5 +1,28 @@ . + +/** + * This file is used to display and organise forum subscribers + * + * @package forum + * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + require_once("../../config.php"); require_once("lib.php"); @@ -16,14 +39,8 @@ if ($edit !== 0) { } $PAGE->set_url($url); -if (! $forum = $DB->get_record('forum', array('id'=>$id))) { - print_error('invalidforumid', 'forum'); -} - -if (! $course = $DB->get_record('course', array('id'=>$forum->course))) { - print_error('invalidcourseid'); -} - +$forum = $DB->get_record('forum', array('id'=>$id), '*', MUST_EXIST); +$course = $DB->get_record('course', array('id'=>$forum->course), '*', MUST_EXIST); if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { $cm->id = 0; } @@ -31,7 +48,6 @@ if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { require_login($course->id, false, $cm); $context = get_context_instance(CONTEXT_MODULE, $cm->id); - if (!has_capability('mod/forum:viewsubscribers', $context)) { print_error('nopermissiontosubscribe', 'forum'); } @@ -40,13 +56,45 @@ unset($SESSION->fromdiscussion); add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id); -$strsubscribeall = get_string("subscribeall", "forum"); -$strsubscribenone = get_string("subscribenone", "forum"); -$strsubscribers = get_string("subscribers", "forum"); -$strforums = get_string("forums", "forum"); +$forumoutput = $PAGE->theme->get_renderer('mod_forum', $PAGE); +$currentgroup = groups_get_activity_group($cm); +$options = array('forumid'=>$forum->id, 'currentgroup'=>$currentgroup, 'context'=>$context); +$existingselector = new forum_existing_subscriber_selector('existingsubscribers', $options); +$subscriberselector = new forum_potential_subscriber_selector('potentialsubscribers', $options); +$subscriberselector->set_existing_subscribers($existingselector->find_users('')); + +if (data_submitted()) { + require_sesskey(); + $subscribe = (bool)optional_param('subscribe', false, PARAM_RAW); + $unsubscribe = (bool)optional_param('unsubscribe', false, PARAM_RAW); + /** It has to be one or the other, not both or neither */ + if (!($subscribe xor $unsubscribe)) { + print_error('invalidaction'); + } + if ($subscribe) { + $users = $subscriberselector->get_selected_users(); + foreach ($users as $user) { + if (!forum_subscribe($user->id, $id)) { + print_error('cannotaddsubscriber', 'forum', '', $user->id); + } + } + } else if ($unsubscribe) { + $users = $existingselector->get_selected_users(); + foreach ($users as $user) { + if (!forum_unsubscribe($user->id, $id)) { + print_error('cannotremovesubscriber', 'forum', '', $user->id); + } + } + } + $subscriberselector->invalidate_selected_users(); + $existingselector->invalidate_selected_users(); + $subscriberselector->set_existing_subscribers($existingselector->find_users('')); +} +$strsubscribers = get_string("subscribers", "forum"); $PAGE->navbar->add($strsubscribers); $PAGE->set_title($strsubscribers); +$PAGE->requires->css('mod/forum/forum.css'); if (has_capability('mod/forum:managesubscriptions', $context)) { $PAGE->set_button(forum_update_subscriptions_button($course->id, $id)); if ($edit != -1) { @@ -56,108 +104,13 @@ if (has_capability('mod/forum:managesubscriptions', $context)) { unset($USER->subscriptionsediting); } echo $OUTPUT->header(); - -/// Check to see if groups are being used in this forum -groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/forum/subscribers.php?id=$forum->id"); -$currentgroup = groups_get_activity_group($cm); -$groupmode = groups_get_activity_groupmode($cm); - -if (empty($USER->subscriptionsediting)) { /// Display an overview of subscribers - - if (! $users = forum_subscribed_users($course, $forum, $currentgroup, $context) ) { - - echo $OUTPUT->heading(get_string("nosubscribers", "forum")); - - } else { - - echo $OUTPUT->heading(get_string("subscribersto","forum", "'".format_string($forum->name)."'")); - - echo ''; - foreach ($users as $user) { - echo ''; - } - echo "
'; - echo $OUTPUT->user_picture(moodle_user_picture::make($user, $course->id)); - echo ''; - echo fullname($user); - echo ''; - echo $user->email; - echo '
"; - } - - echo $OUTPUT->footer(); - exit; -} - -/// We are in editing mode. - -$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"); - -$searchtext = optional_param('searchtext', '', PARAM_RAW); -if ($frm = data_submitted() and confirm_sesskey()) { - -/// 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)) { - print_error('cannotaddsubscriber', 'forum', '', $addsubscriber); - } - } - } else if (!empty($frm->remove) and !empty($frm->removeselect)) { - foreach ($frm->removeselect as $removesubscriber) { - if (! forum_unsubscribe($removesubscriber, $id)) { - print_error('cannotremovesubscriber', 'forum', '', $removesubscriber); - } - } - } else if (!empty($frm->showall)) { - $searchtext = ''; - } -} - -/// Get all existing subscribers for this forum. -if (!$subscribers = forum_subscribed_users($course, $forum, $currentgroup, $context)) { - $subscribers = array(); -} - -/// Get all the potential subscribers excluding users already subscribed -$users = forum_get_potential_subscribers($context, $currentgroup, 'id,email,firstname,lastname', 'firstname ASC, lastname ASC'); -if (!$users) { - $users = array(); -} -foreach ($subscribers as $subscriber) { - unset($users[$subscriber->id]); -} - -/// This is yucky, but do the search in PHP, becuase the list we are using comes from get_users_by_capability, -/// which does not allow searching in the database. Fortunately the list is only this list of users in this -/// course, which is normally OK, except on the site course of a big site. But before you can enter a search -/// term, you have already seen a page that lists everyone, since this code never does paging, so you have probably -/// already crashed your server if you are going to. This will be fixed properly for Moodle 2.0: MDL-17550. -if ($searchtext) { - $searchusers = array(); - $lcsearchtext = moodle_strtolower($searchtext); - foreach ($users as $userid => $user) { - if (strpos(moodle_strtolower($user->email), $lcsearchtext) !== false || - strpos(moodle_strtolower($user->firstname . ' ' . $user->lastname), $lcsearchtext) !== false) { - $searchusers[$userid] = $user; - } - unset($users[$userid]); - } +echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers); +if (empty($USER->subscriptionsediting)) { + echo $forumoutput->subscriber_overview(forum_subscribed_users($course, $forum, $currentgroup, $context), $forum, $course); +} else if (forum_is_forcesubscribed($forum)) { + $subscriberselector->set_force_subscribed(true); + echo $forumoutput->subscribed_users($subscriberselector); +} else { + echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector); } - -echo $OUTPUT->box_start('generalbox boxaligncenter'); - -include('subscriber.html'); - -echo $OUTPUT->box_end(); - -echo $OUTPUT->footer(); - +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/user/selector/lib.php b/user/selector/lib.php index 587f5b817e..46c5f05b66 100644 --- a/user/selector/lib.php +++ b/user/selector/lib.php @@ -63,9 +63,9 @@ abstract class user_selector_base { /** When searching, do we only match the starts of fields (better performace) * or do we match occurrences anywhere? */ protected $searchanywhere = false; - // This is used by get selected users, - private $validatinguserids = null; + protected $validatinguserids = null; + // Used to ensure we only output the search options for one user selector on // each page. private static $searchoptionsoutput = false; @@ -372,7 +372,7 @@ abstract class user_selector_base { $users = array(); foreach ($groupedusers as $group) { foreach ($group as $user) { - if (!isset($users[$user->id]) && empty($user->disabled)) { + if (!isset($users[$user->id]) && empty($user->disabled) && in_array($user->id, $userids)) { $users[$user->id] = $user; } }