Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  namespace report_progress\local;
  18  
  19  /**
  20   * Helper for report progress.
  21   *
  22   * @package   report_progress
  23   * @copyright 2021 The Open University
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL Juv3 or later
  25   */
  26  class helper {
  27  
  28      /** The default number of results to be shown per page. */
  29      const COMPLETION_REPORT_PAGE = 25;
  30  
  31      /**
  32       * Get activities information by the activity include and activity order option.
  33       *
  34       * @param \completion_info $completion Completion information of course.
  35       * @param string $activityinclude Activity type for filtering.
  36       * @param string $activityorder Activity sort option.
  37       * @return array The available activity types and activities array after filtering and sorting.
  38       * @throws \coding_exception
  39       */
  40      public static function get_activities_to_show(\completion_info $completion, string $activityinclude,
  41              string $activityorder): array {
  42          // Get all activity types.
  43          $activities = $completion->get_activities();
  44          $availableactivitytypes = [];
  45  
  46          foreach ($activities as $activity) {
  47              $availableactivitytypes[$activity->modname] = $activity->get_module_type_name(true);
  48          }
  49  
  50          asort($availableactivitytypes);
  51          $availableactivitytypes = ['all' => get_string('allactivitiesandresources', 'report_progress')] +
  52              $availableactivitytypes;
  53  
  54          // Filter activities by type.
  55          if (!empty($activityinclude) && $activityinclude !== 'all') {
  56              $activities = array_filter($activities, function($activity) use ($activityinclude) {
  57                  return $activity->modname === $activityinclude;
  58              });
  59          }
  60  
  61          // The activities are sorted by activity order on course page by default.
  62          if ($activityorder === 'alphabetical') {
  63              usort($activities, function($a, $b) {
  64                  return strcmp($a->name, $b->name);
  65              });
  66          }
  67  
  68          return [$availableactivitytypes, $activities];
  69      }
  70  }