4 * Library of functions and constants for notes
8 * Constants for states.
10 define('NOTES_STATE_DRAFT', 'draft');
11 define('NOTES_STATE_PUBLIC', 'public');
12 define('NOTES_STATE_SITE', 'site');
15 * Constants for note parts (flags used by note_print and note_print_list).
17 define('NOTES_SHOW_FULL', 0x07);
18 define('NOTES_SHOW_HEAD', 0x02);
19 define('NOTES_SHOW_BODY', 0x01);
20 define('NOTES_SHOW_FOOT', 0x04);
23 * Retrieves a list of note objects with specific atributes.
25 * @param int $courseid id of the course in which the notes were posted (0 means any)
26 * @param int $userid id of the user to which the notes refer (0 means any)
27 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
28 * @param int $author id of the user who modified the note last time (0 means any)
29 * @param string $order an order to sort the results in
30 * @param int $limitfrom number of records to skip (offset)
31 * @param int $limitnum number of records to fetch
32 * @return array of note objects
34 function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {
41 $selects[] = 'courseid=?';
42 $params[] = $courseid;
45 $selects[] = 'userid=?';
49 $selects[] = 'usermodified=?';
53 $selects[] = 'publishstate=?';
56 $selects[] = "module=?";
59 $select = implode(' AND ', $selects);
60 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
62 return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
66 * Retrieves a note object based on its id.
68 * @param int $note_id id of the note to retrieve
69 * @return stdClass object
71 function note_load($note_id) {
74 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
75 return $DB->get_record('post', array('id'=>$note_id, 'module'=>'notes'), $fields);
79 * Saves a note object. The note object is passed by reference and its fields (i.e. id)
80 * might change during the save.
82 * @param stdClass $note object to save
83 * @return boolean true if the object was saved; false otherwise
85 function note_save(&$note) {
88 // Setup & clean fields.
89 $note->module
= 'notes';
90 $note->lastmodified
= time();
91 $note->usermodified
= $USER->id
;
92 if (empty($note->format
)) {
93 $note->format
= FORMAT_PLAIN
;
95 if (empty($note->publishstate
)) {
96 $note->publishstate
= NOTES_STATE_PUBLIC
;
99 if (empty($note->id
)) {
101 $note->created
= $note->lastmodified
;
102 $id = $DB->insert_record('post', $note);
103 $note = note_load($id);
106 $event = \core\event\note_created
::create(array(
107 'objectid' => $note->id
,
108 'courseid' => $note->courseid
,
109 'relateduserid' => $note->userid
,
110 'userid' => $note->usermodified
,
111 'context' => context_course
::instance($note->courseid
),
112 'other' => array('publishstate' => $note->publishstate
)
114 $event->add_record_snapshot('post', $note);
118 $DB->update_record('post', $note);
119 $note = note_load($note->id
);
122 $event = \core\event\note_updated
::create(array(
123 'objectid' => $note->id
,
124 'courseid' => $note->courseid
,
125 'relateduserid' => $note->userid
,
126 'userid' => $note->usermodified
,
127 'context' => context_course
::instance($note->courseid
),
128 'other' => array('publishstate' => $note->publishstate
)
130 $event->add_record_snapshot('post', $note);
133 unset($note->module
);
138 * Deletes a note object based on its id.
140 * @param int|object $note id of the note to delete, or a note object which is to be deleted.
141 * @return boolean true if the object was deleted; false otherwise
143 function note_delete($note) {
146 $note = note_load($note);
147 debugging('Warning: providing note_delete with a note object would improve performance.', DEBUG_DEVELOPER
);
149 $return = $DB->delete_records('post', array('id' => $note->id
, 'module' => 'notes'));
152 $event = \core\event\note_deleted
::create(array(
153 'objectid' => $note->id
,
154 'courseid' => $note->courseid
,
155 'relateduserid' => $note->userid
,
156 'userid' => $note->usermodified
,
157 'context' => context_course
::instance($note->courseid
),
158 'other' => array('publishstate' => $note->publishstate
)
160 $event->add_record_snapshot('post', $note);
167 * Converts a state value to its corespondent name
169 * @param string $state state value to convert
170 * @return string corespondent state name
172 function note_get_state_name($state) {
175 if (empty($states)) {
176 $states = note_get_state_names();
178 if (isset($states[$state])) {
179 return $states[$state];
186 * Returns an array of mappings from state values to state names
188 * @return array of mappings
190 function note_get_state_names() {
192 NOTES_STATE_DRAFT
=> get_string('personal', 'notes'),
193 NOTES_STATE_PUBLIC
=> get_string('course', 'notes'),
194 NOTES_STATE_SITE
=> get_string('site', 'notes'),
199 * Prints a note object
201 * @param note $note the note object to print
202 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
204 function note_print($note, $detail = NOTES_SHOW_FULL
) {
205 global $CFG, $USER, $DB, $OUTPUT;
207 if (!$user = $DB->get_record('user', array('id'=>$note->userid
))) {
208 debugging("User $note->userid not found");
211 if (!$author = $DB->get_record('user', array('id'=>$note->usermodified
))) {
212 debugging("User $note->usermodified not found");
215 $context = context_course
::instance($note->courseid
);
216 $systemcontext = context_system
::instance();
218 $authoring = new stdClass();
219 $authoring->name
= '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$author->id
.'&course='.$note->courseid
.'">'.fullname($author).'</a>';
220 $authoring->date
= userdate($note->lastmodified
);
222 echo '<div class="notepost '. $note->publishstate
. 'notepost' .
223 ($note->usermodified
== $USER->id ?
' ownnotepost' : '') .
224 '" id="note-'. $note->id
.'">';
226 // print note head (e.g. author, user refering to, etc)
227 if ($detail & NOTES_SHOW_HEAD
) {
228 echo '<div class="header">';
229 echo '<div class="user">';
230 echo $OUTPUT->user_picture($user, array('courseid'=>$note->courseid
));
231 echo fullname($user) . '</div>';
232 echo '<div class="info">' .
233 get_string('bynameondate', 'notes', $authoring) .
234 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created
) . ')</div>';
238 // print note content
239 if ($detail & NOTES_SHOW_BODY
) {
240 echo '<div class="content">';
241 echo format_text($note->content
, $note->format
, array('overflowdiv'=>true));
245 // print note options (e.g. delete, edit)
246 if ($detail & NOTES_SHOW_FOOT
) {
247 if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate
== NOTES_STATE_SITE ||
248 has_capability('moodle/notes:manage', $context) && ($note->publishstate
== NOTES_STATE_PUBLIC ||
$note->usermodified
== $USER->id
)) {
249 echo '<div class="footer"><p>';
250 echo '<a href="'.$CFG->wwwroot
.'/notes/edit.php?id='.$note->id
. '">'. get_string('edit') .'</a> | ';
251 echo '<a href="'.$CFG->wwwroot
.'/notes/delete.php?id='.$note->id
. '">'. get_string('delete') .'</a>';
259 * Prints a list of note objects
261 * @param array $notes array of note objects to print
262 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
264 function note_print_list($notes, $detail = NOTES_SHOW_FULL
) {
266 /// Start printing of the note
267 echo '<div class="notelist">';
268 foreach ($notes as $note) {
269 note_print($note, $detail);
275 * Retrieves and prints a list of note objects with specific atributes.
277 * @param string $header HTML to print above the list
278 * @param int $addcourseid id of the course for the add notes link (0 hide link)
279 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
280 * @param int $courseid id of the course in which the notes were posted (0 means any)
281 * @param int $userid id of the user to which the notes refer (0 means any)
282 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
283 * @param int $author id of the user who modified the note last time (0 means any)
285 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
289 echo '<h3 class="notestitle">' . $header . '</h3>';
290 echo '<div class="notesgroup">';
294 echo '<p><a href="'. $CFG->wwwroot
.'/notes/edit.php?courseid=' . $addcourseid . '&userid=' . $userid . '&publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
296 echo '<p><a href="'. $CFG->wwwroot
.'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
300 $notes = note_list($courseid, $userid, $state, $author);
302 note_print_list($notes);
305 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
308 echo '</div>'; // notesgroup
313 * Delete all notes about users in course-
314 * @param int $courseid
315 * @return bool success
317 function note_delete_all($courseid) {
320 return $DB->delete_records('post', array('module'=>'notes', 'courseid'=>$courseid));
324 * Return a list of page types
325 * @param string $pagetype current page type
326 * @param stdClass $parentcontext Block's parent context
327 * @param stdClass $currentcontext Current context of block
329 function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
330 return array('notes-*'=>get_string('page-notes-x', 'notes'));