| 1 | efrain | 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 |         $totalactivities = array_reduce($mods, fn($carry, $item) => $carry + ($item["count"] ?? 0), 0);
 | 
        
           |  |  | 74 |         $data = (object)[
 | 
        
           |  |  | 75 |             'showcompletion' => $showcompletion,
 | 
        
           |  |  | 76 |             'total' => $total,
 | 
        
           |  |  | 77 |             'complete' => $complete,
 | 
        
           |  |  | 78 |             'mods' => array_values($mods),
 | 
        
           |  |  | 79 |             'totalactivities' => $totalactivities,
 | 
        
           |  |  | 80 |         ];
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         $data->modprogress = get_string('progresstotal', 'completion', $data);
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         return $data;
 | 
        
           |  |  | 85 |     }
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |     /**
 | 
        
           |  |  | 88 |      * Calculate the activities count of the current section.
 | 
        
           |  |  | 89 |      *
 | 
        
           |  |  | 90 |      * @return array with [[count by activity type], completed activities, total of activitites]
 | 
        
           |  |  | 91 |      */
 | 
        
           | 1441 | ariadna | 92 |     protected function calculate_section_stats(): array {
 | 
        
           | 1 | efrain | 93 |         $format = $this->format;
 | 
        
           |  |  | 94 |         $course = $format->get_course();
 | 
        
           |  |  | 95 |         $section = $this->section;
 | 
        
           |  |  | 96 |         $modinfo = $format->get_modinfo();
 | 
        
           |  |  | 97 |         $completioninfo = new completion_info($course);
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         $mods = [];
 | 
        
           |  |  | 100 |         $total = 0;
 | 
        
           |  |  | 101 |         $complete = 0;
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |         $cmids = $modinfo->sections[$section->section] ?? [];
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |         $cancomplete = isloggedin() && !isguestuser();
 | 
        
           |  |  | 106 |         $showcompletion = false;
 | 
        
           |  |  | 107 |         foreach ($cmids as $cmid) {
 | 
        
           |  |  | 108 |             $thismod = $modinfo->cms[$cmid];
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |             if ($thismod->uservisible) {
 | 
        
           |  |  | 111 |                 if (isset($mods[$thismod->modname])) {
 | 
        
           |  |  | 112 |                     $mods[$thismod->modname]['name'] = $thismod->modplural;
 | 
        
           |  |  | 113 |                     $mods[$thismod->modname]['count']++;
 | 
        
           |  |  | 114 |                 } else {
 | 
        
           |  |  | 115 |                     $mods[$thismod->modname]['name'] = $thismod->modfullname;
 | 
        
           |  |  | 116 |                     $mods[$thismod->modname]['count'] = 1;
 | 
        
           |  |  | 117 |                 }
 | 
        
           |  |  | 118 |                 if ($cancomplete && $completioninfo->is_enabled($thismod) != COMPLETION_TRACKING_NONE) {
 | 
        
           |  |  | 119 |                     $showcompletion = true;
 | 
        
           |  |  | 120 |                     $total++;
 | 
        
           |  |  | 121 |                     $completiondata = $completioninfo->get_data($thismod, true);
 | 
        
           |  |  | 122 |                     if ($completiondata->completionstate == COMPLETION_COMPLETE ||
 | 
        
           |  |  | 123 |                             $completiondata->completionstate == COMPLETION_COMPLETE_PASS) {
 | 
        
           |  |  | 124 |                         $complete++;
 | 
        
           |  |  | 125 |                     }
 | 
        
           |  |  | 126 |                 }
 | 
        
           |  |  | 127 |             }
 | 
        
           |  |  | 128 |         }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |         return [$mods, $complete, $total, $showcompletion];
 | 
        
           |  |  | 131 |     }
 | 
        
           |  |  | 132 | }
 |