Skip to content

Commit

Permalink
MDL-46316 notes: coding style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Jul 18, 2014
1 parent ec36faf commit ed6fc26
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 93 deletions.
49 changes: 31 additions & 18 deletions notes/delete.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once('../config.php');
require_once('lib.php');

// retrieve parameters
$noteid = required_param('id', PARAM_INT);

$PAGE->set_url('/notes/delete.php', array('id'=>$noteid));
$PAGE->set_url('/notes/delete.php', array('id' => $noteid));

// locate note information
if (!$note = note_load($noteid)) {
print_error('invalidid');
}

// locate course information
if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
if (!$course = $DB->get_record('course', array('id' => $note->courseid))) {
print_error('invalidcourseid');
}

// require login to access notes
require_login($course);

if (empty($CFG->enablenotes)) {
Expand All @@ -29,41 +39,44 @@
print_error('invaliduserid');
}

// locate context information
$context = context_course::instance($course->id);

// check capability
if (!has_capability('moodle/notes:manage', $context)) {
print_error('nopermissiontodelete', 'notes');
}

if (data_submitted() && confirm_sesskey()) {
//if data was submitted and is valid, then delete note
// If data was submitted and is valid, then delete note.
$returnurl = $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&amp;user=' . $note->userid;
if (!note_delete($note)) {
print_error('cannotdeletepost', 'notes', $returnurl);
}
redirect($returnurl);

} else {
// if data was not submitted yet, then show note data with a delete confirmation form
// If data was not submitted yet, then show note data with a delete confirmation form.
$strnotes = get_string('notes', 'notes');
$optionsyes = array('id'=>$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
// Output HTML.
$link = null;
if (has_capability('moodle/course:viewparticipants', $context) || has_capability('moodle/site:viewparticipants', context_system::instance())) {
$link = new moodle_url('/user/index.php',array('id'=>$course->id));
if (has_capability('moodle/course:viewparticipants', $context)
|| has_capability('moodle/site:viewparticipants', context_system::instance())) {

$link = new moodle_url('/user/index.php', array('id' => $course->id));
}
$PAGE->navbar->add(get_string('participants'), $link);
$PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$course->id)));
$PAGE->navbar->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('user'=>$user->id,'course'=>$course->id)));
$PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)));
$PAGE->navbar->add(get_string('notes', 'notes'),
new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $course->id)));
$PAGE->navbar->add(get_string('delete'));
$PAGE->set_title($course->shortname . ': ' . $strnotes);
$PAGE->set_heading($course->fullname);
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string('deleteconfirm', 'notes'), new moodle_url('delete.php',$optionsyes), new moodle_url('index.php',$optionsno));
echo $OUTPUT->confirm(get_string('deleteconfirm', 'notes'),
new moodle_url('delete.php', $optionsyes),
new moodle_url('index.php', $optionsno));
echo '<br />';
note_print($note, NOTES_SHOW_BODY | NOTES_SHOW_HEAD);
echo $OUTPUT->footer();
Expand Down
48 changes: 29 additions & 19 deletions notes/edit.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once('../config.php');
require_once('lib.php');
require_once('edit_form.php');

/// retrieve parameters
$noteid = optional_param('id', 0, PARAM_INT);

$url = new moodle_url('/notes/edit.php');

if ($noteid) {
//existing note
// Existing note.
$url->param('id', $noteid);
if (!$note = note_load($noteid)) {
print_error('invalidid', 'notes');
}

} else {
// adding new note
// Adding new note.
$courseid = required_param('courseid', PARAM_INT);
$userid = required_param('userid', PARAM_INT);
$state = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);
Expand All @@ -36,47 +49,41 @@

$PAGE->set_url($url);

/// locate course information
if (!$course = $DB->get_record('course', array('id'=>$note->courseid))) {
if (!$course = $DB->get_record('course', array('id' => $note->courseid))) {
print_error('invalidcourseid');
}

/// require login to access notes
require_login($course);

if (empty($CFG->enablenotes)) {
print_error('notesdisabled', 'notes');
}

/// locate context information
$context = context_course::instance($course->id);
require_capability('moodle/notes:manage', $context);

if (!$user = $DB->get_record('user', array('id' => $note->userid))) {
print_error('invaliduserid');
}

/// 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 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 ($note = $noteform->get_data()){
// If data was submitted and validated, then save it to database.
if ($note = $noteform->get_data()) {
if ($noteid) {
// A noteid has been used, we don't allow editing of course or user so
// lets unset them to be sure we never change that by accident.
unset($note->courseid);
unset($note->userid);
}
note_save($note);
// redirect to notes list that contains this note
// Redirect to notes list that contains this note.
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&amp;user=' . $note->userid);
}

Expand All @@ -86,14 +93,17 @@
$strnotes = get_string('addnewnote', 'notes');
}

/// output HTML
// Output HTML.
$link = null;
if (has_capability('moodle/course:viewparticipants', $context) || has_capability('moodle/site:viewparticipants', context_system::instance())) {
$link = new moodle_url('/user/index.php',array('id'=>$course->id));
if (has_capability('moodle/course:viewparticipants', $context)
|| has_capability('moodle/site:viewparticipants', context_system::instance())) {

$link = new moodle_url('/user/index.php', array('id' => $course->id));
}
$PAGE->navbar->add(get_string('participants'), $link);
$PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id'=>$user->id,'course'=>$course->id)));
$PAGE->navbar->add(get_string('notes', 'notes'), new moodle_url('/notes/index.php', array('user'=>$user->id,'course'=>$course->id)));
$PAGE->navbar->add(fullname($user), new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)));
$PAGE->navbar->add(get_string('notes', 'notes'),
new moodle_url('/notes/index.php', array('user' => $user->id, 'course' => $course->id)));
$PAGE->navbar->add($strnotes);
$PAGE->set_title($course->shortname . ': ' . $strnotes);
$PAGE->set_heading($course->fullname);
Expand Down
23 changes: 20 additions & 3 deletions notes/edit_form.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}

require_once($CFG->libdir.'/formslib.php');

class note_edit_form extends moodleform {

function definition() {
/**
* Define the form for editing notes
*/
public function definition() {
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string('note', 'notes'));

$mform->addElement('textarea', 'content', get_string('content', 'notes'), array('rows'=>15, 'cols'=>40));
$mform->addElement('textarea', 'content', get_string('content', 'notes'), array('rows' => 15, 'cols' => 40));
$mform->setType('content', PARAM_RAW);
$mform->addRule('content', get_string('nocontent', 'notes'), 'required', null, 'client');

Expand Down
38 changes: 29 additions & 9 deletions notes/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ public static function create_notes($notes = array()) {

// Retrieve all courses.
$courseids = array();
foreach($params['notes'] as $note) {
foreach ($params['notes'] as $note) {
$courseids[] = $note['courseid'];
}
$courses = $DB->get_records_list("course", "id", $courseids);

// Retrieve all users of the notes.
$userids = array();
foreach($params['notes'] as $note) {
foreach ($params['notes'] as $note) {
$userids[] = $note['userid'];
}
list($sqluserids, $sqlparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid_');
Expand Down Expand Up @@ -191,7 +191,7 @@ public static function create_notes_returns() {
new external_single_structure(
array(
'clientnoteid' => new external_value(PARAM_ALPHANUMEXT, 'your own id for the note', VALUE_OPTIONAL),
'noteid' => new external_value(PARAM_INT, 'test this to know if it success: id of the created note when successed, -1 when failed'),
'noteid' => new external_value(PARAM_INT, 'ID of the created note when successful, -1 when failed'),
'errormessage' => new external_value(PARAM_TEXT, 'error message - if failed', VALUE_OPTIONAL)
)
)
Expand Down Expand Up @@ -241,7 +241,10 @@ public static function delete_notes($notes = array()) {
self::validate_context($context);
require_capability('moodle/notes:manage', $context);
if (!note_delete($note)) {
$warnings[] = array(array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'savedfailed', 'message'=>'Note could not be modified'));
$warnings[] = array(array('item' => 'note',
'itemid' => $noteid,
'warningcode' => 'savedfailed',
'message' => 'Note could not be modified'));
}
} else {
$warnings[] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
Expand Down Expand Up @@ -307,14 +310,22 @@ public static function get_notes($notes) {
$context = context_course::instance($note->courseid);
self::validate_context($context);
require_capability('moodle/notes:view', $context);
list($gotnote['text'], $gotnote['format']) = external_format_text($note->content, $note->format, $context->id, 'notes', '', '');
list($gotnote['text'], $gotnote['format']) = external_format_text($note->content,
$note->format,
$context->id,
'notes',
'',
'');
$gotnote['noteid'] = $note->id;
$gotnote['userid'] = $note->userid;
$gotnote['publishstate'] = $note->publishstate;
$gotnote['courseid'] = $note->courseid;
$resultnotes["notes"][] = $gotnote;
} else {
$resultnotes["warnings"][] = array('item'=>'note', 'itemid'=>$noteid, 'warningcode'=>'badid', 'message'=>'Note does not exist');
$resultnotes["warnings"][] = array('item' => 'note',
'itemid' => $noteid,
'warningcode' => 'badid',
'message' => 'Note does not exist');
}
}
return $resultnotes;
Expand Down Expand Up @@ -417,14 +428,23 @@ public static function update_notes($notes = array()) {
$dbnote->courseid = SITEID;
break;
default:
$warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'badparam', 'message'=>'Provided publishstate incorrect');
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'badparam',
'message' => 'Provided publishstate incorrect');
break;
}
if (!note_save($dbnote)) {
$warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'savedfailed', 'message'=>'Note could not be modified');
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'savedfailed',
'message' => 'Note could not be modified');
}
} else {
$warnings[] = array('item'=>'note', 'itemid'=>$note["id"], 'warningcode'=>'badid', 'message'=>'Note does not exist');
$warnings[] = array('item' => 'note',
'itemid' => $note["id"],
'warningcode' => 'badid',
'message' => 'Note does not exist');
}
}
return $warnings;
Expand Down
Loading

0 comments on commit ed6fc26

Please sign in to comment.