Search moodle.org's
Developer Documentation


/notes/ -> edit.php (source)
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  require_once('../config.php');
  18  require_once ('lib.php');
  19  require_once ('edit_form.php');
  20  
  21  $noteid = optional_param('id', 0, PARAM_INT);
  22  
  23  $url = new moodle_url('/notes/edit.php');
  24  
  25  if ($noteid) {
  26      // Existing note.
  27      $url->param('id', $noteid);
  28      if (!$note = note_load($noteid)) {
  29          print_error('invalidid', 'notes');
  30      }
  31  
  32  } else {
  33      // Adding new note.
  34      $courseid = required_param('courseid', PARAM_INT);
  35      $userid   = required_param('userid', PARAM_INT);
  36      $state    = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);
  37  
  38      $note = new stdClass();
  39      $note->courseid     = $courseid;
  40      $note->userid       = $userid;
  41      $note->publishstate = $state;
  42  
  43      $url->param('courseid', $courseid);
  44      $url->param('userid', $userid);
  45      if ($state !== NOTES_STATE_PUBLIC) {
  46          $url->param('publishstate', $state);
  47      }
  48  }
  49  
  50  $PAGE->set_url($url);
  51  
  52  if (!$course = $DB->get_record('course', array('id' => $note->courseid))) {
  53      print_error('invalidcourseid');
  54  }
  55  
  56  require_login($course);
  57  
  58  if (empty($CFG->enablenotes)) {
  59      print_error('notesdisabled', 'notes');
  60  }
  61  
  62  $context = context_course::instance($course->id);
  63  require_capability('moodle/notes:manage', $context);
  64  
  65  if (!$user = $DB->get_record('user', array('id' => $note->userid))) {
  66      print_error('invaliduserid');
  67  }
  68  
  69  $noteform = new note_edit_form();
  70  $noteform->set_data($note);
  71  
  72  // If form was cancelled then return to the notes list of the note.
  73  if ($noteform->is_cancelled()) {
  74      redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
  75  }
  76  
  77  // If data was submitted and validated, then save it to database.
  78  if ($note = $noteform->get_data()) {
  79      if ($noteid) {
  80          // A noteid has been used, we don't allow editing of course or user so
  81          // lets unset them to be sure we never change that by accident.
  82          unset($note->courseid);
  83          unset($note->userid);
  84      }
  85      note_save($note);
  86      // Redirect to notes list that contains this note.
  87      redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
  88  }
  89  
  90  if ($noteid) {
  91      $strnotes = get_string('editnote', 'notes');
  92  } else {
  93      $strnotes = get_string('addnewnote', 'notes');
  94  }
  95  
  96  // Output HTML.
  97  $link = null;
  98  if (has_capability('moodle/course:viewparticipants', $context)
  99      || has_capability('moodle/site:viewparticipants', context_system::instance())) {
 100  
 101      $link = new moodle_url('/user/index.php', array('id' => $course->id));
 102  }
 103  $PAGE->navbar->add(get_string('participants'), $link);
 104  $PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)));
 105  $PAGE->navbar->add(get_string('notes', 'notes'),
 106                     new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $course->id)));
 107  $PAGE->navbar->add($strnotes);
 108  $PAGE->set_title($course->shortname . ': ' . $strnotes);
 109  $PAGE->set_heading($course->fullname);
 110  
 111  echo $OUTPUT->header();
 112  echo $OUTPUT->heading(fullname($user));
 113  
 114  $noteform->display();
 115  echo $OUTPUT->footer();

Search This Site: