Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions backup/moodle2/backup_format_softcourse_plugin.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
// This file is part of the softcourse course format
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* format_softcourse course format.
* Provides the information to backup grid course format
*
* @package format_softcourse
* @copyright 2026 Pimenko <contact@pimenko.com>.
* @category backup
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_format_softcourse_plugin extends backup_format_plugin {
/**
* Returns the format information to attach to section element
*/
protected function define_course_plugin_structure() {

// Define the virtual plugin element with the condition to fulfill.
$plugin = $this->get_plugin_element(
null,
'/course/format',
'softcourse',
);

// Create one standard named plugin element (the visible container).
$pluginwrapper = new backup_nested_element($this->get_recommended_name());

$plugin->add_child($pluginwrapper);

// Introduction.
$pluginwrapper->annotate_files('format_softcourse', 'introduction', null);

// Section image.
$pluginwrapper->annotate_files('format_softcourse', 'sectionimage', null);

return $plugin;
}
}
54 changes: 36 additions & 18 deletions backup/moodle2/restore_format_softcourse_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Specialised restore for format_softcourse
*
* @package format_softcourse
* @category backup
* @copyright 2021 Pimenko <contact@pimneko.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Specialised restore for format_softcourse
* Specialised restore for format_softcourse.
*
* Processes 'numsections' from the old backup files and hides sections that used to be "orphaned"
*
Expand All @@ -34,7 +25,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_format_softcourse_plugin extends restore_format_plugin {

/** @var int */
protected $originalnumsections = 0;

Expand All @@ -61,11 +51,14 @@ public function define_course_plugin_structure() {
// Since this method is executed before the restore we can do some pre-checks here.
// In case of merging backup into existing course find the current number of sections.
$target = $this->step->get_task()->get_target();
if (($target == backup::TARGET_CURRENT_ADDING || $target == backup::TARGET_EXISTING_ADDING) &&
$this->need_restore_numsections()) {
if (
($target == backup::TARGET_CURRENT_ADDING || $target == backup::TARGET_EXISTING_ADDING) &&
$this->need_restore_numsections()
) {
$maxsection = $DB->get_field_sql(
'SELECT max(section) FROM {course_sections} WHERE course = ?',
[$this->step->get_task()->get_courseid()]);
[$this->step->get_task()->get_courseid()]
);
$this->originalnumsections = (int)$maxsection;
}

Expand All @@ -77,7 +70,6 @@ public function define_course_plugin_structure() {
* Dummy process method
*/
public function process_dummy_course() {

}

/**
Expand All @@ -86,7 +78,31 @@ public function process_dummy_course() {
* This method is only executed if course configuration was overridden
*/
public function after_restore_course() {
global $DB;
global $DB, $CFG;

$this->add_related_files('format_softcourse', 'introduction', null);
$this->add_related_files('format_softcourse', 'sectionimage', 'course_section');

$courseid = $this->step->get_task()->get_courseid();
$newcontext = \context_course::instance($courseid);

$record = $DB->get_record('course_format_options', [
'courseid' => $courseid,
'format' => 'softcourse',
'name' => 'introduction',
]);

if ($record && !empty($record->value)) {
$newvalue = preg_replace(
'/\$@PLUGINFILEBYCONTEXT\*\d+@\$/',
$CFG->wwwroot . '/pluginfile.php/' . $newcontext->id,
$record->value
);
if ($newvalue !== $record->value) {
$record->value = $newvalue;
$DB->update_record('course_format_options', $record);
}
}

if (!$this->need_restore_numsections()) {
// Backup file was made in Moodle 3.3 or later, we don't need to process 'numsecitons'.
Expand All @@ -110,8 +126,10 @@ public function after_restore_course() {
if ($this->step->get_task()->get_setting_value($key . '_included')) {
$sectionnum = (int)$section->title;
if ($sectionnum > $numsections && $sectionnum > $this->originalnumsections) {
$DB->execute("UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?",
[$this->step->get_task()->get_courseid(), $sectionnum]);
$DB->execute(
"UPDATE {course_sections} SET visible = 0 WHERE course = ? AND section = ?",
[$this->step->get_task()->get_courseid(), $sectionnum]
);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions classes/output/courseformat/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class content extends content_base {

/**
* @var bool Topic format has add section after each topic.
*
* The responsible for the buttons is core_courseformat\output\local\content\section.
*/
protected $hasaddsection = true;

}
52 changes: 26 additions & 26 deletions classes/output/courseformat/content/section.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class section extends section_base {

/** @var course_format the course format */
protected $format;

Expand Down Expand Up @@ -75,7 +74,7 @@ public function export_for_template(\renderer_base $output): stdClass {

// Single pass: initialize section fields once, attach cminfo, and compute completion in the same loop.
$hidesectionzero = (int) ($format->get_format_options()['hidesectionzero'] ?? 0) === 1;
$sectionFieldsSet = false;
$sectionfieldsset = false;

$nbcomplete = 0;
$nbcompletion = 0;
Expand All @@ -93,7 +92,7 @@ public function export_for_template(\renderer_base $output): stdClass {
}

// Initialize section-level data only once (avoid overwriting on subsequent iterations).
if (!$sectionFieldsSet) {
if (!$sectionfieldsset) {
$info = $modinfo->get_section_info($idsection);
$data->idsection = $idsection;
$data->name = $info->name ?? null;
Expand All @@ -102,7 +101,7 @@ public function export_for_template(\renderer_base $output): stdClass {
$data->visible = (int) ($info->visible ?? 0);
$data->available = (bool) ($info->available ?? false);
$data->skip = false;
$sectionFieldsSet = true;
$sectionfieldsset = true;
}

// Attach cminfo so templates and later logic can access full CM data.
Expand Down Expand Up @@ -148,20 +147,22 @@ public function export_for_template(\renderer_base $output): stdClass {
}

// If the section itself is hidden or not available, skip rendering it.
if ((isset($data->visible) && (int) $data->visible === 0) || (isset($data->uservisible) && $data->uservisible === false) ||
(isset($data->available) && $data->available === false)) {
if (
(isset($data->visible) && (int) $data->visible === 0) || (isset($data->uservisible) && $data->uservisible === false) ||
(isset($data->available) && $data->available === false)
) {
$data->skip = true;
return $data;
}

// If there is only one CM and it is hidden/unavailable on the course page, skip.
if (isset($data->cmlist) && isset($data->cmlist->cms) && count($data->cmlist->cms) === 1) {
$only = $data->cmlist->cms[0]->cminfo ?? null;
$onlyHiddenOrUnavailable = ($only && isset($only->visible) && (int) $only->visible === 0) ||
$onlyhiddenorunavailable = ($only && isset($only->visible) && (int) $only->visible === 0) ||
($only && isset($only->visibleoncoursepage) && (int) $only->visibleoncoursepage === 0) ||
($only && isset($only->uservisible) && $only->uservisible === false) ||
($only && isset($only->available) && $only->available === false);
if ($onlyHiddenOrUnavailable) {
if ($onlyhiddenorunavailable) {
$data->skip = true;
return $data;
}
Expand Down Expand Up @@ -199,10 +200,7 @@ public function export_for_template(\renderer_base $output): stdClass {
);

// Capability checks for section image edit/delete actions.
if (has_capability(
'moodle/course:update',
$context,
)) {
if (has_capability('moodle/course:update', $context)) {
$data->update_img = get_string(
'update_img',
'format_softcourse',
Expand Down Expand Up @@ -266,29 +264,29 @@ public function export_for_template(\renderer_base $output): stdClass {
*
* @param object $cm The course module object, which may include or reference cm_info.
* @param stdClass $data The data object containing details about activities and URLs.
* @param \completion_info $completioninfo The completion information object
* @param \completion_info $completioninfo The completion information object.
* @param int $nbcompletion The total number of completion items.
* @param int $nbcomplete The number of completed items.
* @return void
*/
function get_completion($cm, $data, $completioninfo, $nbcompletion, $nbcomplete) {

// Determine if the desired information is in $cm or $cm->cminfo
public function get_completion($cm, $data, $completioninfo, $nbcompletion, $nbcomplete) {
// Determine if the desired information is in $cm or $cm->cminfo.
$cminfo = null;
if (is_object($cm)) {
if ($cm instanceof \cm_info) {
$cminfo = $cm;
} else if (property_exists(
$cm,
'cminfo',
) && $cm->cminfo instanceof \cm_info) {
} else if (property_exists($cm, 'cminfo') && $cm->cminfo instanceof \cm_info) {
$cminfo = $cm->cminfo;
}
}

if ($cminfo !== null) {

if ($cminfo->get_user_visible() &&
if (
$cminfo->get_user_visible() &&
(isset($cminfo->available) && $cminfo->available) &&
(($cminfo->uservisible && !$cminfo->is_stealth() && $cminfo->modname != 'label') || !empty($cm->url)) &&
$data->first_cm_url == '') {
$data->first_cm_url == ''
) {
if ($cminfo->modname == 'resource') {
$cminfo->url->param(
'forceview',
Expand All @@ -311,16 +309,18 @@ function get_completion($cm, $data, $completioninfo, $nbcompletion, $nbcomplete)
true,
)->completionstate;

if ($cminfo->deletioninprogress == 0 && $cminfo->visible == 1 && $cminfo->modname != "label" &&
$cminfo->visibleoncoursepage == 1 && $cminfo->uservisible && $cminfo->available == true) {
if (
$cminfo->deletioninprogress == 0 && $cminfo->visible == 1 && $cminfo->modname != "label" &&
$cminfo->visibleoncoursepage == 1 && $cminfo->uservisible && $cminfo->available == true
) {
$data->countactivities += 1;
}
}
}
return [
$data,
$nbcompletion,
$nbcomplete
$nbcomplete,
];
}
}
21 changes: 8 additions & 13 deletions classes/output/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends section_renderer {

/**
* @var stdClass Course
*/
Expand Down Expand Up @@ -125,10 +124,9 @@ public function render_content($widget) {
$options,
);

if ($this->page->user_is_editing() && has_capability(
'moodle/course:update',
$context,
)) {
if (
$this->page->user_is_editing() && has_capability('moodle/course:update', $context)
) {
// Base template.
$content = $this->render_from_template(
'core_courseformat/local/content',
Expand All @@ -151,7 +149,6 @@ public function render_content($widget) {
}

if ($this->courseformat->get_format_options()['hideallsections'] == 1) {

if (!$data->start_url) {
foreach ($data->sections as $section) {
if ($section->skip != true && $section->start_url != null) {
Expand All @@ -161,7 +158,6 @@ public function render_content($widget) {
}
}
$data->sections = false;

} else {
if (!$data->start_url) {
foreach ($data->sections as $section) {
Expand Down Expand Up @@ -239,10 +235,12 @@ protected function section_edit_control_items($course, $section, $onsectionpage
);

$controls = [];
if ($section->section && has_capability(
if (
$section->section && has_capability(
'moodle/course:setcurrentsection',
$coursecontext,
)) {
)
) {
if ($course->marker == $section->section) { // Show the "light globe" on/off.
$url->param(
'marker',
Expand Down Expand Up @@ -295,10 +293,7 @@ protected function section_edit_control_items($course, $section, $onsectionpage
);

// If the edit key exists, we are going to insert our controls after it.
if (array_key_exists(
"edit",
$parentcontrols,
)) {
if (array_key_exists("edit", $parentcontrols)) {
$merged = [];
// We can't use splice because we are using associative arrays.
// Step through the array and merge the arrays.
Expand Down
1 change: 0 additions & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
* @package YourPackageName
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
Expand Down
3 changes: 0 additions & 3 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function xmldb_format_softcourse_upgrade($oldversion) {
require_once($CFG->dirroot . '/course/format/softcourse/db/upgradelib.php');

if ($oldversion < 2017020200) {

// Remove 'numsections' option and hide or delete orphaned sections.
format_softcourse_upgrade_remove_numsections();

Expand All @@ -48,7 +47,6 @@ function xmldb_format_softcourse_upgrade($oldversion) {
// Put any upgrade step following this.

if ($oldversion < 2018030900) {

// During upgrade to Moodle 3.3 it could happen that general section (section 0) became 'invisible'.
// It should always be visible.
$DB->execute("UPDATE {course_sections} SET visible=1 WHERE visible=0 AND section=0 AND course IN
Expand All @@ -57,7 +55,6 @@ function xmldb_format_softcourse_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2018030900, 'format', 'softcourse');
}
if ($oldversion < 2019103100) {

// Get all sections 0 of courses having softcourse course format.
$sectionsrequest = 'SELECT s.id, s.course, s.summary
FROM {course_sections} s
Expand Down
Loading