-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
181 lines (135 loc) · 6.17 KB
/
index.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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Plugin strings are defined here.
*
* @package local_greetings
* @category string
* @copyright JcSomeShots <juancarlo.castillo20@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot. '/local/greetings/lib.php');
require_once($CFG->dirroot. '/local/greetings/message_form.php');
// Rrequire_once($CFG->dirroot. '/local/greetings/message_accessible_form.php');.
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/greetings/index.php'));
$PAGE->set_pagelayout('standard');
$PAGE->set_title($SITE->fullname);
$PAGE->set_heading(get_string('pluginname', 'local_greetings'));
require_login();
if (isguestuser()) {
throw new moodle_exception('noguest');
}
// Add capability check in your plugin, allow to post a message.
$allowpost = has_capability('local/greetings:postmessages', $context);
// Add capability in your plugin, to delete their own post.
$deletepost = has_capability('local/greetings:deleteownmessage', $context);
// Add capability in your plugin, to delete any post.
$deleteanypost = has_capability('local/greetings:deleteanymessages', $context);
// The real action to delete a message.
$action = optional_param('action', '', PARAM_TEXT);
if ($action == 'del') {
// Adding sesskey protection.
require_sesskey();
$id = required_param('id', PARAM_TEXT);
// Only proceed with deleting the message if the user has be required permission.
if ($deleteanypost || $deletepost ) {
$params = array('id' => $id);
// Users without permission should only delete their own post.
// In the original code o referent code there is putting in a negative way !$deleteanypost. But I can run it like this.
if($deleteanypost) {
$params += ['userid' => $USER->id];
}
// TO DO: Confirm before deleting.
$DB->delete_records('local_greetings_messages', $params);
redirect($PAGE->url);
}
}
// Invoke a personal form.
$messageform = new local_greetings_message_form();
// Invoke a personal accessible form.
// A $messageformaccessible = new local_greetings_accessible_message_form();.
if ($data = $messageform->get_data()) {
require_capability('local/greetings:postmessages', $context);
$message = required_param('message', PARAM_TEXT);
if (!empty($message)) {
$record = new stdClass;
$record->message = $message;
$record->timecreated = time();
$record->userid = $USER->id;
$DB->insert_record('local_greetings_messages', $record);
}
}
echo $OUTPUT->header();
// Say hello to a user.
if (isloggedin()) {
echo local_greetings_get_greeting($USER);
} else {
echo get_string('greetinguser', 'local_greetings');
}
// Check if the user has the relevant permission.
if ($allowpost) {
$messageform->display();
}
if (has_capability('local/greetings:viewmessages', $context)) {
$userfields = \core_user\fields::for_name()->with_identity($context);
$userfieldssql = $userfields->get_sql('u');
// To check which user make a post.
$sql = "SELECT m.id, m.message, m.timecreated, m.userid {$userfieldssql->selects}
FROM {local_greetings_messages} m
LEFT JOIN {user} u ON u.id = m.userid
ORDER BY timecreated DESC";
$messages = $DB->get_records_sql($sql);
echo $OUTPUT->box_start('card-columns');
foreach ($messages as $m) {
echo html_writer::start_tag('div', array('class' => 'card'));
echo html_writer::start_tag('div', array('class' => 'card-body'));
echo html_writer::tag('p', format_text($m->message, FORMAT_PLAIN), array('class' => 'card-text'));
echo html_writer::tag('p', get_string('postedby', 'local_greetings', $m->firstname), array('class' => 'card-text'));
echo html_writer::start_tag('p', array('class' => 'card-text'));
echo html_writer::tag('small', userdate($m->timecreated), array('class' => 'text-muted'));
echo html_writer::end_tag('p');
if ( $deleteanypost || ($deletepost && $m->userid == $USER->id)) {
echo html_writer::start_tag('p', array('class' => 'card-footer text-center'));
echo html_writer::link(
new moodle_url(
'/local/greetings/index.php',
array('action' => 'del', 'id' => $m->id , 'sesskey' => sesskey())
),
// The Output line is commented cause that show icon and text in not accessebility way.
// O$OUTPUT->pix_icon('t/delete', '') . get_string('delete').
$OUTPUT->pix_icon('t/delete', ''), array('role' => 'button', 'aria-label' => get_string('delete'), 'title' => get_string('delete'))
);
// Who to include an Icon?
// echo html_writer::link(
// new moodle_url(
// '/local/greetings/index.php',
// array('action' => 'update', 'id' => $m->id , 'sesskey' => sesskey())
// ),
// Create a update icon.
// $OUTPUT->pix_icon('t/edit', '') . get_string('Edit')
// $OUTPUT->pix_icon('edit', ''), array('role' => 'button', 'aria-label' => get_string('Edit'), 'title' => get_string('Edit'))
// );
echo html_writer::end_tag('p');
}
echo html_writer::end_tag('div');
echo html_writer::end_tag('div');
}
echo $OUTPUT->box_end();
}
echo $OUTPUT->footer();