MDL-10680:
[moodle-linuxchix.git] / user / addnote.php
blob07adf8127d8a1663adf95463d3b06db1ff60f55a
1 <?php // $Id$
2 require_once("../config.php");
3 require_once($CFG->dirroot .'/notes/lib.php');
5 $id = required_param('id', PARAM_INT); // course id
6 $users = optional_param('userid', array(), PARAM_INT); // array of user id
7 $contents = optional_param('contents', array(), PARAM_RAW); // array of user notes
8 $states = optional_param('states', array(), PARAM_ALPHA); // array of notes states
9 if (! $course = get_record('course', 'id', $id)) {
10 error("Course ID is incorrect");
13 $context = get_context_instance(CONTEXT_COURSE, $id);
14 require_login($course->id);
16 // to create notes the current user needs a capability
17 require_capability('moodle/notes:manage', $context);
19 if (!empty($users) && confirm_sesskey()) {
20 if (count($users) != count($contents) || count($users) != count($states)) {
21 error('Parameters malformation', $CFG->wwwroot.'/user/index.php?id='.$id);
24 $note = new object();
25 $note->courseid = $id;
26 $note->format = FORMAT_PLAIN;
27 foreach ($users as $k => $v) {
28 if(!$user = get_record('user', 'id', $v) || empty($contents[$k])) {
29 continue;
31 $note->id = 0;
32 $note->content = $contents[$k];
33 $note->publishstate = $states[$k];
34 $note->userid = $v;
35 if (note_save($note)) {
36 add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&amp;user='.$note->userid . '#note-' . $note->id , 'add note');
39 redirect("$CFG->wwwroot/user/index.php?id=$id");
42 /// Print headers
44 $straddnote = get_string('addnewnote', 'notes');
45 if ($course->id != SITEID) {
46 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname,
47 "<a href=\"../course/view.php?id=$course->id\">$course->shortname</a> -> ".
48 $straddnote, "", "", true, "&nbsp;", navmenu($course));
49 } else {
50 print_header("$course->shortname: ".get_string('extendenrol'), $course->fullname,
51 $straddnote, "", "", true, "&nbsp;", navmenu($course));
54 // this will contain all available the based On select options, but we'll disable some on them on a per user basis
56 print_heading($straddnote);
57 echo '<form method="post" action="addnote.php">';
58 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
59 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
60 $table->head = array (get_string('fullname'),
61 get_string('content', 'notes') . helpbutton('writing', get_string('helpwriting'), 'moodle', true, false, '', true),
62 get_string('publishstate', 'notes') . helpbutton('status', get_string('publishstate', 'notes'), 'notes', true, false, '', true),
64 $table->align = array ('left', 'center', 'center');
65 $state_names = note_get_state_names();
67 // the first time list hack
68 if (empty($users)) {
69 foreach ($_POST as $k => $v) {
70 if (preg_match('/^user(\d+)$/',$k,$m)) {
71 $users[] = $m[1];
76 foreach ($users as $k => $v) {
77 if(!$user = get_record('user', 'id', $v)) {
78 continue;
80 $checkbox = choose_from_menu($state_names, 'states[' . $k . ']', empty($states[$k]) ? NOTES_STATE_PUBLIC : $states[$k], '', '', '0', true);
81 $table->data[] = array(
82 '<input type="hidden" name="userid['.$k.']" value="'.$v.'" />'. fullname($user, true),
83 '<textarea name="contents['. $k . ']" rows="2" cols="40">' . strip_tags(@$contents[$k]) . '</textarea>',
84 $checkbox
87 print_table($table);
88 echo '<div style="width:100%;text-align:center;"><input type="submit" value="' . get_string('savechanges'). '" /></div></form>';
89 print_footer($course);