Merge branch 'MDL-31863' of git://github.com/mouneyrac/moodle
[moodle.git] / notes / edit.php
CommitLineData
2f4a2f27 1<?php
83e70cc2 2
2f4a2f27 3require_once('../config.php');
4require_once('lib.php');
5require_once('edit_form.php');
7e7f6fda 6
7/// retrieve parameters
2f4a2f27 8$noteid = optional_param('id', 0, PARAM_INT);
7e7f6fda 9
a6855934 10$url = new moodle_url('/notes/edit.php');
2f4a2f27 11
12if ($noteid) {
13 //existing note
14 $url->param('id', $noteid);
15 if (!$note = note_load($noteid)) {
16 print_error('invalidid', 'notes');
7e7f6fda 17 }
18
2f4a2f27 19} else {
20 // adding new note
21 $courseid = required_param('courseid', PARAM_INT);
22 $userid = required_param('userid', PARAM_INT);
23 $state = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);
24
6d63fb5c 25 $note = new stdClass();
2f4a2f27 26 $note->courseid = $courseid;
27 $note->userid = $userid;
28 $note->publishstate = $state;
29
30 $url->param('courseid', $courseid);
31 $url->param('userid', $userid);
03d9401e
MD
32 if ($state !== NOTES_STATE_PUBLIC) {
33 $url->param('publishstate', $state);
7e7f6fda 34 }
2f4a2f27 35}
36
37$PAGE->set_url($url);
38
39/// locate course information
40if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
41 print_error('invalidcourseid');
42}
43
44/// locate user information
45if (!$user = $DB->get_record('user', array('id'=>$note->userid))) {
46 print_error('invaliduserid');
47}
7e7f6fda 48
49/// require login to access notes
2f4a2f27 50require_login($course);
7e7f6fda 51
52/// locate context information
2f4a2f27 53$context = get_context_instance(CONTEXT_COURSE, $course->id);
54require_capability('moodle/notes:manage', $context);
7e7f6fda 55
2f4a2f27 56if (empty($CFG->enablenotes)) {
57 print_error('notesdisabled', 'notes');
58}
90658eef 59
7e7f6fda 60/// create form
2f4a2f27 61$noteform = new note_edit_form();
7e7f6fda 62
241bcec5 63/// set defaults
2f4a2f27 64$noteform->set_data($note);
241bcec5 65
7e7f6fda 66/// if form was cancelled then return to the notes list of the note
2f4a2f27 67if ($noteform->is_cancelled()) {
68 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
69}
7e7f6fda 70
71/// if data was submitted and validated, then save it to database
2f4a2f27 72if ($note = $noteform->get_data()){
73 if (note_save($note)) {
74 add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&amp;user='.$note->userid . '#note-' . $note->id, 'update note');
83e70cc2 75 }
2f4a2f27 76 // redirect to notes list that contains this note
77 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
78}
7e7f6fda 79
2f4a2f27 80if ($noteid) {
81 $strnotes = get_string('editnote', 'notes');
82} else {
83 $strnotes = get_string('addnewnote', 'notes');
84}
7e7f6fda 85
86/// output HTML
2f4a2f27 87$link = null;
88if (has_capability('moodle/course:viewparticipants', $context) || has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM))) {
a6855934 89 $link = new moodle_url('/user/index.php',array('id'=>$course->id));
2f4a2f27 90}
91$PAGE->navbar->add(get_string('participants'), $link);
a6855934
PS
92$PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$course->id)));
93$PAGE->navbar->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('user'=>$user->id,'course'=>$course->id)));
2f4a2f27 94$PAGE->navbar->add($strnotes);
95$PAGE->set_title($course->shortname . ': ' . $strnotes);
96$PAGE->set_heading($course->fullname);
97
98echo $OUTPUT->header();
99echo $OUTPUT->heading(fullname($user));
100
101$noteform->display();
1d422980 102echo $OUTPUT->footer();