Cleaned up notes some more
[moodle.git] / notes / lib.php
CommitLineData
83e70cc2 1<?php // $Id$
2
3/**
4 * Library of functions and constants for notes
5 */
6
7/**
8 * Constants for states.
9 */
10define('NOTES_STATE_DRAFT', 'draft');
11define('NOTES_STATE_PUBLIC', 'public');
12define('NOTES_STATE_SITE', 'site');
13
14/**
15 * Constants for note parts (flags used by note_print and note_print_list).
16 */
17define('NOTES_SHOW_FULL', 0x07);
18define('NOTES_SHOW_HEAD', 0x02);
19define('NOTES_SHOW_BODY', 0x01);
20define('NOTES_SHOW_FOOT', 0x04);
21
22/**
23 * Retrieves a list of note objects with specific atributes.
24 *
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
33 */
34function 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;
39 }
40 if($userid) {
41 $selects[] = 'userid=' . $userid;
42 }
43 if($author) {
44 $selects[] = 'usermodified=' . $author;
45 }
46 if($state) {
47 $selects[] = 'publishstate="' . $state . '"';
48 }
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);
55}
56
57/**
58 * Retrieves a note object based on its id.
59 *
60 * @param int $note_id id of the note to retrieve
61 * @return note object
62 */
63function 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);
66}
67
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.
71 *
72 * @param note $note object to save
73 * @return boolean true if the object was saved; false otherwise
74 */
75function 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;
83 }
84 if(empty($note->publishstate)) {
85 $note->publishstate = NOTES_STATE_PUBLIC;
86 }
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;
96 }
97 } else {
98 // update old note
99 $result = update_record('post', $note);
100 }
101 unset($note->module);
102 return $result;
103}
104
105/**
106 * Deletes a note object based on its id.
107 *
108 * @param int $note_id id of the note to delete
109 * @return boolean true if the object was deleted; false otherwise
110 */
111function note_delete($noteid) {
112 return delete_records_select('post', 'id=' . $noteid . ' AND module="notes"');
113}
114
115/**
116 * Converts a state value to its corespondent name
117 *
118 * @param string $state state value to convert
119 * @return string corespondent state name
120 */
121function note_get_state_name($state) {
122 // cache state names
123 static $states;
124 if (empty($states)) {
125 $states = note_get_state_names();
126 }
127 return @$states[$state];
128}
129
130/**
131 * Returns an array of mappings from state values to state names
132 *
133 * @return array of mappings
134 */
135function 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'),
140 );
141}
142
143/**
144 * Prints a note object
145 *
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
148 */
149function note_print($note, $detail = NOTES_SHOW_FULL) {
150
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 .'">';
160
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>';
171 }
172
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>';
178 }
179
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>';
188 }
189 }
190 echo '</div>';
191}
192
193/**
194 * Prints a list of note objects
195 *
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
198 */
199function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
200
201 /// Start printing of the note
202 echo '<div class="notelist">';
203 foreach ($notes as $note) {
204 note_print($note, $detail);
205 }
206 echo '</div>';
207}
208
209/**
210 * Retrieves and prints a list of note objects with specific atributes.
211 *
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)
219 */
220function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)
221{
222 global $CFG;
223 if ($header) {
224 echo '<h3 class="notestitle">' . $header . '</h3>';
225 echo '<div class="notesgroup">';
226 }
227 if ($addcourseid) {
228 echo '<p><a href="'. $CFG->wwwroot .'/notes/add.php?course=' . $addcourseid . '&amp;user=' . $userid . '&amp;state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
229 }
230 if ($viewnotes) {
231 $notes =& note_list($courseid, $userid, $state, $author);
232 if ($notes) {
233 note_print_list($notes);
234 }
235 } else {
236 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
237 }
238 if ($header) {
239 echo '</div>'; // notesgroup
240 }
241}
242
243?>