MDL-42883 Administration: Check sessionkey before taking action on user profile fields
[moodle.git] / notes / lib.php
CommitLineData
1d422980 1<?php
83e70cc2 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) {
b3829d0a 35 global $DB;
36
83e70cc2 37 // setup filters
db4ff7f8 38 $selects = array();
b3829d0a 39 $params = array();
40 if ($courseid) {
41 $selects[] = 'courseid=?';
42 $params[] = $courseid;
83e70cc2 43 }
b3829d0a 44 if ($userid) {
45 $selects[] = 'userid=?';
46 $params[] = $userid;
83e70cc2 47 }
b3829d0a 48 if ($author) {
49 $selects[] = 'usermodified=?';
50 $params[] = $author;
83e70cc2 51 }
b3829d0a 52 if ($state) {
53 $selects[] = 'publishstate=?';
54 $params[] = $state;
83e70cc2 55 }
b3829d0a 56 $selects[] = "module=?";
57 $params[] = 'notes';
58
83e70cc2 59 $select = implode(' AND ', $selects);
60 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
61 // retrieve data
b3829d0a 62 return $DB->get_records_select('post', $select, $params, $order, $fields, $limitfrom, $limitnum);
83e70cc2 63}
64
65/**
66 * Retrieves a note object based on its id.
67 *
68 * @param int $note_id id of the note to retrieve
69 * @return note object
70 */
71function note_load($note_id) {
b3829d0a 72 global $DB;
73
83e70cc2 74 $fields = 'id,courseid,userid,content,format,created,lastmodified,usermodified,publishstate';
b3829d0a 75 return $DB->get_record('post', array('id'=>$note_id, 'module'=>'notes'), $fields);
83e70cc2 76}
77
78/**
1519cdeb 79 * Saves a note object. The note object is passed by reference and its fields (i.e. id)
83e70cc2 80 * might change during the save.
81 *
82 * @param note $note object to save
83 * @return boolean true if the object was saved; false otherwise
84 */
85function note_save(&$note) {
b3829d0a 86 global $USER, $DB;
87
83e70cc2 88 // setup & clean fields
b3829d0a 89 $note->module = 'notes';
83e70cc2 90 $note->lastmodified = time();
91 $note->usermodified = $USER->id;
241bcec5 92 if (empty($note->format)) {
83e70cc2 93 $note->format = FORMAT_PLAIN;
94 }
241bcec5 95 if (empty($note->publishstate)) {
83e70cc2 96 $note->publishstate = NOTES_STATE_PUBLIC;
97 }
98 // save data
241bcec5 99 if (empty($note->id)) {
83e70cc2 100 // insert new note
101 $note->created = $note->lastmodified;
a9637e7d 102 $id = $DB->insert_record('post', $note);
64605986 103 $note = note_load($id);
83e70cc2 104 } else {
105 // update old note
a9637e7d 106 $DB->update_record('post', $note);
64605986 107 $note = note_load($note->id);
83e70cc2 108 }
109 unset($note->module);
a9637e7d 110 return true;
83e70cc2 111}
112
113/**
114 * Deletes a note object based on its id.
115 *
116 * @param int $note_id id of the note to delete
117 * @return boolean true if the object was deleted; false otherwise
118 */
119function note_delete($noteid) {
b3829d0a 120 global $DB;
121
122 return $DB->delete_records('post', array('id'=>$noteid, 'module'=>'notes'));
83e70cc2 123}
124
125/**
126 * Converts a state value to its corespondent name
127 *
128 * @param string $state state value to convert
129 * @return string corespondent state name
130 */
131function note_get_state_name($state) {
132 // cache state names
133 static $states;
134 if (empty($states)) {
135 $states = note_get_state_names();
136 }
b3829d0a 137 if (isset($states[$state])) {
138 return $states[$state];
139 } else {
140 return null;
141 }
83e70cc2 142}
143
144/**
145 * Returns an array of mappings from state values to state names
146 *
147 * @return array of mappings
148 */
149function note_get_state_names() {
150 return array(
151 NOTES_STATE_DRAFT => get_string('personal', 'notes'),
152 NOTES_STATE_PUBLIC => get_string('course', 'notes'),
153 NOTES_STATE_SITE => get_string('site', 'notes'),
154 );
155}
156
157/**
158 * Prints a note object
159 *
160 * @param note $note the note object to print
161 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
162 */
163function note_print($note, $detail = NOTES_SHOW_FULL) {
4fa7dc61 164 global $CFG, $USER, $DB, $OUTPUT;
83e70cc2 165
b3829d0a 166 if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
807188eb 167 debugging("User $note->userid not found");
168 return;
169 }
b3829d0a 170 if (!$author = $DB->get_record('user', array('id'=>$note->usermodified))) {
807188eb 171 debugging("User $note->usermodified not found");
172 return;
173 }
83e70cc2 174 $context = get_context_instance(CONTEXT_COURSE, $note->courseid);
241bcec5 175 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
807188eb 176
6d63fb5c 177 $authoring = new stdClass();
807188eb 178 $authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
83e70cc2 179 $authoring->date = userdate($note->lastmodified);
807188eb 180
1519cdeb 181 echo '<div class="notepost '. $note->publishstate . 'notepost' .
83e70cc2 182 ($note->usermodified == $USER->id ? ' ownnotepost' : '') .
183 '" id="note-'. $note->id .'">';
184
185 // print note head (e.g. author, user refering to, etc)
241bcec5 186 if ($detail & NOTES_SHOW_HEAD) {
83e70cc2 187 echo '<div class="header">';
188 echo '<div class="user">';
812dbaf7 189 echo $OUTPUT->user_picture($user, array('courseid'=>$note->courseid));
83e70cc2 190 echo fullname($user) . '</div>';
1519cdeb 191 echo '<div class="info">' .
192 get_string('bynameondate', 'notes', $authoring) .
83e70cc2 193 ' (' . get_string('created', 'notes') . ': ' . userdate($note->created) . ')</div>';
194 echo '</div>';
195 }
1519cdeb 196
83e70cc2 197 // print note content
241bcec5 198 if ($detail & NOTES_SHOW_BODY) {
83e70cc2 199 echo '<div class="content">';
367a75fa 200 echo format_text($note->content, $note->format, array('overflowdiv'=>true));
83e70cc2 201 echo '</div>';
202 }
1519cdeb 203
83e70cc2 204 // print note options (e.g. delete, edit)
241bcec5 205 if ($detail & NOTES_SHOW_FOOT) {
206 if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate == NOTES_STATE_SITE ||
83e70cc2 207 has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
208 echo '<div class="footer"><p>';
241bcec5 209 echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?id='.$note->id. '">'. get_string('edit') .'</a> | ';
210 echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?id='.$note->id. '">'. get_string('delete') .'</a>';
83e70cc2 211 echo '</p></div>';
212 }
213 }
214 echo '</div>';
215}
216
217/**
218 * Prints a list of note objects
219 *
220 * @param array $notes array of note objects to print
221 * @param int $detail OR-ed NOTES_SHOW_xyz flags that specify which note parts to print
222 */
223function note_print_list($notes, $detail = NOTES_SHOW_FULL) {
224
225 /// Start printing of the note
226 echo '<div class="notelist">';
227 foreach ($notes as $note) {
228 note_print($note, $detail);
229 }
230 echo '</div>';
231}
232
233/**
234 * Retrieves and prints a list of note objects with specific atributes.
235 *
236 * @param string $header HTML to print above the list
237 * @param int $addcourseid id of the course for the add notes link (0 hide link)
238 * @param boolean $viewnotes true if the notes should be printed; false otherwise (print notesnotvisible string)
239 * @param int $courseid id of the course in which the notes were posted (0 means any)
240 * @param int $userid id of the user to which the notes refer (0 means any)
241 * @param string $state state of the notes (i.e. draft, public, site) ('' means any)
242 * @param int $author id of the user who modified the note last time (0 means any)
243 */
b3829d0a 244function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $courseid = 0, $userid = 0, $state = '', $author = 0) {
83e70cc2 245 global $CFG;
b3829d0a 246
83e70cc2 247 if ($header) {
248 echo '<h3 class="notestitle">' . $header . '</h3>';
249 echo '<div class="notesgroup">';
250 }
251 if ($addcourseid) {
9f26cce6 252 if ($userid) {
241bcec5 253 echo '<p><a href="'. $CFG->wwwroot .'/notes/edit.php?courseid=' . $addcourseid . '&amp;userid=' . $userid . '&amp;publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
9f26cce6 254 } else {
255 echo '<p><a href="'. $CFG->wwwroot .'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
256 }
83e70cc2 257 }
258 if ($viewnotes) {
b3829d0a 259 $notes = note_list($courseid, $userid, $state, $author);
83e70cc2 260 if ($notes) {
261 note_print_list($notes);
262 }
263 } else {
264 echo '<p>' . get_string('notesnotvisible', 'notes') . '</p>';
265 }
266 if ($header) {
267 echo '</div>'; // notesgroup
268 }
269}
270
0b5a80a1 271/**
272 * Delete all notes about users in course-
273 * @param int $courseid
274 * @return bool success
275 */
276function note_delete_all($courseid) {
b3829d0a 277 global $DB;
278
279 return $DB->delete_records('post', array('module'=>'notes', 'courseid'=>$courseid));
0b5a80a1 280}
b1627a92
DC
281
282/**
283 * Return a list of page types
284 * @param string $pagetype current page type
285 * @param stdClass $parentcontext Block's parent context
286 * @param stdClass $currentcontext Current context of block
287 */
b38e2e28 288function note_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
289 return array('notes-*'=>get_string('page-notes-x', 'notes'));
290}