MDL-19667 report: Add a new capability to view user outline report
authorDmitrii Metelkin <dmitriim@catalyst-au.net>
Thu, 14 Dec 2017 05:16:04 +0000 (16:16 +1100)
committerDmitrii Metelkin <dmitriim@catalyst-au.net>
Thu, 14 Dec 2017 05:16:04 +0000 (16:16 +1100)
report/outline/db/access.php
report/outline/lang/en/report_outline.php
report/outline/lib.php
report/outline/tests/lib_test.php
report/outline/version.php

index 1d992be..2865763 100644 (file)
@@ -37,6 +37,17 @@ $capabilities = array(
         ),
 
         'clonepermissionsfrom' => 'coursereport/outline:view',
+    ),
+
+    'report/outline:viewuserreport' => array(
+        'riskbitmask' => RISK_PERSONAL,
+        'captype' => 'read',
+        'contextlevel' => CONTEXT_COURSE,
+        'archetypes' => array(
+            'teacher' => CAP_ALLOW,
+            'editingteacher' => CAP_ALLOW,
+            'manager' => CAP_ALLOW
+        ),
     )
 );
 
index 407f9e5..fb10e8c 100644 (file)
@@ -30,6 +30,7 @@ $string['nocapability'] = 'Can not access user outline report';
 $string['nologreaderenabled'] = 'No log reader enabled';
 $string['numviews'] = '{$a->numviews} views by {$a->distinctusers} users';
 $string['outline:view'] = 'View activity report';
+$string['outline:viewuserreport'] = 'View user activity report';
 $string['page-report-outline-x'] = 'Any outline report';
 $string['page-report-outline-index'] = 'Course outline report';
 $string['page-report-outline-user'] = 'User course outline report';
index 996e10a..7e4cd42 100644 (file)
@@ -86,7 +86,7 @@ function report_outline_can_access_user_report($user, $course) {
         return false;
     }
 
-    if (has_capability('report/outline:view', $coursecontext)) {
+    if (has_capability('report/outline:viewuserreport', $coursecontext)) {
         return true;
     }
 
index 3085bb1..fa9f208 100644 (file)
@@ -45,16 +45,28 @@ class report_outline_lib_testcase extends advanced_testcase {
      */
     private $course;
 
+    /**
+     * @var context_course Course context.
+     */
+    private $coursecontext;
+
     /**
      * @var \core_user\output\myprofile\tree The navigation tree.
      */
     private $tree;
 
+    /**
+     * @var int Dummy role for testing.
+     */
+    private $roleid;
+
     public function setUp() {
         $this->user = $this->getDataGenerator()->create_user();
         $this->user2 = $this->getDataGenerator()->create_user();
         $this->course = $this->getDataGenerator()->create_course();
         $this->tree = new \core_user\output\myprofile\tree();
+        $this->coursecontext = context_course::instance($this->course->id);
+        $this->roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
         $this->resetAfterTest();
     }
 
@@ -108,4 +120,25 @@ class report_outline_lib_testcase extends advanced_testcase {
         $this->assertArrayNotHasKey('outline', $nodes->getValue($this->tree));
         $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree));
     }
+
+    /**
+     * Test that the current user can not access user report without report/outline:viewuserreport permission.
+     */
+    public function test_report_outline_can_not_access_user_report_without_viewuserreport_permission() {
+        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
+        $this->setUser($this->user);
+
+        $this->assertFalse(report_outline_can_access_user_report($this->user, $this->course));
+    }
+
+    /**
+     * Test that the current user can access user report with report/outline:viewuserreport permission.
+     */
+    public function test_report_outline_can_access_user_report_with_viewuserreport_permission() {
+        assign_capability('report/outline:viewuserreport', CAP_ALLOW, $this->roleid, $this->coursecontext->id, true);
+        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->coursecontext->id);
+        $this->setUser($this->user);
+
+        $this->assertTrue(report_outline_can_access_user_report($this->user, $this->course));
+    }
 }
index 938d0e6..c764e7c 100644 (file)
@@ -25,6 +25,6 @@
 
 defined('MOODLE_INTERNAL') || die;
 
-$plugin->version   = 2017111300;       // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2017111301;       // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2017110800;       // Requires this Moodle version
 $plugin->component = 'report_outline'; // Full name of the plugin (used for diagnostics)