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) {
38 $selects[] = 'courseid=' . $courseid;
41 $selects[] = 'userid=' . $userid;
44 $selects[] = 'usermodified=' . $author;
47 $selects[] = "publishstate='$state'";
49 $selects[] = "module='notes'";
50 $select = implode(' AND ', $selects);
51 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
53 $rs =& get_recordset_select('post', $select, $order, $fields, $limitfrom, $limitnum);
54 return recordset_to_array($rs);
58 * Retrieves a note object based on its id.
60 * @param int $note_id id of the note to retrieve
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);
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) {
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
;
88 if(empty($note->id
)) {
90 $note->created
= $note->lastmodified
;
91 if($id = insert_record('post', $note)) {
99 $result = update_record('post', $note);
101 unset($note->module
);
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) {
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() {
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
) {
152 if (!$user = get_record('user','id',$note->userid
)) {
153 debugging("User $note->userid not found");
156 if (!$author = get_record('user','id',$note->usermodified
)) {
157 debugging("User $note->usermodified not found");
160 $context = get_context_instance(CONTEXT_COURSE
, $note->courseid
);
161 $sitecontext = get_context_instance(CONTEXT_SYSTEM
);
163 $authoring = new object;
164 $authoring->name
= '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$author->id
.'&course='.$note->courseid
.'">'.fullname($author).'</a>';
165 $authoring->date
= userdate($note->lastmodified
);
167 echo '<div class="notepost '. $note->publishstate
. 'notepost' .
168 ($note->usermodified
== $USER->id ?
' ownnotepost' : '') .
169 '" id="note-'. $note->id
.'">';
171 // print note head (e.g. author, user refering to, etc)
172 if($detail & NOTES_SHOW_HEAD
) {
173 echo '<div class="header">';
174 echo '<div class="user">';
175 print_user_picture($user, $note->courseid
, $user->picture
);
176 echo fullname($user) . '</div>';
177 echo '<div class="info">' .
178 get_string('bynameondate', 'notes', $authoring) .
179 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created
) . ')</div>';
183 // print note content
184 if($detail & NOTES_SHOW_BODY
) {
185 echo '<div class="content">';
186 echo format_text($note->content
, $note->format
);
190 // print note options (e.g. delete, edit)
191 if($detail & NOTES_SHOW_FOOT
) {
192 if (has_capability('moodle/notes:manage', $sitecontext) && $note->publishstate
== NOTES_STATE_SITE ||
193 has_capability('moodle/notes:manage', $context) && ($note->publishstate
== NOTES_STATE_PUBLIC ||
$note->usermodified
== $USER->id
)) {
194 echo '<div class="footer"><p>';
195 echo '<a href="'.$CFG->wwwroot
.'/notes/edit.php?note='.$note->id
. '">'. get_string('edit') .'</a> | ';
196 echo '<a href="'.$CFG->wwwroot
.'/notes/delete.php?note='.$note->id
. '">'. get_string('delete') .'</a>';
204 * Prints a list of note objects
206 * @param array $notes array of note objects to print
207 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
209 function note_print_list($notes, $detail = NOTES_SHOW_FULL
) {
211 /// Start printing of the note
212 echo '<div class="notelist">';
213 foreach ($notes as $note) {
214 note_print($note, $detail);
220 * Retrieves and prints a list of note objects with specific atributes.
222 * @param string $header HTML to print above the list
223 * @param int $addcourseid id of the course for the add notes link (0 hide link)
224 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
225 * @param int $courseid id of the course in which the notes were posted (0 means any)
226 * @param int $userid id of the user to which the notes refer (0 means any)
227 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
228 * @param int $author id of the user who modified the note last time (0 means any)
230 function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0)
234 echo '<h3 class="notestitle">' . $header . '</h3>';
235 echo '<div class="notesgroup">';
239 echo '<p><a href="'. $CFG->wwwroot
.'/notes/add.php?course=' . $addcourseid . '&user=' . $userid . '&state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
241 echo '<p><a href="'. $CFG->wwwroot
.'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
245 $notes =& note_list($courseid, $userid, $state, $author);
247 note_print_list($notes);
250 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
253 echo '</div>'; // notesgroup
258 * Delete all notes about users in course-
259 * @param int $courseid
260 * @return bool success
262 function note_delete_all($courseid) {
263 return delete_records('post', 'module', 'notes', 'courseid', $courseid);