Skip to content

Commit

Permalink
MDL-30072 - Notes, Webservices - adding Webservices for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnfwlr committed Feb 22, 2013
1 parent f29e62c commit 34348b2
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 45 deletions.
27 changes: 27 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,33 @@
'capabilities'=> 'moodle/notes:manage',
),

'core_notes_delete_notes' => array(
'classname' => 'core_notes_external',
'methodname' => 'delete_notes',
'classpath' => 'notes/externallib.php',
'description' => 'Delete notes',
'type' => 'write',
'capabilities'=> 'moodle/notes:manage',
),

'core_notes_get_notes' => array(
'classname' => 'core_notes_external',
'methodname' => 'get_notes',
'classpath' => 'notes/externallib.php',
'description' => 'Get notes',
'type' => 'read',
'capabilities'=> 'moodle/notes:view',
),

'core_notes_update_notes' => array(
'classname' => 'core_notes_external',
'methodname' => 'update_notes',
'classpath' => 'notes/externallib.php',
'description' => 'Update notes',
'type' => 'write',
'capabilities'=> 'moodle/notes:manage',
),

// === webservice related functions ===

'moodle_webservice_get_siteinfo' => array(
Expand Down
4 changes: 1 addition & 3 deletions notes/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
if (data_submitted() && confirm_sesskey()) {
//if data was submitted and is valid, then delete note
$returnurl = $CFG->wwwroot . '/notes/index.php?course=' . $course->id . '&user=' . $note->userid;
if (note_delete($noteid)) {
add_to_log($note->courseid, 'notes', 'delete', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'delete note');
} else {
if (!note_delete($noteid)) {
print_error('cannotdeletepost', 'notes', $returnurl);
}
redirect($returnurl);
Expand Down
4 changes: 1 addition & 3 deletions notes/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@

/// if data was submitted and validated, then save it to database
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');
}
note_save($note);
// redirect to notes list that contains this note
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid);
}
Expand Down
282 changes: 260 additions & 22 deletions notes/externallib.php

Large diffs are not rendered by default.

24 changes: 19 additions & 5 deletions notes/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ function note_save(&$note) {
// insert new note
$note->created = $note->lastmodified;
$id = $DB->insert_record('post', $note);
$note = $DB->get_record('post', array('id'=>$id));
$note = note_load($id);
$logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid));
$logurl->set_anchor('note-' . $id);

add_to_log($note->courseid, 'notes', 'add', $logurl, 'add note');
} else {
// update old note
$DB->update_record('post', $note);
$note = note_load($note->id);
$logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid));
$logurl->set_anchor('note-' . $note->id);
add_to_log($note->courseid, 'notes', 'update', $logurl , 'update note');
}
unset($note->module);
return true;
Expand All @@ -112,13 +120,19 @@ function note_save(&$note) {
/**
* Deletes a note object based on its id.
*
* @param int $note_id id of the note to delete
* @param int|object $note id of the note to delete, or a note object which is to be deleted.
* @return boolean true if the object was deleted; false otherwise
*/
function note_delete($noteid) {
function note_delete($note) {
global $DB;

return $DB->delete_records('post', array('id'=>$noteid, 'module'=>'notes'));
if (is_int($note)) {
$note = note_load($note);
debugging('Warning: providing note_delete with a note object would improve performance.',DEBUG_DEVELOPER);
}
$logurl = new moodle_url('index.php', array('course'=> $note->courseid, 'user'=>$note->userid));
$logurl->set_anchor('note-' . $note->id);
add_to_log($note->courseid, 'notes', 'delete', $logurl, 'delete note');
return $DB->delete_records('post', array('id'=>$note->id, 'module'=>'notes'));
}

/**
Expand Down
173 changes: 168 additions & 5 deletions notes/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class core_notes_external_testcase extends externallib_advanced_testcase {
*/
public function test_create_notes() {

global $DB, $USER, $DB;
global $DB, $USER;

$this->resetAfterTest(true);

$course = self::getDataGenerator()->create_course();
$course = self::getDataGenerator()->create_course();

// Set the required capabilities by the external function
// Set the required capabilities by the external function.
$contextid = context_course::instance($course->id)->id;
$roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
$this->assignUserCapability('moodle/course:view', $contextid, $roleid);
Expand All @@ -58,7 +58,6 @@ public function test_create_notes() {
$notes = array($note1);

$creatednotes = core_notes_external::create_notes($notes);

// We need to execute the return values cleaning process to simulate the web service server.
$creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);

Expand All @@ -71,10 +70,174 @@ public function test_create_notes() {
$this->assertEquals($thenote->content, $note1['text']);
$this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);

// Call without required capability
// Call without required capability.
$this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
$this->setExpectedException('required_capability_exception');
$creatednotes = core_notes_external::create_notes($notes);
}

public function test_delete_notes() {

global $DB, $USER;

$this->resetAfterTest(true);

$course = self::getDataGenerator()->create_course();

// Set the required capabilities by the external function.
$contextid = context_course::instance($course->id)->id;
$roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
$this->assignUserCapability('moodle/course:view', $contextid, $roleid);

// Create test note data.
$cnote = array();
$cnote['userid'] = $USER->id;
$cnote['publishstate'] = 'personal';
$cnote['courseid'] = $course->id;
$cnote['text'] = 'the text';
$cnote['clientnoteid'] = 4;
$cnotes = array($cnote);
$creatednotes = core_notes_external::create_notes($cnotes);
$creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);

$dnotes1 = array("notes"=>array($creatednotes[0]['noteid']));
$deletednotes1 = core_notes_external::delete_notes($dnotes1);
$deletednotes1 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1);

// Confirm that base note data was deleted correctly.
$notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']);
$this->assertEquals(0, $notdeletedcount);

$dnotes2 = array("notes"=>array(33)); // This note does not exist.
$deletednotes2 = core_notes_external::delete_notes($dnotes2);
$deletednotes2 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2);

$this->assertEquals("note", $deletednotes2[0]["item"]);
$this->assertEquals(33, $deletednotes2[0]["itemid"]);
$this->assertEquals("badid", $deletednotes2[0]["warningcode"]);
$this->assertEquals("Note does not exist", $deletednotes2[0]["message"]);

// Call without required capability.
$creatednotes = core_notes_external::create_notes($cnotes);
$dnotes3 = array("notes"=>array($creatednotes[0]['noteid']));

$this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
$this->setExpectedException('required_capability_exception');
$deletednotes = core_notes_external::delete_notes($dnotes3);
}

public function test_get_notes() {

global $DB, $USER;

$this->resetAfterTest(true);

$course = self::getDataGenerator()->create_course();

// Set the required capabilities by the external function.
$contextid = context_course::instance($course->id)->id;
$roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
$this->assignUserCapability('moodle/notes:view', $contextid, $roleid);
$this->assignUserCapability('moodle/course:view', $contextid, $roleid);

// Create test note data.
$cnote = array();
$cnote['userid'] = $USER->id;
$cnote['publishstate'] = 'personal';
$cnote['courseid'] = $course->id;
$cnote['text'] = 'the text';
$cnotes = array($cnote);

$creatednotes1 = core_notes_external::create_notes($cnotes);
$creatednotes2 = core_notes_external::create_notes($cnotes);
$creatednotes3 = core_notes_external::create_notes($cnotes);

$creatednotes1 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1);
$creatednotes2 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2);
$creatednotes3 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3);

// Note 33 does not exist.
$gnotes = array("notes"=>array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33));
$getnotes = core_notes_external::get_notes($gnotes);
$getnotes = external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes);

$this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
// Confirm that base note data was retrieved correctly.
$this->assertEquals($cnote['userid'], $getnotes["notes"][0]["userid"]);
$this->assertEquals($cnote['text'], $getnotes["notes"][0]["text"]);
$this->assertEquals($cnote['userid'], $getnotes["notes"][1]["userid"]);
$this->assertEquals($cnote['text'], $getnotes["notes"][1]["text"]);
$this->assertEquals($cnote['userid'], $getnotes["notes"][2]["userid"]);
$this->assertEquals($cnote['text'], $getnotes["notes"][2]["text"]);
$this->assertEquals("note", $getnotes["warnings"][0]["item"]);
$this->assertEquals(33, $getnotes["warnings"][0]["itemid"]);
$this->assertEquals("badid", $getnotes["warnings"][0]["warningcode"]);
$this->assertEquals("Note does not exist", $getnotes["warnings"][0]["message"]);

// Call without required capability.
$this->unassignUserCapability('moodle/notes:view', $contextid, $roleid);
$this->setExpectedException('required_capability_exception');
$creatednotes = core_notes_external::get_notes($gnotes);
}

public function test_update_notes() {

global $DB, $USER;

$this->resetAfterTest(true);

$course = self::getDataGenerator()->create_course();

// Set the required capabilities by the external function.
$contextid = context_course::instance($course->id)->id;
$roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
$this->assignUserCapability('moodle/course:view', $contextid, $roleid);

// Create test note data.
$note1 = array();
$note1['userid'] = $USER->id;
$note1['publishstate'] = 'personal';
$note1['courseid'] = $course->id;
$note1['text'] = 'the text';
$note2['userid'] = $USER->id;
$note2['publishstate'] = 'course';
$note2['courseid'] = $course->id;
$note2['text'] = 'the text';
$note3['userid'] = $USER->id;
$note3['publishstate'] = 'site';
$note3['courseid'] = $course->id;
$note3['text'] = 'the text';
$notes1 = array($note1, $note2, $note3);

$creatednotes = core_notes_external::create_notes($notes1);
$creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);

$note2 = array();
$note2["id"] = $creatednotes[0]['noteid'];
$note2['publishstate'] = 'personal';
$note2['text'] = 'the new text';
$note2['format'] = FORMAT_HTML;
$notes2 = array($note2);

$updatednotes = core_notes_external::update_notes($notes2);

$updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes);
$thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));

// Confirm that base note data was updated correctly.
$this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
$this->assertEquals($note2['text'], $thenote->content);

// Call without required capability.
$creatednotes = core_notes_external::create_notes($notes1);
$this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
$this->setExpectedException('required_capability_exception');
$note2 = array();
$note2["id"] = $creatednotes[0]['noteid'];
$note2['publishstate'] = 'personal';
$note2['text'] = 'the new text';
$note2['format'] = FORMAT_HTML;
$notes2 = array($note2);
$updatednotes = core_notes_external::update_notes($notes2);
}
}
4 changes: 1 addition & 3 deletions user/addnote.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
$note->content = $contents[$k];
$note->publishstate = $states[$k];
$note->userid = $v;
if (note_save($note)) {
add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'add note');
}
note_save($note);
}
redirect("$CFG->wwwroot/user/index.php?id=$id");
}
Expand Down
4 changes: 1 addition & 3 deletions user/groupaddnote.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
}
$note->id = 0;
$note->userid = $v;
if (note_save($note)) {
add_to_log($note->courseid, 'notes', 'add', 'index.php?course='.$note->courseid.'&user='.$note->userid . '#note-' . $note->id , 'add note');
}
note_save($note);
}

redirect("$CFG->wwwroot/user/index.php?id=$id");
Expand Down
3 changes: 2 additions & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
defined('MOODLE_INTERNAL') || die();


$version = 2013022200.00; // YYYYMMDD = weekly release date of this DEV branch

$version = 2013022300.00; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

Expand Down

0 comments on commit 34348b2

Please sign in to comment.