weekly release 2.4.6+
[moodle.git] / user / addnote.php
blob3636615023fbedefca203efedecdbed4cddd3b22
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file allows you to add a note for a user
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
26 require_once("../config.php");
27 require_once($CFG->dirroot .'/notes/lib.php');
29 $id = required_param('id', PARAM_INT); // course id
30 $users = optional_param_array('userid', array(), PARAM_INT); // array of user id
31 $contents = optional_param_array('contents', array(), PARAM_RAW); // array of user notes
32 $states = optional_param_array('states', array(), PARAM_ALPHA); // array of notes states
34 $PAGE->set_url('/user/addnote.php', array('id'=>$id));
36 if (! $course = $DB->get_record('course', array('id'=>$id))) {
37 print_error('invalidcourseid');
40 $context = context_course::instance($id);
41 require_login($course);
43 // to create notes the current user needs a capability
44 require_capability('moodle/notes:manage', $context);
46 if (empty($CFG->enablenotes)) {
47 print_error('notesdisabled', 'notes');
50 if (!empty($users) && confirm_sesskey()) {
51 if (count($users) != count($contents) || count($users) != count($states)) {
52 print_error('invalidformdata', '', $CFG->wwwroot.'/user/index.php?id='.$id);
55 $note = new stdClass();
56 $note->courseid = $id;
57 $note->format = FORMAT_PLAIN;
58 foreach ($users as $k => $v) {
59 if (!$user = $DB->get_record('user', array('id'=>$v)) || empty($contents[$k])) {
60 continue;
62 $note->id = 0;
63 $note->content = $contents[$k];
64 $note->publishstate = $states[$k];
65 $note->userid = $v;
66 if (note_save($note)) {
67 add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&amp;user='.$note->userid . '#note-' . $note->id , 'add note');
70 redirect("$CFG->wwwroot/user/index.php?id=$id");
73 /// Print headers
75 $straddnote = get_string('addnewnote', 'notes');
77 $PAGE->navbar->add($straddnote);
78 $PAGE->set_title("$course->shortname: ".get_string('extendenrol'));
79 $PAGE->set_heading($course->fullname);
81 echo $OUTPUT->header();
82 // this will contain all available the based On select options, but we'll disable some on them on a per user basis
84 echo $OUTPUT->heading($straddnote);
85 echo '<form method="post" action="addnote.php">';
86 echo '<fieldset class="invisiblefieldset">';
87 echo '<input type="hidden" name="id" value="'.$course->id.'" />';
88 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
89 echo '</fieldset>';
90 $table = new html_table();
91 $table->head = array (get_string('fullnameuser'),
92 get_string('content', 'notes'),
93 get_string('publishstate', 'notes') . $OUTPUT->help_icon('publishstate', 'notes'),
95 $table->align = array ('left', 'center', 'center');
96 $state_names = note_get_state_names();
98 // the first time list hack
99 if (empty($users) and $post = data_submitted()) {
100 foreach ($post as $k => $v) {
101 if (preg_match('/^user(\d+)$/',$k,$m)) {
102 $users[] = $m[1];
106 foreach ($users as $k => $v) {
107 if(!$user = $DB->get_record('user', array('id'=>$v))) {
108 continue;
110 $checkbox = html_writer::label(get_string('selectnotestate', 'notes'), 'menustates', false, array('class' => 'accesshide'));
111 $checkbox .= html_writer::select($state_names, 'states[' . $k . ']', empty($states[$k]) ? NOTES_STATE_PUBLIC : $states[$k], false, array('id' => 'menustates'));
112 $table->data[] = array(
113 '<input type="hidden" name="userid['.$k.']" value="'.$v.'" />'. fullname($user, true),
114 '<textarea name="contents['. $k . ']" rows="2" cols="40">' . strip_tags(@$contents[$k]) . '</textarea>',
115 $checkbox
118 echo html_writer::table($table);
119 echo '<div style="width:100%;text-align:center;"><input type="submit" value="' . get_string('savechanges'). '" /></div></form>';
120 echo $OUTPUT->footer();