weekly release 4.0dev
[moodle.git] / mod / forum / renderer.php
CommitLineData
c23c9009
SH
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file contains a custom renderer class used by the forum module.
20 *
01030f1b 21 * @package mod_forum
c23c9009
SH
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
75590935 27 * A custom renderer class that extends the plugin_renderer_base and
c23c9009 28 * is used by the forum module.
2f67a9b3 29 *
01030f1b 30 * @package mod_forum
c23c9009
SH
31 * @copyright 2009 Sam Hemelryk
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 **/
75590935 34class mod_forum_renderer extends plugin_renderer_base {
d7862878
FM
35
36 /**
37 * Returns the navigation to the previous and next discussion.
38 *
39 * @param mixed $prev Previous discussion record, or false.
40 * @param mixed $next Next discussion record, or false.
41 * @return string The output.
42 */
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'));
b284ab69
LB
51 $html .= html_writer::link($url, $this->output->larrow() . ' ' . format_string($prev->name),
52 array('aria-label' => get_string('prevdiscussiona', 'mod_forum', format_string($prev->name)),
53 'class' => 'btn btn-link'));
d7862878
FM
54 $html .= html_writer::end_tag('li');
55 }
56 if ($next) {
57 $url = new moodle_url('/mod/forum/discuss.php', array('d' => $next->id));
58 $html .= html_writer::start_tag('li', array('class' => 'next-discussion'));
b284ab69
LB
59 $html .= html_writer::link($url, format_string($next->name) . ' ' . $this->output->rarrow(),
60 array('aria-label' => get_string('nextdiscussiona', 'mod_forum', format_string($next->name)),
61 'class' => 'btn btn-link'));
d7862878
FM
62 $html .= html_writer::end_tag('li');
63 }
64 $html .= html_writer::end_tag('ul');
65 $html .= html_writer::end_tag('div');
66 }
67 return $html;
68 }
69
c23c9009
SH
70 /**
71 * This method is used to generate HTML for a subscriber selection form that
72 * uses two user_selector controls
73 *
74 * @param user_selector_base $existinguc
75 * @param user_selector_base $potentialuc
76 * @return string
77 */
78 public function subscriber_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
79 $output = '';
80 $formattributes = array();
81 $formattributes['id'] = 'subscriberform';
82 $formattributes['action'] = '';
83 $formattributes['method'] = 'post';
5d0c95a5
PS
84 $output .= html_writer::start_tag('form', $formattributes);
85 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
c23c9009
SH
86
87 $existingcell = new html_table_cell();
88 $existingcell->text = $existinguc->display(true);
16be8974 89 $existingcell->attributes['class'] = 'existing';
c23c9009 90 $actioncell = new html_table_cell();
5d0c95a5
PS
91 $actioncell->text = html_writer::start_tag('div', array());
92 $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'subscribe', 'value'=>$this->page->theme->larrow.' '.get_string('add'), 'class'=>'actionbutton'));
93 $actioncell->text .= html_writer::empty_tag('br', array());
94 $actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'unsubscribe', 'value'=>$this->page->theme->rarrow.' '.get_string('remove'), 'class'=>'actionbutton'));
95 $actioncell->text .= html_writer::end_tag('div', array());
16be8974 96 $actioncell->attributes['class'] = 'actions';
c23c9009
SH
97 $potentialcell = new html_table_cell();
98 $potentialcell->text = $potentialuc->display(true);
16be8974 99 $potentialcell->attributes['class'] = 'potential';
c23c9009
SH
100
101 $table = new html_table();
16be8974 102 $table->attributes['class'] = 'subscribertable boxaligncenter';
8cea545e 103 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
16be8974 104 $output .= html_writer::table($table);
2f67a9b3 105
5d0c95a5 106 $output .= html_writer::end_tag('form');
c23c9009
SH
107 return $output;
108 }
109
110 /**
111 * This function generates HTML to display a subscriber overview, primarily used on
112 * the subscribers page if editing was turned off
113 *
114 * @param array $users
115 * @param object $forum
116 * @param object $course
117 * @return string
118 */
119 public function subscriber_overview($users, $forum , $course) {
120 $output = '';
dc8f55c3 121 $modinfo = get_fast_modinfo($course);
c23c9009
SH
122 if (!$users || !is_array($users) || count($users)===0) {
123 $output .= $this->output->heading(get_string("nosubscribers", "forum"));
dc8f55c3
MT
124 } else if (!isset($modinfo->instances['forum'][$forum->id])) {
125 $output .= $this->output->heading(get_string("invalidmodule", "error"));
c23c9009 126 } else {
dc8f55c3 127 $cm = $modinfo->instances['forum'][$forum->id];
558cc1b8 128 // TODO Does not support custom user profile fields (MDL-70456).
5e72715e 129 $canviewemail = in_array('email', \core_user\fields::get_identity_fields(context_module::instance($cm->id), false));
54cf9439
DNA
130 $strparams = new stdclass();
131 $strparams->name = format_string($forum->name);
132 $strparams->count = count($users);
133 $output .= $this->output->heading(get_string("subscriberstowithcount", "forum", $strparams));
c23c9009
SH
134 $table = new html_table();
135 $table->cellpadding = 5;
136 $table->cellspacing = 5;
137 $table->tablealign = 'center';
138 $table->data = array();
139 foreach ($users as $user) {
dc8f55c3
MT
140 $info = array($this->output->user_picture($user, array('courseid'=>$course->id)), fullname($user));
141 if ($canviewemail) {
142 array_push($info, $user->email);
143 }
144 $table->data[] = $info;
c23c9009 145 }
16be8974 146 $output .= html_writer::table($table);
c23c9009
SH
147 }
148 return $output;
149 }
150
151 /**
152 * This is used to display a control containing all of the subscribed users so that
153 * it can be searched
154 *
155 * @param user_selector_base $existingusers
156 * @return string
157 */
158 public function subscribed_users(user_selector_base $existingusers) {
159 $output = $this->output->box_start('subscriberdiv boxaligncenter');
ebfb73db 160 $output .= html_writer::tag('p', get_string('forcesubscribed', 'forum'));
c23c9009
SH
161 $output .= $existingusers->display(true);
162 $output .= $this->output->box_end();
163 return $output;
164 }
2f67a9b3 165
05f1455b
AO
166 /**
167 * Generate the HTML for an icon to be displayed beside the subject of a timed discussion.
168 *
169 * @param object $discussion
170 * @param bool $visiblenow Indicicates that the discussion is currently
171 * visible to all users.
172 * @return string
173 */
174 public function timed_discussion_tooltip($discussion, $visiblenow) {
175 $dates = array();
176 if ($discussion->timestart) {
177 $dates[] = get_string('displaystart', 'mod_forum').': '.userdate($discussion->timestart);
178 }
179 if ($discussion->timeend) {
180 $dates[] = get_string('displayend', 'mod_forum').': '.userdate($discussion->timeend);
181 }
c23c9009 182
05f1455b
AO
183 $str = $visiblenow ? 'timedvisible' : 'timedhidden';
184 $dates[] = get_string($str, 'mod_forum');
185
186 $tooltip = implode("\n", $dates);
187 return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
188 }
31793839
AN
189
190 /**
191 * Display a forum post in the relevant context.
192 *
193 * @param \mod_forum\output\forum_post $post The post to display.
194 * @return string
195 */
196 public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
ca0227dd 197 $data = $post->export_for_template($this, $this->target === RENDERER_TARGET_TEXTEMAIL);
31793839
AN
198 return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
199 }
200
201 /**
202 * The template name for this renderer.
203 *
204 * @return string
205 */
206 public function forum_post_template() {
207 return 'forum_post';
208 }
c42c2355
AN
209
210 /**
211 * Create the inplace_editable used to select forum digest options.
212 *
213 * @param stdClass $forum The forum to create the editable for.
214 * @param int $value The current value for this user
215 * @return inplace_editable
216 */
217 public function render_digest_options($forum, $value) {
218 $options = forum_get_user_digest_options();
219 $editable = new \core\output\inplace_editable(
220 'mod_forum',
221 'digestoptions',
222 $forum->id,
223 true,
224 $options[$value],
225 $value
226 );
227
228 $editable->set_type_select($options);
229
230 return $editable;
231 }
66bb9ead
FM
232
233 /**
234 * Render quick search form.
235 *
236 * @param \mod_forum\output\quick_search_form $form The renderable.
237 * @return string
238 */
239 public function render_quick_search_form(\mod_forum\output\quick_search_form $form) {
240 return $this->render_from_template('mod_forum/quick_search_form', $form->export_for_template($this));
241 }
10deddd3
FM
242
243 /**
244 * Render big search form.
245 *
246 * @param \mod_forum\output\big_search_form $form The renderable.
247 * @return string
248 */
249 public function render_big_search_form(\mod_forum\output\big_search_form $form) {
250 return $this->render_from_template('mod_forum/big_search_form', $form->export_for_template($this));
251 }
16be8974 252}