-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
174 lines (140 loc) · 7.5 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
<?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/>;.
/**
* @package local_greetings
* @copyright 2023 Nawar Shabook <nawarshabook@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Most php files that can be directly accessed
// begin with requiring the config.php file,
// located in the root of the Moodle folder.
require_once('../../config.php');
require_once($CFG->dirroot. '/local/greetings/lib.php');
require_once($CFG->dirroot. '/local/greetings/message_form.php');
$context = context_system::instance();
$PAGE->set_context($context);
$PAGE->set_url(new moodle_url('/local/greetings/index.php'));
$PAGE->set_pagelayout('standard');
// We are making use of global variable $SITE
// to access details about the Moodle site.
$PAGE->set_title($SITE->fullname);
$PAGE->set_heading(get_string('pluginname', 'local_greetings'));
//command bellow for require login, that's mean if the guest user try
// to access to greetings plugin he will is redirected to login page
require_login();
//code bellow for prevent guest users from accessing the greetings plugin
if (isguestuser()) {
throw new moodle_exception('noguest');
}
//capability section of code
//The code bellow checks whether or not a user has the capability to make,view,delete a post.
$allowpost = has_capability('local/greetings:postmessages', $context);
$allowview = has_capability('local/greetings:viewmessages', $context);
//delete capability and db for it
$deleteanypost = has_capability('local/greetings:deleteanymessage', $context);
$action = optional_param('action', '', PARAM_TEXT);
if ($action == 'del') {
require_sesskey();
$id = required_param('id', PARAM_TEXT);
if ($deleteanypost) {
$params = array('id' => $id);
$DB->delete_records('local_greetings_messages', $params);
}
}
$messageform = new local_greetings_message_form();
//store message in database
// dont worry if you dont understand something, bellow of code there is an explain for it
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);
}
}
/**
* Here is a breakdown of what the code above is doing:
*The $data variable is assigned the result of calling the get_data() method on an object called $messageform. This method is likely to be part of a form class that is used to handle form submissions and extract the form data.
*The required_param() function is called with the 'message' parameter and the PARAM_TEXT constant as arguments. This function is used to retrieve a required parameter from the current request. In this case, the 'message' parameter is likely to be a value that was submitted through the form. The PARAM_TEXT constant is used to specify that the 'message' parameter is expected to be a plain text value.
*An if statement is used to check if the $message variable is empty. If the $message variable is not empty, the code inside the if statement will be executed.
*A new instance of the stdClass class is created and stored in the $record variable. The stdClass class is a basic PHP class that can be used to create objects with arbitrary properties.
*The message and timecreated properties of the $record object are set to the $message variable and the current time, respectively.
*The insert_record() method of the $DB object is called with the 'local_greetings_messages' table name and the $record object as arguments. This method is likely to be part of a database class that is used to insert records into the Moodle database.
*Overall, this code appears to be part of a form handler that is used to process a form submission, extract the 'message' parameter, and insert it into the local_greetings_messages table in the Moodle database.
*/
//header
echo $OUTPUT->header();
echo (isloggedin())?'<h2>Greetings, ' . fullname($USER) . '</h2>': '<h2>Greetings, user</h2>';
echo (isloggedin())? get_string('greetingloggedinuser', 'local_greetings', fullname($USER)):
get_string('greetinguser', 'local_greetings');
echo '<br>'. local_greetings_get_greeting($USER);
//bellow for check if the user has the relevant permission.
if ($allowpost) {
$messageform->display(); //for display form
}
//This line bellow fetches all the greeting messages from the table local_greetings_message.
// $messages = $DB->get_records('local_greetings_messages');
if($allowview){
$userfields = \core_user\fields::for_name()->with_identity($context);
$userfieldssql = $userfields->get_sql('u');
$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');
//We will use Bootstrap 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'));
//userdate() is core function, which is part of the Time API is used to convert the timestamp into a human readable value.
echo html_writer::tag('small', userdate($m->timecreated), array('class' => 'text-muted'));
//check if the user has delete capability to display delete button to him
if ($deleteanypost) {
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())
),
$OUTPUT->pix_icon('t/delete', '') . get_string('delete')
);
echo html_writer::end_tag('p');
}
echo html_writer::end_tag('p');
echo html_writer::end_tag('div');
echo html_writer::end_tag('div');
}
}
echo $OUTPUT->box_end();
/**
* dsiplay the massege
* this section bellow for dsiplay the massege on screen without store it in db
* bellow for check what data has been submitted by the form.
* if ($data = $messageform->get_data()) {
* //var_dump($data); //simple way for test what we are doing
* $message = required_param('message', PARAM_TEXT);
* echo $OUTPUT->heading($message, 4);
*}
*/
//footer
echo $OUTPUT->footer();