2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Library of functions and constants for notes
21 * @copyright 2007 onwards Yu Zhang
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Constants for states.
28 define('NOTES_STATE_DRAFT', 'draft');
29 define('NOTES_STATE_PUBLIC', 'public');
30 define('NOTES_STATE_SITE', 'site');
33 * Constants for note parts (flags used by note_print and note_print_list).
35 define('NOTES_SHOW_FULL', 0x07);
36 define('NOTES_SHOW_HEAD', 0x02);
37 define('NOTES_SHOW_BODY', 0x01);
38 define('NOTES_SHOW_FOOT', 0x04);
41 * Retrieves a list of note objects with specific atributes.
43 * @param int $courseid id of the course in which the notes were posted (0 means any)
44 * @param int $userid id of the user to which the notes refer (0 means any)
45 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
46 * @param int $author id of the user who modified the note last time (0 means any)
47 * @param string $order an order to sort the results in
48 * @param int $limitfrom number of records to skip (offset)
49 * @param int $limitnum number of records to fetch
50 * @return array of note objects
52 function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
59 $selects[] = 'courseid=?';
60 $params[] = $courseid;
63 $selects[] = 'userid=?';
67 $selects[] = 'usermodified=?';
71 $selects[] = 'publishstate=?';
74 $selects[] = "module=?";
77 $select = implode(' AND ', $selects);
78 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
80 return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
84 * Retrieves a note object based on its id.
86 * @param int $noteid ID of the note to retrieve
87 * @return stdClass object
89 function note_load($noteid) {
92 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
93 return $DB->get_record('post', array('id' => $noteid, 'module' => 'notes'), $fields);
97 * Saves a note object. The note object is passed by reference and its fields (i.e. id)
98 * might change during the save.
100 * @param stdClass $note object to save
101 * @return boolean true if the object was saved; false otherwise
103 function note_save(&$note) {
106 // Setup & clean fields.
107 $note->module
= 'notes';
108 $note->lastmodified
= time();
109 $note->usermodified
= $USER->id
;
110 if (empty($note->format
)) {
111 $note->format
= FORMAT_PLAIN
;
113 if (empty($note->publishstate
)) {
114 $note->publishstate
= NOTES_STATE_PUBLIC
;
117 if (empty(trim($note->content
))) {
118 // Don't save empty notes.
122 if (empty($note->id
)) {
124 $note->created
= $note->lastmodified
;
125 $id = $DB->insert_record('post', $note);
126 $note = note_load($id);
129 $event = \core\event\note_created
::create(array(
130 'objectid' => $note->id
,
131 'courseid' => $note->courseid
,
132 'relateduserid' => $note->userid
,
133 'userid' => $note->usermodified
,
134 'context' => context_course
::instance($note->courseid
),
135 'other' => array('publishstate' => $note->publishstate
)
140 $DB->update_record('post', $note);
141 $note = note_load($note->id
);
144 $event = \core\event\note_updated
::create(array(
145 'objectid' => $note->id
,
146 'courseid' => $note->courseid
,
147 'relateduserid' => $note->userid
,
148 'userid' => $note->usermodified
,
149 'context' => context_course
::instance($note->courseid
),
150 'other' => array('publishstate' => $note->publishstate
)
154 unset($note->module
);
159 * Deletes a note object based on its id.
161 * @param int|object $note id of the note to delete, or a note object which is to be deleted.
162 * @return boolean true always
164 function note_delete($note) {
171 // Get the full record, note_load doesn't return everything.
172 $note = $DB->get_record('post', array('id' => $noteid), '*', MUST_EXIST
);
173 $return = $DB->delete_records('post', array('id' => $note->id
, 'module' => 'notes'));
176 $event = \core\event\note_deleted
::create(array(
177 'objectid' => $note->id
,
178 'courseid' => $note->courseid
,
179 'relateduserid' => $note->userid
,
180 'userid' => $note->usermodified
,
181 'context' => context_course
::instance($note->courseid
),
182 'other' => array('publishstate' => $note->publishstate
)
184 $event->add_record_snapshot('post', $note);
191 * Converts a state value to its corespondent name
193 * @param string $state state value to convert
194 * @return string corespondent state name
196 function note_get_state_name($state) {
197 // Cache state names.
199 if (empty($states)) {
200 $states = note_get_state_names();
202 if (isset($states[$state])) {
203 return $states[$state];
210 * Returns an array of mappings from state values to state names
212 * @return array of mappings
214 function note_get_state_names() {
216 NOTES_STATE_DRAFT
=> get_string('personal', 'notes'),
217 NOTES_STATE_PUBLIC
=> get_string('course', 'notes'),
218 NOTES_STATE_SITE
=> get_string('site', 'notes'),
223 * Prints a note object
225 * @param note $note the note object to print
226 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
228 function note_print($note, $detail = NOTES_SHOW_FULL
) {
229 global $CFG, $USER, $DB, $OUTPUT;
231 if (!$user = $DB->get_record('user', array('id' => $note->userid
))) {
232 debugging("User $note->userid not found");
235 if (!$author = $DB->get_record('user', array('id' => $note->usermodified
))) {
236 debugging("User $note->usermodified not found");
239 $context = context_course
::instance($note->courseid
);
240 $systemcontext = context_system
::instance();
242 $authoring = new stdClass();
243 $authoring->name
= '<a href="' . $CFG->wwwroot
. '/user/view.php?id=' . $author->id
.
244 '&course='.$note->courseid
. '">' . fullname($author) . '</a>';
245 $authoring->date
= userdate($note->lastmodified
);
247 echo '<div class="notepost '. $note->publishstate
. 'notepost' .
248 ($note->usermodified
== $USER->id ?
' ownnotepost' : '') .
249 '" id="note-' . $note->id
. '">';
251 // Print note head (e.g. author, user refering to, etc).
252 if ($detail & NOTES_SHOW_HEAD
) {
253 echo '<div class="header">';
254 echo '<div class="user">';
255 echo $OUTPUT->user_picture($user, array('courseid' => $note->courseid
));
256 echo fullname($user) . '</div>';
257 echo '<div class="info">' .
258 get_string('bynameondate', 'notes', $authoring) .
259 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created
) . ')</div>';
263 // Print note content.
264 if ($detail & NOTES_SHOW_BODY
) {
265 echo '<div class="content">';
266 echo format_text($note->content
, $note->format
, array('overflowdiv' => true));
270 // Print note options (e.g. delete, edit).
271 if ($detail & NOTES_SHOW_FOOT
) {
272 if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate
== NOTES_STATE_SITE ||
273 has_capability('moodle/notes:manage', $context) &&
274 ($note->publishstate
== NOTES_STATE_PUBLIC ||
$note->usermodified
== $USER->id
)) {
275 echo '<div class="footer"><p>';
276 echo '<a href="' . $CFG->wwwroot
. '/notes/edit.php?id=' . $note->id
. '">' . get_string('edit') . '</a> | ';
277 echo '<a href="' . $CFG->wwwroot
. '/notes/delete.php?id=' . $note->id
. '">' . get_string('delete') . '</a>';
285 * Prints a list of note objects
287 * @param array $notes array of note objects to print
288 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
290 function note_print_list($notes, $detail = NOTES_SHOW_FULL
) {
292 echo '<div class="notelist">';
293 foreach ($notes as $note) {
294 note_print($note, $detail);
300 * Retrieves and prints a list of note objects with specific atributes.
302 * @param string $header HTML to print above the list
303 * @param int $addcourseid id of the course for the add notes link (0 hide link)
304 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
305 * @param int $courseid id of the course in which the notes were posted (0 means any)
306 * @param int $userid id of the user to which the notes refer (0 means any)
307 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
308 * @param int $author id of the user who modified the note last time (0 means any)
310 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
314 echo '<h3 class="notestitle">' . $header . '</h3>';
315 echo '<div class="notesgroup">';
319 echo '<p><a href="' . $CFG->wwwroot
. '/notes/edit.php?courseid=' . $addcourseid . '&userid=' . $userid .
320 '&publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
322 echo '<p><a href="' . $CFG->wwwroot
. '/user/index.php?id=' . $addcourseid. '">' .
323 get_string('addnewnoteselect', 'notes') . '</a></p>';
327 $notes = note_list($courseid, $userid, $state, $author);
329 note_print_list($notes);
332 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
335 echo '</div>'; // The notesgroup div.
340 * Delete all notes about users in course-
341 * @param int $courseid
342 * @return bool success
344 function note_delete_all($courseid) {
347 return $DB->delete_records('post', array('module' => 'notes', 'courseid' => $courseid));
351 * Return a list of page types
352 * @param string $pagetype current page type
353 * @param stdClass $parentcontext Block's parent context
354 * @param stdClass $currentcontext Current context of block
356 function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
357 return array('notes-*' => get_string('page-notes-x', 'notes'));
361 * Trigger notes viewed event
363 * @param stdClass $context context object
364 * @param int $userid user id (the user we are viewing the notes)
367 function note_view($context, $userid) {
369 $event = \core\event\notes_viewed
::create(array(
370 'relateduserid' => $userid,
371 'context' => $context
377 * Add nodes to myprofile page.
379 * @param \core_user\output\myprofile\tree $tree Tree object
380 * @param stdClass $user user object
381 * @param bool $iscurrentuser
382 * @param stdClass $course Course object
386 function core_notes_myprofile_navigation(core_user\output\myprofile\tree
$tree, $user, $iscurrentuser, $course) {
389 if (empty($CFG->enablenotes
)) {
390 // Notes are disabled, nothing to do.
394 if (isguestuser($user)) {
395 // No notes for guest users.
399 $url = new moodle_url("/notes/index.php", array('user' => $user->id
));
400 $title = get_string('notes', 'core_notes');
401 if (empty($course)) {
402 // Site level profile.
403 if (!has_capability('moodle/notes:view', context_system
::instance())) {
404 // No cap, nothing to do.
408 if (!has_capability('moodle/notes:view', context_course
::instance($course->id
))) {
409 // No cap, nothing to do.
412 $url->param('course', $course->id
);
414 $notesnode = new core_user\output\myprofile\node
('miscellaneous', 'notes', $title, null, $url);
415 $tree->add_node($notesnode);