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($note->id
)) {
119 $note->created
= $note->lastmodified
;
120 $id = $DB->insert_record('post', $note);
121 $note = note_load($id);
124 $event = \core\event\note_created
::create(array(
125 'objectid' => $note->id
,
126 'courseid' => $note->courseid
,
127 'relateduserid' => $note->userid
,
128 'userid' => $note->usermodified
,
129 'context' => context_course
::instance($note->courseid
),
130 'other' => array('publishstate' => $note->publishstate
)
135 $DB->update_record('post', $note);
136 $note = note_load($note->id
);
139 $event = \core\event\note_updated
::create(array(
140 'objectid' => $note->id
,
141 'courseid' => $note->courseid
,
142 'relateduserid' => $note->userid
,
143 'userid' => $note->usermodified
,
144 'context' => context_course
::instance($note->courseid
),
145 'other' => array('publishstate' => $note->publishstate
)
149 unset($note->module
);
154 * Deletes a note object based on its id.
156 * @param int|object $note id of the note to delete, or a note object which is to be deleted.
157 * @return boolean true always
159 function note_delete($note) {
166 // Get the full record, note_load doesn't return everything.
167 $note = $DB->get_record('post', array('id' => $noteid), '*', MUST_EXIST
);
168 $return = $DB->delete_records('post', array('id' => $note->id
, 'module' => 'notes'));
171 $event = \core\event\note_deleted
::create(array(
172 'objectid' => $note->id
,
173 'courseid' => $note->courseid
,
174 'relateduserid' => $note->userid
,
175 'userid' => $note->usermodified
,
176 'context' => context_course
::instance($note->courseid
),
177 'other' => array('publishstate' => $note->publishstate
)
179 $event->add_record_snapshot('post', $note);
186 * Converts a state value to its corespondent name
188 * @param string $state state value to convert
189 * @return string corespondent state name
191 function note_get_state_name($state) {
192 // Cache state names.
194 if (empty($states)) {
195 $states = note_get_state_names();
197 if (isset($states[$state])) {
198 return $states[$state];
205 * Returns an array of mappings from state values to state names
207 * @return array of mappings
209 function note_get_state_names() {
211 NOTES_STATE_DRAFT
=> get_string('personal', 'notes'),
212 NOTES_STATE_PUBLIC
=> get_string('course', 'notes'),
213 NOTES_STATE_SITE
=> get_string('site', 'notes'),
218 * Prints a note object
220 * @param note $note the note object to print
221 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
223 function note_print($note, $detail = NOTES_SHOW_FULL
) {
224 global $CFG, $USER, $DB, $OUTPUT;
226 if (!$user = $DB->get_record('user', array('id' => $note->userid
))) {
227 debugging("User $note->userid not found");
230 if (!$author = $DB->get_record('user', array('id' => $note->usermodified
))) {
231 debugging("User $note->usermodified not found");
234 $context = context_course
::instance($note->courseid
);
235 $systemcontext = context_system
::instance();
237 $authoring = new stdClass();
238 $authoring->name
= '<a href="' . $CFG->wwwroot
. '/user/view.php?id=' . $author->id
.
239 '&course='.$note->courseid
. '">' . fullname($author) . '</a>';
240 $authoring->date
= userdate($note->lastmodified
);
242 echo '<div class="notepost '. $note->publishstate
. 'notepost' .
243 ($note->usermodified
== $USER->id ?
' ownnotepost' : '') .
244 '" id="note-' . $note->id
. '">';
246 // Print note head (e.g. author, user refering to, etc).
247 if ($detail & NOTES_SHOW_HEAD
) {
248 echo '<div class="header">';
249 echo '<div class="user">';
250 echo $OUTPUT->user_picture($user, array('courseid' => $note->courseid
));
251 echo fullname($user) . '</div>';
252 echo '<div class="info">' .
253 get_string('bynameondate', 'notes', $authoring) .
254 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created
) . ')</div>';
258 // Print note content.
259 if ($detail & NOTES_SHOW_BODY
) {
260 echo '<div class="content">';
261 echo format_text($note->content
, $note->format
, array('overflowdiv' => true));
265 // Print note options (e.g. delete, edit).
266 if ($detail & NOTES_SHOW_FOOT
) {
267 if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate
== NOTES_STATE_SITE ||
268 has_capability('moodle/notes:manage', $context) &&
269 ($note->publishstate
== NOTES_STATE_PUBLIC ||
$note->usermodified
== $USER->id
)) {
270 echo '<div class="footer"><p>';
271 echo '<a href="' . $CFG->wwwroot
. '/notes/edit.php?id=' . $note->id
. '">' . get_string('edit') . '</a> | ';
272 echo '<a href="' . $CFG->wwwroot
. '/notes/delete.php?id=' . $note->id
. '">' . get_string('delete') . '</a>';
280 * Prints a list of note objects
282 * @param array $notes array of note objects to print
283 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
285 function note_print_list($notes, $detail = NOTES_SHOW_FULL
) {
287 echo '<div class="notelist">';
288 foreach ($notes as $note) {
289 note_print($note, $detail);
295 * Retrieves and prints a list of note objects with specific atributes.
297 * @param string $header HTML to print above the list
298 * @param int $addcourseid id of the course for the add notes link (0 hide link)
299 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
300 * @param int $courseid id of the course in which the notes were posted (0 means any)
301 * @param int $userid id of the user to which the notes refer (0 means any)
302 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
303 * @param int $author id of the user who modified the note last time (0 means any)
305 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
309 echo '<h3 class="notestitle">' . $header . '</h3>';
310 echo '<div class="notesgroup">';
314 echo '<p><a href="' . $CFG->wwwroot
. '/notes/edit.php?courseid=' . $addcourseid . '&userid=' . $userid .
315 '&publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
317 echo '<p><a href="' . $CFG->wwwroot
. '/user/index.php?id=' . $addcourseid. '">' .
318 get_string('addnewnoteselect', 'notes') . '</a></p>';
322 $notes = note_list($courseid, $userid, $state, $author);
324 note_print_list($notes);
327 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
330 echo '</div>'; // The notesgroup div.
335 * Delete all notes about users in course-
336 * @param int $courseid
337 * @return bool success
339 function note_delete_all($courseid) {
342 return $DB->delete_records('post', array('module' => 'notes', 'courseid' => $courseid));
346 * Return a list of page types
347 * @param string $pagetype current page type
348 * @param stdClass $parentcontext Block's parent context
349 * @param stdClass $currentcontext Current context of block
351 function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
352 return array('notes-*' => get_string('page-notes-x', 'notes'));
356 * Trigger notes viewed event
358 * @param stdClass $context context object
359 * @param int $userid user id (the user we are viewing the notes)
362 function note_view($context, $userid) {
364 $event = \core\event\notes_viewed
::create(array(
365 'relateduserid' => $userid,
366 'context' => $context
372 * Add nodes to myprofile page.
374 * @param \core_user\output\myprofile\tree $tree Tree object
375 * @param stdClass $user user object
376 * @param bool $iscurrentuser
377 * @param stdClass $course Course object
381 function core_notes_myprofile_navigation(core_user\output\myprofile\tree
$tree, $user, $iscurrentuser, $course) {
384 if (empty($CFG->enablenotes
)) {
385 // Notes are disabled, nothing to do.
389 $url = new moodle_url("/notes/index.php", array('user' => $user->id
));
390 $title = get_string('notes', 'core_notes');
391 if (empty($course)) {
392 // Site level profile.
393 if (!has_capability('moodle/notes:view', context_system
::instance())) {
394 // No cap, nothing to do.
398 if (!has_capability('moodle/notes:view', context_course
::instance($course->id
))) {
399 // No cap, nothing to do.
402 $url->param('course', $course->id
);
404 $notesnode = new core_user\output\myprofile\node
('miscellaneous', 'notes', $title, null, $url);
405 $tree->add_node($notesnode);