MDL-16164 improved use of forms in notes code and some other minor cleanup
authorskodak <skodak>
Thu, 21 Aug 2008 21:40:29 +0000 (21:40 +0000)
committerskodak <skodak>
Thu, 21 Aug 2008 21:40:29 +0000 (21:40 +0000)
notes/add.php [deleted file]
notes/delete.php
notes/edit.php
notes/edit_form.php
notes/index.php
notes/lib.php

diff --git a/notes/add.php b/notes/add.php
deleted file mode 100644 (file)
index 5e94d46..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-<?php // $Id$
-
-    require_once('../config.php');
-    require_once('lib.php');
-
-/// retrieve parameters
-    $courseid      = required_param('course', PARAM_INT);
-    $userid        = required_param('user', PARAM_INT);
-
-/// locate course information
-    if (!($course = $DB->get_record('course', array('id'=>$courseid)))) {
-        print_error('invalidcourseid');
-    }
-
-/// 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);
-
-
-/// locate user information
-    if (!($user = $DB->get_record('user', array('id'=>$userid)))) {
-        print_error('invaliduserid');
-    }
-
-/// build-up form
-    require_once('edit_form.php');
-
-/// create form
-    $noteform = new note_edit_form();
-
-/// if form was cancelled then return to the previous notes list
-    if ($noteform->is_cancelled()) {
-        redirect($CFG->wwwroot . '/notes/index.php?course=' . $courseid . '&amp;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.'&amp;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 . '&amp;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();
-    } 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
-    $nav = array();
-    if (has_capability('moodle/course:viewparticipants', $context) || has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM))) {
-        $nav[] = array('name' => get_string('participants'), 'link' => $CFG->wwwroot . '/user/index.php?id=' . $course->id, 'type' => 'misc');
-    }
-    $nav[] = array('name' => fullname($user), 'link' => $CFG->wwwroot . '/user/view.php?id=' . $user->id. '&amp;course=' . $course->id, 'type' => 'misc');
-    $nav[] = array('name' => get_string('notes', 'notes'), 'link' => $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&amp;user=' . $user->id, 'type' => 'misc');
-    $nav[] = array('name' => $strnotes, 'link' => '', 'type' => 'activity');
-
-    print_header($course->shortname . ': ' . $strnotes, $course->fullname, build_navigation($nav));
-
-    print_heading(fullname($user));
-
-    $noteform->display();
-    print_footer();
-?>
index 94269c0cdef0289b8e4f7d034a8af9686b05044e..777e87d2318a8d2900381dc7c3739cc005ebda1d 100644 (file)
@@ -4,7 +4,7 @@ require_once('../config.php');
 require_once('lib.php');
 
 // retrieve parameters
-$noteid       = required_param('note', PARAM_INT);
+$noteid = required_param('id', PARAM_INT);
 
 // locate note information
 if (!$note = note_load($noteid)) {
@@ -22,7 +22,7 @@ if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
     }
 
 // require login to access notes
-require_login($course->id);
+require_login($course);
 
 // locate context information
 $context = get_context_instance(CONTEXT_COURSE, $course->id);
@@ -41,11 +41,12 @@ if (data_submitted() && confirm_sesskey()) {
         print_error('cannotdeletepost', 'notes', $returnurl);
     }
     redirect($returnurl);
+
 } else {
 // if data was not submitted yet, then show note data with a delete confirmation form
     $strnotes = get_string('notes', 'notes');
-    $optionsyes = array('note'=>$noteid, 'sesskey'=>sesskey());
-    $optionsno = array('course'=>$course->id, 'user'=>$note->userid);
+    $optionsyes = array('id'=>$noteid, 'sesskey'=>sesskey());
+    $optionsno  = array('course'=>$course->id, 'user'=>$note->userid);
 
 // output HTML
     if (has_capability('moodle/course:viewparticipants', $context) || has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM))) {
index dbc8ac3fd8d9eb1523d53e893baad4173cf915ea..72be122eecdd8bc0c8411276e902a2bf5aa8b5cd 100644 (file)
@@ -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))) {
     }
 
 /// 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 . '&amp;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.'&amp;user='.$note->userid . '#note-' . $note->id, 'update note');
         }
         redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;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();
index 0eddb1366b5bd01867c3069888ad8798c5a65c60..7a94d5bd48801cc54cef093a8f81de2a971a940b 100644 (file)
@@ -21,14 +21,14 @@ class note_edit_form extends moodleform {
 
         $this->add_action_buttons();
 
-        $mform->addElement('hidden', 'course');
+        $mform->addElement('hidden', 'courseid');
         $mform->setType('course', PARAM_INT);
 
-        $mform->addElement('hidden', 'user');
+        $mform->addElement('hidden', 'userid');
         $mform->setType('user', PARAM_INT);
 
-        $mform->addElement('hidden', 'note');
-        $mform->setType('note', PARAM_INT);
+        $mform->addElement('hidden', 'id');
+        $mform->setType('id', PARAM_INT);
     }
 }
 ?>
\ No newline at end of file
index 2ebb6734238c35214e0eccbaa6565538b141298f..d120e359efbf05d1cabe14bc9ddbb44f26324c7f 100644 (file)
@@ -51,7 +51,7 @@
     }
 
 /// require login to access notes
-    require_login($course->id);
+    require_login($course);
     add_to_log($courseid, 'notes', 'view', 'index.php?course='.$courseid.'&amp;user='.$userid, 'view notes');
 
 
@@ -77,7 +77,7 @@
 
     $showroles = 1;
     $currenttab = 'notes';
-    require_once($CFG->dirroot .'/user/tabs.php');
+    require($CFG->dirroot .'/user/tabs.php');
 
     $strsitenotes = get_string('sitenotes', 'notes');
     $strcoursenotes = get_string('coursenotes', 'notes');
index 94162606a0d01be7f0fe4f9ad24940c2fce192a0..d0981424395f93f39eb2807e44b07597350926ff 100644 (file)
@@ -89,18 +89,18 @@ function note_save(&$note) {
     $note->module       = 'notes';
     $note->lastmodified = time();
     $note->usermodified = $USER->id;
-    if(empty($note->format)) {
+    if (empty($note->format)) {
         $note->format = FORMAT_PLAIN;
     }
-    if(empty($note->publishstate)) {
+    if (empty($note->publishstate)) {
         $note->publishstate = NOTES_STATE_PUBLIC;
     }
     // save data
-    if(empty($note->id)) {
+    if (empty($note->id)) {
         // insert new note
         $note->created = $note->lastmodified;
         if ($id = $DB->insert_record('post', $note)) {
-            $note->id = $id;
+            $note = $DB->get_record('post', array('id'=>$id));
             $result = true;
         } else {
             $result = false;
@@ -175,7 +175,7 @@ function note_print($note, $detail = NOTES_SHOW_FULL) {
         return;
     }
     $context = get_context_instance(CONTEXT_COURSE, $note->courseid);
-    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
+    $systemcontext = get_context_instance(CONTEXT_SYSTEM);
 
     $authoring = new object();
     $authoring->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$author->id.'&amp;course='.$note->courseid.'">'.fullname($author).'</a>';
@@ -186,7 +186,7 @@ function note_print($note, $detail = NOTES_SHOW_FULL) {
         '" id="note-'. $note->id .'">';
 
     // print note head (e.g. author, user refering to, etc)
-    if($detail & NOTES_SHOW_HEAD) {
+    if ($detail & NOTES_SHOW_HEAD) {
         echo '<div class="header">';
         echo '<div class="user">';
         print_user_picture($user, $note->courseid, $user->picture);
@@ -198,19 +198,19 @@ function note_print($note, $detail = NOTES_SHOW_FULL) {
     }
 
     // print note content
-    if($detail & NOTES_SHOW_BODY) {
+    if ($detail & NOTES_SHOW_BODY) {
         echo '<div class="content">';
         echo format_text($note->content, $note->format);
         echo '</div>';
     }
 
     // print note options (e.g. delete, edit)
-    if($detail & NOTES_SHOW_FOOT) {
-        if (has_capability('moodle/notes:manage', $sitecontext) && $note->publishstate == NOTES_STATE_SITE ||
+    if ($detail & NOTES_SHOW_FOOT) {
+        if (has_capability('moodle/notes:manage', $systemcontext) && $note->publishstate == NOTES_STATE_SITE ||
             has_capability('moodle/notes:manage', $context) && ($note->publishstate == NOTES_STATE_PUBLIC || $note->usermodified == $USER->id)) {
             echo '<div class="footer"><p>';
-            echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?note='.$note->id. '">'. get_string('edit') .'</a> | ';
-            echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?note='.$note->id. '">'. get_string('delete') .'</a>';
+            echo '<a href="'.$CFG->wwwroot.'/notes/edit.php?id='.$note->id. '">'. get_string('edit') .'</a> | ';
+            echo '<a href="'.$CFG->wwwroot.'/notes/delete.php?id='.$note->id. '">'. get_string('delete') .'</a>';
             echo '</p></div>';
         }
     }
@@ -253,7 +253,7 @@ function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $coursei
     }
     if ($addcourseid) {
         if ($userid) {
-           echo '<p><a href="'. $CFG->wwwroot .'/notes/add.php?course=' . $addcourseid . '&amp;user=' . $userid . '&amp;state=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
+           echo '<p><a href="'. $CFG->wwwroot .'/notes/edit.php?courseid=' . $addcourseid . '&amp;userid=' . $userid . '&amp;publishstate=' . $state . '">' . get_string('addnewnote', 'notes') . '</a></p>';
         } else {
            echo '<p><a href="'. $CFG->wwwroot .'/user/index.php?id=' . $addcourseid. '">' . get_string('addnewnoteselect', 'notes') . '</a></p>';
         }