From 7e7f6fda3b6dd71a57b07f68391b5fa8232c5e8a Mon Sep 17 00:00:00 2001 From: moodler Date: Fri, 31 Aug 2007 04:35:42 +0000 Subject: [PATCH] Big cleanups of the code for neatness and performance --- notes/add.php | 139 +++++++++++++++++++++-------------------- notes/edit.php | 148 ++++++++++++++++++++++---------------------- notes/edit_form.php | 6 +- 3 files changed, 148 insertions(+), 145 deletions(-) diff --git a/notes/add.php b/notes/add.php index c24c01470e4..efa80454ee7 100644 --- a/notes/add.php +++ b/notes/add.php @@ -1,78 +1,81 @@ id); - -// locate context information -$context = get_context_instance(CONTEXT_COURSE, $course->id); - -// check capability -if (!has_capability('moodle/notes:manage', $context)) { - error('You may not create notes'); -} - -// build-up form -require_once('edit_form.php'); -// get option values for the user select -$extradata['userlist'] = array(); -$usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id IN (SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid={$context->id})"; -$userlist = get_records_sql($usersincourse); -// format userdata using fullname -if($userlist) { - foreach($userlist as $user) { - $extradata['userlist'][$user->id] = fullname($user); +/// locate course information + if (!($course = get_record('course', 'id', $courseid))) { + error('Incorrect course id found'); } -} -// create form -$noteform = new note_edit_form(null, $extradata); -// if form was cancelled then return to the previous notes list -if ($noteform->is_cancelled()) { - redirect($CFG->wwwroot . '/notes/index.php?course=' . $courseid . '&user=' . $userid); -} +/// require login to access notes + require_login($course->id); -// if data was submitted and validated, then save it to database -if ($formdata = $noteform->get_data()) { - $note = new object(); - $note->courseid = $formdata->course; - $note->content = $formdata->content; - $note->format = FORMAT_PLAIN; - $note->userid = $formdata->user; - $note->publishstate = $formdata->publishstate; - if (note_save($note)) { - add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'add note'); +/// locate context information + $context = get_context_instance(CONTEXT_COURSE, $course->id); + +/// check capability + require_capability('moodle/notes:manage', $context); + + +/// locate user information + if (!($user = get_record('user', 'id', $userid))) { + error('Incorrect user id found'); } -// redirect to notes list that contains this note - 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(false); -} else { -// if data was not submitted yet, then use default values - $note = new object(); - $note->id = 0; - $note->course = $courseid; - $note->user = $userid; - $note->publishstate = optional_param('state', NOTES_STATE_PUBLIC, PARAM_ALPHA); -} -$noteform->set_data($note); -$strnotes = get_string('addnewnote', 'notes'); +/// build-up form + require_once('edit_form.php'); -// output HTML -$crumbs = array(array('name' => $strnotes, 'link' => '', 'type' => 'activity')); -print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs)); -$noteform->display(); -print_footer(); +/// create form + $noteform = new note_edit_form(null, $extradata); + +/// if form was cancelled then return to the previous notes list + if ($noteform->is_cancelled()) { + redirect($CFG->wwwroot . '/notes/index.php?course=' . $courseid . '&user=' . $userid); + } + +/// if data was submitted and validated, then save it to database + if ($formdata = $noteform->get_data()) { + $note = new object(); + $note->courseid = $formdata->course; + $note->content = $formdata->content; + $note->format = FORMAT_PLAIN; + $note->userid = $formdata->user; + $note->publishstate = $formdata->publishstate; + if (note_save($note)) { + add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'add note'); + } + // redirect to notes list that contains this note + 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(false); + } else { + // if data was not submitted yet, then use default values + $note = new object(); + $note->id = 0; + $note->course = $courseid; + $note->user = $userid; + $note->publishstate = optional_param('state', NOTES_STATE_PUBLIC, PARAM_ALPHA); + } + $noteform->set_data($note); + $strnotes = get_string('addnewnote', 'notes'); + +/// output HTML + $crumbs = array(); + $crumbs[] = array('name' => fullname($user), 'link' => $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&user=' . $user->id, 'type' => 'misc'); + $crumbs[] = array('name' => $strnotes, 'link' => '', 'type' => 'activity'); + + print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs)); + + print_heading(fullname($user)); + + $noteform->display(); + print_footer(); +?> diff --git a/notes/edit.php b/notes/edit.php index f8b6f92b18c..a71a14db211 100644 --- a/notes/edit.php +++ b/notes/edit.php @@ -1,84 +1,84 @@ courseid)) { - error('Incorrect course id found'); -} -// require login to access notes -require_login($course->id); - -// locate context information -$context = get_context_instance(CONTEXT_COURSE, $course->id); - -// check capability -if (!has_capability('moodle/notes:manage', $context)) { - error('You may not modify notes'); -} - -// build-up form -require_once('edit_form.php'); -// get option values for the user select -$extradata['userlist'] = array(); -if ($course->id == SITEID) { - $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id={$userid}"; -} else { - $usersincourse = "SELECT * FROM {$CFG->prefix}user WHERE id IN (SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid={$context->id})"; -} -$userlist = get_records_sql($usersincourse); -// format userdata using fullname -if($userlist) { - foreach($userlist as $user) { - $extradata['userlist'][$user->id] = fullname($user); +/// locate note information + if (!$note = note_load($noteid)) { + error('Incorrect note id specified'); } -} -// create form -$noteform = new note_edit_form(null, $extradata); -// 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_save($note)) { - add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id, 'update note'); +/// locate course information + if (!$course = get_record('course', 'id', $note->courseid)) { + error('Incorrect course id found'); + } + +/// locate user information + if (!$user = get_record('user', 'id', $note->userid)) { + error('Incorrect user id found'); + } + +/// require login to access notes + require_login($course->id); + +/// 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(null); + +/// 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_save($note)) { + add_to_log($note->courseid, 'notes', 'update', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id, 'update note'); + } + // redirect to notes list that contains this note + redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid); } -// redirect to notes list that contains this note - 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(false); -}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; -} -$noteform->set_data($note); -$strnotes = get_string('editnote', 'notes'); + if ($noteform->is_submitted()) { + // if data was submitted with errors, then use it as default for new form + $note = $noteform->get_submitted_data(false); + } 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; + } + $noteform->set_data($note); + $strnotes = get_string('editnote', 'notes'); -// output HTML -$crumbs = array(array('name' => $strnotes, 'link' => '', 'type' => 'activity')); -print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs)); -$noteform->display(); -print_footer(); +/// output HTML + $crumbs = array(); + $crumbs[] = array('name' => fullname($user), 'link' => $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&user=' . $user->id, 'type' => 'misc'); + $crumbs[] = array('name' => $strnotes, 'link' => '', 'type' => 'activity'); + + print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($crumbs)); + + print_heading(fullname($user)); + + $noteform->display(); + print_footer(); +?> diff --git a/notes/edit_form.php b/notes/edit_form.php index 7577e2698fb..3a3b9a88598 100644 --- a/notes/edit_form.php +++ b/notes/edit_form.php @@ -10,9 +10,6 @@ class note_edit_form extends moodleform { $strpublishstate = get_string('publishstate', 'notes'); $mform->addElement('header', 'general', get_string('note', 'notes')); - $mform->addElement('select', 'user', get_string('user'), $this->_customdata['userlist']); - $mform->addRule('user', get_string('nouser', 'notes'), 'required', null, 'client'); - $mform->addElement('textarea', 'content', $strcontent, array('rows'=>15, 'cols'=>40)); $mform->setType('content', PARAM_RAW); $mform->addRule('content', get_string('nocontent', 'notes'), 'required', null, 'client'); @@ -28,6 +25,9 @@ class note_edit_form extends moodleform { $mform->addElement('hidden', 'course'); $mform->setType('course', PARAM_INT); + $mform->addElement('hidden', 'user'); + $mform->setType('user', PARAM_INT); + $mform->addElement('hidden', 'note'); $mform->setType('note', PARAM_INT); }