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.
   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   * Contains the default activities summary (used for singlesection format).
  19   *
  20   * @package   core_courseformat
  21   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_courseformat\output\local\content\section;
  26  
  27  use completion_info;
  28  use core\output\named_templatable;
  29  use core_courseformat\base as course_format;
  30  use core_courseformat\output\local\courseformat_named_templatable;
  31  use renderable;
  32  use section_info;
  33  use stdClass;
  34  
  35  /**
  36   * Base class to render a course section summary.
  37   *
  38   * @package   core_courseformat
  39   * @copyright 2020 Ferran Recio <ferran@moodle.com>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class cmsummary implements named_templatable, renderable {
  43  
  44      use courseformat_named_templatable;
  45  
  46      /** @var course_format the course format class */
  47      protected $format;
  48  
  49      /** @var section_info the course section class */
  50      protected $section;
  51  
  52      /**
  53       * Constructor.
  54       *
  55       * @param course_format $format the course format
  56       * @param section_info $section the section info
  57       */
  58      public function __construct(course_format $format, section_info $section) {
  59          $this->format = $format;
  60          $this->section = $section;
  61      }
  62  
  63      /**
  64       * Export this data so it can be used as the context for a mustache template.
  65       *
  66       * @param renderer_base $output typically, the renderer that's calling this function
  67       * @return array data context for a mustache template
  68       */
  69      public function export_for_template(\renderer_base $output): stdClass {
  70  
  71          list($mods, $complete, $total, $showcompletion) = $this->calculate_section_stats();
  72  
  73          if (empty($mods)) {
  74              return new stdClass();
  75          }
  76  
  77          $data = (object)[
  78              'showcompletion' => $showcompletion,
  79              'total' => $total,
  80              'complete' => $complete,
  81              'mods' => array_values($mods),
  82          ];
  83  
  84          $data->modprogress = get_string('progresstotal', 'completion', $data);
  85  
  86          return $data;
  87      }
  88  
  89      /**
  90       * Calculate the activities count of the current section.
  91       *
  92       * @return array with [[count by activity type], completed activities, total of activitites]
  93       */
  94      private function calculate_section_stats(): array {
  95          $format = $this->format;
  96          $course = $format->get_course();
  97          $section = $this->section;
  98          $modinfo = $format->get_modinfo();
  99          $completioninfo = new completion_info($course);
 100  
 101          $mods = [];
 102          $total = 0;
 103          $complete = 0;
 104  
 105          $cmids = $modinfo->sections[$section->section] ?? [];
 106  
 107          $cancomplete = isloggedin() && !isguestuser();
 108          $showcompletion = false;
 109          foreach ($cmids as $cmid) {
 110              $thismod = $modinfo->cms[$cmid];
 111  
 112              if ($thismod->uservisible) {
 113                  if (isset($mods[$thismod->modname])) {
 114                      $mods[$thismod->modname]['name'] = $thismod->modplural;
 115                      $mods[$thismod->modname]['count']++;
 116                  } else {
 117                      $mods[$thismod->modname]['name'] = $thismod->modfullname;
 118                      $mods[$thismod->modname]['count'] = 1;
 119                  }
 120                  if ($cancomplete && $completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
 121                      $showcompletion = true;
 122                      $total++;
 123                      $completiondata = $completioninfo->get_data($thismod, true);
 124                      if ($completiondata->completionstate == COMPLETION_COMPLETE ||
 125                              $completiondata->completionstate == COMPLETION_COMPLETE_PASS) {
 126                          $complete++;
 127                      }
 128                  }
 129              }
 130          }
 131  
 132          return [$mods, $complete, $total, $showcompletion];
 133      }
 134  }