Skip to content

Commit 9157cfc

Browse files
committed
Create section method (MDL-80187) WIP 1
1 parent a9d86f8 commit 9157cfc

3 files changed

Lines changed: 120 additions & 71 deletions

File tree

_course_changenumsections.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
* @since Moodle 2.3
3030
*/
3131

32+
use core_courseformat\formatactions; // ADDED.
33+
3234
require_once(__DIR__.'/../../../config.php'); // CHANGED.
3335
require_once($CFG->dirroot.'/course/lib.php');
34-
require_once(__DIR__.'/locallib.php');
35-
// ADDED LINE ABOVE: For function format_multitopic_course_create_section .
3636

3737
$courseid = required_param('courseid', PARAM_INT);
3838
$increase = optional_param('increase', null, PARAM_BOOL);
@@ -114,7 +114,7 @@
114114
}
115115
$sections = [];
116116
for ($i = 0; $i < max($numsections, 1); $i ++) {
117-
$sections[] = format_multitopic_course_create_section($course, $insertsection);
117+
$sections[] = formatactions::section($course)->create_from_object($insertsection);
118118
// CHANGED LINE ABOVE: Use custom method, and send section info, not section number.
119119
}
120120
$returnurl = course_get_url($course, $sections[0], []);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 format_multitopic\courseformat;
18+
19+
use stdClass;
20+
use core_courseformat\local\sectionactions as base_sectionactions;
21+
22+
/**
23+
* Section course format actions.
24+
*
25+
* @package format_multitopic\courseformat
26+
* @copyright 2024 onwards James Calder and Otago Polytechnic
27+
* @copyright based on work by 2023 Ferran Recio <ferran@moodle.com>
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class sectionactions extends base_sectionactions {
31+
32+
/**
33+
* Create a course section using a record object.
34+
*
35+
* @param stdClass $fields the fields to set on the section
36+
* @param bool $skipcheck the position check has already been made and we know it can be used
37+
* @return stdClass the created section record
38+
*/
39+
public function create_from_object(stdClass $fields, bool $skipcheck = false): stdClass {
40+
global $DB;
41+
require_once(__DIR__.'/../../locallib.php');
42+
43+
$skipcheck = $skipcheck && isset($fields->section);
44+
45+
// Determine the create position, and adapt fields for the move method, if necessary.
46+
if ($skipcheck) {
47+
$createnum = $fields->section;
48+
} else {
49+
$createnum = $DB->get_field_sql(
50+
'SELECT max(section) from {course_sections} WHERE course = ?',
51+
[$this->course->id]
52+
) + 1;
53+
if (!isset($fields->section) && !isset($fields->previd) && !isset($fields->nextid) && !isset($fields->parentid)) {
54+
$fields->nextid = null;
55+
}
56+
}
57+
58+
// First add section to the end.
59+
$sectionrecord = (object) [
60+
'course' => $this->course->id,
61+
'section' => $createnum,
62+
'summary' => $fields->summary ?? '',
63+
'summaryformat' => $fields->summaryformat ?? FORMAT_HTML,
64+
'sequence' => '',
65+
'name' => $fields->name ?? null,
66+
'visible' => $fields->visible ?? 1,
67+
'availability' => $fields->availability ?? null,
68+
'component' => $fields->component ?? null,
69+
'itemid' => $fields->itemid ?? null,
70+
'timemodified' => time(),
71+
];
72+
$sectionrecord->id = $DB->insert_record("course_sections", $sectionrecord);
73+
$DB->insert_record("course_format_options", [
74+
'courseid' => $this->course->id,
75+
'format' => 'multitopic',
76+
'sectionid' => $sectionrecord->id,
77+
'name' => 'level',
78+
'value' => 0
79+
]);
80+
81+
// Now move it to the specified position, if necessary.
82+
if (!$skipcheck && !(!empty($fields->component) && property_exists($fields, 'nextid') && $fields->nextid == null)) {
83+
try {
84+
rebuild_course_cache($this->course->id, true);
85+
$movednews = format_multitopic_move_section_to($this->course, [$sectionrecord], $fields, !empty($fields->component) ? 2 : 1);
86+
$sectionrecord->section = $movednews[$sectionrecord->id]->section;
87+
} catch (\moodle_exception $e) {
88+
$DB->delete_records('course_sections', ['id' => $sectionrecord->id]);
89+
throw $e;
90+
}
91+
}
92+
93+
\core\event\course_section_created::create_from_section($sectionrecord)->trigger();
94+
rebuild_course_cache($this->course->id, true);
95+
return $sectionrecord;
96+
}
97+
98+
}

locallib.php

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -26,81 +26,16 @@
2626
*/
2727

2828

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-
9529
/**
9630
* Moves sections within a course, from a position to another.
9731
*
9832
* @param \stdClass $course
9933
* @param \stdClass|\section_info|array $origins The section(s) to be moved. Must specify id.
10034
* @param \stdClass $destination Where to move it to. Must specify parentid, prevupid, or nextupid. May specify level.
10135
* @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
10237
*/
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 {
10439
// CHANGED LINE ABOVE: Use section info instead of number. Removed $ignorenumsections param. No return value (use exceptions).
10540
// Moves course sections within the course.
10641
// CHANGES THROUGHOUT: Use section info instead of number.
@@ -171,7 +106,23 @@ function format_multitopic_move_section_to(\stdClass $course, $origins, \stdClas
171106
}
172107
// END ADDED.
173108

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.
175126
}
176127

177128

0 commit comments

Comments
 (0)