MDL-9005 Fixed this and another multilang issue (module name in preview didn't filter...
[moodle-pu.git] / notes / lib.php
blob7dfd6ffdd8362dbff97ef63cd2d3da008ea0036e
1 <?php // $Id$
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 // setup filters
36 $select = array();
37 if($courseid) {
38 $selects[] = 'courseid=' . $courseid;
40 if($userid) {
41 $selects[] = 'userid=' . $userid;
43 if($author) {
44 $selects[] = 'usermodified=' . $author;
46 if($state) {
47 $selects[] = 'publishstate="' . $state . '"';
49 $selects[] = 'module="notes"';
50 $select = implode(' AND ', $selects);
51 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
52 // retrieve data
53 $rs =& get_recordset_select('post', $select, $order, $fields, $limitfrom, $limitnum);
54 return recordset_to_array($rs);
57 /**
58 * Retrieves a note object based on its id.
60 * @param int $note_id id of the note to retrieve
61 * @return note object
63 function note_load($note_id) {
64 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
65 return get_record_select('post', 'id=' . $note_id . ' AND module="notes"', $fields);
68 /**
69 * Saves a note object. The note object is passed by reference and its fields (i.e. id)
70 * might change during the save.
72 * @param note $note object to save
73 * @return boolean true if the object was saved; false otherwise
75 function note_save(&$note) {
76 global $USER;
77 // setup & clean fields
78 $note->module = 'notes';
79 $note->lastmodified = time();
80 $note->usermodified = $USER->id;
81 if(empty($note->format)) {
82 $note->format = FORMAT_PLAIN;
84 if(empty($note->publishstate)) {
85 $note->publishstate = NOTES_STATE_PUBLIC;
87 // save data
88 if(empty($note->id)) {
89 // insert new note
90 $note->created = $note->lastmodified;
91 if($id = insert_record('post', $note)) {
92 $note->id = $id;
93 $result = true;
94 } else {
95 $result = false;
97 } else {
98 // update old note
99 $result = update_record('post', $note);
101 unset($note->module);
102 return $result;
106 * Deletes a note object based on its id.
108 * @param int $note_id id of the note to delete
109 * @return boolean true if the object was deleted; false otherwise
111 function note_delete($noteid) {
112 return delete_records_select('post', 'id=' . $noteid . ' AND module="notes"');
116 * Converts a state value to its corespondent name
118 * @param string $state state value to convert
119 * @return string corespondent state name
121 function note_get_state_name($state) {
122 // cache state names
123 static $states;
124 if (empty($states)) {
125 $states = note_get_state_names();
127 return @$states[$state];
131 * Returns an array of mappings from state values to state names
133 * @return array of mappings
135 function note_get_state_names() {
136 return array(
137 NOTES_STATE_DRAFT => get_string('personal', 'notes'),
138 NOTES_STATE_PUBLIC => get_string('course', 'notes'),
139 NOTES_STATE_SITE => get_string('site', 'notes'),
144 * Prints a note object
146 * @param note $note the note object to print
147 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
149 function note_print($note, $detail = NOTES_SHOW_FULL) {
151 global $CFG, $USER;
152 $user = get_record('user','id',$note->userid);
153 $context = get_context_instance(CONTEXT_COURSE, $note->courseid);
154 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
155 $authoring->name = fullname(get_record('user','id',$note->usermodified));
156 $authoring->date = userdate($note->lastmodified);
157 echo '<div class="notepost '. $note->publishstate . 'notepost' .
158 ($note->usermodified == $USER->id ? ' ownnotepost' : '') .
159 '" id="note-'. $note->id .'">';
161 // print note head (e.g. author, user refering to, etc)
162 if($detail & NOTES_SHOW_HEAD) {
163 echo '<div class="header">';
164 echo '<div class="user">';
165 print_user_picture($user->id, $note->courseid, $user->picture);
166 echo fullname($user) . '</div>';
167 echo '<div class="info">' .
168 get_string('bynameondate', 'notes', $authoring) .
169 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
170 echo '</div>';
173 // print note content
174 if($detail & NOTES_SHOW_BODY) {
175 echo '<div class="content">';
176 echo format_text($note->content, $note->format);
177 echo '</div>';
180 // print note options (e.g. delete, edit)
181 if($detail & NOTES_SHOW_FOOT) {
182 if (has_capability('moodle/notes:manage', $sitecontext) && $note->publishstate == NOTES_STATE_SITE ||
183 has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
184 echo '<div class="footer"><p>';
185 echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?note='.$note->id. '">'. get_string('edit') .'</a> | ';
186 echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?note='.$note->id. '">'. get_string('delete') .'</a>';
187 echo '</p></div>';
190 echo '</div>';
194 * Prints a list of note objects
196 * @param array $notes array of note objects to print
197 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
199 function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
201 /// Start printing of the note
202 echo '<div class="notelist">';
203 foreach ($notes as $note) {
204 note_print($note, $detail);
206 echo '</div>';
210 * Retrieves and prints a list of note objects with specific atributes.
212 * @param string $header HTML to print above the list
213 * @param int $addcourseid id of the course for the add notes link (0 hide link)
214 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
215 * @param int $courseid id of the course in which the notes were posted (0 means any)
216 * @param int $userid id of the user to which the notes refer (0 means any)
217 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
218 * @param int $author id of the user who modified the note last time (0 means any)
220 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)
222 global $CFG;
223 if ($header) {
224 echo '<h3 id="notestitle">' . $header . '</h3>';
226 if ($addcourseid) {
227 echo '<p><a href="'. $CFG->wwwroot .'/notes/add.php?course=' . $addcourseid . '&amp;user=' . $userid . '&amp;state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
229 if ($viewnotes) {
230 $notes =& note_list($courseid, $userid, $state, $author);
231 if($notes) {
232 note_print_list($notes);
233 } else {
234 echo '<p>' . get_string('nonotes', 'notes') . '</p>';
236 } else {
237 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';