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');
14 // require login to access notes
15 require_login($course->id
);
17 // locate context information
18 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
21 if (!has_capability('moodle/notes:manage', $context)) {
22 error('You may not create notes');
26 require_once('edit_form.php');
27 // get option values for the user select
28 $extradata['userlist'] = array();
29 $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id IN (SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid={$context->id})";
30 $userlist = get_records_sql($usersincourse);
31 // format userdata using fullname
33 foreach($userlist as $user) {
34 $extradata['userlist'][$user->id
] = fullname($user);
38 $noteform = new note_edit_form(null, $extradata);
40 // if form was cancelled then return to the previous notes list
41 if ($noteform->is_cancelled()) {
42 redirect($CFG->wwwroot
. '/notes/index.php?course=' . $courseid . '&user=' . $userid);
45 // if data was submitted and validated, then save it to database
46 if ($formdata = $noteform->get_data()) {
48 $note->courseid
= $formdata->course
;
49 $note->content
= $formdata->content
;
50 $note->format
= FORMAT_PLAIN
;
51 $note->userid
= $formdata->user
;
52 $note->publishstate
= $formdata->publishstate
;
53 if (note_save($note)) {
54 add_to_log($note->courseid
, 'notes', 'add', 'index.php?course='.$note->courseid
.'&user='.$note->userid
. '#note-' . $note->id
, 'add note');
56 // redirect to notes list that contains this note
57 redirect($CFG->wwwroot
. '/notes/index.php?course=' . $note->courseid
. '&user=' . $note->userid
);
60 if($noteform->is_submitted()) {
61 // if data was submitted with errors, then use it as default for new form
62 $note = $noteform->get_submitted_data(false);
64 // if data was not submitted yet, then use default values
67 $note->course
= $courseid;
68 $note->user
= $userid;
69 $note->publishstate
= optional_param('state', NOTES_STATE_PUBLIC
, PARAM_ALPHA
);
71 $noteform->set_data($note);
72 $strnotes = get_string('addnewnote', 'notes');
75 $crumbs = array(array('name' => $strnotes, 'link' => '', 'type' => 'activity'));
76 print_header($course->shortname
. ': ' . $strnotes, $course->fullname
, build_navigation($crumbs));