MDL-46921 lib: Update get_all_user_name_fields() plus unit tests.
[moodle.git] / mod / forum / renderer.php
blob237ee7788c550a200d6138e8fa22a8c445d5bf36
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file contains a custom renderer class used by the forum module.
21 * @package mod_forum
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /**
27 * A custom renderer class that extends the plugin_renderer_base and
28 * is used by the forum module.
30 * @package mod_forum
31 * @copyright 2009 Sam Hemelryk
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 **/
34 class mod_forum_renderer extends plugin_renderer_base {
36 /**
37 * Returns the navigation to the previous and next discussion.
39 * @param mixed $prev Previous discussion record, or false.
40 * @param mixed $next Next discussion record, or false.
41 * @return string The output.
43 public function neighbouring_discussion_navigation($prev, $next) {
44 $html = '';
45 if ($prev || $next) {
46 $html .= html_writer::start_tag('div', array('class' => 'discussion-nav clearfix'));
47 $html .= html_writer::start_tag('ul');
48 if ($prev) {
49 $url = new moodle_url('/mod/forum/discuss.php', array('d' => $prev->id));
50 $html .= html_writer::start_tag('li', array('class' => 'prev-discussion'));
51 $html .= html_writer::link($url, $prev->name,
52 array('aria-label' => get_string('prevdiscussiona', 'mod_forum', $prev->name)));
53 $html .= html_writer::end_tag('li');
55 if ($next) {
56 $url = new moodle_url('/mod/forum/discuss.php', array('d' => $next->id));
57 $html .= html_writer::start_tag('li', array('class' => 'next-discussion'));
58 $html .= html_writer::link($url, $next->name,
59 array('aria-label' => get_string('nextdiscussiona', 'mod_forum', $next->name)));
60 $html .= html_writer::end_tag('li');
62 $html .= html_writer::end_tag('ul');
63 $html .= html_writer::end_tag('div');
65 return $html;
68 /**
69 * This method is used to generate HTML for a subscriber selection form that
70 * uses two user_selector controls
72 * @param user_selector_base $existinguc
73 * @param user_selector_base $potentialuc
74 * @return string
76 public function subscriber_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
77 $output = '';
78 $formattributes = array();
79 $formattributes['id'] = 'subscriberform';
80 $formattributes['action'] = '';
81 $formattributes['method'] = 'post';
82 $output .= html_writer::start_tag('form', $formattributes);
83 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
85 $existingcell = new html_table_cell();
86 $existingcell->text = $existinguc->display(true);
87 $existingcell->attributes['class'] = 'existing';
88 $actioncell = new html_table_cell();
89 $actioncell->text = html_writer::start_tag('div', array());
90 $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'subscribe', 'value'=>$this->page->theme->larrow.' '.get_string('add'), 'class'=>'actionbutton'));
91 $actioncell->text .= html_writer::empty_tag('br', array());
92 $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'unsubscribe', 'value'=>$this->page->theme->rarrow.' '.get_string('remove'), 'class'=>'actionbutton'));
93 $actioncell->text .= html_writer::end_tag('div', array());
94 $actioncell->attributes['class'] = 'actions';
95 $potentialcell = new html_table_cell();
96 $potentialcell->text = $potentialuc->display(true);
97 $potentialcell->attributes['class'] = 'potential';
99 $table = new html_table();
100 $table->attributes['class'] = 'subscribertable boxaligncenter';
101 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
102 $output .= html_writer::table($table);
104 $output .= html_writer::end_tag('form');
105 return $output;
109 * This function generates HTML to display a subscriber overview, primarily used on
110 * the subscribers page if editing was turned off
112 * @param array $users
113 * @param object $forum
114 * @param object $course
115 * @return string
117 public function subscriber_overview($users, $forum , $course) {
118 $output = '';
119 $modinfo = get_fast_modinfo($course);
120 if (!$users || !is_array($users) || count($users)===0) {
121 $output .= $this->output->heading(get_string("nosubscribers", "forum"));
122 } else if (!isset($modinfo->instances['forum'][$forum->id])) {
123 $output .= $this->output->heading(get_string("invalidmodule", "error"));
124 } else {
125 $cm = $modinfo->instances['forum'][$forum->id];
126 $canviewemail = in_array('email', get_extra_user_fields(context_module::instance($cm->id)));
127 $output .= $this->output->heading(get_string("subscribersto","forum", "'".format_string($forum->name)."'"));
128 $table = new html_table();
129 $table->cellpadding = 5;
130 $table->cellspacing = 5;
131 $table->tablealign = 'center';
132 $table->data = array();
133 foreach ($users as $user) {
134 $info = array($this->output->user_picture($user, array('courseid'=>$course->id)), fullname($user));
135 if ($canviewemail) {
136 array_push($info, $user->email);
138 $table->data[] = $info;
140 $output .= html_writer::table($table);
142 return $output;
146 * This is used to display a control containing all of the subscribed users so that
147 * it can be searched
149 * @param user_selector_base $existingusers
150 * @return string
152 public function subscribed_users(user_selector_base $existingusers) {
153 $output = $this->output->box_start('subscriberdiv boxaligncenter');
154 $output .= html_writer::tag('p', get_string('forcesubscribed', 'forum'));
155 $output .= $existingusers->display(true);
156 $output .= $this->output->box_end();
157 return $output;