Skip to content

Commit

Permalink
Convert 'numsections' to 'gnumsections' to facilitate both a course s…
Browse files Browse the repository at this point in the history
…etting and flexible section management when editing.
  • Loading branch information
gjb2048 committed Jun 15, 2023
1 parent d797c42 commit 5eac72f
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 32 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 402.1.1 - TBR
----------------------------
1. Fix 'Previous/Next section arrows are incorrect for RTL languages' - #183.
2. Fix 'Last section not showing its image when editing'.
3. Convert 'numsections' to 'gnumsections' to facilitate both a course setting and flexible section management when editing.

Version 402.1.0 - 11/05/2023
----------------------------
Expand Down
5 changes: 4 additions & 1 deletion backup/moodle2/restore_format_grid_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ public function after_restore_course() {

$maxsection = $DB->get_field_sql('SELECT max(section) FROM {course_sections} WHERE course = ?', [$courseid]);

$courseformat->restore_numsections($maxsection);
$courseformat->restore_gnumsections($maxsection);
return;
} else {
// The backup file contains 'numsections' so we need to set 'gnumsections' to this value.
$courseformat->restore_gnumsections($settings['numsections']);
}

foreach ($backupinfo->sections as $key => $section) {
Expand Down
80 changes: 80 additions & 0 deletions classes/courseformat/stateactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.

namespace format_grid\courseformat;

use core_courseformat\stateupdates;
use core_courseformat\stateactions as stateactions_base;
use stdClass;

/**
* Contains the core course state actions specific to grid format.
*
* @package format_grid
* @copyright 2022 Ferran Recio <[email protected]>
* @copyright &copy; 2023-onwards G J Barnard based upon work done by Ferran Recio.
* @author G J Barnard - {@link https://gjbarnard.co.uk} and
* {@link http://moodle.org/user/profile.php?id=442195}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class stateactions extends stateactions_base {

/**
* Create a course section.
*
* This method follows the same logic as changenumsections.php.
*
* @param stateupdates $updates the affected course elements track
* @param stdClass $course the course object
* @param int[] $ids not used
* @param int $targetsectionid optional target section id (if not passed section will be appended)
* @param int $targetcmid not used
*/
public function section_add(
stateupdates $updates,
stdClass $course,
array $ids = [],
?int $targetsectionid = null,
?int $targetcmid = null
): void {
parent::section_add($updates, $course, $ids, $targetsectionid, $targetcmid);
$format = course_get_format($course);
$format->section_added();
}

/**
* Delete course sections.
*
* This method follows the same logic as editsection.php.
*
* @param stateupdates $updates the affected course elements track
* @param stdClass $course the course object
* @param int[] $ids section ids
* @param int $targetsectionid not used
* @param int $targetcmid not used
*/
public function section_delete(
stateupdates $updates,
stdClass $course,
array $ids = [],
?int $targetsectionid = null,
?int $targetcmid = null
): void {
parent::section_delete($updates, $course, $ids, $targetsectionid, $targetcmid);
$format = course_get_format($course);
$format->section_deleted();
}
}
4 changes: 0 additions & 4 deletions classes/output/courseformat/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public function export_for_template(\renderer_base $output) {
$course = $format->get_course();
$currentsectionid = 0;

if ($editing) {
$data->coursesettings = new \moodle_url('/course/edit.php', array('id' => $course->id));
}

if (!empty($sections)) {
// Most formats uses section 0 as a separate section so we remove from the list.
$initialsection = array_shift($sections);
Expand Down
63 changes: 63 additions & 0 deletions classes/output/courseformat/content/section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.

/**
* Contains the default section controls output class.
*
* @package format_grid
* @copyright 2020 Ferran Recio <[email protected]>
* @copyright &copy; 2023-onwards G J Barnard based upon work done by Ferran Recio.
* @author G J Barnard - {@link https://gjbarnard.co.uk} and
* {@link http://moodle.org/user/profile.php?id=442195}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace format_grid\output\courseformat\content;

use core_courseformat\base as course_format;
use core_courseformat\output\local\content\section as section_base;
use stdClass;

/**
* Class to render a course section.
*
* @package format_grid
* @copyright 2020 Ferran Recio <[email protected]>
* @copyright &copy; 2023-onwards G J Barnard based upon work done by Ferran Recio.
* @author G J Barnard - {@link https://gjbarnard.co.uk} and
* {@link http://moodle.org/user/profile.php?id=442195}
* @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;

public function export_for_template(\renderer_base $output): stdClass {
$format = $this->format;

$data = parent::export_for_template($output);

if (!$this->format->get_section_number()) {
$addsectionclass = $format->get_output_classname('content\\addsection');
$addsection = new $addsectionclass($format);
$data->numsections = $addsection->export_for_template($output);
$data->insertafter = true;
}

return $data;
}
}
17 changes: 17 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ function xmldb_format_grid_upgrade($oldversion = 0) {
upgrade_plugin_savepoint(true, 2022072200, 'format', 'grid');
}

if ($oldversion < 2023051001) {
$records = $DB->get_records('course_format_options',
array(
'format' => 'grid',
'name' => 'numsections'
), '', 'id'
);

$records = array_keys($records);
foreach ($records as $id) {
$DB->set_field('course_format_options', 'name', 'gnumsections', array('id' => $id));
}

// Grid savepoint reached.
upgrade_plugin_savepoint(true, 2023051001, 'format', 'grid');
}

// Automatic 'Purge all caches'....
purge_all_caches();

Expand Down
2 changes: 1 addition & 1 deletion format.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}

// Make sure section 0 is created.
course_create_sections_if_missing($course, range(0, $course->numsections ));
course_create_sections_if_missing($course, 0);

$renderer = $PAGE->get_renderer('format_grid');

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 @@ -39,6 +39,7 @@
$string['page-course-view-grid-x'] = 'Any course page in the grid format';

$string['addsection'] = 'Add section';
$string['addsections'] = 'Add section';
$string['hidefromothers'] = 'Hide section'; // No longer used kept for legacy versions.
$string['showfromothers'] = 'Show section'; // No longer used kept for legacy versions.
$string['currentsection'] = 'This section'; // No longer used kept for legacy versions.
Expand Down Expand Up @@ -124,7 +125,6 @@
$string['informationsettings'] = 'Information settings';
$string['informationsettingsdesc'] = 'Grid format information';
$string['informationchanges'] = 'Changes';
$string['sectionchangecoursesettings'] = 'Change the number of sections in the course settings';
$string['settings'] = 'Settings';
$string['settingssettings'] = 'Settings settings';
$string['settingssettingsdesc'] = 'Grid format settings';
Expand Down
69 changes: 48 additions & 21 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function course_format_options($foreditform = false) {
WHERE course = ?', array($courseid));
}
$courseformatoptions = array(
'numsections' => array(
'gnumsections' => array(
'default' => $defaultnumsections,
'type' => PARAM_INT,
),
Expand Down Expand Up @@ -358,7 +358,7 @@ public function course_format_options($foreditform = false) {
$sectionmenu[$i] = "$i";
}
$courseformatoptionsedit = array(
'numsections' => array(
'gnumsections' => array(
'label' => new lang_string('numbersections', 'format_grid'),
'element_type' => 'select',
'element_attributes' => array($sectionmenu),
Expand Down Expand Up @@ -515,10 +515,10 @@ public function create_edit_form_elements(&$mform, $forsection = false) {
activities / resources. */
if (!$forsection) {
$maxsections = get_config('moodlecourse', 'maxsections');
$numsections = $mform->getElementValue('numsections');
$numsections = $mform->getElementValue('gnumsections');
$numsections = $numsections[0];
if ($numsections > $maxsections) {
$element = $mform->getElement('numsections');
$element = $mform->getElement('gnumsections');
for ($i = $maxsections + 1; $i <= $numsections; $i++) {
$element->addOption("$i", $i);
}
Expand Down Expand Up @@ -553,30 +553,41 @@ public function update_course_format_options($data, $oldcourse = null) {
if (!array_key_exists($key, $data)) {
if (array_key_exists($key, $oldcourse)) {
$data[$key] = $oldcourse[$key];
} else if ($key === 'numsections') {
/* If previous format does not have the field 'numsections' and $data['numsections'] is not set,
we fill it with the maximum section number from the DB. */
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
if ($maxsection) {
// If there are no sections, or just default 0-section, 'numsections' will be set to default.
$data['numsections'] = $maxsection;
} else if ($key === 'gnumsections') {
if (array_key_exists('numsections', $oldcourse)) {
// Transpose numsections to gnumsections.
$data[$key] = $oldcourse['numsections'];
} else {
/* The previous format does not have the field 'numsections' and $data['gnumsections'] is not set,
we fill it with the maximum section number from the DB. */
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
if ($maxsection) {
// If there are no sections, or just default 0-section, 'gnumsections' will be set to default.
$data['gnumsections'] = $maxsection;
}
}
}
}
}
}
$changes = $this->update_format_options($data);

if ($changes && array_key_exists('numsections', $data)) {
// If the numsections was decreased, try to completely delete the orphaned sections (unless they are not empty).
$numsections = (int)$data['numsections'];
if ($changes && array_key_exists('gnumsections', $data)) {
$numsections = (int)$data['gnumsections'];
$maxsection = $DB->get_field_sql('SELECT max(section) from {course_sections}
WHERE course = ?', array($this->courseid));
for ($sectionnum = $maxsection; $sectionnum > $numsections; $sectionnum--) {
if (!$this->delete_section($sectionnum, false)) {
break;
if ($numsections < $maxsection) {
// The setting gnumsections has decreased, try to completely delete the orphaned sections (unless they are not empty).
for ($sectionnum = $maxsection; $sectionnum > $numsections; $sectionnum--) {
if (!$this->delete_section($sectionnum, false)) {
break;
}
}
} else if ($numsections > $maxsection) {
// The setting gnumsections has increased then create the sections.
$course = $this->get_course();
course_create_sections_if_missing($course, range(0, $numsections ));
}
}

Expand Down Expand Up @@ -785,14 +796,30 @@ public function section_action($section, $action, $sr) {
}

/**
* Restores the numsections if was not in the backup.
* Restores the numsections if was not in the backup or if it was then transposes to gnumsections.
* @param int $numsections The number of sections.
*/
public function restore_numsections($numsections) {
$data = array('numsections' => $numsections);
public function restore_gnumsections($numsections) {
$data = array('gnumsections' => $numsections);
$this->update_course_format_options($data);
}

/**
* A section has been added. Should only be called from the state actions instance.
*/
public function section_added() {
$data = array('gnumsections' => $this->settings['gnumsections'] + 1);
$this->update_format_options($data);
}

/**
* A section has been deleted. Should only be called from the state actions instance.
*/
public function section_deleted() {
$data = array('gnumsections' => $this->settings['gnumsections'] - 1);
$this->update_format_options($data);
}

/**
* Return the plugin configs for external functions.
*
Expand Down
4 changes: 0 additions & 4 deletions templates/local/content.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
"selector": "<select><option>Section 4</option></select>"
},
"sectionreturn": 1,
"coursesettings": "https://mymoodle/course/edit.php?id=4",
"singlesection": {
"num": 1,
"id": 35,
Expand Down Expand Up @@ -215,9 +214,6 @@
{{> core_courseformat/local/content/bulkedittools}}
{{/ core_courseformat/local/content/bulkedittools}}
{{/bulkedittools}}
{{#coursesettings}}
<a class="d-block text-right" href="{{{coursesettings}}}">{{#str}}sectionchangecoursesettings, format_grid{{/str}}</a>
{{/coursesettings}}
</div>
{{#js}}
require(['format_grid/local/content'], function(component) {
Expand Down

0 comments on commit 5eac72f

Please sign in to comment.