forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
368 lines (334 loc) · 12.1 KB
/
lib.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?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/>.
/**
* Library of functions and constants for notes
*
* @package core_notes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Constants for states.
*/
define('NOTES_STATE_DRAFT', 'draft');
define('NOTES_STATE_PUBLIC', 'public');
define('NOTES_STATE_SITE', 'site');
/**
* Constants for note parts (flags used by note_print and note_print_list).
*/
define('NOTES_SHOW_FULL', 0x07);
define('NOTES_SHOW_HEAD', 0x02);
define('NOTES_SHOW_BODY', 0x01);
define('NOTES_SHOW_FOOT', 0x04);
/**
* Retrieves a list of note objects with specific atributes.
*
* @param int $courseid id of the course in which the notes were posted (0 means any)
* @param int $userid id of the user to which the notes refer (0 means any)
* @param string $state state of the notes (i.e. draft, public, site) ('' means any)
* @param int $author id of the user who modified the note last time (0 means any)
* @param string $order an order to sort the results in
* @param int $limitfrom number of records to skip (offset)
* @param int $limitnum number of records to fetch
* @return array of note objects
*/
function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
global $DB;
// Setup filters.
$selects = array();
$params = array();
if ($courseid) {
$selects[] = 'courseid=?';
$params[] = $courseid;
}
if ($userid) {
$selects[] = 'userid=?';
$params[] = $userid;
}
if ($author) {
$selects[] = 'usermodified=?';
$params[] = $author;
}
if ($state) {
$selects[] = 'publishstate=?';
$params[] = $state;
}
$selects[] = "module=?";
$params[] = 'notes';
$select = implode(' AND ', $selects);
$fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
}
/**
* Retrieves a note object based on its id.
*
* @param int $noteid ID of the note to retrieve
* @return stdClass object
*/
function note_load($noteid) {
global $DB;
$fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
return $DB->get_record('post', array('id' => $noteid, 'module' => 'notes'), $fields);
}
/**
* Saves a note object. The note object is passed by reference and its fields (i.e. id)
* might change during the save.
*
* @param stdClass $note object to save
* @return boolean true if the object was saved; false otherwise
*/
function note_save(&$note) {
global $USER, $DB;
// Setup & clean fields.
$note->module = 'notes';
$note->lastmodified = time();
$note->usermodified = $USER->id;
if (empty($note->format)) {
$note->format = FORMAT_PLAIN;
}
if (empty($note->publishstate)) {
$note->publishstate = NOTES_STATE_PUBLIC;
}
// Save data.
if (empty($note->id)) {
// Insert new note.
$note->created = $note->lastmodified;
$id = $DB->insert_record('post', $note);
$note = note_load($id);
// Trigger event.
$event = \core\event\note_created::create(array(
'objectid' => $note->id,
'courseid' => $note->courseid,
'relateduserid' => $note->userid,
'userid' => $note->usermodified,
'context' => context_course::instance($note->courseid),
'other' => array('publishstate' => $note->publishstate)
));
$event->trigger();
} else {
// Update old note.
$DB->update_record('post', $note);
$note = note_load($note->id);
// Trigger event.
$event = \core\event\note_updated::create(array(
'objectid' => $note->id,
'courseid' => $note->courseid,
'relateduserid' => $note->userid,
'userid' => $note->usermodified,
'context' => context_course::instance($note->courseid),
'other' => array('publishstate' => $note->publishstate)
));
$event->trigger();
}
unset($note->module);
return true;
}
/**
* Deletes a note object based on its id.
*
* @param int|object $note id of the note to delete, or a note object which is to be deleted.
* @return boolean true if the object was deleted; false otherwise
*/
function note_delete($note) {
global $DB;
if (is_int($note)) {
$noteid = $note;
} else {
$noteid = $note->id;
}
// Get the full record, note_load doesn't return everything.
$note = $DB->get_record('post', array('id' => $noteid), '*', MUST_EXIST);
$return = $DB->delete_records('post', array('id' => $note->id, 'module' => 'notes'));
// Trigger event.
$event = \core\event\note_deleted::create(array(
'objectid' => $note->id,
'courseid' => $note->courseid,
'relateduserid' => $note->userid,
'userid' => $note->usermodified,
'context' => context_course::instance($note->courseid),
'other' => array('publishstate' => $note->publishstate)
));
$event->add_record_snapshot('post', $note);
$event->trigger();
return $return;
}
/**
* Converts a state value to its corespondent name
*
* @param string $state state value to convert
* @return string corespondent state name
*/
function note_get_state_name($state) {
// Cache state names.
static $states;
if (empty($states)) {
$states = note_get_state_names();
}
if (isset($states[$state])) {
return $states[$state];
} else {
return null;
}
}
/**
* Returns an array of mappings from state values to state names
*
* @return array of mappings
*/
function note_get_state_names() {
return array(
NOTES_STATE_DRAFT => get_string('personal', 'notes'),
NOTES_STATE_PUBLIC => get_string('course', 'notes'),
NOTES_STATE_SITE => get_string('site', 'notes'),
);
}
/**
* Prints a note object
*
* @param note $note the note object to print
* @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
*/
function note_print($note, $detail = NOTES_SHOW_FULL) {
global $CFG, $USER, $DB, $OUTPUT;
if (!$user = $DB->get_record('user', array('id' => $note->userid))) {
debugging("User $note->userid not found");
return;
}
if (!$author = $DB->get_record('user', array('id' => $note->usermodified))) {
debugging("User $note->usermodified not found");
return;
}
$context = context_course::instance($note->courseid);
$systemcontext = context_system::instance();
$authoring = new stdClass();
$authoring->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $author->id .
'&course='.$note->courseid . '">' . fullname($author) . '</a>';
$authoring->date = userdate($note->lastmodified);
echo '<div class="notepost '. $note->publishstate . 'notepost' .
($note->usermodified == $USER->id ? ' ownnotepost' : '') .
'" id="note-' . $note->id . '">';
// Print note head (e.g. author, user refering to, etc).
if ($detail & NOTES_SHOW_HEAD) {
echo '<div class="header">';
echo '<div class="user">';
echo $OUTPUT->user_picture($user, array('courseid' => $note->courseid));
echo fullname($user) . '</div>';
echo '<div class="info">' .
get_string('bynameondate', 'notes', $authoring) .
' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
echo '</div>';
}
// Print note content.
if ($detail & NOTES_SHOW_BODY) {
echo '<div class="content">';
echo format_text($note->content, $note->format, array('overflowdiv' => true));
echo '</div>';
}
// Print note options (e.g. delete, edit).
if ($detail & NOTES_SHOW_FOOT) {
if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate == NOTES_STATE_SITE ||
has_capability('moodle/notes:manage', $context) &&
($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
echo '<div class="footer"><p>';
echo '<a href="' . $CFG->wwwroot . '/notes/edit.php?id=' . $note->id. '">' . get_string('edit') . '</a> | ';
echo '<a href="' . $CFG->wwwroot . '/notes/delete.php?id=' . $note->id. '">' . get_string('delete') . '</a>';
echo '</p></div>';
}
}
echo '</div>';
}
/**
* Prints a list of note objects
*
* @param array $notes array of note objects to print
* @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
*/
function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
echo '<div class="notelist">';
foreach ($notes as $note) {
note_print($note, $detail);
}
echo '</div>';
}
/**
* Retrieves and prints a list of note objects with specific atributes.
*
* @param string $header HTML to print above the list
* @param int $addcourseid id of the course for the add notes link (0 hide link)
* @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
* @param int $courseid id of the course in which the notes were posted (0 means any)
* @param int $userid id of the user to which the notes refer (0 means any)
* @param string $state state of the notes (i.e. draft, public, site) ('' means any)
* @param int $author id of the user who modified the note last time (0 means any)
*/
function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
global $CFG;
if ($header) {
echo '<h3 class="notestitle">' . $header . '</h3>';
echo '<div class="notesgroup">';
}
if ($addcourseid) {
if ($userid) {
echo '<p><a href="' . $CFG->wwwroot . '/notes/edit.php?courseid=' . $addcourseid . '&userid=' . $userid .
'&publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
} else {
echo '<p><a href="' . $CFG->wwwroot . '/user/index.php?id=' . $addcourseid. '">' .
get_string('addnewnoteselect', 'notes') . '</a></p>';
}
}
if ($viewnotes) {
$notes = note_list($courseid, $userid, $state, $author);
if ($notes) {
note_print_list($notes);
}
} else {
echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
}
if ($header) {
echo '</div>'; // The notesgroup div.
}
}
/**
* Delete all notes about users in course-
* @param int $courseid
* @return bool success
*/
function note_delete_all($courseid) {
global $DB;
return $DB->delete_records('post', array('module' => 'notes', 'courseid' => $courseid));
}
/**
* Return a list of page types
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
return array('notes-*' => get_string('page-notes-x', 'notes'));
}
/**
* Trigger notes viewed event
*
* @param stdClass $context context object
* @param int $userid user id (the user we are viewing the notes)
* @since Moodle 2.9
*/
function note_view($context, $userid) {
$event = \core\event\notes_viewed::create(array(
'relateduserid' => $userid,
'context' => $context
));
$event->trigger();
}