mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-16164 improved use of forms in notes code and some other minor cleanup
This commit is contained in:
parent
5b4b959b5a
commit
241bcec5fa
6 changed files with 50 additions and 136 deletions
|
@ -2,14 +2,28 @@
|
|||
|
||||
require_once('../config.php');
|
||||
require_once('lib.php');
|
||||
require_once('edit_form.php');
|
||||
|
||||
/// retrieve parameters
|
||||
$noteid = required_param('note', PARAM_INT);
|
||||
$noteid = optional_param('id', 0, PARAM_INT);
|
||||
|
||||
/// locate note information
|
||||
if (!$note = note_load($noteid)) {
|
||||
print_error('invalidid', 'notes');
|
||||
}
|
||||
if ($noteid) {
|
||||
//existing note
|
||||
if (!$note = note_load($noteid)) {
|
||||
print_error('invalidid', 'notes');
|
||||
}
|
||||
|
||||
} else {
|
||||
// adding new note
|
||||
$courseid = required_param('courseid', PARAM_INT);
|
||||
$userid = required_param('userid', PARAM_INT);
|
||||
$state = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);
|
||||
|
||||
$note = new object();
|
||||
$note->courseid = $courseid;
|
||||
$note->userid = $userid;
|
||||
$note->publishstate = $state;
|
||||
}
|
||||
|
||||
/// locate course information
|
||||
if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
|
||||
|
@ -22,34 +36,25 @@
|
|||
}
|
||||
|
||||
/// require login to access notes
|
||||
require_login($course->id);
|
||||
require_login($course);
|
||||
|
||||
/// locate context information
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
|
||||
/// check capability
|
||||
require_capability('moodle/notes:manage', $context);
|
||||
|
||||
/// build-up form
|
||||
require_once('edit_form.php');
|
||||
|
||||
/// get option values for the user select
|
||||
|
||||
/// create form
|
||||
$noteform = new note_edit_form();
|
||||
|
||||
/// set defaults
|
||||
$noteform->set_data($note);
|
||||
|
||||
/// if form was cancelled then return to the notes list of the note
|
||||
if ($noteform->is_cancelled()) {
|
||||
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid);
|
||||
}
|
||||
|
||||
/// if data was submitted and validated, then save it to database
|
||||
if ($formdata = $noteform->get_data()){
|
||||
$note->courseid = $formdata->course;
|
||||
$note->userid = $formdata->user;
|
||||
$note->content = $formdata->content;
|
||||
$note->format = FORMAT_PLAIN;
|
||||
$note->publishstate = $formdata->publishstate;
|
||||
if ($note = $noteform->get_data()){
|
||||
if (note_save($note)) {
|
||||
add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id, 'update note');
|
||||
}
|
||||
|
@ -57,18 +62,11 @@
|
|||
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid);
|
||||
}
|
||||
|
||||
|
||||
if ($noteform->is_submitted()) {
|
||||
// if data was submitted with errors, then use it as default for new form
|
||||
$note = $noteform->get_submitted_data();
|
||||
if ($noteid) {
|
||||
$strnotes = get_string('editnote', 'notes');
|
||||
} else {
|
||||
// if data was not submitted yet, then used values retrieved from the database
|
||||
$note->user = $note->userid;
|
||||
$note->course = $note->courseid;
|
||||
$note->note = $note->id;
|
||||
$strnotes = get_string('addnewnote', 'notes');
|
||||
}
|
||||
$noteform->set_data($note);
|
||||
$strnotes = get_string('editnote', 'notes');
|
||||
|
||||
/// output HTML
|
||||
$nav = array();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue