From 55a081bd875a1e4cf3b1b8a95cc712523aa8eea2 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 30 Jan 2019 17:01:48 +0100 Subject: [PATCH] MDL-64705 notes: Make notes WS return permissions information We need to know some capabilities in order to enable users to manage notes. --- notes/externallib.php | 41 ++++++++++++++++++-------------- notes/tests/externallib_test.php | 18 ++++++++++++++ notes/upgrade.txt | 8 +++++++ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 notes/upgrade.txt diff --git a/notes/externallib.php b/notes/externallib.php index c393fd81b30..07459c747b5 100644 --- a/notes/externallib.php +++ b/notes/externallib.php @@ -534,10 +534,15 @@ class core_notes_external extends external_api { $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); @@ -548,7 +553,7 @@ class core_notes_external extends external_api { 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); @@ -572,6 +577,8 @@ class core_notes_external extends external_api { 'sitenotes' => $sitenotes, 'coursenotes' => $coursenotes, 'personalnotes' => $personalnotes, + 'canmanagesystemnotes' => $canmanagesystemnotes, + 'canmanagecoursenotes' => $canmanagecoursenotes, 'warnings' => $warnings ); return $results; @@ -607,22 +614,20 @@ class core_notes_external extends external_api { 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' ); } diff --git a/notes/tests/externallib_test.php b/notes/tests/externallib_test.php index 353b88c2eae..8adf7e07bec 100644 --- a/notes/tests/externallib_test.php +++ b/notes/tests/externallib_test.php @@ -308,6 +308,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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) { @@ -328,6 +331,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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) { @@ -342,6 +348,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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); @@ -349,6 +358,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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); @@ -363,6 +375,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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); @@ -382,6 +397,9 @@ class core_notes_externallib_testcase extends externallib_advanced_testcase { $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']); } diff --git a/notes/upgrade.txt b/notes/upgrade.txt new file mode 100644 index 00000000000..af35cb2bacd --- /dev/null +++ b/notes/upgrade.txt @@ -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. \ No newline at end of file