MDL-64705 notes: Make notes WS return permissions information

We need to know some capabilities in order to enable users to manage notes.
This commit is contained in:
Juan Leyva
2019-03-25 19:01:20 +01:00
parent 3271c39c74
commit 55a081bd87
3 changed files with 49 additions and 18 deletions
+23 -18
View File
@@ -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'
);
}
+18
View File
@@ -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']);
}
+8
View File
@@ -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.