3 require_once('../config.php');
4 require_once('lib.php');
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
);
19 if (!has_capability('moodle/notes:manage', $context)) {
20 error('You may not create notes');
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
31 foreach($userlist as $user) {
32 $extradata['userlist'][$user->id
] = fullname($user);
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 . '&user=' . $userid);
43 // if data was submitted and validated, then save it to database
44 if ($formdata = $noteform->get_data()) {
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
.'&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
. '&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);
63 // if data was not submitted yet, then use default values
66 $note->course
= $courseid;
67 $note->user
= $userid;
70 $noteform->set_data($note);
71 $strnotes = get_string('notes', 'notes');
74 print_header($course->shortname
. ': ' . $strnotes, $course->fullname
);