Skip to content

Commit

Permalink
feat #5: Enable backup retrieval after course deletion by storing met…
Browse files Browse the repository at this point in the history
…adata
  • Loading branch information
Vithusha Kethiri committed Oct 21, 2024
1 parent 981b22f commit 59ce35c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 11 deletions.
22 changes: 15 additions & 7 deletions classes/lifecycle/course_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Table class for handling course backup data.
*
* @package tool_lcbackupcoursestep
* @copyright 2024 Catalyst
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_lcbackupcoursestep\lifecycle;

defined('MOODLE_INTERNAL') || die;
Expand Down Expand Up @@ -52,29 +60,29 @@ public function __construct($filterdata = []) {

// Build the SQL.
$fields = 'f.id as id,
co.id as courseid, co.shortname as courseshortname, co.fullname as coursefullname,
f.filename as filename, f.filesize as filesize, f.timecreated as createdat';
md.oldcourseid as courseid, md.shortname as courseshortname, md.fullname as coursefullname,
f.filename as filename, f.filesize as filesize, f.timecreated as createdat';
$from = '{files} f
JOIN {context} c ON c.id = f.contextid
JOIN {course} co ON co.id = c.instanceid';
JOIN {context} c ON c.id = f.contextid
LEFT JOIN {tool_lcbackupcoursestep_metadata} md ON f.id = md.fileid';

$where = ["f.component = :component AND filename <> '.'"];
$params = ['component' => 'tool_lcbackupcoursestep'];

// Filtering.
if ($filterdata) {
if ($filterdata->shortname) {
$where[] = $DB->sql_like('co.shortname', ':shortname', false, false);
$where[] = $DB->sql_like('md.shortname', ':shortname', false, false);
$params['shortname'] = '%' . $DB->sql_like_escape($filterdata->shortname) . '%';
}

if ($filterdata->fullname) {
$where[] = $DB->sql_like('co.fullname', ':fullname', false, false);
$where[] = $DB->sql_like('md.fullname', ':fullname', false, false);
$params['fullname'] = '%' . $DB->sql_like_escape($filterdata->fullname) . '%';
}

if ($filterdata->courseid) {
$where[] = 'co.id = :courseid';
$where[] = 'md.oldcourseid = :courseid';
$params['courseid'] = $filterdata->courseid;
}
}
Expand Down
22 changes: 20 additions & 2 deletions classes/lifecycle/step.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Step for backing up a course in the lifecycle process.
*
* @package tool_lcbackupcoursestep
* @copyright 2024 Catalyst
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_lcbackupcoursestep\lifecycle;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -66,6 +74,8 @@ public function get_plugin_description() {
* @return step_response
*/
public function process_course($processid, $instanceid, $course) {
global $DB;

$courseid = $course->id;

// Get backup settings.
Expand Down Expand Up @@ -110,7 +120,7 @@ public function process_course($processid, $instanceid, $course) {
if (!empty($file)) {
// Prepare file record.
$filerecord = [
'contextid' => \context_course::instance($courseid)->id,
'contextid' => \context_system::instance()->id,
'component' => 'tool_lcbackupcoursestep',
'filearea' => 'course_backup',
'itemid' => $instanceid,
Expand All @@ -120,7 +130,15 @@ public function process_course($processid, $instanceid, $course) {

// Save file.
$fs = get_file_storage();
$fs->create_file_from_storedfile($filerecord, $file);
$newfile = $fs->create_file_from_storedfile($filerecord, $file);

$DB->insert_record('tool_lcbackupcoursestep_metadata', [
'shortname' => $course->shortname,
'fullname' => $course->fullname,
'oldcourseid' => $course->id,
'fileid' => $newfile->get_id(),
'timecreated' => time()
]);

// Delete file.
$file->delete();
Expand Down
22 changes: 22 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/lcbackupcoursestep/db" VERSION="2024101000" COMMENT="XMLDB file for Moodle tool/lcbackupcoursestep"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="tool_lcbackupcoursestep_metadata" COMMENT="Metadata for course backups.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="true"/>
<FIELD NAME="fullname" TYPE="char" LENGTH="255" NOTNULL="true"/>
<FIELD NAME="oldcourseid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="fileid" TYPE="int" LENGTH="10" NOTNULL="true"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" COMMENT="Timestamp when the backup was created"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="fileid_fk" TYPE="foreign" FIELDS="fileid" REFTABLE="files" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
92 changes: 92 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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/>.

/**
* Upgrade script for tool_lcbackupcoursestep.
*
* This function handles any necessary upgrades to the tool_lcbackupcoursestep plugin.
*
* @param int $oldversion The version of the plugin we are upgrading from.
* @return bool True if the upgrade was successful.
*/

function xmldb_tool_lcbackupcoursestep_upgrade($oldversion) {
global $DB;

if ($oldversion < 2024101000) {

$table = new xmldb_table('tool_lcbackupcoursestep_metadata');

$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('oldcourseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('fileid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);

$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('fileid_fk', XMLDB_KEY_FOREIGN, ['fileid'], 'files', ['id']);

if (!$DB->get_manager()->table_exists($table)) {
$DB->get_manager()->create_table($table);
}

$sql1 = "
INSERT INTO {tool_lcbackupcoursestep_metadata} (shortname, fullname, oldcourseid, fileid, timecreated)
SELECT crs.shortname,
crs.fullname,
crs.id AS oldcourseid,
f.id AS fileid,
f.timecreated
FROM {files} f
JOIN {context} ctx ON ctx.id = f.contextid
JOIN {course} crs ON ctx.instanceid = crs.id
WHERE ctx.contextlevel = :contextlevel
AND f.component = :component
AND f.filearea = :filearea
AND f.filesize > 0
";
$DB->execute($sql1, [
'contextlevel' => CONTEXT_COURSE,
'component' => 'tool_lcbackupcoursestep',
'filearea' => 'course_backup'
]);

$sql2 = "
UPDATE {files}
SET contextid = :contextid
WHERE id IN (
SELECT f.id
FROM {files} f
JOIN {context} ctx ON ctx.id = f.contextid
WHERE ctx.contextlevel = :contextlevel
AND f.component = :component
AND f.filearea = :filearea
)
";
$DB->execute($sql2, [
'contextid' => \context_system::instance()->id,
'contextlevel' => CONTEXT_COURSE,
'component' => 'tool_lcbackupcoursestep',
'filearea' => 'course_backup'
]);

upgrade_plugin_savepoint(true, 2024101000, 'tool', 'lcbackupcoursestep');
}

return true;
}

10 changes: 9 additions & 1 deletion tests/step_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function test_backup_course_step() {
$processor->process_courses();

// Check that the log file is created.
$contextid = \context_course::instance($this->course->id)->id;
$contextid = \context_system::instance()->id;
$sql = "contextid = :contextid
AND component = :component
AND filearea = :filearea
Expand Down Expand Up @@ -201,5 +201,13 @@ public function test_backup_course_step() {

// There is an assignment in the course.
$this->assertNotEmpty($DB->get_record('assign', ['course' => $courseid]));

// Check the metadata table for the file backup entry.
$metadata = $DB->get_record('tool_lcbackupcoursestep_metadata', ['fileid' => $file->id]);
$this->assertNotEmpty($metadata);

// Check metadata details.
$this->assertEquals($this->course->id, $metadata->oldcourseid);
$this->assertEquals($this->course->shortname, $metadata->shortname);
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

$plugin->version = 2023100400;
$plugin->version = 2024101000;
$plugin->requires = 2022041200;
$plugin->component = 'tool_lcbackupcoursestep';

Expand Down

0 comments on commit 59ce35c

Please sign in to comment.