Skip to content

Commit

Permalink
Course section break cache is now reset every time a section break is…
Browse files Browse the repository at this point in the history
… changed

Added section breaks to the course backup
Moved the /cards backup element to /cardimage under a best practice plugin-specific backup element
  • Loading branch information
Vidalia committed May 21, 2024
1 parent 5779275 commit 3563dda
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 17 deletions.
66 changes: 59 additions & 7 deletions backup/moodle2/backup_format_cards_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

use format_cards\section_break;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . "/../../lib.php");
Expand All @@ -34,24 +36,74 @@ class backup_format_cards_plugin extends backup_format_plugin {
* @return backup_plugin_element
* @throws base_element_struct_exception
*/
protected function define_section_plugin_structure() {
protected function define_section_plugin_structure(): backup_plugin_element {
$parent = $this->get_plugin_element(null, $this->get_format_condition(), 'cards');

$plugin = $this->get_plugin_element(null, $this->get_format_condition(), 'cards');
$pluginwrapper = new backup_nested_element($this->get_recommended_name());

// Create a nested element under each backed up section, this is just a dummy container.
$wrapper = new backup_nested_element('cards', [ 'id' ], [ 'section' ]);
$wrapper->set_source_table('course_sections', [ 'id' => backup::VAR_SECTIONID ]);
$imageswrapper = new backup_nested_element(
'cardimage',
[ 'id' ],
[ 'contenthash', 'pathnamehash', 'filename', 'mimetype' ]
);
$imageswrapper->set_source_table(
'files',
[
'itemid' => backup::VAR_SECTIONID,
'component' => backup_helper::is_sqlparam('format_cards'),
'filearea' => backup_helper::is_sqlparam(FORMAT_CARDS_FILEAREA_IMAGE),
]);

// Annotate files in the format_cards/image filearea for this course's context ID
// The itemid doesn't get mapped to the new section id, if it changes.
$wrapper->annotate_files(
$imageswrapper->annotate_files(
'format_cards',
FORMAT_CARDS_FILEAREA_IMAGE,
null
);

$plugin->add_child($wrapper);
return $plugin;
$pluginwrapper->add_child($imageswrapper);

// We need a wrapper for section breaks as well.
$sectionbreakwrapper = new backup_nested_element('sectionbreak', [ 'id' ], [ 'name', 'section' ]);

// We store section information as courseid / section number, so we need to map the section ID we're backing
// up to a section number.
$sectionbreakwrapper->set_source_sql("
SELECT section_break.id, section_break.name, section_break.section
FROM {format_cards_break} section_break
JOIN {course_sections} section
ON section.course = section_break.courseid
AND section.section = section_break.section
WHERE section.id = ?",
[ backup::VAR_SECTIONID ]);

$pluginwrapper->add_child($sectionbreakwrapper);

$parent->add_child($pluginwrapper);

return $parent;
}

/**
* Adds section break data to the course structure
*
* @return backup_plugin_element
* @throws base_element_struct_exception
*/
/*protected function define_course_plugin_structure(): backup_plugin_element {
$plugin = $this->get_plugin_element(null, '/course/format', 'cards');
$pluginwrapper = new backup_nested_element($this->get_recommended_name());
$sectionbreakswrapper = new backup_nested_element('sectionbreak', [ 'section' ], [ 'name' ]);
$sectionbreakswrapper->set_source_table(section_break::TABLE, [ 'courseid' => backup::VAR_COURSEID ]);
$pluginwrapper->add_child($sectionbreakswrapper);
$plugin->add_child($pluginwrapper);
return $plugin;
}*/

}
81 changes: 71 additions & 10 deletions backup/moodle2/restore_format_cards_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

use format_cards\section_break;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . "/../../lib.php");
Expand All @@ -34,19 +36,71 @@ class restore_format_cards_plugin extends restore_format_plugin {
*
* @return restore_path_element[]
*/
public function define_section_plugin_structure() {
public function define_section_plugin_structure(): array {

$target = $this->step->get_task()->get_target();

// If we're backing up into an existing course, but want to empty it out first,
// make sure we delete all the section breaks before continuing.
if ($target == backup::TARGET_CURRENT_DELETING
|| $target == backup::TARGET_EXISTING_DELETING) {

$breaks = section_break::get_breaks_for_course($this->step->get_task()->get_courseid());

foreach ($breaks as $break) {
$break->delete();
}
}

$this->add_related_files('format_cards', FORMAT_CARDS_FILEAREA_IMAGE, null);
return [ new restore_path_element('cards', $this->get_pathfor('/cards')) ];

return [
// Keep this for backwards compatibility with previous versions.
new restore_path_element('cards', $this->get_pathfor('/cards')),
new restore_path_element('cardimage', $this->get_pathfor('/cardimage')),
new restore_path_element('sectionbreak', $this->get_pathfor('/sectionbreak')),
];
}

/**
* Process an individual section break.
*
* @param array $data
* @return void
*/
public function process_sectionbreak(array $data): void {
$courseid = $this->step->get_task()->get_courseid();

$break = section_break::get_record([ 'courseid' => $courseid, 'section' => $data['section'] ], IGNORE_MISSING);

// Does this section already exist?
if ($break !== false) {
$break->set('name', $data['name']);
} else {
$break = new section_break(0, (object) $data);
$break->set('courseid', $courseid);
}

$break->save();
}

/**
* Dummy method
*
* @param mixed $data
* @param array $data
* @return void
*/
public function process_cards($data) {
public function process_cardimage(array $data): void {
// No-op.
}

/**
* Dummy method
*
* @param array $data
* @return void
*/
public function process_cards(array $data) {
// No-op.
}

Expand All @@ -61,7 +115,7 @@ public function process_cards($data) {
* @throws file_exception
* @throws stored_file_creation_exception
*/
public function after_restore_section() {
public function after_restore_section(): void {
global $DB;
$data = $this->connectionpoint->get_data();

Expand All @@ -77,7 +131,7 @@ public function after_restore_section() {
$newcourseid = $this->step->get_task()->get_courseid();
$newsectionid = $DB->get_field('course_sections', 'id', [
'course' => $newcourseid,
'section' => $oldsectionnum
'section' => $oldsectionnum,
]);

if (!$newsectionid) {
Expand All @@ -98,7 +152,7 @@ public function after_restore_section() {
* @throws file_exception
* @throws stored_file_creation_exception
*/
private static function move_section_image(int $newcourseid, int $oldsectionid, int $newsectionid) {
private static function move_section_image(int $newcourseid, int $oldsectionid, int $newsectionid): void {
$filestorage = get_file_storage();
$context = context_course::instance($newcourseid);

Expand Down Expand Up @@ -160,9 +214,7 @@ private static function move_section_image(int $newcourseid, int $oldsectionid,
}

$movedimage = $filestorage->create_file_from_storedfile(
[
'itemid' => $newsectionid
],
[ 'itemid' => $newsectionid ],
$restoredimage
);

Expand All @@ -179,4 +231,13 @@ private static function move_section_image(int $newcourseid, int $oldsectionid,
);
}
}

/**
* All the other cool format plugins have this.
*
* @return void
*/
protected function after_execute_structure(): void {
// No-op.
}
}
50 changes: 50 additions & 0 deletions classes/section_break.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace format_cards;

use cache;
use cache_store;
use core\persistent;
use dml_exception;
use section_info;
Expand Down Expand Up @@ -57,6 +59,54 @@ protected static function define_properties() {
];
}

/**
* Reset the course section break cache after a new one is created
*
* @return void
*/
public function after_create(): void {
parent::after_create();
self::purge_break_cache($this->get('courseid'));
}

/**
* Reset the course section break cache after one is deleted
*
* @param bool $result
* @return void
*/
public function after_delete($result): void {
parent::after_delete($result);
self::purge_break_cache($this->get('courseid'));
}

/**
* Reset the course section break cache after one is updated
*
* @param bool $result
* @return void
*/
public function after_update($result): void {
parent::after_update($result);
self::purge_break_cache($this->get('courseid'));
}

/**
* Purge the section break cache for a given course
*
* @param int $courseid
* @return void
*/
private static function purge_break_cache(int $courseid): void {
$cache = cache::make_from_params(
cache_store::MODE_APPLICATION,
'format_cards',
'section_breaks'
);

$cache->delete($courseid);
}

/**
* Get all the section breaks for a course
*
Expand Down

0 comments on commit 3563dda

Please sign in to comment.