Small upgrade to fix some guest->mnethostid. MDL-10375
[pfb-moodle.git] / notes / add.php
blob60fbb91d3c6f35c58ec4e94c1d930cd25f2b4aaf
1 <?php // $Id$
3 require_once('../config.php');
4 require_once('lib.php');
6 // retrieve parameters
7 $courseid = required_param('course', PARAM_INT);
8 $userid = optional_param('user', 0, PARAM_INT);
10 // locate course information
11 if (!($course = get_record('course', 'id', $courseid))) {
12 error('Incorrect course id found');
15 // locate context information
16 $context = get_context_instance(CONTEXT_COURSE, $course->id);
18 // check capability
19 if (!has_capability('moodle/notes:manage', $context)) {
20 error('You may not create notes');
23 // build-up form
24 require_once('edit_form.php');
25 // get option values for the user select
26 $extradata['userlist'] = array();
27 $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id IN (SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid={$context->id})";
28 $userlist = get_records_sql($usersincourse);
29 // format userdata using fullname
30 if($userlist) {
31 foreach($userlist as $user) {
32 $extradata['userlist'][$user->id] = fullname($user);
35 // create form
36 $noteform = new note_edit_form(null, $extradata);
38 // if form was cancelled then return to the previous notes list
39 if ($noteform->is_cancelled()) {
40 redirect($CFG->wwwroot . '/notes/index.php?course=' . $courseid . '&amp;user=' . $userid);
43 // if data was submitted and validated, then save it to database
44 if ($formdata = $noteform->get_data()) {
45 $note = new object();
46 $note->courseid = $formdata->course;
47 $note->content = $formdata->content;
48 $note->format = FORMAT_PLAIN;
49 $note->rating = $formdata->rating;
50 $note->userid = $formdata->user;
51 $note->publishstate = $formdata->publishstate;
52 if (note_save($note)) {
53 add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&amp;user='.$note->userid . '#note-' . $note->id , 'add note');
55 // redirect to notes list that contains this note
56 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
59 if($noteform->is_submitted()) {
60 // if data was submitted with errors, then use it as default for new form
61 $note = $noteform->get_submitted_data(false);
62 } else {
63 // if data was not submitted yet, then use default values
64 $note = new object();
65 $note->id = 0;
66 $note->course = $courseid;
67 $note->user = $userid;
70 $noteform->set_data($note);
71 $strnotes = get_string('notes', 'notes');
73 // output HTML
74 print_header($course->shortname . ': ' . $strnotes, $course->fullname);
75 $noteform->display();
76 print_footer();