Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * block_online_users data generator
  19   *
  20   * @package    block_online_users
  21   * @category   test
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Online users block data generator class
  31   *
  32   * @package    block_online_users
  33   * @category   test
  34   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class block_online_users_generator extends testing_block_generator {
  38  
  39      /**
  40       * Create (simulated) logged in users and add some of them to groups in a course
  41       */
  42      public function create_logged_in_users() {
  43          global $DB;
  44  
  45          $generator = advanced_testcase::getDataGenerator();
  46          $data = array();
  47  
  48          // Create 2 courses.
  49          $course1 = $generator->create_course();
  50          $data['course1'] = $course1;
  51          $course2 = $generator->create_course();
  52          $data['course2'] = $course2;
  53  
  54          // Create 9 (simulated) logged in users enroled into $course1.
  55          for ($i = 1; $i <= 9; $i++) {
  56              $user = $generator->create_user();
  57              $DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
  58              $generator->enrol_user($user->id, $course1->id);
  59              $DB->insert_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course1->id, 'timeaccess' => time()));
  60              $data['user' . $i] = $user;
  61          }
  62          // Create 3 (simulated) logged in users who are not enroled into $course1.
  63          for ($i = 10; $i <= 12; $i++) {
  64              $user = $generator->create_user();
  65              $DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
  66              $data['user' . $i] = $user;
  67          }
  68  
  69          // Create 3 groups in course 1.
  70          $group1 = $generator->create_group(array('courseid' => $course1->id));
  71          $data['group1'] = $group1;
  72          $group2 = $generator->create_group(array('courseid' => $course1->id));
  73          $data['group2'] = $group2;
  74          $group3 = $generator->create_group(array('courseid' => $course1->id));
  75          $data['group3'] = $group3;
  76  
  77          // Add 3 users to course group 1.
  78          $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user1']->id));
  79          $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user2']->id));
  80          $generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user3']->id));
  81  
  82          // Add 4 users to course group 2.
  83          $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user3']->id));
  84          $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user4']->id));
  85          $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user5']->id));
  86          $generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user6']->id));
  87  
  88          return $data; // Return the user, course and group objects.
  89      }
  90  }