|
| 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 |
| 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 | + * If no position is specified, the section is added to the end of the course. |
| 36 | + * |
| 37 | + * @param stdClass $fields the fields to set on the section |
| 38 | + * @param bool $skipcheck the position check has already been made and we know it can be used |
| 39 | + * @return stdClass the created section record |
| 40 | + */ |
| 41 | + public function fmt_create_from_object(stdClass $fields, bool $skipcheck = false): stdClass { |
| 42 | + global $DB; |
| 43 | + require_once(__DIR__.'/../../locallib.php'); |
| 44 | + |
| 45 | + $skipcheck = $skipcheck && isset($fields->section); |
| 46 | + |
| 47 | + // Determine the create position, and adapt fields for the move method, if necessary. |
| 48 | + if ($skipcheck) { |
| 49 | + $createnum = $fields->section; |
| 50 | + } else { |
| 51 | + $createnum = $DB->get_field_sql( |
| 52 | + 'SELECT max(section) from {course_sections} WHERE course = ?', |
| 53 | + [$this->course->id] |
| 54 | + ) + 1; |
| 55 | + if (!isset($fields->section) && !isset($fields->prevupid) && !isset($fields->nextupid) && !isset($fields->parentid)) { |
| 56 | + $fields->nextupid = null; |
| 57 | + } |
| 58 | + if (empty($fields->component) && !isset($fields->level)) { |
| 59 | + $fields->level = 2; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // First add section to the end. |
| 64 | + $sectionrecord = (object) [ |
| 65 | + 'course' => $this->course->id, |
| 66 | + 'section' => $createnum, |
| 67 | + 'summary' => $fields->summary ?? '', |
| 68 | + 'summaryformat' => $fields->summaryformat ?? FORMAT_HTML, |
| 69 | + 'sequence' => '', |
| 70 | + 'name' => $fields->name ?? null, |
| 71 | + 'visible' => $fields->visible ?? 1, |
| 72 | + 'availability' => $fields->availability ?? null, |
| 73 | + 'component' => $fields->component ?? null, |
| 74 | + 'itemid' => $fields->itemid ?? null, |
| 75 | + 'timemodified' => time(), |
| 76 | + ]; |
| 77 | + $sectionrecord->id = $DB->insert_record("course_sections", $sectionrecord); |
| 78 | + if (empty($fields->component) && !($skipcheck && ($fields->section > 0))) { |
| 79 | + $DB->insert_record("course_format_options", [ |
| 80 | + 'courseid' => $this->course->id, |
| 81 | + 'format' => 'multitopic', |
| 82 | + 'sectionid' => $sectionrecord->id, |
| 83 | + 'name' => 'level', |
| 84 | + 'value' => 0, |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + // Now move it to the specified position, if necessary. |
| 89 | + $skipmove = $skipcheck || !empty($fields->component) && property_exists($fields, 'nextupid') && ($fields->nextupid == null); |
| 90 | + if (!$skipmove) { |
| 91 | + try { |
| 92 | + $movednews = format_multitopic_move_section_to( |
| 93 | + $this->course, [$sectionrecord], $fields, !empty($fields->component) ? 2 : 1 |
| 94 | + ); |
| 95 | + $sectionrecord->section = $movednews[$sectionrecord->id]->section; |
| 96 | + } catch (\moodle_exception $e) { |
| 97 | + $DB->delete_records('course_sections', ['id' => $sectionrecord->id]); |
| 98 | + throw $e; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + \core\event\course_section_created::create_from_section($sectionrecord)->trigger(); |
| 103 | + |
| 104 | + rebuild_course_cache($this->course->id, true); |
| 105 | + return $sectionrecord; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments