Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Data provider tests.
  19   *
  20   * @package    core_competency
  21   * @category   test
  22   * @copyright  2018 Frédéric Massart
  23   * @author     Frédéric Massart <fred@branchup.tech>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  namespace core_competency\privacy;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  global $CFG, $DB;
  30  
  31  use core_privacy\tests\provider_testcase;
  32  use core_privacy\local\request\contextlist;
  33  use core_privacy\local\request\approved_contextlist;
  34  use core_privacy\local\request\approved_userlist;
  35  use core_privacy\local\request\transform;
  36  use core_privacy\local\request\userlist;
  37  use core_privacy\local\request\writer;
  38  use core_competency\api;
  39  use core_competency\privacy\provider;
  40  
  41  /**
  42   * Data provider testcase class.
  43   *
  44   * @package    core_competency
  45   * @category   test
  46   * @copyright  2018 Frédéric Massart
  47   * @author     Frédéric Massart <fred@branchup.tech>
  48   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   */
  50  class provider_test extends provider_testcase {
  51  
  52      public function setUp(): void {
  53          global $PAGE;
  54          $this->resetAfterTest();
  55  
  56          // We need this or exporters (core\external\exporter) do not receive the right renderer.
  57          $PAGE->get_renderer('core');
  58      }
  59  
  60      public function test_get_contexts_for_userid_with_usermodified_for_framework() {
  61          $dg = $this->getDataGenerator();
  62          $ccg = $dg->get_plugin_generator('core_competency');
  63  
  64          $cat1 = $dg->create_category();
  65          $cat2 = $dg->create_category();
  66          $u1 = $dg->create_user();
  67          $u2 = $dg->create_user();
  68          $u3 = $dg->create_user();
  69          $u4 = $dg->create_user();
  70  
  71          $sysctx = \context_system::instance();
  72          $cat1ctx = \context_coursecat::instance($cat1->id);
  73          $cat2ctx = \context_coursecat::instance($cat2->id);
  74  
  75          // Test recovery through framework context.
  76          $this->setUser($u1);
  77          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
  78          $f1 = $ccg->create_framework();
  79          $contextlist = provider::get_contexts_for_userid($u1->id);
  80          $this->assert_contextlist($contextlist, [$sysctx]);
  81          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
  82          $contextlist = provider::get_contexts_for_userid($u1->id);
  83          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
  84  
  85          // Test recovery of category context alone.
  86          $this->setUser($u2);
  87          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
  88          $ccg->create_framework(['contextid' => $cat2ctx->id]);
  89          $contextlist = provider::get_contexts_for_userid($u2->id);
  90          $this->assert_contextlist($contextlist, [$cat2ctx]);
  91  
  92          // Test recovery through competency.
  93          $this->setUser($u3);
  94          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
  95          $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  96          $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  97          $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
  98          $contextlist = provider::get_contexts_for_userid($u3->id);
  99          $this->assert_contextlist($contextlist, [$sysctx]);
 100          $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 101          $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 102          $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 103          $contextlist = provider::get_contexts_for_userid($u3->id);
 104          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 105  
 106          // Test recovery through related competency.
 107          $this->setUser($u4);
 108          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 109          $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]);
 110          $contextlist = provider::get_contexts_for_userid($u4->id);
 111          $this->assert_contextlist($contextlist, [$sysctx]);
 112          $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]);
 113          $contextlist = provider::get_contexts_for_userid($u4->id);
 114          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 115      }
 116  
 117      public function test_get_users_in_context_with_usermodified_for_framework() {
 118          $dg = $this->getDataGenerator();
 119          $ccg = $dg->get_plugin_generator('core_competency');
 120  
 121          $cat1 = $dg->create_category();
 122          $cat2 = $dg->create_category();
 123          $u1 = $dg->create_user();
 124          $u2 = $dg->create_user();
 125          $u3 = $dg->create_user();
 126          $u4 = $dg->create_user();
 127  
 128          $sysctx = \context_system::instance();
 129          $cat1ctx = \context_coursecat::instance($cat1->id);
 130          $cat2ctx = \context_coursecat::instance($cat2->id);
 131  
 132          // Add frameworks.
 133          $this->setUser($u1);
 134          $f1 = $ccg->create_framework();
 135          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 136  
 137          $this->setUser($u2);
 138          $ccg->create_framework(['contextid' => $cat2ctx->id]);
 139  
 140          // Add competencies.
 141          $this->setUser($u3);
 142          $c1 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 143          $c2 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 144          $c3 = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
 145          $c4 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 146          $c5 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 147          $c6 = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
 148  
 149          // Add related competencies.
 150          $this->setUser($u4);
 151          $cr = $ccg->create_related_competency(['competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')]);
 152          $cr = $ccg->create_related_competency(['competencyid' => $c4->get('id'), 'relatedcompetencyid' => $c5->get('id')]);
 153  
 154          // Test correct users appear in each context.
 155          $component = 'core_competency';
 156  
 157          $userlist = new userlist($sysctx, $component);
 158          provider::get_users_in_context($userlist);
 159          $expected = [$u1->id, $u3->id, $u4->id];
 160          $this->assert_array_match($expected, $userlist->get_userids());
 161  
 162          $userlist = new userlist($cat1ctx, $component);
 163          provider::get_users_in_context($userlist);
 164          $expected = [$u1->id, $u3->id, $u4->id];
 165          $this->assert_array_match($expected, $userlist->get_userids());
 166  
 167          $userlist = new userlist($cat2ctx, $component);
 168          provider::get_users_in_context($userlist);
 169          $expected = [$u2->id];
 170          $this->assert_array_match($expected, $userlist->get_userids());
 171      }
 172  
 173      public function test_get_contexts_for_userid_with_usermodified_for_template() {
 174          $dg = $this->getDataGenerator();
 175          $ccg = $dg->get_plugin_generator('core_competency');
 176  
 177          $cat1 = $dg->create_category();
 178          $cat2 = $dg->create_category();
 179          $u1 = $dg->create_user();
 180          $u2 = $dg->create_user();
 181          $u3 = $dg->create_user();
 182          $u4 = $dg->create_user();
 183          $cohort = $dg->create_cohort();
 184  
 185          $sysctx = \context_system::instance();
 186          $cat1ctx = \context_coursecat::instance($cat1->id);
 187          $cat2ctx = \context_coursecat::instance($cat2->id);
 188  
 189          $f1 = $ccg->create_framework();
 190          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 191          $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
 192          $cs = [];
 193  
 194          foreach ([$f1, $f2, $f3] as $f) {
 195              $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 196          }
 197  
 198          // Test recovery through template context.
 199          $this->setUser($u1);
 200          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 201          $t1 = $ccg->create_template();
 202          $contextlist = provider::get_contexts_for_userid($u1->id);
 203          $this->assert_contextlist($contextlist, [$sysctx]);
 204          $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
 205          $contextlist = provider::get_contexts_for_userid($u1->id);
 206          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 207  
 208          // Test recovery of category context alone.
 209          $this->setUser($u2);
 210          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 211          $ccg->create_template(['contextid' => $cat2ctx->id]);
 212          $contextlist = provider::get_contexts_for_userid($u2->id);
 213          $this->assert_contextlist($contextlist, [$cat2ctx]);
 214  
 215          // Test recovery through template competency.
 216          $this->setUser($u3);
 217          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 218          $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]);
 219          $contextlist = provider::get_contexts_for_userid($u3->id);
 220          $this->assert_contextlist($contextlist, [$sysctx]);
 221          $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]);
 222          $contextlist = provider::get_contexts_for_userid($u3->id);
 223          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 224  
 225          // Test recovery through template cohort.
 226          $this->setUser($u4);
 227          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 228          $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]);
 229          $contextlist = provider::get_contexts_for_userid($u4->id);
 230          $this->assert_contextlist($contextlist, [$sysctx]);
 231          $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]);
 232          $contextlist = provider::get_contexts_for_userid($u4->id);
 233          $this->assert_contextlist($contextlist, [$sysctx, $cat1ctx]);
 234      }
 235  
 236      public function test_get_users_in_context_with_usermodified_for_template() {
 237          $dg = $this->getDataGenerator();
 238          $ccg = $dg->get_plugin_generator('core_competency');
 239  
 240          $cat1 = $dg->create_category();
 241          $cat2 = $dg->create_category();
 242          $u1 = $dg->create_user();
 243          $u2 = $dg->create_user();
 244          $u3 = $dg->create_user();
 245          $u4 = $dg->create_user();
 246          $cohort = $dg->create_cohort();
 247  
 248          $sysctx = \context_system::instance();
 249          $cat1ctx = \context_coursecat::instance($cat1->id);
 250          $cat2ctx = \context_coursecat::instance($cat2->id);
 251  
 252          $f1 = $ccg->create_framework();
 253          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
 254          $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
 255          $cs = [];
 256  
 257          foreach ([$f1, $f2, $f3] as $f) {
 258              $cs[$f->get('id')] = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 259          }
 260  
 261          // Create template context.
 262          $this->setUser($u1);
 263          $t1 = $ccg->create_template();
 264          $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
 265  
 266          // Add to category context.
 267          $this->setUser($u2);
 268          $ccg->create_template(['contextid' => $cat2ctx->id]);
 269  
 270          // Create template competencies.
 271          $this->setUser($u3);
 272          $c1 = $ccg->create_template_competency(['competencyid' => $cs[$f1->get('id')]->get('id'), 'templateid' => $t1->get('id')]);
 273          $c4 = $ccg->create_template_competency(['competencyid' => $cs[$f2->get('id')]->get('id'), 'templateid' => $t2->get('id')]);
 274  
 275          // Create template cohorts.
 276          $this->setUser($u4);
 277          $c1 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t1->get('id')]);
 278          $c4 = $ccg->create_template_cohort(['cohortid' => $cohort->id, 'templateid' => $t2->get('id')]);
 279  
 280          // Test correct users appear in each context.
 281          $component = 'core_competency';
 282  
 283          $userlist = new userlist($sysctx, $component);
 284          provider::get_users_in_context($userlist);
 285          $expected = [$u1->id, $u3->id, $u4->id];
 286          $this->assert_array_match($expected, $userlist->get_userids());
 287  
 288          $userlist = new userlist($cat1ctx, $component);
 289          provider::get_users_in_context($userlist);
 290          $expected = [$u1->id, $u3->id, $u4->id];
 291          $this->assert_array_match($expected, $userlist->get_userids());
 292  
 293          $userlist = new userlist($cat2ctx, $component);
 294          provider::get_users_in_context($userlist);
 295          $expected = [$u2->id];
 296          $this->assert_array_match($expected, $userlist->get_userids());
 297      }
 298  
 299      public function test_get_contexts_for_userid_with_usermodified_for_course() {
 300          $dg = $this->getDataGenerator();
 301          $ccg = $dg->get_plugin_generator('core_competency');
 302          $c1 = $dg->create_course();
 303          $c2 = $dg->create_course();
 304          $u0 = $dg->create_user();
 305          $u1 = $dg->create_user();
 306          $u2 = $dg->create_user();
 307          $u3 = $dg->create_user();
 308          $u4 = $dg->create_user();
 309          $c1ctx = \context_course::instance($c1->id);
 310          $c2ctx = \context_course::instance($c2->id);
 311  
 312          $f = $ccg->create_framework();
 313          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 314          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 315  
 316          $this->setUser($u1);
 317          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 318          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 319          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]);
 320          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 321          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 322  
 323          $this->setUser($u2);
 324          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 325          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 326          $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]);
 327          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 328          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c2ctx]);
 329          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]);
 330          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 331          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 332  
 333          $this->setUser($u3);
 334          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 335          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
 336          $ccs->create();
 337          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 338          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 339          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 340  
 341          $this->setUser($u4);
 342          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 343          $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]);
 344          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$c1ctx]);
 345          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$c1ctx, $c2ctx]);
 346          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 347          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$c2ctx]);
 348      }
 349  
 350      public function test_get_users_in_context_with_usermodified_for_course() {
 351          $dg = $this->getDataGenerator();
 352          $ccg = $dg->get_plugin_generator('core_competency');
 353          $c1 = $dg->create_course();
 354          $c2 = $dg->create_course();
 355          $u0 = $dg->create_user();
 356          $u1 = $dg->create_user();
 357          $u2 = $dg->create_user();
 358          $u3 = $dg->create_user();
 359          $u4 = $dg->create_user();
 360          $c1ctx = \context_course::instance($c1->id);
 361          $c2ctx = \context_course::instance($c2->id);
 362  
 363          $f = $ccg->create_framework();
 364          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 365          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 366  
 367          $this->setUser($u1);
 368          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp1->get('id')]);
 369  
 370          $this->setUser($u2);
 371          $ccg->create_course_competency(['courseid' => $c2->id, 'competencyid' => $comp2->get('id')]);
 372          $ccg->create_course_competency(['courseid' => $c1->id, 'competencyid' => $comp2->get('id')]);
 373  
 374          $this->setUser($u3);
 375          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
 376          $ccs->create();
 377  
 378          $this->setUser($u4);
 379          $ccg->create_user_competency_course(['courseid' => $c2->id, 'userid' => $u0->id, 'competencyid' => $comp1->get('id')]);
 380  
 381          // Test correct users appear in each context.
 382          $component = 'core_competency';
 383  
 384          $userlist = new userlist($c1ctx, $component);
 385          provider::get_users_in_context($userlist);
 386          $expected = [$u1->id, $u2->id, $u3->id];
 387          $this->assert_array_match($expected, $userlist->get_userids());
 388  
 389          $userlist = new userlist($c2ctx, $component);
 390          provider::get_users_in_context($userlist);
 391          $expected = [$u0->id, $u2->id, $u4->id];
 392          $this->assert_array_match($expected, $userlist->get_userids());
 393      }
 394  
 395      public function test_get_contexts_for_userid_with_usermodified_for_module() {
 396          $dg = $this->getDataGenerator();
 397          $ccg = $dg->get_plugin_generator('core_competency');
 398          $c1 = $dg->create_course();
 399          $m1 = $dg->create_module('choice', ['course' => $c1]);
 400          $m2 = $dg->create_module('choice', ['course' => $c1]);
 401          $u1 = $dg->create_user();
 402          $u2 = $dg->create_user();
 403          $m1ctx = \context_module::instance($m1->cmid);
 404          $m2ctx = \context_module::instance($m2->cmid);
 405  
 406          $f = $ccg->create_framework();
 407          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 408          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 409  
 410          $this->setUser($u1);
 411          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 412          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 413          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]);
 414          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 415          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 416  
 417          $this->setUser($u2);
 418          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 419          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 420          $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]);
 421          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 422          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m2ctx]);
 423          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]);
 424          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$m1ctx]);
 425          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$m1ctx, $m2ctx]);
 426      }
 427  
 428      public function test_get_users_in_context_with_usermodified_for_module() {
 429          $dg = $this->getDataGenerator();
 430          $ccg = $dg->get_plugin_generator('core_competency');
 431          $c1 = $dg->create_course();
 432          $m1 = $dg->create_module('choice', ['course' => $c1]);
 433          $m2 = $dg->create_module('choice', ['course' => $c1]);
 434          $u1 = $dg->create_user();
 435          $u2 = $dg->create_user();
 436          $m1ctx = \context_module::instance($m1->cmid);
 437          $m2ctx = \context_module::instance($m2->cmid);
 438  
 439          $f = $ccg->create_framework();
 440          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 441          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 442  
 443          $this->setUser($u1);
 444          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp1->get('id')]);
 445  
 446          $this->setUser($u2);
 447          $ccg->create_course_module_competency(['cmid' => $m2->cmid, 'competencyid' => $comp2->get('id')]);
 448          $ccg->create_course_module_competency(['cmid' => $m1->cmid, 'competencyid' => $comp2->get('id')]);
 449  
 450          // Test correct users appear in each context.
 451          $component = 'core_competency';
 452  
 453          $userlist = new userlist($m1ctx, $component);
 454          provider::get_users_in_context($userlist);
 455          $expected = [$u1->id, $u2->id];
 456          $this->assert_array_match($expected, $userlist->get_userids());
 457  
 458          $userlist = new userlist($m2ctx, $component);
 459          provider::get_users_in_context($userlist);
 460          $expected = [$u2->id];
 461          $this->assert_array_match($expected, $userlist->get_userids());
 462      }
 463  
 464      public function test_get_contexts_for_userid_with_usermodified_for_plan() {
 465          $dg = $this->getDataGenerator();
 466          $ccg = $dg->get_plugin_generator('core_competency');
 467          $u0 = $dg->create_user();
 468          $u1 = $dg->create_user();
 469          $u2 = $dg->create_user();
 470          $u3 = $dg->create_user();
 471          $u0ctx = \context_user::instance($u0->id);
 472  
 473          $f = $ccg->create_framework();
 474          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 475          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 476  
 477          $this->setUser($u1);
 478          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 479          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 480          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 481          $plan = $ccg->create_plan(['userid' => $u0->id]);
 482          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 483          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 484          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 485  
 486          $this->setUser($u2);
 487          $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]);
 488          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 489          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 490          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 491  
 492          $this->setUser($u3);
 493          $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'),
 494              'userid' => $u0->id]);
 495          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 496          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 497          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 498      }
 499  
 500      public function test_get_users_in_context_with_usermodified_for_plan() {
 501          $dg = $this->getDataGenerator();
 502          $ccg = $dg->get_plugin_generator('core_competency');
 503          $u0 = $dg->create_user();
 504          $u1 = $dg->create_user();
 505          $u2 = $dg->create_user();
 506          $u3 = $dg->create_user();
 507          $u0ctx = \context_user::instance($u0->id);
 508  
 509          $f = $ccg->create_framework();
 510          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 511          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 512  
 513          $this->setUser($u1);
 514          $plan = $ccg->create_plan(['userid' => $u0->id]);
 515  
 516          $this->setUser($u2);
 517          $ccg->create_plan_competency(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')]);
 518  
 519          $this->setUser($u3);
 520          $ccg->create_user_competency_plan(['planid' => $plan->get('id'), 'competencyid' => $comp1->get('id'),
 521              'userid' => $u0->id]);
 522  
 523          // Test correct users appear in the context.
 524          $component = 'core_competency';
 525  
 526          $userlist = new userlist($u0ctx, $component);
 527          provider::get_users_in_context($userlist);
 528          $expected = [$u0->id, $u1->id, $u2->id, $u3->id];
 529          $this->assert_array_match($expected, $userlist->get_userids());
 530      }
 531  
 532      public function test_get_contexts_for_userid_with_usermodified_for_competency_data() {
 533          $dg = $this->getDataGenerator();
 534          $ccg = $dg->get_plugin_generator('core_competency');
 535          $u0 = $dg->create_user();
 536          $u1 = $dg->create_user();
 537          $u2 = $dg->create_user();
 538          $u3 = $dg->create_user();
 539          $u4 = $dg->create_user();
 540          $u5 = $dg->create_user();
 541          $u6 = $dg->create_user();
 542          $u7 = $dg->create_user();
 543          $u8 = $dg->create_user();
 544          $u0ctx = \context_user::instance($u0->id);
 545  
 546          $f = $ccg->create_framework();
 547          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 548          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 549  
 550          $this->setUser($u1);
 551          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 552          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 553          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 554          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 555          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
 556          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), []);
 557          $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'),
 558              'reviewerid' => $u6->id]);
 559          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 560          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 561          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 562          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 563          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), []);
 564          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 565  
 566          $this->setUser($u2);
 567          $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]);
 568          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 569          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 570          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 571          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 572          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 573          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 574  
 575          $this->setUser($u3);
 576          $ccg->create_user_evidence(['userid' => $u0->id]);
 577          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 578          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 579          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 580          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 581          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 582          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 583  
 584          $this->setUser($u4);
 585          $ccg->create_user_evidence(['userid' => $u0->id]);
 586          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u0ctx]);
 587          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u0ctx]);
 588          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$u0ctx]);
 589          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u0ctx]);
 590          $this->assert_contextlist(provider::get_contexts_for_userid($u5->id), [$u0ctx]);
 591          $this->assert_contextlist(provider::get_contexts_for_userid($u6->id), [$u0ctx]);
 592  
 593          // Comment on competency.
 594          $this->allow_anyone_to_comment_anywhere();
 595          $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), []);
 596          $this->setUser($u7);
 597          $comments = $uc->get_comment_object();
 598          $comments->add('Hello there!');
 599          $this->assert_contextlist(provider::get_contexts_for_userid($u7->id), [$u0ctx]);
 600  
 601          // Comment on plan.
 602          $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), []);
 603          $this->setUser($u8);
 604          $plan = $ccg->create_plan(['userid' => $u0->id]);
 605          $comments = $plan->get_comment_object();
 606          $comments->add('Hi, planet!');
 607          $this->assert_contextlist(provider::get_contexts_for_userid($u8->id), [$u0ctx]);
 608      }
 609  
 610      public function test_get_users_in_context_with_usermodified_for_competency_data() {
 611          $dg = $this->getDataGenerator();
 612          $ccg = $dg->get_plugin_generator('core_competency');
 613          $u0 = $dg->create_user();
 614          $u1 = $dg->create_user();
 615          $u2 = $dg->create_user();
 616          $u3 = $dg->create_user();
 617          $u4 = $dg->create_user();
 618          $u5 = $dg->create_user();
 619          $u6 = $dg->create_user();
 620          $u7 = $dg->create_user();
 621          $u8 = $dg->create_user();
 622          $u0ctx = \context_user::instance($u0->id);
 623  
 624          $f = $ccg->create_framework();
 625          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 626  
 627          $this->setUser($u1);
 628          $uc = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp1->get('id'),
 629              'reviewerid' => $u6->id]);
 630  
 631          $this->setUser($u2);
 632          $e = $ccg->create_evidence(['usercompetencyid' => $uc->get('id'), 'actionuserid' => $u5->id]);
 633  
 634          $this->setUser($u3);
 635          $ccg->create_user_evidence(['userid' => $u0->id]);
 636  
 637          $this->setUser($u4);
 638          $ccg->create_user_evidence(['userid' => $u0->id]);
 639  
 640          // Comment on competency.
 641          $this->allow_anyone_to_comment_anywhere();
 642          $this->setUser($u7);
 643          $comments = $uc->get_comment_object();
 644          $comments->add('Hello there!');
 645  
 646          // Comment on plan.
 647          $this->setUser($u8);
 648          $plan = $ccg->create_plan(['userid' => $u0->id]);
 649          $comments = $plan->get_comment_object();
 650          $comments->add('Hi, planet!');
 651  
 652          // Test correct users appear in the context.
 653          $component = 'core_competency';
 654  
 655          $userlist = new userlist($u0ctx, $component);
 656          provider::get_users_in_context($userlist);
 657          $expected = [$u0->id, $u1->id, $u2->id, $u3->id, $u4->id, $u5->id, $u6->id, $u7->id, $u8->id];
 658          $this->assert_array_match($expected, $userlist->get_userids());
 659      }
 660  
 661      public function test_get_contexts_for_userid_with_actual_data_and_actual_data_is_goooood() {
 662          $dg = $this->getDataGenerator();
 663          $ccg = $dg->get_plugin_generator('core_competency');
 664          $c1 = $dg->create_course();
 665          $u1 = $dg->create_user();
 666          $u2 = $dg->create_user();
 667          $u3 = $dg->create_user();
 668          $u4 = $dg->create_user();
 669  
 670          $c1ctx = \context_course::instance($c1->id);
 671          $u1ctx = \context_user::instance($u1->id);
 672          $u2ctx = \context_user::instance($u2->id);
 673          $u3ctx = \context_user::instance($u3->id);
 674          $u4ctx = \context_user::instance($u4->id);
 675  
 676          $f = $ccg->create_framework();
 677          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 678          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 679  
 680          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), []);
 681          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 682          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 683          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 684  
 685          $ccg->create_plan(['userid' => $u1->id]);
 686          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 687          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), []);
 688          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 689          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 690  
 691          $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
 692          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 693          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 694          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), []);
 695          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 696  
 697          $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 698          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 699          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 700          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 701          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), []);
 702  
 703          $ccg->create_user_evidence(['userid' => $u4->id]);
 704          $this->assert_contextlist(provider::get_contexts_for_userid($u1->id), [$u1ctx]);
 705          $this->assert_contextlist(provider::get_contexts_for_userid($u2->id), [$u2ctx]);
 706          $this->assert_contextlist(provider::get_contexts_for_userid($u3->id), [$c1ctx]);
 707          $this->assert_contextlist(provider::get_contexts_for_userid($u4->id), [$u4ctx]);
 708      }
 709  
 710      public function test_get_users_in_context_with_actual_data_and_actual_data_is_goooood() {
 711          $dg = $this->getDataGenerator();
 712          $ccg = $dg->get_plugin_generator('core_competency');
 713          $c1 = $dg->create_course();
 714          $u1 = $dg->create_user();
 715          $u2 = $dg->create_user();
 716          $u3 = $dg->create_user();
 717          $u4 = $dg->create_user();
 718  
 719          $c1ctx = \context_course::instance($c1->id);
 720          $u1ctx = \context_user::instance($u1->id);
 721          $u2ctx = \context_user::instance($u2->id);
 722          $u3ctx = \context_user::instance($u3->id);
 723          $u4ctx = \context_user::instance($u4->id);
 724  
 725          $f = $ccg->create_framework();
 726          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 727          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 728  
 729          $ccg->create_plan(['userid' => $u1->id]);
 730  
 731          $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
 732  
 733          $ccg->create_user_competency_course(['userid' => $u3->id, 'competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
 734  
 735          $ccg->create_user_evidence(['userid' => $u4->id]);
 736  
 737          // Test correct users appear in each context.
 738          $component = 'core_competency';
 739  
 740          $userlist = new userlist($u1ctx, $component);
 741          provider::get_users_in_context($userlist);
 742          $this->assert_array_match([$u1->id], $userlist->get_userids());
 743  
 744          $userlist = new userlist($u2ctx, $component);
 745          provider::get_users_in_context($userlist);
 746          $this->assert_array_match([$u2->id], $userlist->get_userids());
 747  
 748          $userlist = new userlist($c1ctx, $component);
 749          provider::get_users_in_context($userlist);
 750          $this->assert_array_match([$u3->id], $userlist->get_userids());
 751  
 752          $userlist = new userlist($u4ctx, $component);
 753          provider::get_users_in_context($userlist);
 754          $this->assert_array_match([$u4->id], $userlist->get_userids());
 755      }
 756  
 757      public function test_delete_data_for_user() {
 758          $dg = $this->getDataGenerator();
 759          $ccg = $dg->get_plugin_generator('core_competency');
 760  
 761          $c1 = $dg->create_course();
 762          $c2 = $dg->create_course();
 763          $u1 = $dg->create_user();
 764          $u2 = $dg->create_user();
 765  
 766          $c1ctx = \context_course::instance($c1->id);
 767          $u1ctx = \context_user::instance($u1->id);
 768          $u2ctx = \context_user::instance($u2->id);
 769  
 770          $f = $ccg->create_framework();
 771          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 772          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 773  
 774          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
 775          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
 776          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
 777          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
 778              'competencyid' => $comp1->get('id')]);
 779          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
 780              'competencyid' => $comp2->get('id')]);
 781          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
 782              'competencyid' => $comp1->get('id')]);
 783  
 784          $p1a = $ccg->create_plan(['userid' => $u1->id]);
 785          $p1b = $ccg->create_plan(['userid' => $u1->id]);
 786          $p2 = $ccg->create_plan(['userid' => $u2->id]);
 787          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
 788          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
 789          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
 790          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
 791              'competencyid' => $comp1->get('id')]);
 792          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
 793              'competencyid' => $comp2->get('id')]);
 794          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
 795              'competencyid' => $comp1->get('id')]);
 796  
 797          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
 798          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
 799          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
 800          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
 801          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
 802          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
 803  
 804          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
 805              'competencyid' => $comp1->get('id')]);
 806          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
 807              'competencyid' => $comp1->get('id')]);
 808          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
 809              'competencyid' => $comp1->get('id')]);
 810  
 811          // User 1 comments on both plans.
 812          $this->allow_anyone_to_comment_anywhere();
 813          $this->setUser($u1);
 814          $p1a->get_comment_object()->add('Hi...');
 815          $p1a->get_comment_object()->add('mister');
 816          $p2->get_comment_object()->add('Ahoy!');
 817  
 818          // User 2 comments on both competencies.
 819          $this->setUser($u2);
 820          $uc1a->get_comment_object()->add('Hi, too!');
 821          $uc1a->get_comment_object()->add('How are you?');
 822          $uc2->get_comment_object()->add('Ahoy, too!');
 823  
 824          $p1acommentobj = $p1a->get_comment_object();
 825          $p2commentobj = $p2->get_comment_object();
 826          $uc1acommentobj = $uc1a->get_comment_object();
 827          $uc2commentobj = $uc2->get_comment_object();
 828  
 829          $this->setAdminUser();
 830          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 831          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
 832          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 833          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 834          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
 835          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 836          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 837          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
 838          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 839          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 840          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
 841          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 842          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 843          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
 844          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 845          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 846          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
 847          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 848          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 849          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
 850          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 851          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 852          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 853          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 854          $this->assert_has_comments($p1acommentobj);
 855          $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id));
 856          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
 857          $this->assert_has_comments($p2commentobj);
 858          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
 859          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
 860          $this->assert_has_comments($uc1acommentobj);
 861          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
 862          $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id));
 863          $this->assert_has_comments($uc2commentobj);
 864          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
 865          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
 866  
 867          // Deleting user context only.
 868          $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id]);
 869          provider::delete_data_for_user($appctx);
 870  
 871          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 872          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
 873          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 874          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
 875          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
 876          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
 877          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 878          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
 879          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 880          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
 881          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
 882          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
 883          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
 884          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
 885  
 886          $this->assert_has_no_comments($p1acommentobj);
 887          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id));
 888          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
 889          $this->assert_has_no_comments($uc1acommentobj);
 890          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
 891          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id));
 892  
 893          // This should not have been affected.
 894          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 895          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 896  
 897          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 898          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 899          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 900          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 901          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 902          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 903          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 904          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 905          $this->assert_has_comments($p2commentobj);
 906          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
 907          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
 908          $this->assert_has_comments($uc2commentobj);
 909          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
 910          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
 911  
 912          // Deleting course context as well.
 913          $appctx = new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $c1ctx->id]);
 914          provider::delete_data_for_user($appctx);
 915  
 916          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
 917  
 918          // The rest belongs to another course, or the other user.
 919          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
 920          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
 921          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
 922          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
 923          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
 924          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
 925          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
 926          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
 927          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
 928      }
 929  
 930      public function test_delete_data_for_user_with_other_user_context() {
 931          $dg = $this->getDataGenerator();
 932          $ccg = $dg->get_plugin_generator('core_competency');
 933  
 934          $u1 = $dg->create_user();
 935          $u2 = $dg->create_user();
 936  
 937          $u1ctx = \context_user::instance($u1->id);
 938          $u2ctx = \context_user::instance($u2->id);
 939  
 940          $f = $ccg->create_framework();
 941          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
 942  
 943          // Create a bunch of data for user 1.
 944          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
 945          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
 946              'competencyid' => $comp1->get('id')]);
 947          $p1a = $ccg->create_plan(['userid' => $u1->id]);
 948          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
 949          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
 950              'competencyid' => $comp1->get('id')]);
 951          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
 952          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
 953  
 954          $p2a = $ccg->create_plan(['userid' => $u2->id]);
 955  
 956          // User 2 comments.
 957          $this->allow_anyone_to_comment_anywhere();
 958          $this->setUser($u2);
 959          $p1a->get_comment_object()->add('Hi...');
 960          $p2a->get_comment_object()->add('Hi, hi!');
 961          $uc1a->get_comment_object()->add('Hi, too!');
 962  
 963          // Confirm state.
 964          $this->setAdminUser();
 965          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 966          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 967          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 968          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 969          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 970          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 971          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 972          $this->assert_has_comments($p1a->get_comment_object());
 973          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
 974          $this->assert_has_comments($p2a->get_comment_object());
 975          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
 976          $this->assert_has_comments($uc1a->get_comment_object());
 977          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
 978  
 979          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
 980  
 981          // Delete for user 2, but we pass u1 context.
 982          provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id]));
 983  
 984          // Nothing should have happened.
 985          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
 986          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
 987          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
 988          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
 989          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
 990          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
 991          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
 992          $this->assert_has_comments($p1a->get_comment_object());
 993          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
 994          $this->assert_has_comments($p2a->get_comment_object());
 995          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
 996          $this->assert_has_comments($uc1a->get_comment_object());
 997          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
 998  
 999          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1000  
1001          // Delete for user 2, but we pass u1 and u2 context.
1002          $p2acommentobj = $p2a->get_comment_object();
1003          provider::delete_data_for_user(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
1004  
1005          // The plan got deleted.
1006          $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id')));
1007          $this->assert_has_no_comments($p2acommentobj);
1008  
1009          // Nothing should have happened for u1.
1010          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1011          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1012          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1013          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1014          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1015          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1016          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1017          $this->assert_has_comments($p1a->get_comment_object());
1018          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1019          $this->assert_has_comments($uc1a->get_comment_object());
1020          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1021      }
1022  
1023      public function test_delete_data_for_users() {
1024          $dg = $this->getDataGenerator();
1025          $ccg = $dg->get_plugin_generator('core_competency');
1026  
1027          $c1 = $dg->create_course();
1028          $c2 = $dg->create_course();
1029          $u1 = $dg->create_user();
1030          $u2 = $dg->create_user();
1031  
1032          $c1ctx = \context_course::instance($c1->id);
1033          $u1ctx = \context_user::instance($u1->id);
1034  
1035          $f = $ccg->create_framework();
1036          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1037          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1038  
1039          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1040          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
1041          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
1042          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1043              'competencyid' => $comp1->get('id')]);
1044          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
1045              'competencyid' => $comp2->get('id')]);
1046          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
1047              'competencyid' => $comp1->get('id')]);
1048  
1049          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1050          $p1b = $ccg->create_plan(['userid' => $u1->id]);
1051          $p2 = $ccg->create_plan(['userid' => $u2->id]);
1052          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1053          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
1054          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
1055          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1056              'competencyid' => $comp1->get('id')]);
1057          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
1058              'competencyid' => $comp2->get('id')]);
1059          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
1060              'competencyid' => $comp1->get('id')]);
1061  
1062          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1063          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
1064          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
1065          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1066          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
1067          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
1068  
1069          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
1070              'competencyid' => $comp1->get('id')]);
1071          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
1072              'competencyid' => $comp1->get('id')]);
1073          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
1074              'competencyid' => $comp1->get('id')]);
1075  
1076          // User 1 comments on both plans.
1077          $this->allow_anyone_to_comment_anywhere();
1078          $this->setUser($u1);
1079          $p1a->get_comment_object()->add('Hi...');
1080          $p1a->get_comment_object()->add('mister');
1081          $p2->get_comment_object()->add('Ahoy!');
1082  
1083          // User 2 comments on both competencies.
1084          $this->setUser($u2);
1085          $uc1a->get_comment_object()->add('Hi, too!');
1086          $uc1a->get_comment_object()->add('How are you?');
1087          $uc2->get_comment_object()->add('Ahoy, too!');
1088  
1089          $p1acommentobj = $p1a->get_comment_object();
1090          $p2commentobj = $p2->get_comment_object();
1091          $uc1acommentobj = $uc1a->get_comment_object();
1092          $uc2commentobj = $uc2->get_comment_object();
1093  
1094          $this->setAdminUser();
1095          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1096          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1097          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1098          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1099          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1100          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1101          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1102          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1103          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1104          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1105          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1106          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1107          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1108          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1109          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1110          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1111          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1112          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1113          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1114          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1115          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1116          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1117          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1118          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1119          $this->assert_has_comments($p1acommentobj);
1120          $this->assertEquals(2, $this->get_comments_count($p1acommentobj, $u1->id));
1121          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
1122          $this->assert_has_comments($p2commentobj);
1123          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
1124          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
1125          $this->assert_has_comments($uc1acommentobj);
1126          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
1127          $this->assertEquals(2, $this->get_comments_count($uc1acommentobj, $u2->id));
1128          $this->assert_has_comments($uc2commentobj);
1129          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
1130          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
1131  
1132          // Deleting user context.
1133          $userlist = new approved_userlist($u1ctx, 'core_competency', [$u1->id, $u2->id]);
1134          provider::delete_data_for_users($userlist);
1135  
1136          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1137          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1138          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1139          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1140          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
1141          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
1142          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1143          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1144          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1145          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1146          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
1147          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
1148          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
1149          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
1150  
1151          $this->assert_has_no_comments($p1acommentobj);
1152          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u1->id));
1153          $this->assertEquals(0, $this->get_comments_count($p1acommentobj, $u2->id));
1154          $this->assert_has_no_comments($uc1acommentobj);
1155          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u1->id));
1156          $this->assertEquals(0, $this->get_comments_count($uc1acommentobj, $u2->id));
1157  
1158          // This should not have been affected.
1159          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1160          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1161  
1162          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1163          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1164          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1165          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1166          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1167          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1168          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1169          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1170          $this->assert_has_comments($p2commentobj);
1171          $this->assertEquals(1, $this->get_comments_count($p2commentobj, $u1->id));
1172          $this->assertEquals(0, $this->get_comments_count($p2commentobj, $u2->id));
1173          $this->assert_has_comments($uc2commentobj);
1174          $this->assertEquals(0, $this->get_comments_count($uc2commentobj, $u1->id));
1175          $this->assertEquals(1, $this->get_comments_count($uc2commentobj, $u2->id));
1176  
1177          // Deleting course context as well.
1178          $userlist = new approved_userlist($c1ctx, 'core_competency', [$u1->id]);
1179          provider::delete_data_for_users($userlist);
1180  
1181          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1182  
1183          // The rest belongs to another course, or the other user.
1184          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1185          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1186          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1187          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1188          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1189          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1190          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1191          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1192          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1193      }
1194  
1195      public function test_delete_data_for_users_with_other_user_context() {
1196          $dg = $this->getDataGenerator();
1197          $ccg = $dg->get_plugin_generator('core_competency');
1198  
1199          $u1 = $dg->create_user();
1200          $u2 = $dg->create_user();
1201  
1202          $u1ctx = \context_user::instance($u1->id);
1203          $u2ctx = \context_user::instance($u2->id);
1204  
1205          $f = $ccg->create_framework();
1206          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1207  
1208          // Create a bunch of data for user 1.
1209          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1210          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1211              'competencyid' => $comp1->get('id')]);
1212          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1213          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1214          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1215              'competencyid' => $comp1->get('id')]);
1216          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1217          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1218  
1219          $p2a = $ccg->create_plan(['userid' => $u2->id]);
1220  
1221          // User 2 comments.
1222          $this->allow_anyone_to_comment_anywhere();
1223          $this->setUser($u2);
1224          $p1a->get_comment_object()->add('Hi...');
1225          $p2a->get_comment_object()->add('Hi, hi!');
1226          $uc1a->get_comment_object()->add('Hi, too!');
1227  
1228          // Confirm state.
1229          $this->setAdminUser();
1230          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1231          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1232          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1233          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1234          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1235          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1236          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1237          $this->assert_has_comments($p1a->get_comment_object());
1238          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1239          $this->assert_has_comments($p2a->get_comment_object());
1240          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
1241          $this->assert_has_comments($uc1a->get_comment_object());
1242          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1243  
1244          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1245  
1246          // Delete for user 2, but we pass u1 context.
1247          $userlist = new approved_userlist($u1ctx, 'core_competency', [$u2->id]);
1248          provider::delete_data_for_users($userlist);
1249  
1250          // Nothing should have happened.
1251          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1252          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1253          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1254          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1255          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1256          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1257          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1258          $this->assert_has_comments($p1a->get_comment_object());
1259          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1260          $this->assert_has_comments($p2a->get_comment_object());
1261          $this->assertEquals(1, $this->get_comments_count($p2a->get_comment_object(), $u2->id));
1262          $this->assert_has_comments($uc1a->get_comment_object());
1263          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1264  
1265          $this->assertTrue(\core_competency\plan::record_exists($p2a->get('id')));
1266  
1267          // Delete for user 2, in user 2 context.
1268          $p2acommentobj = $p2a->get_comment_object();
1269          $userlist = new approved_userlist($u2ctx, 'core_competency', [$u2->id]);
1270          provider::delete_data_for_users($userlist);
1271  
1272          // The plan got deleted.
1273          $this->assertFalse(\core_competency\plan::record_exists($p2a->get('id')));
1274          $this->assert_has_no_comments($p2acommentobj);
1275  
1276          // Nothing should have happened for u1.
1277          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1278          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1279          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1280          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1281          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1282          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1283          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1284          $this->assert_has_comments($p1a->get_comment_object());
1285          $this->assertEquals(1, $this->get_comments_count($p1a->get_comment_object(), $u2->id));
1286          $this->assert_has_comments($uc1a->get_comment_object());
1287          $this->assertEquals(1, $this->get_comments_count($uc1a->get_comment_object(), $u2->id));
1288      }
1289  
1290      public function test_delete_data_for_all_users_in_context() {
1291          $dg = $this->getDataGenerator();
1292          $ccg = $dg->get_plugin_generator('core_competency');
1293  
1294          $c1 = $dg->create_course();
1295          $c2 = $dg->create_course();
1296          $u1 = $dg->create_user();
1297          $u2 = $dg->create_user();
1298  
1299          $c1ctx = \context_course::instance($c1->id);
1300          $u1ctx = \context_user::instance($u1->id);
1301          $u2ctx = \context_user::instance($u2->id);
1302  
1303          $f = $ccg->create_framework();
1304          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1305          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1306  
1307          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
1308          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
1309          $ue2 = $ccg->create_user_evidence(['userid' => $u2->id]);
1310          $uec1a = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'),
1311              'competencyid' => $comp1->get('id')]);
1312          $uec1b = $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'),
1313              'competencyid' => $comp2->get('id')]);
1314          $uec2 = $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'),
1315              'competencyid' => $comp1->get('id')]);
1316  
1317          $p1a = $ccg->create_plan(['userid' => $u1->id]);
1318          $p1b = $ccg->create_plan(['userid' => $u1->id]);
1319          $p2 = $ccg->create_plan(['userid' => $u2->id]);
1320          $pc1a = $ccg->create_plan_competency(['planid' => $p1a->get('id'), 'competencyid' => $comp1->get('id')]);
1321          $pc1b = $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
1322          $pc2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp1->get('id')]);
1323          $ucp1a = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1a->get('id'),
1324              'competencyid' => $comp1->get('id')]);
1325          $ucp1b = $ccg->create_user_competency_plan(['userid' => $u1->id, 'planid' => $p1b->get('id'),
1326              'competencyid' => $comp2->get('id')]);
1327          $ucp2 = $ccg->create_user_competency_plan(['userid' => $u2->id, 'planid' => $p2->get('id'),
1328              'competencyid' => $comp1->get('id')]);
1329  
1330          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
1331          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id')]);
1332          $uc2 = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
1333          $e1a = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id')]);
1334          $e1b = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id')]);
1335          $e2 = $ccg->create_evidence(['usercompetencyid' => $uc2->get('id')]);
1336  
1337          $ucc1a = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c1->id,
1338              'competencyid' => $comp1->get('id')]);
1339          $ucc1b = $ccg->create_user_competency_course(['userid' => $u1->id, 'courseid' => $c2->id,
1340              'competencyid' => $comp1->get('id')]);
1341          $ucc2 = $ccg->create_user_competency_course(['userid' => $u2->id, 'courseid' => $c1->id,
1342              'competencyid' => $comp1->get('id')]);
1343  
1344          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1345          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1346          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1347          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1348          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1349          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1350          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1351          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1352          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1353          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1354          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1355          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1356          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1357          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1358          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1359          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1360          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1361          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1362          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1363          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1364          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1365          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1366          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1367          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1368  
1369          // Deleting the course 1 context.
1370          provider::delete_data_for_all_users_in_context($c1ctx);
1371          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc1a->get('id')));
1372          $this->assertFalse(\core_competency\user_competency_course::record_exists($ucc2->get('id')));
1373  
1374          // Not affected.
1375          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1376          $this->assertTrue(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1377          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1378          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1379          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1380          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1381          $this->assertTrue(\core_competency\plan::record_exists($p1a->get('id')));
1382          $this->assertTrue(\core_competency\plan::record_exists($p1b->get('id')));
1383          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1384          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1385          $this->assertTrue(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1386          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1387          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1388          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1389          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1390          $this->assertTrue(\core_competency\user_competency::record_exists($uc1a->get('id')));
1391          $this->assertTrue(\core_competency\user_competency::record_exists($uc1b->get('id')));
1392          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1393          $this->assertTrue(\core_competency\evidence::record_exists($e1a->get('id')));
1394          $this->assertTrue(\core_competency\evidence::record_exists($e1b->get('id')));
1395          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1396          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1397  
1398          // Deleting the user 1 context.
1399          provider::delete_data_for_all_users_in_context($u1ctx);
1400          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1a->get('id')));
1401          $this->assertFalse(\core_competency\user_evidence::record_exists($ue1b->get('id')));
1402          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1a->get('id')));
1403          $this->assertFalse(\core_competency\user_evidence_competency::record_exists($uec1b->get('id')));
1404          $this->assertFalse(\core_competency\plan::record_exists($p1a->get('id')));
1405          $this->assertFalse(\core_competency\plan::record_exists($p1b->get('id')));
1406          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1a->get('id')));
1407          $this->assertFalse(\core_competency\plan_competency::record_exists($pc1b->get('id')));
1408          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1a->get('id')));
1409          $this->assertFalse(\core_competency\user_competency_plan::record_exists($ucp1b->get('id')));
1410          $this->assertFalse(\core_competency\user_competency::record_exists($uc1a->get('id')));
1411          $this->assertFalse(\core_competency\user_competency::record_exists($uc1b->get('id')));
1412          $this->assertFalse(\core_competency\evidence::record_exists($e1a->get('id')));
1413          $this->assertFalse(\core_competency\evidence::record_exists($e1b->get('id')));
1414  
1415          // Not affected.
1416          $this->assertTrue(\core_competency\user_evidence::record_exists($ue2->get('id')));
1417          $this->assertTrue(\core_competency\user_evidence_competency::record_exists($uec2->get('id')));
1418          $this->assertTrue(\core_competency\plan::record_exists($p2->get('id')));
1419          $this->assertTrue(\core_competency\plan_competency::record_exists($pc2->get('id')));
1420          $this->assertTrue(\core_competency\user_competency_plan::record_exists($ucp2->get('id')));
1421          $this->assertTrue(\core_competency\user_competency::record_exists($uc2->get('id')));
1422          $this->assertTrue(\core_competency\evidence::record_exists($e2->get('id')));
1423          $this->assertTrue(\core_competency\user_competency_course::record_exists($ucc1b->get('id')));
1424      }
1425  
1426      public function test_export_data_for_user_in_module_context_where_usermodified_matches() {
1427          $dg = $this->getDataGenerator();
1428          $ccg = $dg->get_plugin_generator('core_competency');
1429  
1430          $c1 = $dg->create_course();
1431          $u1 = $dg->create_user();
1432          $u2 = $dg->create_user();
1433          $m1 = $dg->create_module('page', ['course' => $c1]);
1434          $m2 = $dg->create_module('page', ['course' => $c1]);
1435  
1436          $m1ctx = \context_module::instance($m1->cmid);
1437          $m2ctx = \context_module::instance($m2->cmid);
1438  
1439          $f = $ccg->create_framework();
1440          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1441          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1442          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1443          $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m1->cmid]);
1444  
1445          $this->setUser($u1);
1446          $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $m1->cmid]);
1447          $ccg->create_course_module_competency(['competencyid' => $comp2->get('id'), 'cmid' => $m2->cmid]);
1448  
1449          $this->setUser($u2);
1450          $ccg->create_course_module_competency(['competencyid' => $comp3->get('id'), 'cmid' => $m2->cmid]);
1451  
1452          // Export.
1453          $this->setAdminUser();
1454          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id]));
1455  
1456          // Check exported context 1.
1457          $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
1458          $this->assertCount(1, $data->associations);
1459          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1460  
1461          // Check exported context 2.
1462          $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
1463          $this->assertEmpty($data);
1464  
1465          // Export both contexts.
1466          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$m1ctx->id, $m2ctx->id]));
1467  
1468          // Check exported context 1.
1469          $data = writer::with_context($m1ctx)->get_data([get_string('competencies', 'core_competency')]);
1470          $this->assertCount(1, $data->associations);
1471          $this->assertEquals($comp1->get('shortname'), $data->associations[0]['name']);
1472          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1473  
1474          // Check exported context 2.
1475          $data = writer::with_context($m2ctx)->get_data([get_string('competencies', 'core_competency')]);
1476          $this->assertCount(1, $data->associations);
1477          $this->assertEquals($comp2->get('shortname'), $data->associations[0]['name']);
1478          $this->assertEquals(transform::yesno(true), $data->associations[0]['created_or_modified_by_you']);
1479      }
1480  
1481      public function test_export_data_for_user_in_course_context_where_usermodified_matches() {
1482          $dg = $this->getDataGenerator();
1483          $ccg = $dg->get_plugin_generator('core_competency');
1484  
1485          $c1 = $dg->create_course();
1486          $c2 = $dg->create_course();
1487          $u0 = $dg->create_user();
1488          $u1 = $dg->create_user();
1489          $u2 = $dg->create_user();
1490          $u3 = $dg->create_user();
1491          $u4 = $dg->create_user();
1492  
1493          $c1ctx = \context_course::instance($c1->id);
1494          $c2ctx = \context_course::instance($c2->id);
1495  
1496          $f = $ccg->create_framework();
1497          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1498          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1499          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1500          $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1501          $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id]);
1502          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1503  
1504          $this->setUser($u1);
1505          $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
1506          $ccg->create_course_competency(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id]);
1507          $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
1508          $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1509          $ccg->create_user_competency_course(['competencyid' => $comp4->get('id'), 'courseid' => $c1->id, 'userid' => $u0->id]);
1510          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
1511          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c1->id]);
1512          $ccs->create();
1513  
1514          $this->setUser($u2);
1515          $ccg->create_course_competency(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id]);
1516          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u0->id]);
1517          $ccs = new \core_competency\course_competency_settings(null, (object) ['courseid' => $c2->id]);
1518          $ccs->create();
1519  
1520          // Export.
1521          $this->setAdminUser();
1522          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
1523  
1524          // Check exported context 1.
1525          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1526          $this->assertCount(2, $data->competencies);
1527          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1528          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1529          $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
1530          $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
1531          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1532          $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
1533          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1534          $this->assertCount(2, $data->ratings);
1535          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1536          $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
1537  
1538          // Check exported context 2.
1539          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1540          $this->assertEmpty($data);
1541          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1542          $this->assertEmpty($data);
1543          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1544          $this->assertEmpty($data);
1545  
1546          // Export both contexts.
1547          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id, $c2ctx->id]));
1548  
1549          // Check exported context 1.
1550          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1551          $this->assertCount(2, $data->competencies);
1552          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1553          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1554          $this->assertEquals($comp4->get('shortname'), $data->competencies[1]['name']);
1555          $this->assertEquals(transform::yesno(true), $data->competencies[1]['created_or_modified_by_you']);
1556          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1557          $this->assertEquals(transform::yesno(true), $data->created_or_modified_by_you);
1558          $data = writer::with_context($c1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1559          $this->assertCount(2, $data->ratings);
1560          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1561          $this->assertEquals($comp4->get('shortname'), $data->ratings[1]['name']);
1562  
1563          // Check exported context 2.
1564          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'associations');
1565          $this->assertCount(1, $data->competencies);
1566          $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
1567          $this->assertEquals(transform::yesno(true), $data->competencies[0]['created_or_modified_by_you']);
1568          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'settings');
1569          $this->assertEmpty($data);
1570          $data = writer::with_context($c2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'rated_by_me');
1571          $this->assertCount(1, $data->ratings);
1572          $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
1573      }
1574  
1575      public function test_export_data_for_user_in_course_context_with_real_data() {
1576          $dg = $this->getDataGenerator();
1577          $ccg = $dg->get_plugin_generator('core_competency');
1578  
1579          $c1 = $dg->create_course();
1580          $c2 = $dg->create_course();
1581          $u1 = $dg->create_user();
1582          $u2 = $dg->create_user();
1583  
1584          $c1ctx = \context_course::instance($c1->id);
1585          $c2ctx = \context_course::instance($c2->id);
1586  
1587          $f = $ccg->create_framework();
1588          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1589          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1590          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1591  
1592          $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
1593              'userid' => $u1->id, 'grade' => 1, 'proficiency' => true]);
1594          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
1595              'userid' => $u1->id, 'grade' => 2, 'proficiency' => false]);
1596          $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
1597              'userid' => $u1->id, 'grade' => 3, 'proficiency' => false]);
1598          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id,
1599              'userid' => $u1->id]);
1600  
1601          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c1->id, 'userid' => $u2->id]);
1602          $ccg->create_user_competency_course(['competencyid' => $comp3->get('id'), 'courseid' => $c2->id, 'userid' => $u2->id]);
1603  
1604          // Export user 1, in course 1.
1605          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c1ctx->id]));
1606  
1607          // Check course 1.
1608          $data = writer::with_context($c1ctx)->get_data([get_string('competencies', 'core_competency')]);
1609          $this->assertCount(2, $data->ratings);
1610          $this->assertEquals($comp1->get('shortname'), $data->ratings[0]['name']);
1611          $this->assertEquals('A', $data->ratings[0]['rating']['rating']);
1612          $this->assertEquals(transform::yesno(true), $data->ratings[0]['rating']['proficient']);
1613          $this->assertEquals($comp2->get('shortname'), $data->ratings[1]['name']);
1614          $this->assertEquals('B', $data->ratings[1]['rating']['rating']);
1615          $this->assertEquals(transform::yesno(false), $data->ratings[1]['rating']['proficient']);
1616  
1617          // Check course 2.
1618          $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1619          $this->assertEmpty($data);
1620  
1621          // Export user 1, in course 2.
1622          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$c2ctx->id]));
1623          $data = writer::with_context($c2ctx)->get_data([get_string('competencies', 'core_competency')]);
1624          $this->assertCount(2, $data->ratings);
1625          $this->assertEquals($comp2->get('shortname'), $data->ratings[0]['name']);
1626          $this->assertEquals('C', $data->ratings[0]['rating']['rating']);
1627          $this->assertEquals(transform::yesno(false), $data->ratings[0]['rating']['proficient']);
1628          $this->assertEquals($comp3->get('shortname'), $data->ratings[1]['name']);
1629          $this->assertEquals('-', $data->ratings[1]['rating']['rating']);
1630          $this->assertEquals('-', $data->ratings[1]['rating']['proficient']);
1631      }
1632  
1633      public function test_export_data_for_user_in_system_and_category_contexts() {
1634          $dg = $this->getDataGenerator();
1635          $ccg = $dg->get_plugin_generator('core_competency');
1636  
1637          $c1 = $dg->create_cohort();
1638          $c2 = $dg->create_cohort();
1639          $cat1 = $dg->create_category();
1640          $cat2 = $dg->create_category();
1641  
1642          $cat1ctx = \context_coursecat::instance($cat1->id);
1643          $cat2ctx = \context_coursecat::instance($cat2->id);
1644          $sysctx = \context_system::instance();
1645  
1646          $u1 = $dg->create_user();
1647          $u2 = $dg->create_user();
1648          $u3 = $dg->create_user();
1649          $u4 = $dg->create_user();
1650          $u2 = $dg->create_user();
1651  
1652          $this->setUser($u1);
1653          $f1 = $ccg->create_framework();
1654          $f1bis = $ccg->create_framework();
1655          $f2 = $ccg->create_framework(['contextid' => $cat1ctx->id]);
1656          $c2a = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
1657          $c2b = $ccg->create_competency(['competencyframeworkid' => $f2->get('id')]);
1658  
1659          $t1 = $ccg->create_template();
1660          $t2 = $ccg->create_template(['contextid' => $cat1ctx->id]);
1661          $tc2a = $ccg->create_template_competency(['templateid' => $t2->get('id'), 'competencyid' => $c2a->get('id')]);
1662          $tch2 = $ccg->create_template_cohort(['templateid' => $t2->get('id'), 'cohortid' => $c1->id]);
1663  
1664          $this->setUser($u2);
1665          $f3 = $ccg->create_framework(['contextid' => $cat2ctx->id]);
1666          $c1a = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
1667          $c1b = $ccg->create_competency(['competencyframeworkid' => $f1->get('id')]);
1668          $c3a = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1669          $c3b = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1670          $c3c = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1671          $c3d = $ccg->create_competency(['competencyframeworkid' => $f3->get('id')]);
1672          $rc1 = $ccg->create_related_competency(['competencyid' => $c2a->get('id'), 'relatedcompetencyid' => $c2b->get('id')]);
1673  
1674          $t3 = $ccg->create_template(['contextid' => $cat2ctx->id]);
1675          $tch1 = $ccg->create_template_cohort(['templateid' => $t1->get('id'), 'cohortid' => $c2->id]);
1676          $tc1a = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c1a->get('id')]);
1677          $tc1b = $ccg->create_template_competency(['templateid' => $t1->get('id'), 'competencyid' => $c2a->get('id')]);
1678          $tc3a = $ccg->create_template_competency(['templateid' => $t3->get('id'), 'competencyid' => $c3a->get('id')]);
1679  
1680          $this->setUser($u1);
1681          $rc2 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3b->get('id')]);
1682          $rc3 = $ccg->create_related_competency(['competencyid' => $c3a->get('id'), 'relatedcompetencyid' => $c3c->get('id')]);
1683  
1684          $this->setAdminUser();
1685          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id]));
1686  
1687          // Check frameworks for u1 in system.
1688          $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1689          $this->assertCount(2, $data->frameworks);
1690          $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']);
1691          $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']);
1692          $this->assertEquals($f1bis->get('shortname'), $data->frameworks[1]['name']);
1693          $this->assertEquals(transform::yesno(true), $data->frameworks[1]['created_or_modified_by_you']);
1694          $this->assertEmpty($data->frameworks[0]['competencies']);
1695          $this->assertEmpty($data->frameworks[1]['competencies']);
1696  
1697          // Check templates for u1 in system.
1698          $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1699          $this->assertCount(1, $data->templates);
1700          $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']);
1701          $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1702          $this->assertEmpty($data->templates[0]['competencies']);
1703          $this->assertEmpty($data->templates[0]['cohorts']);
1704  
1705          // Check frameworks for u1 in cat1.
1706          $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1707          $this->assertCount(1, $data->frameworks);
1708          $this->assertEquals($f2->get('shortname'), $data->frameworks[0]['name']);
1709          $this->assertEquals(transform::yesno(true), $data->frameworks[0]['created_or_modified_by_you']);
1710          $this->assertCount(2, $data->frameworks[0]['competencies']);
1711          $this->assertEquals($c2a->get('shortname'), $data->frameworks[0]['competencies'][0]['name']);
1712          $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][0]['created_or_modified_by_you']);
1713          $this->assertEquals($c2b->get('shortname'), $data->frameworks[0]['competencies'][1]['name']);
1714          $this->assertEquals(transform::yesno(true), $data->frameworks[0]['competencies'][1]['created_or_modified_by_you']);
1715  
1716          // Check templates for u1 in cat1.
1717          $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1718          $this->assertCount(1, $data->templates);
1719          $this->assertEquals($t2->get('shortname'), $data->templates[0]['name']);
1720          $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1721          $this->assertCount(1, $data->templates[0]['competencies']);
1722          $this->assertEquals($c2a->get('shortname'), $data->templates[0]['competencies'][0]['name']);
1723          $this->assertEquals(transform::yesno(true), $data->templates[0]['competencies'][0]['created_or_modified_by_you']);
1724          $this->assertCount(1, $data->templates[0]['cohorts']);
1725          $this->assertEquals($c1->name, $data->templates[0]['cohorts'][0]['name']);
1726          $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']);
1727  
1728          // Check frameworks for u1 in cat2.
1729          $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1730          $this->assertCount(1, $data->frameworks);
1731          $this->assertEquals($f3->get('shortname'), $data->frameworks[0]['name']);
1732          $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1733          $this->assertCount(3, $data->frameworks[0]['competencies']);
1734          $competency = $data->frameworks[0]['competencies'][0];
1735          $this->assertEquals($c3a->get('shortname'), $competency['name']);
1736          $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']);
1737          $this->assertCount(2, $competency['related_competencies']);
1738          $this->assertEquals($c3b->get('shortname'), $competency['related_competencies'][0]['name']);
1739          $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']);
1740          $this->assertEquals($c3c->get('shortname'), $competency['related_competencies'][1]['name']);
1741          $this->assertEquals(transform::yesno(true), $competency['related_competencies'][1]['created_or_modified_by_you']);
1742          $competency = $data->frameworks[0]['competencies'][1];
1743          $this->assertEquals($c3b->get('shortname'), $competency['name']);
1744          $this->assertCount(1, $competency['related_competencies']);
1745          $competency = $data->frameworks[0]['competencies'][2];
1746          $this->assertEquals($c3c->get('shortname'), $competency['name']);
1747          $this->assertCount(1, $competency['related_competencies']);
1748  
1749          // Check templates for u1 in cat2.
1750          $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1751          $this->assertEmpty($data->templates);
1752  
1753          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$sysctx->id, $cat1ctx->id, $cat2ctx->id]));
1754  
1755          // Check frameworks for u2 in system.
1756          $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1757          $this->assertCount(1, $data->frameworks);
1758          $this->assertEquals($f1->get('shortname'), $data->frameworks[0]['name']);
1759          $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1760          $this->assertCount(2, $data->frameworks[0]['competencies']);
1761          $competency = $data->frameworks[0]['competencies'][0];
1762          $this->assertEquals($c1a->get('shortname'), $competency['name']);
1763          $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1764          $competency = $data->frameworks[0]['competencies'][1];
1765          $this->assertEquals($c1b->get('shortname'), $competency['name']);
1766          $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1767  
1768          // Check templates for u2 in system.
1769          $data = writer::with_context($sysctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1770          $this->assertCount(1, $data->templates);
1771          $this->assertEquals($t1->get('shortname'), $data->templates[0]['name']);
1772          $this->assertEquals(transform::yesno(false), $data->templates[0]['created_or_modified_by_you']);
1773          $this->assertCount(2, $data->templates[0]['competencies']);
1774          $competency = $data->templates[0]['competencies'][0];
1775          $this->assertEquals($c1a->get('shortname'), $competency['name']);
1776          $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1777          $competency = $data->templates[0]['competencies'][1];
1778          $this->assertEquals($c2a->get('shortname'), $competency['name']);
1779          $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1780          $this->assertCount(1, $data->templates[0]['cohorts']);
1781          $this->assertEquals($c2->name, $data->templates[0]['cohorts'][0]['name']);
1782          $this->assertEquals(transform::yesno(true), $data->templates[0]['cohorts'][0]['created_or_modified_by_you']);
1783  
1784          // Check frameworks for u2 in cat1.
1785          $data = writer::with_context($cat1ctx)->get_related_data([get_string('competencies', 'core_competency')], 'frameworks');
1786          $this->assertCount(1, $data->frameworks);
1787          $this->assertEquals(transform::yesno(false), $data->frameworks[0]['created_or_modified_by_you']);
1788          $this->assertCount(2, $data->frameworks[0]['competencies']);
1789          $competency = $data->frameworks[0]['competencies'][0];
1790          $this->assertEquals($c2a->get('shortname'), $competency['name']);
1791          $this->assertEquals(transform::yesno(false), $competency['created_or_modified_by_you']);
1792          $this->assertCount(1, $competency['related_competencies']);
1793          $this->assertEquals($c2b->get('shortname'), $competency['related_competencies'][0]['name']);
1794          $this->assertEquals(transform::yesno(true), $competency['related_competencies'][0]['created_or_modified_by_you']);
1795  
1796          // Check templates for u2 in system.
1797          $data = writer::with_context($cat2ctx)->get_related_data([get_string('competencies', 'core_competency')], 'templates');
1798          $this->assertCount(1, $data->templates);
1799          $this->assertEquals($t3->get('shortname'), $data->templates[0]['name']);
1800          $this->assertEquals(transform::yesno(true), $data->templates[0]['created_or_modified_by_you']);
1801          $this->assertCount(1, $data->templates[0]['competencies']);
1802          $competency = $data->templates[0]['competencies'][0];
1803          $this->assertEquals($c3a->get('shortname'), $competency['name']);
1804          $this->assertEquals(transform::yesno(true), $competency['created_or_modified_by_you']);
1805      }
1806  
1807      public function test_export_data_for_user_with_related_learning_plans() {
1808          global $DB;
1809  
1810          $path = [
1811              get_string('competencies', 'core_competency'),
1812              get_string('privacy:path:relatedtome', 'core_competency'),
1813              get_string('privacy:path:plans', 'core_competency'),
1814          ];
1815          $yes = transform::yesno(true);
1816          $no = transform::yesno(false);
1817  
1818          $dg = $this->getDataGenerator();
1819          $ccg = $dg->get_plugin_generator('core_competency');
1820  
1821          $u0 = $dg->create_user();
1822          $u1 = $dg->create_user();
1823          $u2 = $dg->create_user();
1824          $u3 = $dg->create_user();
1825          $u4 = $dg->create_user();
1826          $u5 = $dg->create_user();
1827          $u6 = $dg->create_user();
1828          $u7 = $dg->create_user();
1829          $u8 = $dg->create_user();
1830  
1831          $dg->role_assign($DB->get_field('role', 'id', ['archetype' => 'manager'], IGNORE_MULTIPLE), $u6->id);
1832          $u0ctx = \context_user::instance($u0->id);
1833  
1834          $f = $ccg->create_framework();
1835          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1836          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1837          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1838          $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
1839  
1840          $t = $ccg->create_template();
1841          $tc1 = $ccg->create_template_competency(['competencyid' => $comp1->get('id'), 'templateid' => $t->get('id')]);
1842          $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]);
1843          $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]);
1844          $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]);
1845  
1846          $this->setUser($u1);
1847          $p1 = $ccg->create_plan(['templateid' => $t->get('id'), 'userid' => $u0->id]);
1848  
1849          $this->setUser($u2);
1850          $p2 = $ccg->create_plan(['userid' => $u0->id, 'reviewerid' => $u7->id]);
1851  
1852          $this->setUser($u3);
1853          $p1c1 = $ccg->create_plan_competency(['planid' => $p1->get('id'), 'competencyid' => $comp1->get('id')]);
1854          $p2c2 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp2->get('id')]);
1855          $p2c3 = $ccg->create_plan_competency(['planid' => $p2->get('id'), 'competencyid' => $comp3->get('id')]);
1856  
1857          $this->setUser($u4);
1858          $uc1 = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u0->id, 'grade' => 1,
1859              'proficiency' => true]);
1860          $uc2 = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u0->id, 'grade' => 2,
1861              'proficiency' => false]);
1862          $uc3 = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u0->id]);
1863          $uc4 = $ccg->create_user_competency(['competencyid' => $comp4->get('id'), 'userid' => $u0->id, 'reviewerid' => $u5->id]);
1864  
1865          $this->setUser($u5);
1866          $p3 = $ccg->create_plan(['userid' => $u0->id]);
1867          $p3c1 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp1->get('id')]);
1868          $p3c3 = $ccg->create_plan_competency(['planid' => $p3->get('id'), 'competencyid' => $comp3->get('id')]);
1869  
1870          // Add comments on plan.
1871          $this->allow_anyone_to_comment_anywhere();
1872          $this->setUser($u0);
1873          $p1->get_comment_object()->add('Hello.');
1874          $this->setUser($u8);
1875          $p1->get_comment_object()->add('Hi.');
1876  
1877          // Export data for user 1.
1878          writer::reset();
1879          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
1880          $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1881          $data = writer::with_context($u0ctx)->get_data($planpath);
1882          $this->assertEquals($p1->get('name'), $data->name);
1883          $this->assertEquals($yes, $data->created_or_modified_by_you);
1884  
1885          // Export data for user 2.
1886          writer::reset();
1887          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
1888          $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1889          $data = writer::with_context($u0ctx)->get_data($planpath);
1890          $this->assertEquals($p2->get('name'), $data->name);
1891          $this->assertEquals($yes, $data->created_or_modified_by_you);
1892  
1893          // Export data for user 3.
1894          writer::reset();
1895          provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
1896          $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1897          $data = writer::with_context($u0ctx)->get_data($planpath);
1898          $this->assertEquals($p1->get('name'), $data->name);
1899          $this->assertEquals($no, $data->created_or_modified_by_you);
1900          $this->assertCount(1, $data->competencies);
1901          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
1902          $this->assertEquals($yes, $data->competencies[0]['created_or_modified_by_you']);
1903  
1904          $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1905          $data = writer::with_context($u0ctx)->get_data($planpath);
1906          $this->assertEquals($p2->get('name'), $data->name);
1907          $this->assertEquals($no, $data->created_or_modified_by_you);
1908          $competencies = $data->competencies;
1909          $this->assertCount(2, $competencies);
1910          $this->assertEquals($comp2->get('shortname'), $competencies[0]['name']);
1911          $this->assertEquals($yes, $competencies[0]['created_or_modified_by_you']);
1912          $this->assertEquals($comp3->get('shortname'), $competencies[1]['name']);
1913          $this->assertEquals($yes, $competencies[1]['created_or_modified_by_you']);
1914  
1915          // Export data for user 4.
1916          writer::reset();
1917          provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id]));
1918          foreach ([$p1, $p2, $p3] as $plan) {
1919              $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1920              $data = writer::with_context($u0ctx)->get_data($planpath);
1921              $this->assertEmpty($data);
1922          }
1923  
1924          // Export data for user 5.
1925          writer::reset();
1926          provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id]));
1927          $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]);
1928          $data = writer::with_context($u0ctx)->get_data($planpath);
1929          $this->assertEquals($p3->get('name'), $data->name);
1930          $this->assertEquals($yes, $data->created_or_modified_by_you);
1931          $this->assertCount(2, $data->competencies);
1932          $competency = $data->competencies[0];
1933          $this->assertEquals($comp1->get('shortname'), $competency['name']);
1934          $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1935          $competency = $data->competencies[1];
1936          $this->assertEquals($comp3->get('shortname'), $competency['name']);
1937          $this->assertEquals($yes, $competency['created_or_modified_by_you']);
1938  
1939          // Do some stuff.
1940          $this->setUser($u6);
1941          api::complete_plan($p3);
1942  
1943          // Export data for user 6.
1944          writer::reset();
1945          provider::export_user_data(new approved_contextlist($u6, 'core_competency', [$u0ctx->id]));
1946          $planpath = array_merge($path, ["{$p3->get('name')} ({$p3->get('id')})"]);
1947          $data = writer::with_context($u0ctx)->get_data($planpath);
1948          $this->assertEquals($p3->get('name'), $data->name);
1949          $this->assertEquals($yes, $data->created_or_modified_by_you);
1950          $this->assertCount(2, $data->competencies);
1951          $competency = $data->competencies[0];
1952          $this->assertEquals($comp1->get('shortname'), $competency['name']);
1953          $this->assertArrayNotHasKey('created_or_modified_by_you', $competency);
1954          $this->assertEquals('A', $competency['rating']['rating']);
1955          $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1956          $competency = $data->competencies[1];
1957          $this->assertEquals($comp3->get('shortname'), $competency['name']);
1958          $this->assertArrayNotHasKey('created_or_modified_by_you', $competency);
1959          $this->assertEquals('-', $competency['rating']['rating']);
1960          $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
1961  
1962          // Export data for user 7.
1963          writer::reset();
1964          provider::export_user_data(new approved_contextlist($u7, 'core_competency', [$u0ctx->id]));
1965          $planpath = array_merge($path, ["{$p2->get('name')} ({$p2->get('id')})"]);
1966          $data = writer::with_context($u0ctx)->get_data($planpath);
1967          $this->assertEquals($p2->get('name'), $data->name);
1968          $this->assertEquals($no, $data->created_or_modified_by_you);
1969          $this->assertEquals($yes, $data->reviewer_is_you);
1970  
1971          // Export data for user 8.
1972          writer::reset();
1973          $this->setUser($u8);
1974          provider::export_user_data(new approved_contextlist($u8, 'core_competency', [$u0ctx->id]));
1975          $planpath = array_merge($path, ["{$p1->get('name')} ({$p1->get('id')})"]);
1976          $data = writer::with_context($u0ctx)->get_data($planpath);
1977          $this->assertEquals($p1->get('name'), $data->name);
1978          $this->assertEquals($no, $data->created_or_modified_by_you);
1979          $this->assertEquals($no, $data->reviewer_is_you);
1980          $commentspath = array_merge($planpath,  [get_string('commentsubcontext', 'core_comment')]);
1981          $data = writer::with_context($u0ctx)->get_data($commentspath);
1982          $this->assert_exported_comments(['Hi.'], $data->comments);
1983      }
1984  
1985      public function test_export_data_for_user_with_related_competencies() {
1986          $path = [
1987              get_string('competencies', 'core_competency'),
1988              get_string('privacy:path:relatedtome', 'core_competency'),
1989              get_string('competencies', 'core_competency'),
1990          ];
1991          $yes = transform::yesno(true);
1992          $no = transform::yesno(false);
1993          $makecomppath = function($comp) use ($path) {
1994              return array_merge($path, ["{$comp->get('shortname')} ({$comp->get('id')})"]);
1995          };
1996  
1997          $dg = $this->getDataGenerator();
1998          $ccg = $dg->get_plugin_generator('core_competency');
1999  
2000          $u0 = $dg->create_user();
2001          $u1 = $dg->create_user();
2002          $u2 = $dg->create_user();
2003          $u3 = $dg->create_user();
2004          $u4 = $dg->create_user();
2005          $u5 = $dg->create_user();
2006  
2007          $u0ctx = \context_user::instance($u0->id);
2008  
2009          $f = $ccg->create_framework();
2010          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2011          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2012          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2013          $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2014  
2015          $this->setUser($u1);
2016          api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
2017              'privacy:metadata:competency_evidence', 'core_competency');
2018          api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
2019              'privacy:metadata:competency_evidence', 'core_competency');
2020          api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_LOG,
2021              'privacy:metadata:competency_evidence', 'core_competency');
2022  
2023          $this->setUser($u2);
2024          api::add_evidence($u0->id, $comp1->get('id'), $u0ctx, \core_competency\evidence::ACTION_COMPLETE,
2025              'privacy:metadata:competency_evidence', 'core_competency', null, false, null, null, $u3->id);
2026  
2027          $this->setUser($u3);
2028          api::add_evidence($u0->id, $comp2->get('id'), $u0ctx, \core_competency\evidence::ACTION_OVERRIDE,
2029              'privacy:metadata:competency_evidence', 'core_competency', null, false, null, 1, $u4->id, 'Ze note');
2030  
2031          $this->setUser($u4);
2032          $uc3 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp3->get('id')]);
2033          $uc4 = $ccg->create_user_competency(['userid' => $u0->id, 'competencyid' => $comp4->get('id'), 'reviewerid' => $u2->id]);
2034  
2035          $this->allow_anyone_to_comment_anywhere();
2036          $this->setUser($u0);
2037          $uc3->get_comment_object()->add('...');
2038          $this->setUser($u5);
2039          $uc3->get_comment_object()->add('Hello!');
2040          $uc3->get_comment_object()->add('It\'s me...');
2041  
2042          // Export data for user 1.
2043          writer::reset();
2044          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
2045          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
2046          $competency = (array) $data;
2047          $this->assertEquals($comp1->get('shortname'), $competency['name']);
2048          $evidence = $competency['evidence'];
2049          $this->assertCount(2, $evidence);
2050          $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']);
2051          $this->assertEquals('-', $evidence[0]['actionuserid']);
2052          $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
2053          $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
2054          $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[1]['action']);
2055          $this->assertEquals('-', $evidence[1]['actionuserid']);
2056          $this->assertEquals($no, $evidence[1]['acting_user_is_you']);
2057          $this->assertEquals($yes, $evidence[1]['created_or_modified_by_you']);
2058          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
2059          $competency = (array) $data;
2060          $this->assertEquals($comp2->get('shortname'), $competency['name']);
2061          $evidence = $competency['evidence'];
2062          $this->assertCount(1, $evidence);
2063          $this->assertEquals(get_string('privacy:evidence:action:log', 'core_competency'), $evidence[0]['action']);
2064          $this->assertEquals('-', $evidence[0]['actionuserid']);
2065          $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
2066          $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
2067  
2068          // Export data for user 2.
2069          writer::reset();
2070          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
2071          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
2072          $competency = (array) $data;
2073          $this->assertEquals($comp1->get('shortname'), $competency['name']);
2074          $evidence = $competency['evidence'];
2075          $this->assertCount(1, $evidence);
2076          $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $evidence[0]['action']);
2077          $this->assertEquals($u3->id, $evidence[0]['actionuserid']);
2078          $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
2079          $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
2080          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4));
2081          $competency = (array) $data;
2082          $this->assertEquals($comp4->get('shortname'), $competency['name']);
2083          $this->assertCount(0, $competency['evidence']);
2084          $this->assertEquals($yes, $competency['rating']['reviewer_is_you']);
2085          $this->assertEquals($no, $competency['rating']['created_or_modified_by_you']);
2086  
2087          // Export data for user 3.
2088          writer::reset();
2089          provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
2090          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp1));
2091          $competency = (array) $data;
2092          $this->assertEquals($comp1->get('shortname'), $competency['name']);
2093          $evidence = $competency['evidence'];
2094          $this->assertCount(1, $evidence);
2095          $this->assertEquals($u3->id, $evidence[0]['actionuserid']);
2096          $this->assertEquals($yes, $evidence[0]['acting_user_is_you']);
2097          $this->assertEquals($no, $evidence[0]['created_or_modified_by_you']);
2098          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
2099          $competency = (array) $data;
2100          $this->assertEquals($comp2->get('shortname'), $competency['name']);
2101          $evidence = $competency['evidence'];
2102          $this->assertCount(1, $evidence);
2103          $this->assertEquals(get_string('privacy:evidence:action:override', 'core_competency'), $evidence[0]['action']);
2104          $this->assertEquals($u4->id, $evidence[0]['actionuserid']);
2105          $this->assertEquals($no, $evidence[0]['acting_user_is_you']);
2106          $this->assertEquals($yes, $evidence[0]['created_or_modified_by_you']);
2107  
2108          // Export data for user 4.
2109          writer::reset();
2110          provider::export_user_data(new approved_contextlist($u4, 'core_competency', [$u0ctx->id]));
2111          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp2));
2112          $competency = (array) $data;
2113          $this->assertEquals($comp2->get('shortname'), $competency['name']);
2114          $this->assertNull($competency['rating']);
2115          $this->assertCount(1, $competency['evidence']);
2116          $evidence = $competency['evidence'][0];
2117          $this->assertEquals($u4->id, $evidence['actionuserid']);
2118          $this->assertEquals($yes, $evidence['acting_user_is_you']);
2119          $this->assertEquals($no, $evidence['created_or_modified_by_you']);
2120          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3));
2121          $competency = (array) $data;
2122          $this->assertEquals($comp3->get('shortname'), $competency['name']);
2123          $this->assertEquals($no, $competency['rating']['reviewer_is_you']);
2124          $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
2125          $this->assertEmpty($competency['evidence']);
2126          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp4));
2127          $competency = (array) $data;
2128          $this->assertEquals($comp4->get('shortname'), $competency['name']);
2129          $this->assertEquals($no, $competency['rating']['reviewer_is_you']);
2130          $this->assertEquals($yes, $competency['rating']['created_or_modified_by_you']);
2131          $this->assertEmpty($competency['evidence']);
2132  
2133          // Export data for user 5.
2134          $this->setUser($u5);
2135          writer::reset();
2136          provider::export_user_data(new approved_contextlist($u5, 'core_competency', [$u0ctx->id]));
2137          $data = writer::with_context($u0ctx)->get_data($makecomppath($comp3));
2138          $competency = (array) $data;
2139          $this->assertEquals($comp3->get('shortname'), $competency['name']);
2140          $data = writer::with_context($u0ctx)->get_data(array_merge($makecomppath($comp3),
2141              [get_string('commentsubcontext', 'core_comment')]));
2142          $this->assert_exported_comments(['Hello!', 'It\'s me...'], $data->comments);
2143      }
2144  
2145      public function test_export_data_for_user_with_related_user_evidence() {
2146          $path = [
2147              get_string('competencies', 'core_competency'),
2148              get_string('privacy:path:relatedtome', 'core_competency'),
2149              get_string('privacy:path:userevidence', 'core_competency')
2150          ];
2151          $yes = transform::yesno(true);
2152          $no = transform::yesno(false);
2153  
2154          $dg = $this->getDataGenerator();
2155          $ccg = $dg->get_plugin_generator('core_competency');
2156  
2157          $u0 = $dg->create_user();
2158          $u0b = $dg->create_user();
2159          $u1 = $dg->create_user();
2160          $u2 = $dg->create_user();
2161          $u3 = $dg->create_user();
2162          $u4 = $dg->create_user();
2163  
2164          $u0ctx = \context_user::instance($u0->id);
2165  
2166          $f = $ccg->create_framework();
2167          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2168          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2169          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2170  
2171          $this->setUser($u0);
2172          $ue0 = $ccg->create_user_evidence(['userid' => $u0->id]);
2173  
2174          $this->setUser($u1);
2175          $ue1 = $ccg->create_user_evidence(['userid' => $u0->id]);
2176          $ue1b = $ccg->create_user_evidence(['userid' => $u0b->id]);
2177  
2178          $this->setUser($u2);
2179          $ccg->create_user_evidence_competency(['userevidenceid' => $ue1->get('id'), 'competencyid' => $comp1->get('id')]);
2180          $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp1->get('id')]);
2181          $ue2 = $ccg->create_user_evidence(['userid' => $u0->id]);
2182          $ue2b = $ccg->create_user_evidence(['userid' => $u0b->id]);
2183  
2184          $this->setUser($u3);
2185          $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp2->get('id')]);
2186          $ccg->create_user_evidence_competency(['userevidenceid' => $ue2->get('id'), 'competencyid' => $comp3->get('id')]);
2187  
2188          // Export for user 1.
2189          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u0ctx->id]));
2190          $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]);
2191          $data = writer::with_context($u0ctx)->get_data($uepath);
2192          $this->assertEquals($ue1->get('name'), $data->name);
2193          $this->assertEquals($yes, $data->created_or_modified_by_you);
2194          $this->assertEmpty($data->competencies);
2195  
2196          // Export for user 2.
2197          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u0ctx->id]));
2198          $uepath = array_merge($path, ["{$ue1->get('name')} ({$ue1->get('id')})"]);
2199          $data = writer::with_context($u0ctx)->get_data($uepath);
2200          $this->assertEquals($ue1->get('name'), $data->name);
2201          $this->assertEquals($no, $data->created_or_modified_by_you);
2202          $this->assertCount(1, $data->competencies);
2203          $competency = $data->competencies[0];
2204          $this->assertEquals($comp1->get('shortname'), $competency['name']);
2205          $this->assertEquals($yes, $competency['created_or_modified_by_you']);
2206  
2207          $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]);
2208          $data = writer::with_context($u0ctx)->get_data($uepath);
2209          $this->assertEquals($ue2->get('name'), $data->name);
2210          $this->assertEquals($yes, $data->created_or_modified_by_you);
2211          $this->assertEmpty($data->competencies);
2212  
2213          // Export for user 3.
2214          provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u0ctx->id]));
2215          $uepath = array_merge($path, ["{$ue2->get('name')} ({$ue2->get('id')})"]);
2216          $evidence = writer::with_context($u0ctx)->get_data($uepath);
2217          $this->assertEquals($ue2->get('name'), $evidence->name);
2218          $this->assertEquals($no, $evidence->created_or_modified_by_you);
2219          $this->assertCount(2, $evidence->competencies);
2220          $competency = $evidence->competencies[0];
2221          $this->assertEquals($comp2->get('shortname'), $competency['name']);
2222          $this->assertEquals($yes, $competency['created_or_modified_by_you']);
2223          $competency = $evidence->competencies[1];
2224          $this->assertEquals($comp3->get('shortname'), $competency['name']);
2225          $this->assertEquals($yes, $competency['created_or_modified_by_you']);
2226      }
2227  
2228      public function test_export_data_for_user_about_their_learning_plans() {
2229          $this->setAdminUser();
2230          $dg = $this->getDataGenerator();
2231          $ccg = $dg->get_plugin_generator('core_competency');
2232          $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:plans', 'core_competency')];
2233          $yes = transform::yesno(true);
2234          $no = transform::yesno(false);
2235  
2236          $u1 = $dg->create_user();
2237          $u2 = $dg->create_user();
2238          $u3 = $dg->create_user();
2239          $u1ctx = \context_user::instance($u1->id);
2240          $u2ctx = \context_user::instance($u2->id);
2241  
2242          $f = $ccg->create_framework();
2243          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2244          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2245          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2246          $comp4 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2247  
2248          $t = $ccg->create_template();
2249          $tc2 = $ccg->create_template_competency(['competencyid' => $comp2->get('id'), 'templateid' => $t->get('id')]);
2250          $tc3 = $ccg->create_template_competency(['competencyid' => $comp3->get('id'), 'templateid' => $t->get('id')]);
2251          $tc4 = $ccg->create_template_competency(['competencyid' => $comp4->get('id'), 'templateid' => $t->get('id')]);
2252  
2253          $p1a = $ccg->create_plan(['userid' => $u1->id, 'templateid' => $t->get('id'),
2254              'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW]);
2255          $p1b = $ccg->create_plan(['userid' => $u1->id]);
2256          $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp1->get('id')]);
2257          $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp2->get('id')]);
2258          $ccg->create_plan_competency(['planid' => $p1b->get('id'), 'competencyid' => $comp4->get('id')]);
2259          $p1c = $ccg->create_plan(['userid' => $u1->id]);
2260          $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp1->get('id')]);
2261          $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp3->get('id')]);
2262          $ccg->create_plan_competency(['planid' => $p1c->get('id'), 'competencyid' => $comp4->get('id')]);
2263          $p1d = $ccg->create_plan(['userid' => $u1->id]);
2264  
2265          $p2a = $ccg->create_plan(['userid' => $u2->id]);
2266          $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp1->get('id')]);
2267          $ccg->create_plan_competency(['planid' => $p2a->get('id'), 'competencyid' => $comp2->get('id')]);
2268  
2269          $uc1a = $ccg->create_user_competency(['competencyid' => $comp1->get('id'), 'userid' => $u1->id,
2270              'grade' => 2, 'proficiency' => false]);
2271          $uc1b = $ccg->create_user_competency(['competencyid' => $comp2->get('id'), 'userid' => $u1->id,
2272              'grade' => 3, 'proficiency' => false]);
2273          $uc1c = $ccg->create_user_competency(['competencyid' => $comp3->get('id'), 'userid' => $u1->id]);
2274  
2275          // Add comments on plan.
2276          $this->allow_anyone_to_comment_anywhere();
2277          $this->setUser($u1);
2278          $p1a->get_comment_object()->add('Hello.');
2279          $p1a->get_comment_object()->add('It\'s me.');
2280          $this->setUser($u3);
2281          $p1a->get_comment_object()->add('After all these years...');
2282  
2283          // Complete the plan to create archiving, and modify the user competency again.
2284          api::complete_plan($p1c);
2285          $uc1a->set('grade', 1);
2286          $uc1a->set('proficiency', true);
2287          $uc1a->update();
2288  
2289          // Export user data in both contexts.
2290          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
2291  
2292          // This plan is based off a template.
2293          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
2294          $this->assertNotEmpty($data);
2295          $this->assertEquals($p1a->get('name'), $data->name);
2296          $this->assertEquals($p1a->get_statusname(), $data->status);
2297          $this->assertCount(3, $data->competencies);
2298          $comp = $data->competencies[0];
2299          $this->assertEquals($comp2->get('shortname'), $comp['name']);
2300          $this->assertEquals('C', $comp['rating']['rating']);
2301          $comp = $data->competencies[1];
2302          $this->assertEquals($comp3->get('shortname'), $comp['name']);
2303          $this->assertEquals('-', $comp['rating']['rating']);
2304          $comp = $data->competencies[2];
2305          $this->assertEquals($comp4->get('shortname'), $comp['name']);
2306          $this->assertNull($comp['rating']);
2307          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})",
2308              get_string('commentsubcontext', 'core_comment')]));
2309          $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments);
2310  
2311          // This plan is manually created.
2312          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
2313          $this->assertNotEmpty($data);
2314          $this->assertEquals($p1b->get('name'), $data->name);
2315          $this->assertCount(3, $data->competencies);
2316          $comp = $data->competencies[0];
2317          $this->assertEquals($comp1->get('shortname'), $comp['name']);
2318          $this->assertEquals('A', $comp['rating']['rating']);
2319          $comp = $data->competencies[1];
2320          $this->assertEquals($comp2->get('shortname'), $comp['name']);
2321          $this->assertEquals('C', $comp['rating']['rating']);
2322          $comp = $data->competencies[2];
2323          $this->assertEquals($comp4->get('shortname'), $comp['name']);
2324          $this->assertNull($comp['rating']);
2325  
2326          // This plan is complete.
2327          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
2328          $this->assertNotEmpty($data);
2329          $this->assertEquals($p1c->get('name'), $data->name);
2330          $this->assertCount(3, $data->competencies);
2331          $comp = $data->competencies[0];
2332          $this->assertEquals($comp1->get('shortname'), $comp['name']);
2333          $this->assertEquals('B', $comp['rating']['rating']);
2334          $comp = $data->competencies[1];
2335          $this->assertEquals($comp3->get('shortname'), $comp['name']);
2336          $this->assertEquals('-', $comp['rating']['rating']);
2337          $comp = $data->competencies[2];
2338          $this->assertEquals($comp4->get('shortname'), $comp['name']);
2339          $this->assertEquals('-', $comp['rating']['rating']);
2340  
2341          // This plan is empty.
2342          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1d->get('name')} ({$p1d->get('id')})"]));
2343          $this->assertNotEmpty($data);
2344          $this->assertEquals($p1d->get('name'), $data->name);
2345          $this->assertEquals($p1d->get_statusname(), $data->status);
2346          $this->assertEmpty($data->competencies);
2347  
2348          // Confirm that we do not get export what we shouldn't.
2349          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
2350          $this->assertEmpty($data);
2351          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
2352          $this->assertEmpty($data);
2353          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
2354          $this->assertEmpty($data);
2355          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
2356          $this->assertEmpty($data);
2357          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
2358          $this->assertEmpty($data);
2359  
2360          // Export for user 2.
2361          writer::reset();
2362          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
2363  
2364          // Validate the basic plan.
2365          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p2a->get('name')} ({$p2a->get('id')})"]));
2366          $this->assertNotEmpty($data);
2367          $this->assertEquals($p2a->get('name'), $data->name);
2368          $this->assertCount(2, $data->competencies);
2369          $comp = $data->competencies[0];
2370          $this->assertEquals($comp1->get('shortname'), $comp['name']);
2371          $this->assertNull($comp['rating']);
2372          $comp = $data->competencies[1];
2373          $this->assertEquals($comp2->get('shortname'), $comp['name']);
2374          $this->assertNull($comp['rating']);
2375  
2376          // Confirm that we do not get export what we shouldn't.
2377          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
2378          $this->assertEmpty($data);
2379          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
2380          $this->assertEmpty($data);
2381          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
2382          $this->assertEmpty($data);
2383          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1a->get('name')} ({$p1a->get('id')})"]));
2384          $this->assertEmpty($data);
2385          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1b->get('name')} ({$p1b->get('id')})"]));
2386          $this->assertEmpty($data);
2387          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$p1c->get('name')} ({$p1c->get('id')})"]));
2388          $this->assertEmpty($data);
2389      }
2390  
2391      public function test_export_data_for_user_about_their_competencies() {
2392          $dg = $this->getDataGenerator();
2393          $ccg = $dg->get_plugin_generator('core_competency');
2394          $path = [get_string('competencies', 'core_competency'), get_string('competencies', 'core_competency')];
2395          $no = transform::yesno(false);
2396  
2397          $u1 = $dg->create_user();
2398          $u2 = $dg->create_user();
2399          $u3 = $dg->create_user();
2400          $u1ctx = \context_user::instance($u1->id);
2401          $u2ctx = \context_user::instance($u2->id);
2402  
2403          $f = $ccg->create_framework();
2404          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2405          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2406          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2407  
2408          $uc1a = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp1->get('id')]);
2409          $uc1b = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp2->get('id'),
2410              'grade' => 2, 'proficiency' => false]);
2411          $uc1c = $ccg->create_user_competency(['userid' => $u1->id, 'competencyid' => $comp3->get('id')]);
2412          $e1a1 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'),
2413              'action' => \core_competency\evidence::ACTION_COMPLETE, 'grade' => 1]);
2414          $e1a2 = $ccg->create_evidence(['usercompetencyid' => $uc1a->get('id'), 'note' => 'Not too bad']);
2415          $e1b1 = $ccg->create_evidence(['usercompetencyid' => $uc1b->get('id'), 'url' => 'https://example.com']);
2416  
2417          $uc2a = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp1->get('id')]);
2418          $uc2b = $ccg->create_user_competency(['userid' => $u2->id, 'competencyid' => $comp2->get('id')]);
2419          $e2a1 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'A']);
2420          $e2a2 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'B']);
2421          $e2a3 = $ccg->create_evidence(['usercompetencyid' => $uc2b->get('id'), 'note' => 'C']);
2422  
2423          // Add comments on competency.
2424          $this->allow_anyone_to_comment_anywhere();
2425          $this->setUser($u1);
2426          $uc1a->get_comment_object()->add('Hello.');
2427          $uc1a->get_comment_object()->add('It\'s me.');
2428          $this->setUser($u3);
2429          $uc1a->get_comment_object()->add('After all these years...');
2430  
2431          // Export for user 1 in both contexts.
2432          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
2433          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
2434          $this->assertNotEmpty($data);
2435          $this->assertEquals($comp1->get('shortname'), $data->name);
2436          $this->assertEquals('-', $data->rating['rating']);
2437          $this->assertCount(2, $data->evidence);
2438          $this->assertEquals(get_string('privacy:evidence:action:complete', 'core_competency'), $data->evidence[1]['action']);
2439          $this->assertEquals('Not too bad', $data->evidence[0]['note']);
2440          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})",
2441              get_string('commentsubcontext', 'core_comment')]));
2442          $this->assert_exported_comments(['Hello.', 'It\'s me.', 'After all these years...'], $data->comments);
2443  
2444          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
2445          $this->assertNotEmpty($data);
2446          $this->assertEquals($comp2->get('shortname'), $data->name);
2447          $this->assertEquals('B', $data->rating['rating']);
2448          $this->assertEquals($no, $data->rating['proficient']);
2449          $this->assertCount(1, $data->evidence);
2450          $this->assertEquals('https://example.com', $data->evidence[0]['url']);
2451  
2452          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
2453          $this->assertNotEmpty($data);
2454          $this->assertEquals($comp3->get('shortname'), $data->name);
2455          $this->assertEquals('-', $data->rating['rating']);
2456          $this->assertEquals('-', $data->rating['proficient']);
2457          $this->assertEmpty($data->evidence);
2458  
2459          // We don't know anything about user 2.
2460          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
2461          $this->assertEmpty($data);
2462          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
2463          $this->assertEmpty($data);
2464          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
2465          $this->assertEmpty($data);
2466  
2467          // Export for user 2 in both contexts.
2468          writer::reset();
2469          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u1ctx->id, $u2ctx->id]));
2470          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
2471          $this->assertNotEmpty($data);
2472          $this->assertEquals($comp1->get('shortname'), $data->name);
2473          $this->assertEquals('-', $data->rating['rating']);
2474          $this->assertCount(0, $data->evidence);
2475  
2476          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
2477          $this->assertNotEmpty($data);
2478          $this->assertEquals($comp2->get('shortname'), $data->name);
2479          $this->assertEquals('-', $data->rating['rating']);
2480          $this->assertCount(3, $data->evidence);
2481          $this->assertEquals('C', $data->evidence[0]['note']);
2482          $this->assertEquals('B', $data->evidence[1]['note']);
2483          $this->assertEquals('A', $data->evidence[2]['note']);
2484  
2485          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
2486          $this->assertEmpty($data);
2487  
2488          // We don't know anything about user 1.
2489          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp1->get('shortname')} ({$comp1->get('id')})"]));
2490          $this->assertEmpty($data);
2491          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp2->get('shortname')} ({$comp2->get('id')})"]));
2492          $this->assertEmpty($data);
2493          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$comp3->get('shortname')} ({$comp3->get('id')})"]));
2494          $this->assertEmpty($data);
2495      }
2496  
2497      public function test_export_data_for_user_about_their_user_evidence() {
2498          $dg = $this->getDataGenerator();
2499          $ccg = $dg->get_plugin_generator('core_competency');
2500          $path = [get_string('competencies', 'core_competency'), get_string('privacy:path:userevidence', 'core_competency')];
2501  
2502          $u1 = $dg->create_user();
2503          $u2 = $dg->create_user();
2504          $u3 = $dg->create_user();
2505  
2506          $u1ctx = \context_user::instance($u1->id);
2507          $u2ctx = \context_user::instance($u2->id);
2508          $u3ctx = \context_user::instance($u3->id);
2509  
2510          $f = $ccg->create_framework();
2511          $comp1 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2512          $comp2 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2513          $comp3 = $ccg->create_competency(['competencyframeworkid' => $f->get('id')]);
2514  
2515          $ue1a = $ccg->create_user_evidence(['userid' => $u1->id]);
2516          $ue1b = $ccg->create_user_evidence(['userid' => $u1->id]);
2517          $ue2a = $ccg->create_user_evidence(['userid' => $u2->id]);
2518          $ue3a = $ccg->create_user_evidence(['userid' => $u3->id]);
2519          $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp1->get('id')]);
2520          $ccg->create_user_evidence_competency(['userevidenceid' => $ue1a->get('id'), 'competencyid' => $comp2->get('id')]);
2521          $ccg->create_user_evidence_competency(['userevidenceid' => $ue1b->get('id'), 'competencyid' => $comp2->get('id')]);
2522          $ccg->create_user_evidence_competency(['userevidenceid' => $ue2a->get('id'), 'competencyid' => $comp2->get('id')]);
2523  
2524          // Export for user 1 in two contexts to make sure.
2525          provider::export_user_data(new approved_contextlist($u1, 'core_competency', [$u1ctx->id, $u2ctx->id]));
2526          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
2527          $this->assertNotEmpty($data);
2528          $this->assertEquals($ue1a->get('name'), $data->name);
2529          $this->assertCount(2, $data->competencies);
2530          $this->assertEquals($comp1->get('shortname'), $data->competencies[0]['name']);
2531          $this->assertEquals($comp2->get('shortname'), $data->competencies[1]['name']);
2532  
2533          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
2534          $this->assertNotEmpty($data);
2535          $this->assertEquals($ue1b->get('name'), $data->name);
2536          $this->assertCount(1, $data->competencies);
2537          $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
2538  
2539          // We should not have access to other's info.
2540          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
2541          $this->assertEmpty($data);
2542          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
2543          $this->assertEmpty($data);
2544  
2545          // Export for user 2 in two contexts to make sure.
2546          writer::reset();
2547          provider::export_user_data(new approved_contextlist($u2, 'core_competency', [$u2ctx->id, $u1ctx->id]));
2548          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue2a->get('name')} ({$ue2a->get('id')})"]));
2549          $this->assertNotEmpty($data);
2550          $this->assertEquals($ue2a->get('name'), $data->name);
2551          $this->assertCount(1, $data->competencies);
2552          $this->assertEquals($comp2->get('shortname'), $data->competencies[0]['name']);
2553  
2554          // We should not have access to other's info.
2555          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
2556          $this->assertEmpty($data);
2557          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1a->get('name')} ({$ue1a->get('id')})"]));
2558          $this->assertEmpty($data);
2559          $data = writer::with_context($u1ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
2560          $this->assertEmpty($data);
2561          $data = writer::with_context($u2ctx)->get_data(array_merge($path, ["{$ue1b->get('name')} ({$ue1b->get('id')})"]));
2562          $this->assertEmpty($data);
2563  
2564          // Export for user 3.
2565          writer::reset();
2566          provider::export_user_data(new approved_contextlist($u3, 'core_competency', [$u3ctx->id]));
2567          $data = writer::with_context($u3ctx)->get_data(array_merge($path, ["{$ue3a->get('name')} ({$ue3a->get('id')})"]));
2568          $this->assertNotEmpty($data);
2569          $this->assertEquals($ue3a->get('name'), $data->name);
2570          $this->assertCount(0, $data->competencies);
2571      }
2572  
2573      /**
2574       * Helps testing comments on plans.
2575       *
2576       * @return void
2577       */
2578      protected function allow_anyone_to_comment_anywhere() {
2579          global $DB;
2580          $roleid = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
2581          assign_capability('moodle/competency:plancomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
2582          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
2583          assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
2584          assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
2585          assign_capability('moodle/competency:usercompetencyview', CAP_ALLOW, $roleid, SYSCONTEXTID, true);
2586          accesslib_clear_all_caches_for_unit_testing();
2587      }
2588  
2589      /**
2590       * Assert the content of a contextlist.
2591       *
2592       * @param contextlist $contextlist The list.
2593       * @param array $expectedcontextsorids The expected content.
2594       * @return void
2595       */
2596      protected function assert_contextlist(contextlist $contextlist, $expectedcontextsorids) {
2597          $contextids = array_unique($contextlist->get_contextids());
2598          $expectedids = array_unique(array_map(function($item) {
2599              return $item instanceof \context ? $item->id : $id;
2600          }, $expectedcontextsorids));
2601          $this->assert_array_match($contextids, $expectedids);
2602      }
2603  
2604      /**
2605       * Assert that array match.
2606       *
2607       * @param array $array1 The first one.
2608       * @param array $array2 The second one.
2609       * @return void
2610       */
2611      protected function assert_array_match($array1, $array2) {
2612          $array1 = (array) (object) $array1;
2613          $array2 = (array) (object) $array2;
2614          sort($array1);
2615          sort($array2);
2616          $this->assertEquals($array1, $array2);
2617      }
2618  
2619      /**
2620       * Assert the content of exported comments.
2621       *
2622       * @param array $expected The content of the comments.
2623       * @param array $comments The exported comments.
2624       * @return void
2625       */
2626      protected function assert_exported_comments($expected, $comments) {
2627          $this->assertCount(count($expected), $comments);
2628          $contents = array_map(function($comment) {
2629              return strip_tags($comment->content);
2630          }, $comments);
2631          $this->assert_array_match($expected, $contents);
2632      }
2633  
2634      /**
2635       * Assert that a comment object has comments.
2636       *
2637       * @param \comment $comment The comment object.
2638       * @return void
2639       */
2640      protected function assert_has_comments(\comment $comment) {
2641          global $DB;
2642          $this->assertTrue($DB->record_exists('comments', [
2643              'contextid' => $comment->get_context()->id,
2644              'component' => $comment->get_component(),
2645              'commentarea' => $comment->get_commentarea(),
2646              'itemid' => $comment->get_itemid()
2647          ]));
2648      }
2649  
2650      /**
2651       * Assert that a comment object does not have any comments.
2652       *
2653       * @param \comment $comment The comment object.
2654       * @return void
2655       */
2656      protected function assert_has_no_comments(\comment $comment) {
2657          global $DB;
2658          $this->assertFalse($DB->record_exists('comments', [
2659              'contextid' => $comment->get_context()->id,
2660              'component' => $comment->get_component(),
2661              'commentarea' => $comment->get_commentarea(),
2662              'itemid' => $comment->get_itemid()
2663          ]));
2664      }
2665  
2666      /**
2667       * Get the count of comments.
2668       *
2669       * @param \comment $comment The comment object.
2670       * @param int $userid The user ID.
2671       * @return int
2672       */
2673      protected function get_comments_count(\comment $comment, $userid = null) {
2674          global $DB;
2675          $params = [
2676              'contextid' => $comment->get_context()->id,
2677              'component' => $comment->get_component(),
2678              'commentarea' => $comment->get_commentarea(),
2679              'itemid' => $comment->get_itemid(),
2680          ];
2681          if ($userid) {
2682              $params['userid'] = $userid;
2683          }
2684          return $DB->count_records('comments', $params);
2685      }
2686  }