-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.php
229 lines (198 loc) · 7.92 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Renderer for local_helloworld
*
*
* @package local_helloworld
* @copyright 2020 Jakub Kleban <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Renderer for displaying local_helloworld as HTML
*
* @package local_helloworld
* @copyright 2020 Jakub Kleban <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_helloworld_renderer extends plugin_renderer_base {
/**
* Generates html to display script
*
* @param string $url URL of index.php
* @return string
*/
public function display_script($url) {
$output = '';
$context = context_system::instance();
if (has_capability('local/helloworld:postmessages', $context)) {
$output .= $this->message_input($url);
}
if (has_capability('local/helloworld:viewmessages', $context)) {
$output .= $this->display_messages($url);
}
$output .= $this->footer($url);
return $output;
}
/**
* Generate html to make textarea message form
*
* @param string $url URL of index.php
* @return string Form for message-textarea
*/
private function message_input($url) {
$output = '';
$output .= html_writer::start_tag('form', array(
'method' => 'post',
'action' => $url
));
$output .= html_writer::start_div('form-group');
$output .= html_writer::label(get_string('typemessage', 'local_helloworld'), 'messagetextarea');
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden',
'name' => 'sesskey',
'value' => sesskey()
));
$output .= html_writer::tag('textarea', '', array(
'class' => 'form-control',
'id' => 'messagetextarea',
'rows' => 5,
'name' => 'message'
));
$output .= html_writer::end_div();
$output .= html_writer::tag('button', get_string('messageselectadd', 'core_moodle') , array(
'type' => 'submit',
'class' => 'btn btn-primary rounded'
));
$output .= html_writer::end_tag('form');
return $output;
}
/**
* Generate html to display messages
*
* @param string $url URL of index.php
* @return string html for displaying messages
*/
private function display_messages(string $url) {
global $DB;
$output = '';
$output .= $this->make_break();
$output .= html_writer::start_div('card-columns');
$userfields = get_all_user_name_fields(true, 'u');
$sql = "SELECT m.id, m.message, m.timecreated, u.id AS userid, $userfields
FROM {local_helloworld_messages} m
LEFT JOIN {user} u ON u.id = m.userid
ORDER BY timemodified DESC";
$messages = $DB->get_records_sql($sql);
foreach ($messages as $message) {
$now = new DateTime('now', core_date::get_server_timezone_object());
$timediff = $now->getTimestamp() - $message->timecreated;
$formatedtime = format_time($timediff);
$lastupdated = get_string('lastupdated', 'local_helloworld', $formatedtime);
$id = $message->id;
$output .= $this->display_message($url, $id, fullname($message) , $message->message, $lastupdated);
}
$output .= html_writer::end_div();
return $output;
}
/**
* Generate html to display one message
*
* @param string $url URL of index.php
* @param int $id Id of message
* @param string $fullname Full name of author
* @param string $message Content of the message
* @param string $lastupdated Formated text displaying how long ago was the message created
* @return string html for displaying one message
*/
private function display_message(string $url, int $id, string $fullname, string $message, string $lastupdated) {
$output = '';
$context = context_system::instance();
$output .= html_writer::start_div('card');
$output .= html_writer::start_div('card-body');
$output .= html_writer::start_tag('h5', array(
'class' => 'card-title d-flex justify-content-between'
));
$output .= $fullname;
if (has_capability('local/helloworld:deleteanymessage', $context)) {
$output .= html_writer::start_tag('form', array(
'method' => 'post',
'action' => $url
));
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden',
'name' => 'sesskey',
'value' => sesskey()
));
$output .= html_writer::start_tag('button', array(
'type' => 'submit',
'class' => 'btn btn-danger rounded',
'name' => 'deleteid',
'value' => $id
));
$output .= html_writer::tag('i', '', array(
'class' => 'fa fa-trash',
'aria-hidden' => 'true'
));
$output .= html_writer::end_tag('button');
$output .= html_writer::end_tag('form');
}
$output .= html_writer::end_tag('h5');
$output .= html_writer::tag('p', $message, array(
'class' => 'card-text'
));
$output .= html_writer::start_tag('p', array(
'class' => 'card-text'
));
$output .= html_writer::tag('small', $lastupdated , array('class' => 'text-muted'));
$output .= html_writer::end_tag('p');
$output .= html_writer::end_div();
$output .= html_writer::end_div();
return $output;
}
/**
* Generate html for footer that contain links
*
* @param string $url URL of index.php
* @return string html for footer
*/
private function footer($url) {
$output = '';
$output .= $this->make_break();
$output .= html_writer::start_tag('ul');
$output .= html_writer::start_tag('li');
$output .= html_writer::link(new moodle_url('/'), get_string('backtohome', 'core_moodle'));
$output .= html_writer::end_tag('li');
$output .= html_writer::start_tag('li');
$output .= html_writer::link($url, get_string('backtopageyouwereon', 'core_moodle'));
$output .= html_writer::end_tag('li');
$output .= html_writer::end_tag('ul');
return $output;
}
/**
* Generate html to make a brake between two HTML elements
*
* @return string html with break-lines and horizontal-line
*/
private function make_break() {
$output = '';
$output .= html_writer::empty_tag('br');
$output .= html_writer::empty_tag('hr');
$output .= html_writer::empty_tag('br');
return $output;
}
}