| 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 | namespace core_courseformat\output\local\state;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core_availability\info_section;
 | 
        
           |  |  | 20 | use core_courseformat\base as course_format;
 | 
        
           |  |  | 21 | use section_info;
 | 
        
           |  |  | 22 | use renderable;
 | 
        
           |  |  | 23 | use stdClass;
 | 
        
           |  |  | 24 | use context_course;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | /**
 | 
        
           |  |  | 27 |  * Contains the ajax update section structure.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package   core_course
 | 
        
           |  |  | 30 |  * @copyright 2021 Ferran Recio <ferran@moodle.com>
 | 
        
           |  |  | 31 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 32 |  */
 | 
        
           |  |  | 33 | class section implements renderable {
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     /** @var course_format the course format class */
 | 
        
           |  |  | 36 |     protected $format;
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |     /** @var section_info the course section class */
 | 
        
           |  |  | 39 |     protected $section;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |     /**
 | 
        
           |  |  | 42 |      * Constructor.
 | 
        
           |  |  | 43 |      *
 | 
        
           |  |  | 44 |      * @param course_format $format the course format
 | 
        
           |  |  | 45 |      * @param section_info $section the section info
 | 
        
           |  |  | 46 |      */
 | 
        
           |  |  | 47 |     public function __construct(course_format $format, section_info $section) {
 | 
        
           |  |  | 48 |         $this->format = $format;
 | 
        
           |  |  | 49 |         $this->section = $section;
 | 
        
           |  |  | 50 |     }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |     /**
 | 
        
           |  |  | 53 |      * Export this data so it can be used as state object in the course editor.
 | 
        
           |  |  | 54 |      *
 | 
        
           |  |  | 55 |      * @param \renderer_base $output typically, the renderer that's calling this function
 | 
        
           |  |  | 56 |      * @return array data context for a mustache template
 | 
        
           |  |  | 57 |      */
 | 
        
           |  |  | 58 |     public function export_for_template(\renderer_base $output): stdClass {
 | 
        
           |  |  | 59 |         $format = $this->format;
 | 
        
           |  |  | 60 |         $course = $format->get_course();
 | 
        
           |  |  | 61 |         $section = $this->section;
 | 
        
           |  |  | 62 |         $modinfo = $format->get_modinfo();
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         $indexcollapsed = false;
 | 
        
           |  |  | 65 |         $contentcollapsed = false;
 | 
        
           |  |  | 66 |         $preferences = $format->get_sections_preferences();
 | 
        
           |  |  | 67 |         if (isset($preferences[$section->id])) {
 | 
        
           |  |  | 68 |             $sectionpreferences = $preferences[$section->id];
 | 
        
           |  |  | 69 |             if (!empty($sectionpreferences->contentcollapsed)) {
 | 
        
           |  |  | 70 |                 $contentcollapsed = true;
 | 
        
           |  |  | 71 |             }
 | 
        
           |  |  | 72 |             if (!empty($sectionpreferences->indexcollapsed)) {
 | 
        
           |  |  | 73 |                 $indexcollapsed = true;
 | 
        
           |  |  | 74 |             }
 | 
        
           |  |  | 75 |         }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         $data = (object)[
 | 
        
           |  |  | 78 |             'id' => $section->id,
 | 
        
           |  |  | 79 |             'section' => $section->section,
 | 
        
           |  |  | 80 |             'number' => $section->section,
 | 
        
           |  |  | 81 |             'title' => $format->get_section_name($section),
 | 
        
           |  |  | 82 |             'hassummary' => !empty($section->summary),
 | 
        
           |  |  | 83 |             'rawtitle' => $section->name,
 | 
        
           |  |  | 84 |             'cmlist' => [],
 | 
        
           |  |  | 85 |             'visible' => !empty($section->visible),
 | 
        
           |  |  | 86 |             'sectionurl' => course_get_url($course, $section->section, ['navigation' => true])->out(),
 | 
        
           |  |  | 87 |             'current' => $format->is_section_current($section),
 | 
        
           |  |  | 88 |             'indexcollapsed' => $indexcollapsed,
 | 
        
           |  |  | 89 |             'contentcollapsed' => $contentcollapsed,
 | 
        
           |  |  | 90 |             'hasrestrictions' => $this->get_has_restrictions(),
 | 
        
           |  |  | 91 |             'bulkeditable' => $this->is_bulk_editable(),
 | 
        
           |  |  | 92 |             'component' => $section->component,
 | 
        
           |  |  | 93 |             'itemid' => $section->itemid,
 | 
        
           |  |  | 94 |         ];
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |         if (empty($modinfo->sections[$section->section])) {
 | 
        
           |  |  | 97 |             return $data;
 | 
        
           |  |  | 98 |         }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         foreach ($modinfo->sections[$section->section] as $modnumber) {
 | 
        
           |  |  | 101 |             $mod = $modinfo->cms[$modnumber];
 | 
        
           |  |  | 102 |             if ($section->uservisible && $mod->is_visible_on_course_page()) {
 | 
        
           |  |  | 103 |                 $data->cmlist[] = $mod->id;
 | 
        
           |  |  | 104 |             }
 | 
        
           |  |  | 105 |         }
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         return $data;
 | 
        
           |  |  | 108 |     }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |     /**
 | 
        
           |  |  | 111 |      * Return if the section can be selected for bulk editing.
 | 
        
           |  |  | 112 |      * @return bool if the section can be edited in bulk
 | 
        
           |  |  | 113 |      */
 | 
        
           |  |  | 114 |     protected function is_bulk_editable(): bool {
 | 
        
           |  |  | 115 |         $section = $this->section;
 | 
        
           |  |  | 116 |         return ($section->section != 0);
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /**
 | 
        
           |  |  | 120 |      * Return if the section has a restrictions icon displayed or not.
 | 
        
           |  |  | 121 |      *
 | 
        
           |  |  | 122 |      * @return bool if the section has visible restrictions for the user.
 | 
        
           |  |  | 123 |      */
 | 
        
           |  |  | 124 |     protected function get_has_restrictions(): bool {
 | 
        
           |  |  | 125 |         global $CFG;
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |         $section = $this->section;
 | 
        
           |  |  | 128 |         $course = $this->format->get_course();
 | 
        
           |  |  | 129 |         $context = context_course::instance($course->id);
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |         // Hidden sections have no restriction indicator displayed.
 | 
        
           |  |  | 132 |         if (empty($section->visible) || empty($CFG->enableavailability)) {
 | 
        
           |  |  | 133 |             return false;
 | 
        
           |  |  | 134 |         }
 | 
        
           |  |  | 135 |         // The activity is not visible to the user but it may have some availability information.
 | 
        
           |  |  | 136 |         if (!$section->uservisible) {
 | 
        
           |  |  | 137 |             return !empty($section->availableinfo);
 | 
        
           |  |  | 138 |         }
 | 
        
           |  |  | 139 |         // Course editors can see all restrictions if the section is visible.
 | 
        
           |  |  | 140 |         if (has_capability('moodle/course:viewhiddensections', $context)) {
 | 
        
           |  |  | 141 |             $ci = new info_section($section);
 | 
        
           |  |  | 142 |             return !empty($ci->get_full_information());
 | 
        
           |  |  | 143 |         }
 | 
        
           |  |  | 144 |         // Regular users can only see restrictions if apply to them.
 | 
        
           |  |  | 145 |         return false;
 | 
        
           |  |  | 146 |     }
 | 
        
           |  |  | 147 | }
 |