MDL-45219 add \mod\chat\course_module_viewed event
[moodle.git] / notes / lib.php
blob45046a3537dd53a968d3c4356a511aed60430b50
1 <?php
3 /**
4 * Library of functions and constants for notes
5 */
7 /**
8 * Constants for states.
9 */
10 define('NOTES_STATE_DRAFT', 'draft');
11 define('NOTES_STATE_PUBLIC', 'public');
12 define('NOTES_STATE_SITE', 'site');
14 /**
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);
22 /**
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) {
35 global $DB;
37 // setup filters
38 $selects = array();
39 $params = array();
40 if ($courseid) {
41 $selects[] = 'courseid=?';
42 $params[] = $courseid;
44 if ($userid) {
45 $selects[] = 'userid=?';
46 $params[] = $userid;
48 if ($author) {
49 $selects[] = 'usermodified=?';
50 $params[] = $author;
52 if ($state) {
53 $selects[] = 'publishstate=?';
54 $params[] = $state;
56 $selects[] = "module=?";
57 $params[] = 'notes';
59 $select = implode(' AND ', $selects);
60 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
61 // retrieve data
62 return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
65 /**
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) {
72 global $DB;
74 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
75 return $DB->get_record('post', array('id'=>$note_id, 'module'=>'notes'), $fields);
78 /**
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) {
86 global $USER, $DB;
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;
98 // Save data.
99 if (empty($note->id)) {
100 // Insert new note.
101 $note->created = $note->lastmodified;
102 $id = $DB->insert_record('post', $note);
103 $note = note_load($id);
105 // Trigger event.
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->trigger();
115 } else {
116 // Update old note.
117 $DB->update_record('post', $note);
118 $note = note_load($note->id);
120 // Trigger event.
121 $event = \core\event\note_updated::create(array(
122 'objectid' => $note->id,
123 'courseid' => $note->courseid,
124 'relateduserid' => $note->userid,
125 'userid' => $note->usermodified,
126 'context' => context_course::instance($note->courseid),
127 'other' => array('publishstate' => $note->publishstate)
129 $event->trigger();
131 unset($note->module);
132 return true;
136 * Deletes a note object based on its id.
138 * @param int|object $note id of the note to delete, or a note object which is to be deleted.
139 * @return boolean true if the object was deleted; false otherwise
141 function note_delete($note) {
142 global $DB;
143 if (is_int($note)) {
144 $noteid = $note;
145 } else {
146 $noteid = $note->id;
148 // Get the full record, note_load doesn't return everything.
149 $note = $DB->get_record('post', array('id' => $noteid), '*', MUST_EXIST);
150 $return = $DB->delete_records('post', array('id' => $note->id, 'module' => 'notes'));
152 // Trigger event.
153 $event = \core\event\note_deleted::create(array(
154 'objectid' => $note->id,
155 'courseid' => $note->courseid,
156 'relateduserid' => $note->userid,
157 'userid' => $note->usermodified,
158 'context' => context_course::instance($note->courseid),
159 'other' => array('publishstate' => $note->publishstate)
161 $event->add_record_snapshot('post', $note);
162 $event->trigger();
164 return $return;
168 * Converts a state value to its corespondent name
170 * @param string $state state value to convert
171 * @return string corespondent state name
173 function note_get_state_name($state) {
174 // cache state names
175 static $states;
176 if (empty($states)) {
177 $states = note_get_state_names();
179 if (isset($states[$state])) {
180 return $states[$state];
181 } else {
182 return null;
187 * Returns an array of mappings from state values to state names
189 * @return array of mappings
191 function note_get_state_names() {
192 return array(
193 NOTES_STATE_DRAFT => get_string('personal', 'notes'),
194 NOTES_STATE_PUBLIC => get_string('course', 'notes'),
195 NOTES_STATE_SITE => get_string('site', 'notes'),
200 * Prints a note object
202 * @param note $note the note object to print
203 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
205 function note_print($note, $detail = NOTES_SHOW_FULL) {
206 global $CFG, $USER, $DB, $OUTPUT;
208 if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
209 debugging("User $note->userid not found");
210 return;
212 if (!$author = $DB->get_record('user', array('id'=>$note->usermodified))) {
213 debugging("User $note->usermodified not found");
214 return;
216 $context = context_course::instance($note->courseid);
217 $systemcontext = context_system::instance();
219 $authoring = new stdClass();
220 $authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
221 $authoring->date = userdate($note->lastmodified);
223 echo '<div class="notepost '. $note->publishstate . 'notepost' .
224 ($note->usermodified == $USER->id ? ' ownnotepost' : '') .
225 '" id="note-'. $note->id .'">';
227 // print note head (e.g. author, user refering to, etc)
228 if ($detail & NOTES_SHOW_HEAD) {
229 echo '<div class="header">';
230 echo '<div class="user">';
231 echo $OUTPUT->user_picture($user, array('courseid'=>$note->courseid));
232 echo fullname($user) . '</div>';
233 echo '<div class="info">' .
234 get_string('bynameondate', 'notes', $authoring) .
235 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
236 echo '</div>';
239 // print note content
240 if ($detail & NOTES_SHOW_BODY) {
241 echo '<div class="content">';
242 echo format_text($note->content, $note->format, array('overflowdiv'=>true));
243 echo '</div>';
246 // print note options (e.g. delete, edit)
247 if ($detail & NOTES_SHOW_FOOT) {
248 if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate == NOTES_STATE_SITE ||
249 has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
250 echo '<div class="footer"><p>';
251 echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?id='.$note->id. '">'. get_string('edit') .'</a> | ';
252 echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?id='.$note->id. '">'. get_string('delete') .'</a>';
253 echo '</p></div>';
256 echo '</div>';
260 * Prints a list of note objects
262 * @param array $notes array of note objects to print
263 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
265 function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
267 /// Start printing of the note
268 echo '<div class="notelist">';
269 foreach ($notes as $note) {
270 note_print($note, $detail);
272 echo '</div>';
276 * Retrieves and prints a list of note objects with specific atributes.
278 * @param string $header HTML to print above the list
279 * @param int $addcourseid id of the course for the add notes link (0 hide link)
280 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
281 * @param int $courseid id of the course in which the notes were posted (0 means any)
282 * @param int $userid id of the user to which the notes refer (0 means any)
283 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
284 * @param int $author id of the user who modified the note last time (0 means any)
286 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
287 global $CFG;
289 if ($header) {
290 echo '<h3 class="notestitle">' . $header . '</h3>';
291 echo '<div class="notesgroup">';
293 if ($addcourseid) {
294 if ($userid) {
295 echo '<p><a href="'. $CFG->wwwroot .'/notes/edit.php?courseid=' . $addcourseid . '&amp;userid=' . $userid . '&amp;publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
296 } else {
297 echo '<p><a href="'. $CFG->wwwroot .'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
300 if ($viewnotes) {
301 $notes = note_list($courseid, $userid, $state, $author);
302 if ($notes) {
303 note_print_list($notes);
305 } else {
306 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
308 if ($header) {
309 echo '</div>'; // notesgroup
314 * Delete all notes about users in course-
315 * @param int $courseid
316 * @return bool success
318 function note_delete_all($courseid) {
319 global $DB;
321 return $DB->delete_records('post', array('module'=>'notes', 'courseid'=>$courseid));
325 * Return a list of page types
326 * @param string $pagetype current page type
327 * @param stdClass $parentcontext Block's parent context
328 * @param stdClass $currentcontext Current context of block
330 function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
331 return array('notes-*'=>get_string('page-notes-x', 'notes'));