Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 46... Línea 46...
46
     * @param stateupdates $updates the affected course elements track
46
     * @param stateupdates $updates the affected course elements track
47
     * @param stdClass $course the course object
47
     * @param stdClass $course the course object
48
     * @param int[] $ids the list of affected course module ids
48
     * @param int[] $ids the list of affected course module ids
49
     * @param int $targetsectionid optional target section id
49
     * @param int $targetsectionid optional target section id
50
     * @param int $targetcmid optional target cm id
50
     * @param int $targetcmid optional target cm id
-
 
51
     * @throws moodle_exception
51
     */
52
     */
52
    public function cm_move(
53
    public function cm_move(
53
        stateupdates $updates,
54
        stateupdates $updates,
54
        stdClass $course,
55
        stdClass $course,
55
        array $ids,
56
        array $ids,
Línea 83... Línea 84...
83
            // An updated $modinfo is needed on every loop as activities list change.
84
            // An updated $modinfo is needed on every loop as activities list change.
84
            $modinfo = get_fast_modinfo($course);
85
            $modinfo = get_fast_modinfo($course);
85
            $cm = $modinfo->get_cm($cmid);
86
            $cm = $modinfo->get_cm($cmid);
86
            $currentsectionid = $cm->section;
87
            $currentsectionid = $cm->section;
87
            $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST);
88
            $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST);
-
 
89
            if ($targetsection->is_delegated() && $cm->get_delegated_section_info()) {
-
 
90
                throw new moodle_exception('subsectionmoveerror', 'core');
-
 
91
            }
88
            $beforecm = (!empty($beforecmdid)) ? $modinfo->get_cm($beforecmdid) : null;
92
            $beforecm = (!empty($beforecmdid)) ? $modinfo->get_cm($beforecmdid) : null;
89
            if ($beforecm === null || $beforecm->id != $cmid) {
93
            if ($beforecm === null || $beforecm->id != $cmid) {
90
                moveto_module($cm, $targetsection, $beforecm);
94
                moveto_module($cm, $targetsection, $beforecm);
91
            }
95
            }
92
            $beforecmdid = $cm->id;
96
            $beforecmdid = $cm->id;
Línea 136... Línea 140...
136
        usort($cmids, $sortfunction);
140
        usort($cmids, $sortfunction);
137
        return $cmids;
141
        return $cmids;
138
    }
142
    }
Línea 139... Línea 143...
139
 
143
 
140
    /**
-
 
141
     * Move course sections to another location in the same course.
-
 
142
     *
144
    /**
143
     * @deprecated since Moodle 4.4 MDL-77038.
-
 
144
     * @todo MDL-80116 This will be deleted in Moodle 4.8.
-
 
145
     * @param stateupdates $updates the affected course elements track
-
 
146
     * @param stdClass $course the course object
-
 
147
     * @param int[] $ids the list of affected course module ids
-
 
148
     * @param int $targetsectionid optional target section id
-
 
149
     * @param int $targetcmid optional target cm id
145
     * @deprecated since Moodle 4.4 MDL-77038.
-
 
146
     */
-
 
147
    #[\core\attribute\deprecated(
-
 
148
        replacement: 'stateactions::section_move_after',
-
 
149
        since: '4.4',
-
 
150
        mdl: 'MDL-77038',
-
 
151
        final: true,
150
     */
152
    )]
151
    public function section_move(
153
    public function section_move(
152
        stateupdates $updates,
154
        stateupdates $updates,
153
        stdClass $course,
155
        stdClass $course,
154
        array $ids,
156
        array $ids,
155
        ?int $targetsectionid = null,
157
        ?int $targetsectionid = null,
156
        ?int $targetcmid = null
158
        ?int $targetcmid = null
157
    ): void {
-
 
158
        debugging(
-
 
159
            'The method stateactions::section_move() has been deprecated, please use stateactions::section_move_after() instead.',
-
 
160
            DEBUG_DEVELOPER
-
 
161
        );
-
 
162
        // Validate target elements.
-
 
163
        if (!$targetsectionid) {
-
 
164
            throw new moodle_exception("Action cm_move requires targetsectionid");
-
 
165
        }
-
 
166
 
159
    ): void {
167
        $this->validate_sections($course, $ids, __FUNCTION__);
-
 
168
 
-
 
169
        $coursecontext = context_course::instance($course->id);
-
 
170
        require_capability('moodle/course:movesections', $coursecontext);
-
 
171
 
-
 
172
        $modinfo = get_fast_modinfo($course);
-
 
173
 
-
 
174
        // Target section.
-
 
175
        $this->validate_sections($course, [$targetsectionid], __FUNCTION__);
-
 
176
        $targetsection = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST);
-
 
177
 
-
 
178
        $affectedsections = [$targetsection->section => true];
-
 
179
 
-
 
180
        $sections = $this->get_section_info($modinfo, $ids);
-
 
181
        foreach ($sections as $section) {
-
 
182
            $affectedsections[$section->section] = true;
-
 
183
            move_section_to($course, $section->section, $targetsection->section);
-
 
184
        }
-
 
185
 
-
 
186
        // Use section_state to return the section and activities updated state.
-
 
187
        $this->section_state($updates, $course, $ids, $targetsectionid);
-
 
188
 
-
 
189
        // All course sections can be renamed because of the resort.
-
 
190
        $allsections = $modinfo->get_section_info_all();
-
 
191
        foreach ($allsections as $section) {
-
 
192
            // Ignore the affected sections because they are already in the updates.
-
 
193
            if (isset($affectedsections[$section->section])) {
-
 
194
                continue;
-
 
195
            }
-
 
196
            $updates->add_section_put($section->id);
-
 
197
        }
-
 
198
        // The section order is at a course level.
-
 
199
        $updates->add_course_put();
160
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
Línea 200... Línea 161...
200
    }
161
    }
201
 
162
 
202
    /**
163
    /**
Línea 310... Línea 271...
310
        $format = course_get_format($course->id);
271
        $format = course_get_format($course->id);
311
        $lastsectionnumber = $format->get_last_section_number();
272
        $lastsectionnumber = $format->get_last_section_number();
312
        $maxsections = $format->get_max_sections();
273
        $maxsections = $format->get_max_sections();
Línea 313... Línea 274...
313
 
274
 
314
        if ($lastsectionnumber >= $maxsections) {
275
        if ($lastsectionnumber >= $maxsections) {
315
            throw new moodle_exception('maxsectionslimit', 'moodle', $maxsections);
276
            throw new moodle_exception('maxsectionslimit', 'moodle', '', $maxsections);
Línea 316... Línea 277...
316
        }
277
        }
Línea 317... Línea 278...
317
 
278
 
Línea 360... Línea 321...
360
 
321
 
361
        foreach ($ids as $sectionid) {
322
        foreach ($ids as $sectionid) {
362
            // We need to get the latest modinfo on each iteration because the section numbers change.
323
            // We need to get the latest modinfo on each iteration because the section numbers change.
363
            $modinfo = get_fast_modinfo($course);
324
            $modinfo = get_fast_modinfo($course);
-
 
325
            $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST);
-
 
326
            if (!course_can_delete_section($course, $section)) {
-
 
327
                continue;
364
            $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST);
328
            }
365
            // Send all activity deletions.
329
            // Send all activity deletions.
366
            if (!empty($modinfo->sections[$section->section])) {
330
            if (!empty($modinfo->sections[$section->section])) {
367
                foreach ($modinfo->sections[$section->section] as $modnumber) {
331
                foreach ($modinfo->sections[$section->section] as $modnumber) {
368
                    $cm = $modinfo->cms[$modnumber];
332
                    $cm = $modinfo->cms[$modnumber];
Línea 519... Línea 483...
519
 
483
 
520
        $this->validate_cms(
484
        $this->validate_cms(
521
            $course,
485
            $course,
522
            $ids,
486
            $ids,
523
            __FUNCTION__,
487
            __FUNCTION__,
524
            ['moodle/course:manageactivities', 'moodle/course:activityvisibility']
488
            ['moodle/course:activityvisibility']
Línea 525... Línea 489...
525
        );
489
        );
526
 
490
 
Línea 540... Línea 504...
540
            course_module_updated::create_from_cm($cm, $modcontext)->trigger();
504
            course_module_updated::create_from_cm($cm, $modcontext)->trigger();
541
        }
505
        }
542
        course_modinfo::purge_course_modules_cache($course->id, $ids);
506
        course_modinfo::purge_course_modules_cache($course->id, $ids);
543
        rebuild_course_cache($course->id, false, true);
507
        rebuild_course_cache($course->id, false, true);
Línea -... Línea 508...
-
 
508
 
544
 
509
        $delegatedsections = [];
545
        foreach ($cms as $cm) {
510
        foreach ($cms as $cm) {
-
 
511
            $updates->add_cm_put($cm->id);
-
 
512
            if (!$delegatedsection = $cm->get_delegated_section_info()) {
-
 
513
                continue;
-
 
514
            }
-
 
515
            if (!in_array($delegatedsection->id, $delegatedsections)) {
-
 
516
                $delegatedsections[] = $delegatedsection->id;
-
 
517
            }
-
 
518
        }
-
 
519
        foreach ($delegatedsections as $sectionid => $section) {
546
            $updates->add_cm_put($cm->id);
520
            $updates->add_section_put($sectionid);
547
        }
521
        }
Línea 548... Línea 522...
548
    }
522
    }
549
 
523
 
Línea 565... Línea 539...
565
    ): void {
539
    ): void {
566
        $this->validate_cms(
540
        $this->validate_cms(
567
            $course,
541
            $course,
568
            $ids,
542
            $ids,
569
            __FUNCTION__,
543
            __FUNCTION__,
570
            ['moodle/course:manageactivities', 'moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport']
544
            ['moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport'],
-
 
545
            false
571
        );
546
        );
Línea 572... Línea 547...
572
 
547
 
573
        $modinfo = get_fast_modinfo($course);
548
        $modinfo = get_fast_modinfo($course);
Línea 703... Línea 678...
703
        global $DB;
678
        global $DB;
Línea 704... Línea 679...
704
 
679
 
705
        $this->validate_cms($course, $ids, __FUNCTION__, ['moodle/course:manageactivities']);
680
        $this->validate_cms($course, $ids, __FUNCTION__, ['moodle/course:manageactivities']);
706
        $modinfo = get_fast_modinfo($course);
681
        $modinfo = get_fast_modinfo($course);
-
 
682
        $cms = $this->get_cm_info($modinfo, $ids);
-
 
683
        $cms = $this->filter_cms_with_section_delegate($cms);
-
 
684
        if (empty($cms)) {
-
 
685
            return;
707
        $cms = $this->get_cm_info($modinfo, $ids);
686
        }
708
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cms), SQL_PARAMS_NAMED);
687
        list($insql, $inparams) = $DB->get_in_or_equal(array_keys($cms), SQL_PARAMS_NAMED);
709
        $DB->set_field_select('course_modules', 'indent', $indent, "id $insql", $inparams);
688
        $DB->set_field_select('course_modules', 'indent', $indent, "id $insql", $inparams);
710
        rebuild_course_cache($course->id, false, true);
689
        rebuild_course_cache($course->id, false, true);
711
        foreach ($cms as $cm) {
690
        foreach ($cms as $cm) {
Línea 1070... Línea 1049...
1070
            $this->section_state($updates, $course, $sectionids);
1049
            $this->section_state($updates, $course, $sectionids);
1071
        }
1050
        }
1072
    }
1051
    }
Línea 1073... Línea 1052...
1073
 
1052
 
-
 
1053
    /**
-
 
1054
     * Remove course modules with section delegate from a list.
-
 
1055
     *
-
 
1056
     * @param cm_info[] $cms the list of course modules to filter.
-
 
1057
     * @return cm_info[] the filtered list of course modules indexed by id.
-
 
1058
     */
-
 
1059
    protected function filter_cms_with_section_delegate(array $cms): array {
-
 
1060
        $filtered = [];
-
 
1061
        $modules = [];
-
 
1062
        foreach ($cms as $cm) {
-
 
1063
            if (!isset($modules[$cm->module])) {
-
 
1064
                $modules[$cm->module] = sectiondelegate::has_delegate_class('mod_' . $cm->modname);
-
 
1065
            }
-
 
1066
            if (!$modules[$cm->module]) {
-
 
1067
                $filtered[$cm->id] = $cm;
-
 
1068
            }
-
 
1069
        }
-
 
1070
        return $filtered;
-
 
1071
    }
-
 
1072
 
1074
    /**
1073
    /**
1075
     * Checks related to sections: course format support them, all given sections exist and topic 0 is not included.
1074
     * Checks related to sections: course format support them, all given sections exist and topic 0 is not included.
1076
     *
1075
     *
1077
     * @param stdClass $course The course where given $sectionids belong.
1076
     * @param stdClass $course The course where given $sectionids belong.
1078
     * @param array $sectionids List of sections to validate.
1077
     * @param array $sectionids List of sections to validate.
Línea 1105... Línea 1104...
1105
     * Checks related to course modules: all given cm exist and the user has the required capabilities.
1104
     * Checks related to course modules: all given cm exist and the user has the required capabilities.
1106
     *
1105
     *
1107
     * @param stdClass $course The course where given $cmids belong.
1106
     * @param stdClass $course The course where given $cmids belong.
1108
     * @param array $cmids List of course module ids to validate.
1107
     * @param array $cmids List of course module ids to validate.
1109
     * @param string $info additional information in case of error.
1108
     * @param string $info additional information in case of error.
1110
     * @param array $capabilities optional capabilities checks per each cm context.
1109
     * @param array $capabilities optional capabilities checks to require.
-
 
1110
     * @param bool $usemodcontext whether to use each module context, or the course context
1111
     * @throws moodle_exception if any id is not valid
1111
     * @throws moodle_exception if any id is not valid
1112
     */
1112
     */
1113
    protected function validate_cms(stdClass $course, array $cmids, ?string $info = null, array $capabilities = []): void {
1113
    protected function validate_cms(
-
 
1114
        stdClass $course,
-
 
1115
        array $cmids,
-
 
1116
        ?string $info = null,
-
 
1117
        array $capabilities = [],
-
 
1118
        bool $usemodcontext = true,
-
 
1119
    ): void {
Línea 1114... Línea 1120...
1114
 
1120
 
1115
        if (empty($cmids)) {
1121
        if (empty($cmids)) {
1116
            throw new moodle_exception('emptycmids', 'core', null, $info);
1122
            throw new moodle_exception('emptycmids', 'core', null, $info);
Línea 1117... Línea 1123...
1117
        }
1123
        }
1118
 
1124
 
1119
        $moduleinfo = get_fast_modinfo($course->id);
1125
        $moduleinfo = get_fast_modinfo($course->id);
1120
        $intersect = array_intersect($cmids, array_keys($moduleinfo->get_cms()));
1126
        $intersect = array_intersect($cmids, array_keys($moduleinfo->get_cms()));
1121
        if (count($cmids) != count($intersect)) {
1127
        if (count($cmids) != count($intersect)) {
-
 
1128
            throw new moodle_exception('unexistingcmid', 'core', null, $info);
1122
            throw new moodle_exception('unexistingcmid', 'core', null, $info);
1129
        }
-
 
1130
 
1123
        }
1131
        if (!empty($capabilities)) {
1124
        if (!empty($capabilities)) {
1132
            if ($usemodcontext) {
1125
            foreach ($cmids as $cmid) {
1133
                foreach ($cmids as $cmid) {
-
 
1134
                    $modcontext = context_module::instance($cmid);
-
 
1135
                    require_all_capabilities($capabilities, $modcontext);
-
 
1136
                }
-
 
1137
            } else {
1126
                $modcontext = context_module::instance($cmid);
1138
                $coursecontext = context_course::instance($course->id);
1127
                require_all_capabilities($capabilities, $modcontext);
1139
                require_all_capabilities($capabilities, $coursecontext);
1128
            }
1140
            }
-
 
1141
        }
-
 
1142
    }
-
 
1143
 
-
 
1144
    /**
-
 
1145
     * Create a course module.
-
 
1146
     *
-
 
1147
     * @deprecated since Moodle 5.0, use new_module instead.
-
 
1148
     * @todo MDL-83851: final deprecation of this method in Moodle 6.0.
-
 
1149
     * @param stateupdates $updates the affected course elements track
-
 
1150
     * @param stdClass $course the course object
-
 
1151
     * @param string $modname the module name
-
 
1152
     * @param int $targetsectionnum target section number
-
 
1153
     * @param int|null $targetcmid optional target cm id
-
 
1154
     */
-
 
1155
    #[\core\attribute\deprecated(
-
 
1156
        replacement: 'new_module',
-
 
1157
        since: '5.0',
-
 
1158
        mdl: 'MDL-83469',
-
 
1159
    )]
-
 
1160
    public function create_module(
-
 
1161
        stateupdates $updates,
-
 
1162
        stdClass $course,
-
 
1163
        string $modname,
-
 
1164
        int $targetsectionnum,
-
 
1165
        ?int $targetcmid = null
-
 
1166
    ): void {
-
 
1167
        global $CFG;
-
 
1168
        require_once($CFG->dirroot . '/course/modlib.php');
-
 
1169
 
-
 
1170
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
-
 
1171
 
-
 
1172
        $coursecontext = context_course::instance($course->id);
-
 
1173
        require_capability('moodle/course:update', $coursecontext);
-
 
1174
 
-
 
1175
        // Method "can_add_moduleinfo" called in "prepare_new_moduleinfo_data" will handle the capability checks.
-
 
1176
        [, , , , $moduleinfo] = prepare_new_moduleinfo_data($course, $modname, $targetsectionnum);
-
 
1177
        $moduleinfo->beforemod = $targetcmid;
-
 
1178
        create_module((object) $moduleinfo);
-
 
1179
 
-
 
1180
        // Adding module affects section structure, and if the module has a delegated section even the course structure.
-
 
1181
        $this->course_state($updates, $course);
-
 
1182
    }
-
 
1183
 
-
 
1184
    /**
-
 
1185
     * Create a new course module.
-
 
1186
     *
-
 
1187
     * @param stateupdates $updates the affected course elements track
-
 
1188
     * @param stdClass $course the course object
-
 
1189
     * @param string $modname the module name
-
 
1190
     * @param int $targetsectionid target section id
-
 
1191
     * @param int|null $targetcmid optional target cm id
-
 
1192
     */
-
 
1193
    public function new_module(
-
 
1194
        stateupdates $updates,
-
 
1195
        stdClass $course,
-
 
1196
        string $modname,
-
 
1197
        int $targetsectionid,
-
 
1198
        ?int $targetcmid = null
-
 
1199
    ): void {
-
 
1200
        global $CFG;
-
 
1201
        require_once($CFG->dirroot . '/course/modlib.php');
-
 
1202
 
-
 
1203
        $coursecontext = context_course::instance($course->id);
-
 
1204
        require_capability('moodle/course:update', $coursecontext);
-
 
1205
 
-
 
1206
        $modinfo = get_fast_modinfo($course);
-
 
1207
        $section = $modinfo->get_section_info_by_id($targetsectionid, MUST_EXIST);
-
 
1208
 
-
 
1209
        // Method "can_add_moduleinfo" called in "prepare_new_moduleinfo_data" will handle the capability checks.
-
 
1210
        [, , , , $moduleinfo] = prepare_new_moduleinfo_data($course, $modname, $section->sectionnum);
-
 
1211
        $moduleinfo->beforemod = $targetcmid;
-
 
1212
        create_module((object) $moduleinfo);
-
 
1213
 
-
 
1214
        // Adding module affects section structure, and if the module has a delegated section even the course structure.
1129
        }
1215
        $this->course_state($updates, $course);