Skip to content

Commit

Permalink
Improved solution to 'Problems with creating multiple courses from te…
Browse files Browse the repository at this point in the history
…mplate via CSV in Grid Format' - #189, related to #203.
  • Loading branch information
gjb2048 committed Mar 24, 2024
1 parent ed4021d commit 7504fed
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 403.1.2 - TBR
1. Fix 'Orphaned Section still appear in Index Drawer'.
2. Fix 'Restrict access can cause no content in other sections to be shown.' - #202.
3. Fix 'Import quiz error + Number of topics reduced to 1' - #203.
4. Improved solution to 'Problems with creating multiple courses from template via CSV in Grid Format' - #189, related to #203.

Version 403.1.1 - 22/01/2024
----------------------------
Expand Down
12 changes: 9 additions & 3 deletions backup/moodle2/restore_format_grid_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public function process_gridsection($data) {
$target = $task->get_target();
if (
($target == backup::TARGET_NEW_COURSE) ||
// TARGET_CURRENT_ADDING used by both import and CSV course creation with template course, go figure!
($target == backup::TARGET_CURRENT_ADDING) ||
($target == backup::TARGET_CURRENT_DELETING) ||
($target == backup::TARGET_EXISTING_DELETING)
) {
Expand All @@ -199,9 +201,13 @@ public function process_gridsection($data) {
// We don't know how many more sections there is and also don't know if this is the last.
$courseformat = course_get_format($courseid);
if ($courseformat->get_format() == 'grid') {
static $gnumsections = 0;
$gnumsections++;
$courseformat->restore_gnumsections($gnumsections);
// Not calling the format's 'restore_gnumsections' as '-1' fails validation.
$DB->set_field('course_format_options', 'value', -1,
[
'courseid' => $courseid,
'name' => 'gnumsections',
]
);
}
}
/* Allow this to process even if not in the grid format so that our event observer on 'course_restored'
Expand Down
7 changes: 7 additions & 0 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
course_set_marker($course->id, $marker);
}

if ($courseformatoptions['gnumsections'] < 0) {
// Repair if number of sections is unknown on a restore.
$numberofsections = $format->get_last_section_number();
$format->restore_gnumsections($numberofsections);
$courseformatoptions['gnumsections'] = $numberofsections;
}

// Make sure all sections are created.
course_create_sections_if_missing($course, range(0, $courseformatoptions['gnumsections']));

Expand Down
2 changes: 1 addition & 1 deletion lang/en/format_grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
$string['settings'] = 'Settings';
$string['settingssettings'] = 'Settings settings';
$string['settingssettingsdesc'] = 'Grid format settings';
$string['stealthwarning'] = 'Warning: Course has {$a} orphaned section(s) with content.';
$string['stealthwarning'] = 'Warning: Course has {$a} orphaned section(s) with content. Resolve as soon as possible. Note: Importing into this course from another will turn orphaned sections into real ones - there is no current solution to this!';
$string['love'] = 'love';
$string['versioninfo'] = 'Release {$a->release}, version {$a->version} on Moodle {$a->moodle}. Made with {$a->love} in Great Britain.';
$string['versionalpha'] = 'Alpha version - Almost certainly contains bugs. This is a development version for developers \'only\'! Don\'t even think of installing on a production server!';
Expand Down
12 changes: 11 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public function get_settings($invalidate = false) {
public function get_last_section_number() {
$course = $this->get_course();
if (isset($course->gnumsections)) {
return $course->gnumsections;
if ($course->gnumsections >= 0) {
return $course->gnumsections;
}
}

return parent::get_last_section_number();
Expand Down Expand Up @@ -593,6 +595,14 @@ public function create_edit_form_elements(&$mform, $forsection = false) {
$maxsections = get_config('moodlecourse', 'maxsections');
$numsections = $mform->getElementValue('gnumsections');
$numsections = $numsections[0];
if ($numsections < 0) {
$numsections = $this->get_last_section_number();
/* Instead of setValue on the element as the default gets reused when the form is re-arranged by
'definition_after_data' in '/course/edit_form.php', specifically the calls to 'insertElementBefore'
after this method was called. */
$mform->setDefault('gnumsections', $numsections);
$this->restore_gnumsections($numsections);
}
if ($numsections > $maxsections) {
$element = $mform->getElement('gnumsections');
for ($i = $maxsections + 1; $i <= $numsections; $i++) {
Expand Down

0 comments on commit 7504fed

Please sign in to comment.