Skip to content

Commit

Permalink
#111: Fix restore issue
Browse files Browse the repository at this point in the history
Fix restore settings that cause restore issue

Added moodle log when a sharing cart item got backup, restored or deleted
  • Loading branch information
sampraxis committed Dec 19, 2023
1 parent 83b5596 commit 6dbd6fd
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 79 deletions.
22 changes: 8 additions & 14 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,25 @@ jobs:
strategy:
matrix:
include:
## Moodle 4.1+ with PHP 8.0
- os: ubuntu-22.04
php: 8.0
db: mysqli
moodle: MOODLE_401_STABLE
experimental: false
# Future supported versions
## Moodle 4.1+ with PHP 8.1
- os: ubuntu-22.04
php: 8.1
db: mysqli
moodle: MOODLE_401_STABLE
experimental: true
## Moodle development version with PHP 8.1
## Moodle 4.2+ with PHP 8.2
- os: ubuntu-22.04
php: 8.1
php: 8.2
db: mysqli
moodle: master
moodle: MOODLE_402_STABLE
experimental: true
## Moodle 4.1+ with PHP 8.2
## Moodle 4.3+ with PHP 8.2
- os: ubuntu-22.04
php: 8.2
db: mysqli
moodle: MOODLE_401_STABLE
moodle: MOODLE_403_STABLE
experimental: true
# Future supported versions
## Moodle development version with PHP 8.2
- os: ubuntu-22.04
php: 8.2
Expand Down Expand Up @@ -68,13 +62,13 @@ jobs:
ini-values: 'max_input_vars=5000'

- name: Checking out code from moodle/moodle
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
repository: moodle/moodle
ref: ${{ matrix.moodle }}

- name: Check out code from ${{ github.repository }}
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: ${{ github.workspace }}/blocks/sharing_cart
ref: ${{ github.ref }}
Expand Down
61 changes: 0 additions & 61 deletions .travis.yml

This file was deleted.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Warning: PHP versions 7.2 and older are deprecated, and will cause problems, unr

Change Log
----------
* 4.3, release 2 2023.12.15
* Fixed sharing cart restore process.
* Added moodle log when a sharing cart item got backup, restored or deleted.
When the backup file has user completion data but the backup file has no user data.
It causes Moodle try to restore something that does not exist.
* 4.3, release 1 2023.11.01
* Adapted Sharing Cart to new core Moodle 4.3 Backup feature which allows backup without editing the backup.
* 4.2, release skipped.
Expand Down
40 changes: 38 additions & 2 deletions classes/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
namespace block_sharing_cart;

use backup_controller;
use block_sharing_cart\event\backup_activity_created;
use block_sharing_cart\event\backup_activity_started;
use block_sharing_cart\event\restore_activity_created;
use block_sharing_cart\event\restore_activity_started;
use block_sharing_cart\event\section_backedup;
use block_sharing_cart\event\section_deleted;
use block_sharing_cart\event\section_restored;
use block_sharing_cart\event\sharing_cart_item_deleted;
use block_sharing_cart\exceptions\no_backup_support_exception;
use block_sharing_cart\repositories\course_repository;
use cache_helper;
Expand Down Expand Up @@ -215,6 +220,11 @@ public function backup(
// THIS FILE REQUIRES $CFG, DO NOT REMOVE IT
require_once __DIR__ . '/../../../backup/util/includes/backup_includes.php';

backup_activity_started::create_by_course_module_id(
$course,
$cmid
)->trigger();

// validate parameters and capabilities
$cm = \get_coursemodule_from_id(null, $cmid, 0, false, MUST_EXIST);
$context = \context_module::instance($cm->id);
Expand Down Expand Up @@ -295,7 +305,15 @@ public function backup(
'course' => $course,
'section' => $section
));
return $record->insert();
$id = $record->insert();

backup_activity_created::create_by_course_module_id(
$course,
$cmid,
$id
)->trigger();

return $id;
}

/**
Expand Down Expand Up @@ -507,6 +525,8 @@ public function restore($id, $courseid, $sectionnumber): void {
require_once __DIR__ . '/../../../backup/util/includes/restore_includes.php';
require_once __DIR__ . '/../backup/util/helper/restore_fix_missings_helper.php';

restore_activity_started::create_by_sharing_cart_backup_id($id)->trigger();

// cleanup temporary files when we exit this scope
$tempfiles = array();
$scope = new scoped(function() use (&$tempfiles) {
Expand Down Expand Up @@ -551,6 +571,15 @@ public function restore($id, $courseid, $sectionnumber): void {
if ($task->setting_exists('overwrite_conf')) {
$task->get_setting('overwrite_conf')->set_value(false);
}
if ($task->setting_exists('userscompletion')) {
$has_user_data = false;
if ($task->setting_exists('user')) {
$has_user_data = (bool)$task->get_setting('userscompletion')->get_value();
}
if (!$has_user_data) {
$task->get_setting('userscompletion')->set_value(false);
}
}
}
if (\get_config('block_sharing_cart', 'workaround_qtypes')) {
\restore_fix_missings_helper::fix_plan($controller->get_plan());
Expand All @@ -567,6 +596,12 @@ public function restore($id, $courseid, $sectionnumber): void {
// Fire event.
$event = \core\event\course_module_created::create_from_cm($cm);
$event->trigger();

restore_activity_created::create_by_course_module_id(
$course->id,
$cmid,
$id
)->trigger();
}
}
\rebuild_course_cache($course->id);
Expand Down Expand Up @@ -726,8 +761,9 @@ public function delete($id): void {

$storage = new storage();
$storage->delete($record->filename);

$record->delete();

sharing_cart_item_deleted::create_by_sharing_cart_item_id($id, $record->course)->trigger();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions classes/event/backup_activity_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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/>.

/**
* Sharing Cart
*
* @package block_sharing_cart
* @copyright 2017 (C) VERSION2, INC.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_sharing_cart\event;


// @codeCoverageIgnoreStart
defined('MOODLE_INTERNAL') || die();
// @codeCoverageIgnoreEnd


class backup_activity_created extends base
{
public function get_description(): string
{
return "User with id {$this->userid} created a backup of the activity with course module id {$this->get_course_module_id()}";
}
}
44 changes: 44 additions & 0 deletions classes/event/backup_activity_started.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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/>.

/**
* Sharing Cart
*
* @package block_sharing_cart
* @copyright 2017 (C) VERSION2, INC.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace block_sharing_cart\event;


// @codeCoverageIgnoreStart
defined('MOODLE_INTERNAL') || die();
// @codeCoverageIgnoreEnd


class backup_activity_started extends base
{
protected function get_crud(): string
{
return static::CRUD_READ;
}

public function get_description(): string
{
return "User with id {$this->userid} started a backup of the activity with course module id {$this->get_course_module_id()}";
}
}
Loading

0 comments on commit 6dbd6fd

Please sign in to comment.