Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / notes / edit.php
blobe16b867e415561898c4ed743296deb7d56d80b0c
1 <?php // $Id$
3 require_once('../config.php');
4 require_once('lib.php');
6 // retrieve parameters
7 $noteid = required_param('note', PARAM_INT);
9 // locate note information
10 if (!$note = note_load($noteid)) {
11 error('Incorrect note id specified');
14 // locate course information
15 if (!$course = get_record('course', 'id', $note->courseid)) {
16 error('Incorrect course id found');
18 // require login to access notes
19 require_login($course->id);
21 // locate context information
22 $context = get_context_instance(CONTEXT_COURSE, $course->id);
24 // check capability
25 if (!has_capability('moodle/notes:manage', $context)) {
26 error('You may not modify notes');
29 // build-up form
30 require_once('edit_form.php');
31 // get option values for the user select
32 $extradata['userlist'] = array();
33 if ($course->id == SITEID) {
34 $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id={$userid}";
35 } else {
36 $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id IN (SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid={$context->id})";
38 $userlist = get_records_sql($usersincourse);
39 // format userdata using fullname
40 if($userlist) {
41 foreach($userlist as $user) {
42 $extradata['userlist'][$user->id] = fullname($user);
45 // create form
46 $noteform = new note_edit_form(null, $extradata);
48 // if form was cancelled then return to the notes list of the note
49 if ($noteform->is_cancelled()) {
50 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
53 // if data was submitted and validated, then save it to database
54 if ($formdata = $noteform->get_data()){
55 $note->courseid = $formdata->course;
56 $note->userid = $formdata->user;
57 $note->content = $formdata->content;
58 $note->format = FORMAT_PLAIN;
59 $note->publishstate = $formdata->publishstate;
60 if (note_save($note)) {
61 add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&amp;user='.$note->userid . '#note-' . $note->id, 'update note');
63 // redirect to notes list that contains this note
64 redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
68 if($noteform->is_submitted()) {
69 // if data was submitted with errors, then use it as default for new form
70 $note = $noteform->get_submitted_data(false);
71 }else{
72 // if data was not submitted yet, then used values retrieved from the database
73 $note->user = $note->userid;
74 $note->course = $note->courseid;
75 $note->note = $note->id;
77 $noteform->set_data($note);
78 $strnotes = get_string('editnote', 'notes');
80 // output HTML
81 $crumbs = array(array('name' => $strnotes, 'link' => '', 'type' => 'activity'));
82 print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs));
83 $noteform->display();
84 print_footer();