MDL-55594 output: Adding template for html_writer::select_time
[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'));
fc62987c
MG
51 $html .= html_writer::link($url, format_string($prev->name),
52 array('aria-label' => get_string('prevdiscussiona', 'mod_forum', format_string($prev->name))));
d7862878
FM
53 $html .= html_writer::end_tag('li');
54 }
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'));
fc62987c
MG
58 $html .= html_writer::link($url, format_string($next->name),
59 array('aria-label' => get_string('nextdiscussiona', 'mod_forum', format_string($next->name))));
d7862878
FM
60 $html .= html_writer::end_tag('li');
61 }
62 $html .= html_writer::end_tag('ul');
63 $html .= html_writer::end_tag('div');
64 }
65 return $html;
66 }
67
c23c9009
SH
68 /**
69 * This method is used to generate HTML for a subscriber selection form that
70 * uses two user_selector controls
71 *
72 * @param user_selector_base $existinguc
73 * @param user_selector_base $potentialuc
74 * @return string
75 */
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';
5d0c95a5
PS
82 $output .= html_writer::start_tag('form', $formattributes);
83 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
c23c9009
SH
84
85 $existingcell = new html_table_cell();
86 $existingcell->text = $existinguc->display(true);
16be8974 87 $existingcell->attributes['class'] = 'existing';
c23c9009 88 $actioncell = new html_table_cell();
5d0c95a5
PS
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());
16be8974 94 $actioncell->attributes['class'] = 'actions';
c23c9009
SH
95 $potentialcell = new html_table_cell();
96 $potentialcell->text = $potentialuc->display(true);
16be8974 97 $potentialcell->attributes['class'] = 'potential';
c23c9009
SH
98
99 $table = new html_table();
16be8974 100 $table->attributes['class'] = 'subscribertable boxaligncenter';
8cea545e 101 $table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
16be8974 102 $output .= html_writer::table($table);
2f67a9b3 103
5d0c95a5 104 $output .= html_writer::end_tag('form');
c23c9009
SH
105 return $output;
106 }
107
108 /**
109 * This function generates HTML to display a subscriber overview, primarily used on
110 * the subscribers page if editing was turned off
111 *
112 * @param array $users
113 * @param object $forum
114 * @param object $course
115 * @return string
116 */
117 public function subscriber_overview($users, $forum , $course) {
118 $output = '';
dc8f55c3 119 $modinfo = get_fast_modinfo($course);
c23c9009
SH
120 if (!$users || !is_array($users) || count($users)===0) {
121 $output .= $this->output->heading(get_string("nosubscribers", "forum"));
dc8f55c3
MT
122 } else if (!isset($modinfo->instances['forum'][$forum->id])) {
123 $output .= $this->output->heading(get_string("invalidmodule", "error"));
c23c9009 124 } else {
dc8f55c3
MT
125 $cm = $modinfo->instances['forum'][$forum->id];
126 $canviewemail = in_array('email', get_extra_user_fields(context_module::instance($cm->id)));
54cf9439
DNA
127 $strparams = new stdclass();
128 $strparams->name = format_string($forum->name);
129 $strparams->count = count($users);
130 $output .= $this->output->heading(get_string("subscriberstowithcount", "forum", $strparams));
c23c9009
SH
131 $table = new html_table();
132 $table->cellpadding = 5;
133 $table->cellspacing = 5;
134 $table->tablealign = 'center';
135 $table->data = array();
136 foreach ($users as $user) {
dc8f55c3
MT
137 $info = array($this->output->user_picture($user, array('courseid'=>$course->id)), fullname($user));
138 if ($canviewemail) {
139 array_push($info, $user->email);
140 }
141 $table->data[] = $info;
c23c9009 142 }
16be8974 143 $output .= html_writer::table($table);
c23c9009
SH
144 }
145 return $output;
146 }
147
148 /**
149 * This is used to display a control containing all of the subscribed users so that
150 * it can be searched
151 *
152 * @param user_selector_base $existingusers
153 * @return string
154 */
155 public function subscribed_users(user_selector_base $existingusers) {
156 $output = $this->output->box_start('subscriberdiv boxaligncenter');
ebfb73db 157 $output .= html_writer::tag('p', get_string('forcesubscribed', 'forum'));
c23c9009
SH
158 $output .= $existingusers->display(true);
159 $output .= $this->output->box_end();
160 return $output;
161 }
2f67a9b3 162
05f1455b
AO
163 /**
164 * Generate the HTML for an icon to be displayed beside the subject of a timed discussion.
165 *
166 * @param object $discussion
167 * @param bool $visiblenow Indicicates that the discussion is currently
168 * visible to all users.
169 * @return string
170 */
171 public function timed_discussion_tooltip($discussion, $visiblenow) {
172 $dates = array();
173 if ($discussion->timestart) {
174 $dates[] = get_string('displaystart', 'mod_forum').': '.userdate($discussion->timestart);
175 }
176 if ($discussion->timeend) {
177 $dates[] = get_string('displayend', 'mod_forum').': '.userdate($discussion->timeend);
178 }
c23c9009 179
05f1455b
AO
180 $str = $visiblenow ? 'timedvisible' : 'timedhidden';
181 $dates[] = get_string($str, 'mod_forum');
182
183 $tooltip = implode("\n", $dates);
184 return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
185 }
31793839
AN
186
187 /**
188 * Display a forum post in the relevant context.
189 *
190 * @param \mod_forum\output\forum_post $post The post to display.
191 * @return string
192 */
193 public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
ca0227dd 194 $data = $post->export_for_template($this, $this->target === RENDERER_TARGET_TEXTEMAIL);
31793839
AN
195 return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
196 }
197
198 /**
199 * The template name for this renderer.
200 *
201 * @return string
202 */
203 public function forum_post_template() {
204 return 'forum_post';
205 }
c42c2355
AN
206
207 /**
208 * Create the inplace_editable used to select forum digest options.
209 *
210 * @param stdClass $forum The forum to create the editable for.
211 * @param int $value The current value for this user
212 * @return inplace_editable
213 */
214 public function render_digest_options($forum, $value) {
215 $options = forum_get_user_digest_options();
216 $editable = new \core\output\inplace_editable(
217 'mod_forum',
218 'digestoptions',
219 $forum->id,
220 true,
221 $options[$value],
222 $value
223 );
224
225 $editable->set_type_select($options);
226
227 return $editable;
228 }
66bb9ead
FM
229
230 /**
231 * Render quick search form.
232 *
233 * @param \mod_forum\output\quick_search_form $form The renderable.
234 * @return string
235 */
236 public function render_quick_search_form(\mod_forum\output\quick_search_form $form) {
237 return $this->render_from_template('mod_forum/quick_search_form', $form->export_for_template($this));
238 }
16be8974 239}