/**
  * Populate forum id arrays.
  * @throws \coding_exception
  */
 protected function populate_forums()
 {
     local::swap_global_user($this->user->id);
     // Note - we don't include the site in the list of courses. This is intentional - we want student engagement to
     // be increased in courses where learning takes place and the front page is unlikely to fit that model.
     // Currently we are using local::swap_global_user as a hack for the following function (MDL-51353).
     $this->courses = enrol_get_my_courses();
     $forums = [];
     $hsuforums = [];
     foreach ($this->courses as $course) {
         $forums = $forums + forum_get_readable_forums($this->user->id, $course->id);
         if (function_exists('hsuforum_get_readable_forums')) {
             $hsuforums = $hsuforums + hsuforum_get_readable_forums($this->user->id, $course->id, true);
         }
     }
     // Remove Q&A forums from array.
     $forums = $this->purge_qa_forums($forums);
     $hsuforums = $this->purge_qa_forums($hsuforums);
     // Rmove forums in courses not accessed for a long time.
     $forums = $this->process_stale_forums($forums);
     $hsuforums = $this->process_stale_forums($hsuforums, true);
     $this->forums = $forums;
     $this->hsuforums = $hsuforums;
     $this->forumids = array_keys($forums);
     $this->forumidsallgroups = $this->forumids_accessallgroups($forums);
     $this->hsuforumids = array_keys($hsuforums);
     $this->hsuforumidsallgroups = $this->forumids_accessallgroups($hsuforums, 'hsuforum');
     local::swap_global_user(false);
 }
 /**
  * Test swap global user.
  */
 public function test_swap_global_user()
 {
     global $USER;
     $this->resetAfterTest();
     $generator = $this->getDataGenerator();
     $originaluserid = $USER->id;
     $user1 = $generator->create_user();
     $user2 = $generator->create_user();
     $user3 = $generator->create_user();
     local::swap_global_user($user1);
     $this->assertEquals($user1->id, $USER->id);
     local::swap_global_user($user2);
     $this->assertEquals($user2->id, $USER->id);
     local::swap_global_user($user3);
     $this->assertEquals($user3->id, $USER->id);
     local::swap_global_user(false);
     $this->assertEquals($user2->id, $USER->id);
     local::swap_global_user(false);
     $this->assertEquals($user1->id, $USER->id);
     local::swap_global_user(false);
     $this->assertEquals($originaluserid, $USER->id);
 }