Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 32... Línea 32...
32
    define('MAX_MODINFO_CACHE_SIZE', 10);
32
    define('MAX_MODINFO_CACHE_SIZE', 10);
33
}
33
}
Línea 34... Línea 34...
34
 
34
 
35
use core_courseformat\output\activitybadge;
35
use core_courseformat\output\activitybadge;
-
 
36
use core_courseformat\sectiondelegate;
Línea 36... Línea 37...
36
use core_courseformat\sectiondelegate;
37
use core_courseformat\sectiondelegatemodule;
37
 
38
 
38
/**
39
/**
39
 * Information about a course that is cached in the course table 'modinfo' field (and then in
40
 * Information about a course that is cached in the course table 'modinfo' field (and then in
Línea 92... Línea 93...
92
     * @var array
93
     * @var array
93
     */
94
     */
94
    private $delegatedsections;
95
    private $delegatedsections;
Línea 95... Línea 96...
95
 
96
 
-
 
97
    /**
-
 
98
     * Index of sections delegated by course modules, indexed by course module instance.
-
 
99
     * @var null|section_info[]
-
 
100
     */
-
 
101
    private ?array $delegatedbycm = null;
-
 
102
 
-
 
103
    /**
-
 
104
     * Contains the course content weights so they can be sorted accordingly.
-
 
105
     *
-
 
106
     * @var array|null
-
 
107
     */
-
 
108
    private ?array $weights = null;
-
 
109
 
96
    /**
110
    /**
97
     * User ID
111
     * User ID
98
     * @var int
112
     * @var int
99
     */
113
     */
Línea 137... Línea 151...
137
        'userid' => 'get_user_id',
151
        'userid' => 'get_user_id',
138
        'sections' => 'get_sections',
152
        'sections' => 'get_sections',
139
        'cms' => 'get_cms',
153
        'cms' => 'get_cms',
140
        'instances' => 'get_instances',
154
        'instances' => 'get_instances',
141
        'groups' => 'get_groups_all',
155
        'groups' => 'get_groups_all',
-
 
156
        'delegatedbycm' => 'get_sections_delegated_by_cm',
142
    );
157
    );
Línea 143... Línea 158...
143
 
158
 
144
    /**
159
    /**
145
     * Magic method getter
160
     * Magic method getter
Línea 288... Línea 303...
288
        }
303
        }
289
        return $this->instances[$modname];
304
        return $this->instances[$modname];
290
    }
305
    }
Línea 291... Línea 306...
291
 
306
 
-
 
307
    /**
-
 
308
     * Obtains a single instance of a particular module on this course.
-
 
309
     *
-
 
310
     * @param string $modname Name of module (not full frankenstyle) e.g. 'label'
-
 
311
     * @param int $instanceid Instance id
-
 
312
     * @param int $strictness Use IGNORE_MISSING to return null if not found, or MUST_EXIST to throw exception
-
 
313
     * @return cm_info|null cm_info for the instance on this course or null if not found
-
 
314
     * @throws moodle_exception If the instance is not found
-
 
315
     */
-
 
316
    public function get_instance_of(string $modname, int $instanceid, int $strictness = IGNORE_MISSING): ?cm_info {
-
 
317
        if (empty($this->instances[$modname]) || empty($this->instances[$modname][$instanceid])) {
-
 
318
            if ($strictness === IGNORE_MISSING) {
-
 
319
                return null;
-
 
320
            }
-
 
321
            throw new moodle_exception('invalidmoduleid', 'error', '', $instanceid);
-
 
322
        }
-
 
323
        return $this->instances[$modname][$instanceid];
-
 
324
    }
-
 
325
 
-
 
326
    /**
-
 
327
     * Sorts the given array of course modules according to the order they appear on the course page.
-
 
328
     *
-
 
329
     * @param cm_info[] $cms Array of cm_info objects to sort by reference
-
 
330
     * @return void
-
 
331
     */
-
 
332
    public function sort_cm_array(array &$cms): void {
-
 
333
        $weights = $this->get_content_weights();
-
 
334
        uasort($cms, function ($a, $b) use ($weights) {
-
 
335
            $weighta = $weights['cm' . $a->id] ?? PHP_INT_MAX;
-
 
336
            $weightb = $weights['cm' . $b->id] ?? PHP_INT_MAX;
-
 
337
            return $weighta <=> $weightb;
-
 
338
        });
-
 
339
    }
-
 
340
 
292
    /**
341
    /**
293
     * Groups that the current user belongs to organised by grouping id. Calculated on the first request.
342
     * Groups that the current user belongs to organised by grouping id. Calculated on the first request.
294
     * @return int[][] array of grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'
343
     * @return int[][] array of grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'
295
     */
344
     */
296
    private function get_groups_all() {
345
    private function get_groups_all() {
Línea 337... Línea 386...
337
        if (empty($this->delegatedsections)) {
386
        if (empty($this->delegatedsections)) {
338
            return $this->sectioninfobynum;
387
            return $this->sectioninfobynum;
339
        }
388
        }
340
        $sections = [];
389
        $sections = [];
341
        foreach ($this->sectioninfobynum as $section) {
390
        foreach ($this->sectioninfobynum as $section) {
342
            if (!$section->is_delegated()) {
391
            if (!$section->get_component_instance()) {
343
                $sections[$section->section] = $section;
392
                $sections[$section->section] = $section;
344
            }
393
            }
345
        }
394
        }
346
        return $sections;
395
        return $sections;
347
    }
396
    }
Línea 409... Línea 458...
409
    public function has_delegated_sections(): bool {
458
    public function has_delegated_sections(): bool {
410
        return !empty($this->delegatedsections);
459
        return !empty($this->delegatedsections);
411
    }
460
    }
Línea 412... Línea 461...
412
 
461
 
-
 
462
    /**
-
 
463
     * Gets data about section delegated by course modules.
-
 
464
     *
-
 
465
     * @return section_info[] sections array indexed by course module ID
-
 
466
     */
-
 
467
    public function get_sections_delegated_by_cm(): array {
-
 
468
        if (!is_null($this->delegatedbycm)) {
-
 
469
            return $this->delegatedbycm;
-
 
470
        }
-
 
471
        $this->delegatedbycm = [];
-
 
472
        foreach ($this->delegatedsections as $componentsections) {
-
 
473
            foreach ($componentsections as $section) {
-
 
474
                $delegateinstance = $section->get_component_instance();
-
 
475
                // We only return sections delegated by course modules. Sections delegated to other
-
 
476
                // types of components must implement their own methods to get the section.
-
 
477
                if (!$delegateinstance || !($delegateinstance instanceof sectiondelegatemodule)) {
-
 
478
                    continue;
-
 
479
                }
-
 
480
                if (!$cm = $delegateinstance->get_cm()) {
-
 
481
                    continue;
-
 
482
                }
-
 
483
                $this->delegatedbycm[$cm->id] = $section;
-
 
484
            }
-
 
485
        }
-
 
486
        return $this->delegatedbycm;
-
 
487
    }
-
 
488
 
413
    /**
489
    /**
414
     * Static cache for generated course_modinfo instances
490
     * Static cache for generated course_modinfo instances
415
     *
491
     *
416
     * @see course_modinfo::instance()
492
     * @see course_modinfo::instance()
417
     * @see course_modinfo::clear_instance_cache()
493
     * @see course_modinfo::clear_instance_cache()
Línea 657... Línea 733...
657
        }
733
        }
658
        ksort($this->sectioninfobynum);
734
        ksort($this->sectioninfobynum);
659
    }
735
    }
Línea 660... Línea 736...
660
 
736
 
661
    /**
-
 
662
     * This method can not be used anymore.
-
 
663
     *
-
 
664
     * @see course_modinfo::build_course_cache()
-
 
665
     * @deprecated since 2.6
-
 
666
     */
-
 
667
    public static function build_section_cache($courseid) {
-
 
668
        throw new coding_exception('Function course_modinfo::build_section_cache() can not be used anymore.' .
-
 
669
            ' Please use course_modinfo::build_course_cache() whenever applicable.');
-
 
670
    }
-
 
671
 
-
 
672
    /**
737
    /**
673
     * Builds a list of information about sections on a course to be stored in
738
     * Builds a list of information about sections on a course to be stored in
674
     * the course cache. (Does not include information that is already cached
739
     * the course cache. (Does not include information that is already cached
675
     * in some other way.)
740
     * in some other way.)
676
     *
741
     *
Línea 1112... Línea 1177...
1112
    public static function purge_course_cache(int $courseid): void {
1177
    public static function purge_course_cache(int $courseid): void {
1113
        increment_revision_number('course', 'cacherev', 'id = :id', array('id' => $courseid));
1178
        increment_revision_number('course', 'cacherev', 'id = :id', array('id' => $courseid));
1114
        // Because this is a versioned cache, there is no need to actually delete the cache item,
1179
        // Because this is a versioned cache, there is no need to actually delete the cache item,
1115
        // only increase the required version number.
1180
        // only increase the required version number.
1116
    }
1181
    }
-
 
1182
 
-
 
1183
    /**
-
 
1184
     * Can this module type be displayed on a course page or selected from the activity types when adding an activity to a course?
-
 
1185
     *
-
 
1186
     * @param string $modname The module type name
-
 
1187
     * @return bool
-
 
1188
     */
-
 
1189
    public static function is_mod_type_visible_on_course(string $modname): bool {
-
 
1190
        return plugin_supports('mod', $modname, FEATURE_CAN_DISPLAY, true);
-
 
1191
    }
-
 
1192
 
-
 
1193
    /**
-
 
1194
     * Get content weights for all sections and modules in the course.
-
 
1195
     *
-
 
1196
     * The weights are calculated based on the order of sections and modules
-
 
1197
     * as they appear on the course page, including delegated sections.
-
 
1198
     *
-
 
1199
     * @return array Associative array with keys 'section{sectionid}' and 'cm{cmid}' and integer weights as values.
-
 
1200
     */
-
 
1201
    private function get_content_weights(): array {
-
 
1202
        if ($this->weights !== null) {
-
 
1203
            return $this->weights;
-
 
1204
        }
-
 
1205
        $result = [];
-
 
1206
        foreach ($this->sectioninfobynum as $section) {
-
 
1207
            // Delegated sections are always at the end of the course and they will
-
 
1208
            // be added only if they are part of any section sequence.
-
 
1209
            if ($section->is_delegated()) {
-
 
1210
                continue;
-
 
1211
            }
-
 
1212
            $sortedelements = $this->calculate_section_weights($section, count($result));
-
 
1213
            $result += $sortedelements;
-
 
1214
        }
-
 
1215
        $this->weights = $result;
-
 
1216
        return $result;
-
 
1217
    }
-
 
1218
 
-
 
1219
    /**
-
 
1220
     * Calculate weights for a section and its modules, including delegated sections.
-
 
1221
     *
-
 
1222
     * @param section_info $section The section to calculate weights for.
-
 
1223
     * @param int $currentweight The starting weight to use for this section.
-
 
1224
     * @return section_info[] Associative array of section_info objects, indexed by the cmid of the delegating module.
-
 
1225
     */
-
 
1226
    private function calculate_section_weights(section_info $section, int $currentweight = 0): array {
-
 
1227
        $delegatedcms = $this->get_sections_delegated_by_cm();
-
 
1228
 
-
 
1229
        $weights = [
-
 
1230
            'section' . $section->id => $currentweight++,
-
 
1231
        ];
-
 
1232
 
-
 
1233
        foreach ($section->get_sequence_cm_infos() as $cm) {
-
 
1234
            $weights['cm' . $cm->id] = $currentweight++;
-
 
1235
 
-
 
1236
            if (array_key_exists($cm->id, $delegatedcms)) {
-
 
1237
                $subweights = $this->calculate_section_weights($delegatedcms[$cm->id], $currentweight);
-
 
1238
                $weights += $subweights;
-
 
1239
                $currentweight += count($subweights);
-
 
1240
            }
-
 
1241
        }
-
 
1242
        return $weights;
-
 
1243
    }
1117
}
1244
}
Línea 1118... Línea 1245...
1118
 
1245
 
1119
 
1246
 
Línea 1435... Línea 1562...
1435
     * @var string
1562
     * @var string
1436
     */
1563
     */
1437
    private $iconcomponent;
1564
    private $iconcomponent;
Línea 1438... Línea 1565...
1438
 
1565
 
-
 
1566
    /**
-
 
1567
     * The instance record form the module table
-
 
1568
     * @var stdClass
-
 
1569
     */
-
 
1570
    private $instancerecord;
-
 
1571
 
1439
    /**
1572
    /**
1440
     * Name of module e.g. 'forum' (this is the same name as the module's main database
1573
     * Name of module e.g. 'forum' (this is the same name as the module's main database
1441
     * table) - from cached data in modinfo field
1574
     * table) - from cached data in modinfo field
1442
     * @var string
1575
     * @var string
1443
     */
1576
     */
Línea 2169... Línea 2302...
2169
        }
2302
        }
Línea 2170... Línea 2303...
2170
 
2303
 
2171
        return $cmrecord;
2304
        return $cmrecord;
Línea -... Línea 2305...
-
 
2305
    }
-
 
2306
 
-
 
2307
    /**
-
 
2308
     * Return the activity database table record.
-
 
2309
     *
-
 
2310
     * The instance record will be cached after the first call.
-
 
2311
     *
-
 
2312
     * @return stdClass
-
 
2313
     */
-
 
2314
    public function get_instance_record() {
-
 
2315
        global $DB;
-
 
2316
        if (!isset($this->instancerecord)) {
-
 
2317
            $this->instancerecord = $DB->get_record(
-
 
2318
                table: $this->modname,
-
 
2319
                conditions: ['id' => $this->instance],
-
 
2320
                strictness: MUST_EXIST,
-
 
2321
            );
-
 
2322
        }
-
 
2323
        return $this->instancerecord;
-
 
2324
    }
-
 
2325
 
-
 
2326
    /**
-
 
2327
     * Returns the section delegated by this module, if any.
-
 
2328
     *
-
 
2329
     * @return ?section_info
-
 
2330
     */
-
 
2331
    public function get_delegated_section_info(): ?section_info {
-
 
2332
        $delegatedsections = $this->modinfo->get_sections_delegated_by_cm();
-
 
2333
        if (!array_key_exists($this->id, $delegatedsections)) {
-
 
2334
            return null;
-
 
2335
        }
-
 
2336
        return $delegatedsections[$this->id];
2172
    }
2337
    }
2173
 
2338
 
Línea 2174... Línea 2339...
2174
    // Set functions
2339
    // Set functions
2175
    ////////////////
2340
    ////////////////
Línea 2518... Línea 2683...
2518
        $this->obtain_dynamic_data();
2683
        $this->obtain_dynamic_data();
2519
        return $this->uservisibleoncoursepage;
2684
        return $this->uservisibleoncoursepage;
2520
    }
2685
    }
Línea 2521... Línea 2686...
2521
 
2686
 
-
 
2687
    /**
-
 
2688
     * Use this method if you want to check if the plugin overrides any visibility checks to block rendering to the display.
-
 
2689
     *
-
 
2690
     * @return bool
-
 
2691
     */
-
 
2692
    public function is_of_type_that_can_display(): bool {
-
 
2693
        return course_modinfo::is_mod_type_visible_on_course($this->modname);
-
 
2694
    }
-
 
2695
 
2522
    /**
2696
    /**
2523
     * Whether this module is available but hidden from course page
2697
     * Whether this module is available but hidden from course page
2524
     *
2698
     *
2525
     * "Stealth" modules are the ones that are not shown on course page but available by following url.
2699
     * "Stealth" modules are the ones that are not shown on course page but available by following url.
2526
     * They are normally also displayed in grade reports and other reports.
2700
     * They are normally also displayed in grade reports and other reports.
Línea 2542... Línea 2716...
2542
        $this->obtain_dynamic_data();
2716
        $this->obtain_dynamic_data();
2543
        return $this->available;
2717
        return $this->available;
2544
    }
2718
    }
Línea 2545... Línea 2719...
2545
 
2719
 
2546
    /**
-
 
2547
     * This method can not be used anymore.
-
 
2548
     *
-
 
2549
     * @see \core_availability\info_module::filter_user_list()
-
 
2550
     * @deprecated Since Moodle 2.8
-
 
2551
     */
-
 
2552
    private function get_deprecated_group_members_only() {
-
 
2553
        throw new coding_exception('$cm->groupmembersonly can not be used anymore. ' .
-
 
2554
                'If used to restrict a list of enrolled users to only those who can ' .
-
 
2555
                'access the module, consider \core_availability\info_module::filter_user_list.');
-
 
2556
    }
-
 
2557
 
-
 
2558
    /**
2720
    /**
2559
     * Getter method for property $availableinfo, ensures that dynamic data is retrieved
2721
     * Getter method for property $availableinfo, ensures that dynamic data is retrieved
2560
     *
2722
     *
2561
     * @return string Available info (HTML)
2723
     * @return string Available info (HTML)
2562
     */
2724
     */
Línea 2600... Línea 2762...
2600
             $this->uservisible = false;
2762
             $this->uservisible = false;
2601
            // Ensure activity is completely hidden from the user.
2763
            // Ensure activity is completely hidden from the user.
2602
            $this->availableinfo = '';
2764
            $this->availableinfo = '';
2603
        }
2765
        }
Línea -... Línea 2766...
-
 
2766
 
-
 
2767
        $capabilities = [
-
 
2768
            'moodle/course:manageactivities',
-
 
2769
            'moodle/course:activityvisibility',
-
 
2770
            'moodle/course:viewhiddenactivities',
2604
 
2771
        ];
2605
        $this->uservisibleoncoursepage = $this->uservisible &&
-
 
2606
            ($this->visibleoncoursepage ||
-
 
2607
                has_capability('moodle/course:manageactivities', $this->get_context(), $userid) ||
2772
        $this->uservisibleoncoursepage = $this->uservisible &&
2608
                has_capability('moodle/course:activityvisibility', $this->get_context(), $userid));
2773
            ($this->visibleoncoursepage || has_any_capability($capabilities, $this->get_context(), $userid));
2609
        // Activity that is not available, not hidden from course page and has availability
2774
        // Activity that is not available, not hidden from course page and has availability
2610
        // info is actually visible on the course page (with availability info and without a link).
2775
        // info is actually visible on the course page (with availability info and without a link).
2611
        if (!$this->uservisible && $this->visibleoncoursepage && $this->availableinfo) {
2776
        if (!$this->uservisible && $this->visibleoncoursepage && $this->availableinfo) {
2612
            $this->uservisibleoncoursepage = true;
2777
            $this->uservisibleoncoursepage = true;
2613
        }
2778
        }
Línea 2614... Línea 2779...
2614
    }
2779
    }
2615
 
-
 
2616
    /**
-
 
2617
     * This method has been deprecated and should not be used.
-
 
2618
     *
-
 
2619
     * @see $uservisible
-
 
2620
     * @deprecated Since Moodle 2.8
-
 
2621
     */
-
 
2622
    public function is_user_access_restricted_by_group() {
-
 
2623
        throw new coding_exception('cm_info::is_user_access_restricted_by_group() can not be used any more.' .
-
 
2624
            ' Use $cm->uservisible to decide whether the current user can access an activity.');
-
 
2625
    }
-
 
2626
 
2780
 
2627
    /**
2781
    /**
2628
     * Checks whether mod/...:view capability restricts the current user's access.
2782
     * Checks whether mod/...:view capability restricts the current user's access.
2629
     *
2783
     *
2630
     * @return bool True if the user access is restricted.
2784
     * @return bool True if the user access is restricted.
Línea 2644... Línea 2798...
2644
        // You are blocked if you don't have the capability.
2798
        // You are blocked if you don't have the capability.
2645
        return !has_capability($capability, $this->get_context(), $userid);
2799
        return !has_capability($capability, $this->get_context(), $userid);
2646
    }
2800
    }
Línea 2647... Línea 2801...
2647
 
2801
 
2648
    /**
-
 
2649
     * Checks whether the module's conditional access settings mean that the
-
 
2650
     * user cannot see the activity at all
-
 
2651
     *
-
 
2652
     * @deprecated since 2.7 MDL-44070
-
 
2653
     */
-
 
2654
    public function is_user_access_restricted_by_conditional_access() {
-
 
2655
        throw new coding_exception('cm_info::is_user_access_restricted_by_conditional_access() ' .
-
 
2656
                'can not be used any more; this function is not needed (use $cm->uservisible ' .
-
 
2657
                'and $cm->availableinfo to decide whether it should be available ' .
-
 
2658
                'or appear)');
-
 
2659
    }
-
 
2660
 
-
 
2661
    /**
2802
    /**
2662
     * Calls a module function (if exists), passing in one parameter: this object.
2803
     * Calls a module function (if exists), passing in one parameter: this object.
2663
     * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is
2804
     * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is
2664
     *   'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb'
2805
     *   'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb'
2665
     * @return void
2806
     * @return void
Línea 2901... Línea 3042...
2901
        }
3042
        }
2902
    }
3043
    }
Línea 2903... Línea 3044...
2903
 
3044
 
2904
    // Get cm from get_fast_modinfo.
3045
    // Get cm from get_fast_modinfo.
2905
    $modinfo = get_fast_modinfo($course, $userid);
3046
    $modinfo = get_fast_modinfo($course, $userid);
2906
    $instances = $modinfo->get_instances_of($modulename);
-
 
2907
    if (!array_key_exists($instanceid, $instances)) {
-
 
2908
        throw new moodle_exception('invalidmoduleid', 'error', '', $instanceid);
-
 
2909
    }
3047
    $instance = $modinfo->get_instance_of($modulename, $instanceid, MUST_EXIST);
2910
    return array($course, $instances[$instanceid]);
3048
    return [$course, $instance];
Línea 2911... Línea 3049...
2911
}
3049
}
2912
 
3050
 
Línea 3173... Línea 3311...
3173
    /**
3311
    /**
3174
     * @var sectiondelegate|null Section delegate instance if any.
3312
     * @var sectiondelegate|null Section delegate instance if any.
3175
     */
3313
     */
3176
    private ?sectiondelegate $_delegateinstance = null;
3314
    private ?sectiondelegate $_delegateinstance = null;
Línea -... Línea 3315...
-
 
3315
 
-
 
3316
    /** @var cm_info[]|null Section cm_info activities, null when it is not loaded yet. */
-
 
3317
    private array|null $_sequencecminfos = null;
-
 
3318
 
-
 
3319
    /**
-
 
3320
     * @var bool|null $_isorphan True if the section is orphan for some reason.
-
 
3321
     */
-
 
3322
    private $_isorphan = null;
3177
 
3323
 
3178
    /**
3324
    /**
3179
     * Availability conditions for this section based on the completion of
3325
     * Availability conditions for this section based on the completion of
3180
     * course-modules (array from course-module id to required completion state
3326
     * course-modules (array from course-module id to required completion state
3181
     * for that module) - from cached data in sectioncache field
3327
     * for that module) - from cached data in sectioncache field
Línea 3419... Línea 3565...
3419
            // Get availability information.
3565
            // Get availability information.
3420
            $ci = new \core_availability\info_section($this);
3566
            $ci = new \core_availability\info_section($this);
3421
            $this->_available = $ci->is_available($this->_availableinfo, true,
3567
            $this->_available = $ci->is_available($this->_availableinfo, true,
3422
                    $userid, $this->modinfo);
3568
                    $userid, $this->modinfo);
3423
        }
3569
        }
-
 
3570
 
-
 
3571
        if ($this->_available) {
-
 
3572
            $this->_available = $this->check_delegated_available();
-
 
3573
        }
3424
        // Execute the hook from the course format that may override the available/availableinfo properties.
3574
        // Execute the hook from the course format that may override the available/availableinfo properties.
3425
        $currentavailable = $this->_available;
3575
        $currentavailable = $this->_available;
3426
        course_get_format($this->modinfo->get_course())->
3576
        course_get_format($this->modinfo->get_course())->
3427
            section_get_available_hook($this, $this->_available, $this->_availableinfo);
3577
            section_get_available_hook($this, $this->_available, $this->_availableinfo);
3428
        if (!$currentavailable && $this->_available) {
3578
        if (!$currentavailable && $this->_available) {
Línea 3431... Línea 3581...
3431
        }
3581
        }
3432
        return $this->_available;
3582
        return $this->_available;
3433
    }
3583
    }
Línea 3434... Línea 3584...
3434
 
3584
 
-
 
3585
    /**
-
 
3586
     * Check if the delegated component is available.
-
 
3587
     *
-
 
3588
     * @return bool
-
 
3589
     */
-
 
3590
    private function check_delegated_available(): bool {
-
 
3591
        /** @var sectiondelegatemodule $sectiondelegate */
-
 
3592
        $sectiondelegate = $this->get_component_instance();
-
 
3593
        if (!$sectiondelegate) {
-
 
3594
            return true;
-
 
3595
        }
-
 
3596
 
-
 
3597
        if ($sectiondelegate instanceof sectiondelegatemodule) {
-
 
3598
            $parentcm = $sectiondelegate->get_cm();
-
 
3599
            if (!$parentcm->available) {
-
 
3600
                return false;
-
 
3601
            }
-
 
3602
            return $parentcm->get_section_info()->available;
-
 
3603
        }
-
 
3604
 
-
 
3605
        return true;
-
 
3606
    }
-
 
3607
 
3435
    /**
3608
    /**
3436
     * Returns the availability text shown next to the section on course page.
3609
     * Returns the availability text shown next to the section on course page.
3437
     *
3610
     *
3438
     * @return string
3611
     * @return string
3439
     */
3612
     */
Línea 3477... Línea 3650...
3477
        $userid = $this->modinfo->get_user_id();
3650
        $userid = $this->modinfo->get_user_id();
3478
        if ($this->_uservisible !== null || $userid == -1) {
3651
        if ($this->_uservisible !== null || $userid == -1) {
3479
            // Has already been calculated or does not need calculation.
3652
            // Has already been calculated or does not need calculation.
3480
            return $this->_uservisible;
3653
            return $this->_uservisible;
3481
        }
3654
        }
-
 
3655
 
-
 
3656
        if (!$this->check_delegated_uservisible()) {
-
 
3657
            $this->_uservisible = false;
-
 
3658
            return $this->_uservisible;
-
 
3659
        }
-
 
3660
 
3482
        $this->_uservisible = true;
3661
        $this->_uservisible = true;
3483
        if (!$this->_visible || !$this->get_available()) {
3662
        if ($this->is_orphan() || !$this->_visible || !$this->get_available()) {
3484
            $coursecontext = context_course::instance($this->get_course());
3663
            $coursecontext = context_course::instance($this->get_course());
-
 
3664
            if (
-
 
3665
                ($this->_isorphan || !$this->_visible)
3485
            if (!$this->_visible && !has_capability('moodle/course:viewhiddensections', $coursecontext, $userid) ||
3666
                && !has_capability('moodle/course:viewhiddensections', $coursecontext, $userid)
-
 
3667
            ) {
-
 
3668
                $this->_uservisible = false;
-
 
3669
            }
-
 
3670
            if (
-
 
3671
                $this->_uservisible
3486
                    (!$this->get_available() &&
3672
                && !$this->get_available()
3487
                    !has_capability('moodle/course:ignoreavailabilityrestrictions', $coursecontext, $userid))) {
3673
                && !has_capability('moodle/course:ignoreavailabilityrestrictions', $coursecontext, $userid)
3488
 
3674
            ) {
3489
                $this->_uservisible = false;
3675
                $this->_uservisible = false;
3490
            }
3676
            }
3491
        }
3677
        }
3492
        return $this->_uservisible;
3678
        return $this->_uservisible;
3493
    }
3679
    }
Línea 3494... Línea 3680...
3494
 
3680
 
-
 
3681
    /**
-
 
3682
     * Check if the delegated component is user visible.
-
 
3683
     *
-
 
3684
     * @return bool
-
 
3685
     */
-
 
3686
    private function check_delegated_uservisible(): bool {
-
 
3687
        /** @var sectiondelegatemodule $sectiondelegate */
-
 
3688
        $sectiondelegate = $this->get_component_instance();
-
 
3689
        if (!$sectiondelegate) {
-
 
3690
            return true;
-
 
3691
        }
-
 
3692
 
-
 
3693
        if ($sectiondelegate instanceof sectiondelegatemodule) {
-
 
3694
            $parentcm = $sectiondelegate->get_cm();
-
 
3695
            if (!$parentcm->uservisible) {
-
 
3696
                return false;
-
 
3697
            }
-
 
3698
            $result = $parentcm->get_section_info()->uservisible;
-
 
3699
            return $result;
-
 
3700
        }
-
 
3701
 
-
 
3702
        return true;
-
 
3703
    }
-
 
3704
 
3495
    /**
3705
    /**
3496
     * Restores the course_sections.sequence value
3706
     * Restores the course_sections.sequence value
3497
     *
3707
     *
3498
     * @return string
3708
     * @return string
3499
     */
3709
     */
Línea 3504... Línea 3714...
3504
            return '';
3714
            return '';
3505
        }
3715
        }
3506
    }
3716
    }
Línea 3507... Línea 3717...
3507
 
3717
 
-
 
3718
    /**
-
 
3719
     * Returns the course modules in this section.
-
 
3720
     *
-
 
3721
     * @return cm_info[]
-
 
3722
     */
-
 
3723
    public function get_sequence_cm_infos(): array {
-
 
3724
        if ($this->_sequencecminfos !== null) {
-
 
3725
            return $this->_sequencecminfos;
-
 
3726
        }
-
 
3727
        $sequence = $this->modinfo->sections[$this->_sectionnum] ?? [];
-
 
3728
        $cms = $this->modinfo->get_cms();
-
 
3729
        $result = [];
-
 
3730
        foreach ($sequence as $cmid) {
-
 
3731
            if (isset($cms[$cmid])) {
-
 
3732
                $result[] = $cms[$cmid];
-
 
3733
            }
-
 
3734
        }
-
 
3735
        $this->_sequencecminfos = $result;
-
 
3736
        return $result;
-
 
3737
    }
-
 
3738
 
3508
    /**
3739
    /**
3509
     * Returns course ID - from course_sections table
3740
     * Returns course ID - from course_sections table
3510
     *
3741
     *
3511
     * @return int
3742
     * @return int
3512
     */
3743
     */
Línea 3536... Línea 3767...
3536
 
3767
 
3537
    /**
3768
    /**
3538
     * Get the delegate component instance.
3769
     * Get the delegate component instance.
3539
     */
3770
     */
3540
    public function get_component_instance(): ?sectiondelegate {
3771
    public function get_component_instance(): ?sectiondelegate {
3541
        if (empty($this->_component)) {
3772
        if (!$this->is_delegated()) {
3542
            return null;
3773
            return null;
3543
        }
3774
        }
3544
        if ($this->_delegateinstance !== null) {
3775
        if ($this->_delegateinstance !== null) {
3545
            return $this->_delegateinstance;
3776
            return $this->_delegateinstance;
Línea 3555... Línea 3786...
3555
    public function is_delegated(): bool {
3786
    public function is_delegated(): bool {
3556
        return !empty($this->_component);
3787
        return !empty($this->_component);
3557
    }
3788
    }
Línea 3558... Línea 3789...
3558
 
3789
 
-
 
3790
    /**
-
 
3791
     * Returns true if this section is orphan.
-
 
3792
     *
-
 
3793
     * @return bool
-
 
3794
     */
-
 
3795
    public function is_orphan(): bool {
-
 
3796
        if ($this->_isorphan !== null) {
-
 
3797
            return $this->_isorphan;
-
 
3798
        }
-
 
3799
 
-
 
3800
        $courseformat = course_get_format($this->modinfo->get_course());
-
 
3801
        // There are some cases where a restored course using third-party formats can
-
 
3802
        // have orphaned sections due to a fixed section number.
-
 
3803
        if ($this->_sectionnum > $courseformat->get_last_section_number()) {
-
 
3804
            $this->_isorphan = true;
-
 
3805
            return $this->_isorphan;
-
 
3806
        }
-
 
3807
        // Some delegated sections can belong to a plugin that is disabled or not present.
-
 
3808
        if ($this->is_delegated() && !$this->get_component_instance()) {
-
 
3809
            $this->_isorphan = true;
-
 
3810
            return $this->_isorphan;
-
 
3811
        }
-
 
3812
 
-
 
3813
        $this->_isorphan = false;
-
 
3814
        return $this->_isorphan;
-
 
3815
    }
-
 
3816
 
3559
    /**
3817
    /**
3560
     * Prepares section data for inclusion in sectioncache cache, removing items
3818
     * Prepares section data for inclusion in sectioncache cache, removing items
3561
     * that are set to defaults, and adding availability data if required.
3819
     * that are set to defaults, and adding availability data if required.
3562
     *
3820
     *
3563
     * Called by build_section_cache in course_modinfo only; do not use otherwise.
3821
     * Called by build_section_cache in course_modinfo only; do not use otherwise.