Fixed some notices on some custom pages
[moodle.git] / notes / lib.php
CommitLineData
eca3af25 1<?php // $Id$\r
2\r
3/**\r
4 * Library of functions and constants for notes\r
5 */\r
6\r
eca3af25 7/**\r
8 * Constants for states.\r
9 */\r
10define('NOTES_STATE_DRAFT', 'draft');\r
11define('NOTES_STATE_PUBLIC', 'public');\r
12define('NOTES_STATE_SITE', 'site');\r
13\r
14/**\r
15 * Constants for note parts (flags used by note_print and note_print_list).\r
16 */\r
17define('NOTES_SHOW_FULL', 0x07);\r
18define('NOTES_SHOW_HEAD', 0x02);\r
19define('NOTES_SHOW_BODY', 0x01);\r
20define('NOTES_SHOW_FOOT', 0x04);\r
21\r
22/**\r
23 * Retrieves a list of note objects with specific atributes.\r
24 *\r
25 * @param int $courseid id of the course in which the notes were posted (0 means any)\r
26 * @param int $userid id of the user to which the notes refer (0 means any)\r
27 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)\r
28 * @param int $author id of the user who modified the note last time (0 means any)\r
29 * @param string $order an order to sort the results in\r
30 * @param int $limitfrom number of records to skip (offset)\r
31 * @param int $limitnum number of records to fetch\r
32 * @return array of note objects\r
33 */\r
34function note_list($courseid=0, $userid=0, $state = '', $author = 0, $order='lastmodified DESC', $limitfrom=0, $limitnum=0) {\r
35 // setup filters\r
36 $select = array();\r
37 if($courseid) {\r
38 $selects[] = 'courseid=' . $courseid;\r
39 }\r
40 if($userid) {\r
41 $selects[] = 'userid=' . $userid;\r
42 }\r
43 if($author) {\r
44 $selects[] = 'usermodified=' . $author;\r
45 }\r
46 if($state) {\r
47 $selects[] = 'publishstate="' . $state . '"';\r
48 }\r
49 $selects[] = 'module="notes"';\r
50 $select = implode(' AND ', $selects);\r
c55dee3f 51 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';\r
eca3af25 52 // retrieve data\r
53 $rs =& get_recordset_select('post', $select, $order, $fields, $limitfrom, $limitnum);\r
54 return recordset_to_array($rs);\r
55}\r
56\r
57/**\r
58 * Retrieves a note object based on its id.\r
59 *\r
60 * @param int $note_id id of the note to retrieve\r
61 * @return note object\r
62 */\r
63function note_load($note_id) {\r
c55dee3f 64 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';\r
eca3af25 65 return get_record_select('post', 'id=' . $note_id . ' AND module="notes"', $fields);\r
66}\r
67\r
68/**\r
69 * Saves a note object. The note object is passed by reference and its fields (i.e. id) \r
70 * might change during the save.\r
71 *\r
72 * @param note $note object to save\r
73 * @return boolean true if the object was saved; false otherwise\r
74 */\r
75function note_save(&$note) {\r
76 global $USER;\r
77 // setup & clean fields\r
78 $note->module = 'notes';\r
79 $note->lastmodified = time();\r
80 $note->usermodified = $USER->id;\r
eca3af25 81 if(empty($note->format)) {\r
82 $note->format = FORMAT_PLAIN;\r
83 }\r
84 if(empty($note->publishstate)) {\r
85 $note->publishstate = NOTES_STATE_PUBLIC;\r
86 }\r
87 // save data\r
88 if(empty($note->id)) {\r
89 // insert new note\r
90 $note->created = $note->lastmodified;\r
91 if($id = insert_record('post', $note)) {\r
92 $note->id = $id;\r
93 $result = true;\r
94 } else {\r
95 $result = false;\r
96 }\r
97 } else {\r
98 // update old note\r
99 $result = update_record('post', $note);\r
100 }\r
101 unset($note->module);\r
102 return $result;\r
103}\r
104\r
105/**\r
106 * Deletes a note object based on its id.\r
107 *\r
108 * @param int $note_id id of the note to delete\r
109 * @return boolean true if the object was deleted; false otherwise\r
110 */\r
111function note_delete($noteid) {\r
112 return delete_records_select('post', 'id=' . $noteid . ' AND module="notes"');\r
113}\r
114\r
eca3af25 115/**\r
116 * Converts a state value to its corespondent name\r
117 *\r
118 * @param string $state state value to convert\r
119 * @return string corespondent state name\r
120 */\r
121function note_get_state_name($state) {\r
122 // cache state names\r
123 static $states;\r
124 if (empty($states)) {\r
125 $states = note_get_state_names();\r
126 }\r
127 return @$states[$state];\r
128}\r
129\r
130/**\r
131 * Returns an array of mappings from state values to state names\r
132 *\r
133 * @return array of mappings\r
134 */\r
135function note_get_state_names() {\r
136 return array(\r
137 NOTES_STATE_DRAFT => get_string('personal', 'notes'),\r
138 NOTES_STATE_PUBLIC => get_string('course', 'notes'),\r
139 NOTES_STATE_SITE => get_string('site', 'notes'),\r
140 );\r
141}\r
142\r
143/**\r
144 * Prints a note object\r
145 *\r
146 * @param note $note the note object to print\r
147 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print\r
148 */\r
149function note_print($note, $detail = NOTES_SHOW_FULL) {\r
150\r
151 global $CFG, $USER;\r
152 $user = get_record('user','id',$note->userid);\r
153 $context = get_context_instance(CONTEXT_COURSE, $note->courseid);\r
154 $sitecontext = get_context_instance(CONTEXT_SYSTEM);\r
155 $authoring->name = fullname(get_record('user','id',$note->usermodified));\r
156 $authoring->date = userdate($note->lastmodified);\r
157 echo '<div class="notepost '. $note->publishstate . 'notepost' . \r
158 ($note->usermodified == $USER->id ? ' ownnotepost' : '') .\r
159 '" id="note-'. $note->id .'">';\r
160\r
c55dee3f 161 // print note head (e.g. author, user refering to, etc)\r
eca3af25 162 if($detail & NOTES_SHOW_HEAD) {\r
163 echo '<div class="header">';\r
164 echo '<div class="user">';\r
165 print_user_picture($user->id, $note->courseid, $user->picture);\r
166 echo fullname($user) . '</div>';\r
eca3af25 167 echo '<div class="info">' . \r
168 get_string('bynameondate', 'notes', $authoring) . \r
169 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';\r
170 echo '</div>';\r
171 }\r
172 \r
173 // print note content\r
174 if($detail & NOTES_SHOW_BODY) {\r
175 echo '<div class="content">';\r
176 echo format_text($note->content, $note->format);\r
177 echo '</div>';\r
178 }\r
179 \r
180 // print note options (e.g. delete, edit)\r
181 if($detail & NOTES_SHOW_FOOT) {\r
182 if (has_capability('moodle/notes:manage', $sitecontext) && $note->publishstate == NOTES_STATE_SITE || \r
183 has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {\r
c55dee3f 184 echo '<div class="footer"><p>';\r
185 echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?note='.$note->id. '">'. get_string('edit') .'</a> | ';\r
eca3af25 186 echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?note='.$note->id. '">'. get_string('delete') .'</a>';\r
c55dee3f 187 echo '</p></div>';\r
eca3af25 188 }\r
189 }\r
190 echo '</div>';\r
191}\r
192\r
193/**\r
194 * Prints a list of note objects\r
195 *\r
196 * @param array $notes array of note objects to print\r
197 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print\r
198 */\r
199function note_print_list($notes, $detail = NOTES_SHOW_FULL) {\r
200\r
201 /// Start printing of the note\r
202 echo '<div class="notelist">';\r
203 foreach ($notes as $note) {\r
204 note_print($note, $detail);\r
205 }\r
206 echo '</div>';\r
207}\r
208\r
209/**\r
210 * Retrieves and prints a list of note objects with specific atributes.\r
211 *\r
212 * @param string $header HTML to print above the list\r
c55dee3f 213 * @param int $addcourseid id of the course for the add notes link (0 hide link)\r
214 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)\r
eca3af25 215 * @param int $courseid id of the course in which the notes were posted (0 means any)\r
216 * @param int $userid id of the user to which the notes refer (0 means any)\r
217 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)\r
218 * @param int $author id of the user who modified the note last time (0 means any)\r
219 */\r
c55dee3f 220function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)\r
eca3af25 221{\r
222 global $CFG;\r
223 if ($header) {\r
224 echo '<h3 id="notestitle">' . $header . '</h3>';\r
225 }\r
c55dee3f 226 if ($addcourseid) {\r
227 echo '<p><a href="'. $CFG->wwwroot .'/notes/add.php?course=' . $addcourseid . '&amp;user=' . $userid . '&amp;state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';\r
228 }\r
229 if ($viewnotes) {\r
eca3af25 230 $notes =& note_list($courseid, $userid, $state, $author);\r
231 if($notes) {\r
232 note_print_list($notes);\r
233 } else {\r
234 echo '<p>' . get_string('nonotes', 'notes') . '</p>';\r
235 }\r
236 } else {\r
237 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';\r
238 }\r
c55dee3f 239}