Skip to content

Commit

Permalink
MDL-64705 notes: Make notes WS return permissions information
Browse files Browse the repository at this point in the history
We need to know some capabilities in order to enable users to manage notes.
  • Loading branch information
jleyva committed Mar 25, 2019
1 parent 3271c39 commit 55a081b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
41 changes: 23 additions & 18 deletions notes/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,15 @@ public static function get_course_notes($courseid, $userid = 0) {

$course = get_course($params['courseid']);

$systemcontext = context_system::instance();
$canmanagesystemnotes = has_capability('moodle/notes:manage', $systemcontext);

if ($course->id == SITEID) {
$context = context_system::instance();
$context = $systemcontext;
$canmanagecoursenotes = $canmanagesystemnotes;
} else {
$context = context_course::instance($course->id);
$canmanagecoursenotes = has_capability('moodle/notes:manage', $context);
}
self::validate_context($context);

Expand All @@ -548,7 +553,7 @@ public static function get_course_notes($courseid, $userid = 0) {
if ($course->id != SITEID) {

require_capability('moodle/notes:view', $context);
$sitenotes = self::create_note_list(0, context_system::instance(), $params['userid'], NOTES_STATE_SITE);
$sitenotes = self::create_note_list(0, $systemcontext, $params['userid'], NOTES_STATE_SITE);
$coursenotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_PUBLIC);
$personalnotes = self::create_note_list($course->id, $context, $params['userid'], NOTES_STATE_DRAFT,
$USER->id);
Expand All @@ -572,6 +577,8 @@ public static function get_course_notes($courseid, $userid = 0) {
'sitenotes' => $sitenotes,
'coursenotes' => $coursenotes,
'personalnotes' => $personalnotes,
'canmanagesystemnotes' => $canmanagesystemnotes,
'canmanagecoursenotes' => $canmanagecoursenotes,
'warnings' => $warnings
);
return $results;
Expand Down Expand Up @@ -607,22 +614,20 @@ protected static function get_note_structure() {
public static function get_course_notes_returns() {
return new external_single_structure(
array(
'sitenotes' => new external_multiple_structure(
new external_single_structure(
self::get_note_structure() , ''
), 'site notes', VALUE_OPTIONAL
),
'coursenotes' => new external_multiple_structure(
new external_single_structure(
self::get_note_structure() , ''
), 'couse notes', VALUE_OPTIONAL
),
'personalnotes' => new external_multiple_structure(
new external_single_structure(
self::get_note_structure() , ''
), 'personal notes', VALUE_OPTIONAL
),
'warnings' => new external_warnings()
'sitenotes' => new external_multiple_structure(
new external_single_structure(self::get_note_structure() , ''), 'site notes', VALUE_OPTIONAL
),
'coursenotes' => new external_multiple_structure(
new external_single_structure(self::get_note_structure() , ''), 'couse notes', VALUE_OPTIONAL
),
'personalnotes' => new external_multiple_structure(
new external_single_structure(self::get_note_structure() , ''), 'personal notes', VALUE_OPTIONAL
),
'canmanagesystemnotes' => new external_value(PARAM_BOOL, 'Whether the user can manage notes at system level.',
VALUE_OPTIONAL),
'canmanagecoursenotes' => new external_value(PARAM_BOOL, 'Whether the user can manage notes at the given course.',
VALUE_OPTIONAL),
'warnings' => new external_warnings()
), 'notes'
);
}
Expand Down
18 changes: 18 additions & 0 deletions notes/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public function test_get_course_notes() {
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
$this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
$this->assertCount(2, $result['coursenotes']);
// Teacher can manage only the course notes.
$this->assertFalse($result['canmanagesystemnotes']);
$this->assertTrue($result['canmanagecoursenotes']);

foreach ($result['coursenotes'] as $coursenote) {
if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
Expand All @@ -328,6 +331,9 @@ public function test_get_course_notes() {
$result = core_notes_external::get_course_notes(0, $student1->id);
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
$this->assertEmpty($result['sitenotes']);
// Teacher can't manage system notes.
$this->assertFalse($result['canmanagesystemnotes']);
$this->assertFalse($result['canmanagecoursenotes']);

foreach ($result['coursenotes'] as $coursenote) {
if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
Expand All @@ -342,13 +348,19 @@ public function test_get_course_notes() {
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
$this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
$this->assertCount(1, $result['sitenotes']);
// Admin user can manage both system and course notes.
$this->assertTrue($result['canmanagesystemnotes']);
$this->assertTrue($result['canmanagecoursenotes']);

$this->setUser($teacher1);
$result = core_notes_external::get_course_notes(0, 0);
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
$this->assertEmpty($result['sitenotes']);
$this->assertEmpty($result['coursenotes']);
$this->assertEmpty($result['personalnotes']);
// Teacher can't manage system notes.
$this->assertFalse($result['canmanagesystemnotes']);
$this->assertFalse($result['canmanagecoursenotes']);

$this->setUser($teacher2);
$result = core_notes_external::get_course_notes($course1->id, $student1->id);
Expand All @@ -363,6 +375,9 @@ public function test_get_course_notes() {

$this->assertCount(1, $result['sitenotes']);
$this->assertCount(2, $result['coursenotes']);
// Teacher can manage only the course notes.
$this->assertFalse($result['canmanagesystemnotes']);
$this->assertTrue($result['canmanagecoursenotes']);

$result = core_notes_external::get_course_notes($course1->id, 0);
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
Expand All @@ -382,6 +397,9 @@ public function test_get_course_notes() {
$result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
$this->assertEquals($notep1->id, $result['personalnotes'][0]['id']);
$this->assertCount(1, $result['personalnotes']);
// Teacher can manage only the course notes.
$this->assertFalse($result['canmanagesystemnotes']);
$this->assertTrue($result['canmanagecoursenotes']);

}

Expand Down
8 changes: 8 additions & 0 deletions notes/upgrade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This file describes API changes in /notes/*,
information provided here is intended especially for developers.

=== 3.7 ===

* External function core_notes_external::get_course_notes now returns this additional two fields:
- canmanagesystemnotes: Whether the user can manage notes at system level.
- canmanagecoursenotes: Whether the user can manage notes at the given course.

0 comments on commit 55a081b

Please sign in to comment.