Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / notes / add.php
blob5dfebf3f8cfee55d5f69d6c62d285e78e0d75961
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');
14 // require login to access notes
15 require_login($course->id);
17 // locate context information
18 $context = get_context_instance(CONTEXT_COURSE, $course->id);
20 // check capability
21 if (!has_capability('moodle/notes:manage', $context)) {
22 error('You may not create notes');
25 // build-up form
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
32 if($userlist) {
33 foreach($userlist as $user) {
34 $extradata['userlist'][$user->id] = fullname($user);
37 // create form
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 . '&amp;user=' . $userid);
45 // if data was submitted and validated, then save it to database
46 if ($formdata = $noteform->get_data()) {
47 $note = new object();
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.'&amp;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 . '&amp;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);
63 } else {
64 // if data was not submitted yet, then use default values
65 $note = new object();
66 $note->id = 0;
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');
74 // output HTML
75 $crumbs = array(array('name' => $strnotes, 'link' => '', 'type' => 'activity'));
76 print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs));
77 $noteform->display();
78 print_footer();