Merge branch 'MDL-77816' of https://github.com/paulholden/moodle
[moodle.git] / notes / edit.php
blob5d7970f40473138d17cd9e7761f8fc45658d54e3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 require_once('../config.php');
18 require_once('lib.php');
19 require_once('edit_form.php');
20 require_once($CFG->dirroot . '/course/lib.php');
22 $noteid = optional_param('id', 0, PARAM_INT);
24 $url = new moodle_url('/notes/edit.php');
26 if ($noteid) {
27 // Existing note.
28 $url->param('id', $noteid);
29 if (!$note = note_load($noteid)) {
30 throw new \moodle_exception('invalidid', 'notes');
33 } else {
34 // Adding new note.
35 $courseid = required_param('courseid', PARAM_INT);
36 $userid = required_param('userid', PARAM_INT);
37 $state = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);
39 $note = new stdClass();
40 $note->courseid = $courseid;
41 $note->userid = $userid;
42 $note->publishstate = $state;
44 $url->param('courseid', $courseid);
45 $url->param('userid', $userid);
46 if ($state !== NOTES_STATE_PUBLIC) {
47 $url->param('publishstate', $state);
51 $PAGE->set_url($url);
53 if (!$course = $DB->get_record('course', array('id' => $note->courseid))) {
54 throw new \moodle_exception('invalidcourseid');
57 require_login($course);
59 if (empty($CFG->enablenotes)) {
60 throw new \moodle_exception('notesdisabled', 'notes');
63 $context = context_course::instance($course->id);
64 require_capability('moodle/notes:manage', $context);
66 if (!$user = $DB->get_record('user', array('id' => $note->userid))) {
67 throw new \moodle_exception('invaliduserid');
70 $noteform = new note_edit_form();
71 $noteform->set_data($note);
73 // If form was cancelled then return to the notes list of the note.
74 if ($noteform->is_cancelled()) {
75 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
78 // If data was submitted and validated, then save it to database.
79 if ($note = $noteform->get_data()) {
80 if ($noteid) {
81 // A noteid has been used, we don't allow editing of course or user so
82 // lets unset them to be sure we never change that by accident.
83 unset($note->courseid);
84 unset($note->userid);
86 note_save($note);
87 // Redirect to notes list that contains this note.
88 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
91 if ($noteid) {
92 $strnotes = get_string('editnote', 'notes');
93 } else {
94 $strnotes = get_string('addnewnote', 'notes');
97 // Output HTML.
98 $link = null;
99 if (course_can_view_participants($context) || course_can_view_participants(context_system::instance())) {
100 $link = new moodle_url('/user/index.php', array('id' => $course->id));
102 $PAGE->navbar->add(get_string('participants'), $link);
103 $PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)));
104 $PAGE->navbar->add(get_string('notes', 'notes'),
105 new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $course->id)));
106 $PAGE->navbar->add($strnotes);
107 $PAGE->set_title($course->shortname . ': ' . $strnotes);
108 $PAGE->set_heading($course->fullname);
110 echo $OUTPUT->header();
111 echo $OUTPUT->heading(fullname($user));
113 $noteform->display();
114 echo $OUTPUT->footer();