Skip to content

Commit

Permalink
Better solution!
Browse files Browse the repository at this point in the history
  • Loading branch information
gjb2048 committed Sep 28, 2023
1 parent dbaa0e2 commit 3e6bf33
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
17 changes: 16 additions & 1 deletion backup/moodle2/restore_format_grid_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,25 @@ public function process_gridsection($data) {

$data = (object) $data;

$target = $this->step->get_task()->get_target();
if (($target == backup::TARGET_NEW_COURSE) ||
($target == backup::TARGET_CURRENT_ADDING) ||
($target == backup::TARGET_CURRENT_DELETING) ||
($target == backup::TARGET_EXISTING_DELETING)) {
/* This ensures that when a course is created from an uploaded CSV file that the number of sections is correct.
Thus when an existing course or course file is used but the course restore code is not called.
Because the backup file / course being restored from has the correct 'sections', i.e. that will be in the
'course_sections' table. */
$courseid = $this->task->get_courseid();
static $gnumsections = 0;
$gnumsections++;
// 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);
$courseformat->restore_gnumsections($gnumsections);
}
/* Allow this to process even if not in the grid format so that our event observer on 'course_restored'
can perform a clean up of restored grid image files after all the data is in place in the database
for this to happen properly. */
$target = $this->step->get_task()->get_target();
if (($target == backup::TARGET_NEW_COURSE) ||
($target == backup::TARGET_CURRENT_DELETING) ||
($target == backup::TARGET_EXISTING_DELETING) ||
Expand Down
13 changes: 0 additions & 13 deletions format.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@
course_set_marker($course->id, $marker);
}

if ($courseformatoptions['gnumsectionsnewcourse'] == 1) {
// A new course that may have sections but does not know how many.
global $DB;
$numsections = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', [$course->id]);
if (!empty($numsections)) {
// Sections created, so we need set gnumsections to this.
$courseformatoptions['gnumsections'] = $numsections;
} // Else remain the same.
$format->set_gnumsections($courseformatoptions['gnumsections']);
$courseformatoptions['gnumsectionsnewcourse'] = 0;
}

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

Expand Down
22 changes: 0 additions & 22 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,16 @@ public function course_format_options($foreditform = false) {
$courseid = $this->get_courseid();
if ($courseid == 1) { // New course.
$defaultnumsections = $courseconfig->numsections;
$defaultgnumsectionsnewcourse = 1;
} else { // Existing course that may not have '(g)numsections' - see get_last_section().
global $DB;
$defaultnumsections = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', [$courseid]);
$defaultgnumsectionsnewcourse = 0;
}
$courseformatoptions = [
'gnumsections' => [
'default' => $defaultnumsections,
'type' => PARAM_INT,
],
'gnumsectionsnewcourse' => [
'default' => $defaultgnumsectionsnewcourse,
'type' => PARAM_INT,
],
'hiddensections' => [
'default' => $courseconfig->hiddensections,
'type' => PARAM_INT,
Expand Down Expand Up @@ -374,10 +368,6 @@ public function course_format_options($foreditform = false) {
'element_type' => 'select',
'element_attributes' => [$sectionmenu],
],
'gnumsectionsnewcourse' => [
'label' => 0,
'element_type' => 'hidden',
],
'hiddensections' => [
'label' => new lang_string('hiddensections'),
'help' => 'hiddensections',
Expand Down Expand Up @@ -837,18 +827,6 @@ public function restore_gnumsections($numsections) {
$this->update_course_format_options($data);
}

/**
* Sets the gnumsections correctly for a new course.
* @param int $numsections The number of sections.
*/
public function set_gnumsections($numsections) {
$data = [
'gnumsections' => $numsections,
'gnumsectionsnewcourse' => 0,
];
$this->update_course_format_options($data);
}

/**
* A section has been added. Should only be called from the state actions instance.
*/
Expand Down

0 comments on commit 3e6bf33

Please sign in to comment.