|
26 | 26 | */ |
27 | 27 |
|
28 | 28 |
|
29 | | -/** |
30 | | - * Creates a course section and adds it to the specified position |
31 | | - * |
32 | | - * @param stdClass $courseorid course id or course object |
33 | | - * @param stdClass $section position to insert at. Must specify parentid. May specify level. |
34 | | - * @return stdClass created section object. Has database properties plus parentid and levelsan. |
35 | | - */ |
36 | | -function format_multitopic_course_create_section(\stdClass $courseorid, \stdClass $section): \stdClass { |
37 | | - // CHANGED LINE ABOVE: Use section info, specifying parentid and level, instead of section number. |
38 | | - global $CFG, $DB; |
39 | | - $courseid = is_object($courseorid) ? $courseorid->id : $courseorid; |
40 | | - |
41 | | - // ADDED: Require that the parent exists. |
42 | | - $parent = $DB->get_record('course_sections', ['id' => $section->parentid, 'course' => $courseid]); |
43 | | - if (!$parent) { |
44 | | - throw new \moodle_exception('sectionnotexist'); |
45 | | - } |
46 | | - // END ADDED. |
47 | | - |
48 | | - // Find the last sectionnum among existing sections. |
49 | | - if (false) { // CHANGED: Don't skip check. |
50 | | - $lastsection = $section->section - 1; // CHANGED: Extract from section info. |
51 | | - } else { |
52 | | - $lastsection = (int)$DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$courseid]); |
53 | | - } |
54 | | - |
55 | | - // First add section to the end. |
56 | | - $cw = new \stdClass(); |
57 | | - $cw->course = $courseid; |
58 | | - $cw->section = $lastsection + 1; |
59 | | - $cw->summary = ''; |
60 | | - $cw->summaryformat = FORMAT_HTML; |
61 | | - $cw->sequence = ''; |
62 | | - $cw->name = null; |
63 | | - $cw->visible = 1; |
64 | | - $cw->availability = null; |
65 | | - if ($CFG->version >= 2023122100.01) { |
66 | | - $cw->component = null; |
67 | | - $cw->itemid = null; |
68 | | - } |
69 | | - $cw->timemodified = time(); |
70 | | - $cw->id = $DB->insert_record("course_sections", $cw); |
71 | | - $DB->insert_record( |
72 | | - "course_format_options", |
73 | | - ['courseid' => $cw->course, 'format' => 'multitopic', 'sectionid' => $cw->id, 'name' => 'level', 'value' => 0] |
74 | | - ); |
75 | | - |
76 | | - // Now move it to the specified position. |
77 | | - if (true) { // CHANGED: We've already checked the parent exists. |
78 | | - $course = is_object($courseorid) ? $courseorid : get_course($courseorid); |
79 | | - rebuild_course_cache($courseid, true); // ADDED. |
80 | | - format_multitopic_move_section_to($course, $cw, $section); // CHANGED: Use section info instead of position. |
81 | | - // END CHANGED. |
82 | | - $cw->section = (int)$DB->get_field_sql('SELECT section from {course_sections} WHERE course = ? AND id = ?', |
83 | | - [$courseid, $cw->id]); // CHANGED. |
84 | | - $cw->parentid = $section->parentid; // ADDED. |
85 | | - $cw->levelsan = $section->level ?? FORMAT_MULTITOPIC_SECTION_LEVEL_TOPIC; // ADDED. |
86 | | - } |
87 | | - |
88 | | - \core\event\course_section_created::create_from_section($cw)->trigger(); |
89 | | - |
90 | | - rebuild_course_cache($courseid, true); |
91 | | - return $cw; |
92 | | -} |
93 | | - |
94 | | - |
95 | 29 | /** |
96 | 30 | * Moves sections within a course, from a position to another. |
97 | 31 | * |
98 | 32 | * @param \stdClass $course |
99 | 33 | * @param \stdClass|\section_info|array $origins The section(s) to be moved. Must specify id. |
100 | 34 | * @param \stdClass $destination Where to move it to. Must specify parentid, prevupid, or nextupid. May specify level. |
101 | 35 | * @param int $include 0 = regular only, 1 = also orphan, 2 = also delegated. |
| 36 | + * @return object[] objects containing section numbers for the moved section(s), indexed by id |
102 | 37 | */ |
103 | | -function format_multitopic_move_section_to(\stdClass $course, $origins, \stdClass $destination, int $include = 1): void { |
| 38 | +function format_multitopic_move_section_to(\stdClass $course, $origins, \stdClass $destination, int $include = 1): array { |
104 | 39 | // CHANGED LINE ABOVE: Use section info instead of number. Removed $ignorenumsections param. No return value (use exceptions). |
105 | 40 | // Moves course sections within the course. |
106 | 41 | // CHANGES THROUGHOUT: Use section info instead of number. |
@@ -171,7 +106,23 @@ function format_multitopic_move_section_to(\stdClass $course, $origins, \stdClas |
171 | 106 | } |
172 | 107 | // END ADDED. |
173 | 108 |
|
174 | | - return; // CHANGED. |
| 109 | + // Provide new section numbers for moved sections. |
| 110 | + $movedorigins = []; |
| 111 | + foreach ($origins as $origin) { |
| 112 | + if (isset($origin->id)) { |
| 113 | + $originid = $origin->id; |
| 114 | + } else { |
| 115 | + foreach ($sectionsextra as $sectionextra) { |
| 116 | + if ($sectionextra->section == $origin->section) { |
| 117 | + $originid = $sectionextra->id; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + $movedorigins[$originid] = (object)['id' => $originid, 'section' => $movedsections[$originid]->section]; |
| 123 | + } |
| 124 | + |
| 125 | + return $movedorigins; // CHANGED. |
175 | 126 | } |
176 | 127 |
|
177 | 128 |
|
|
0 commit comments