Skip to content

Commit 00533ad

Browse files
committed
Add backup functionality
1 parent 53d861a commit 00533ad

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
// This file is part of Moodle - http://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17+
18+
/**
19+
* @package block_vmchecker
20+
* @copyright 2022 Mihai Baruta <baruta.mihai99@gmail.com>
21+
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 or later
22+
*/
23+
24+
// This activity has not particular settings but the inherited from the generic
25+
// backup_activity_task so here there isn't any class definition, like the ones
26+
// existing in /backup/moodle2/backup_settingslib.php (activities section)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once($CFG->dirroot . '/blocks/vmchecker/backup/moodle2/backup_vmchecker_stepslib.php');
4+
5+
/**
6+
* vmchecker backup task that provides all the settings and steps to perform one
7+
* complete backup of the activity
8+
*
9+
* @package block_vmchecker
10+
* @copyright 2022 Mihai Baruta <baruta.mihai99@gmail.com>
11+
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 or later
12+
*/
13+
class backup_vmchecker_activity_task extends backup_activity_task {
14+
15+
/**
16+
* Define (add) particular settings this activity can have
17+
*/
18+
protected function define_my_settings() {
19+
// No particular settings for this activity
20+
}
21+
22+
/**
23+
* Define (add) particular steps this activity can have
24+
*/
25+
protected function define_my_steps() {
26+
// No particular steps for this activity
27+
}
28+
29+
/**
30+
* Code the transformations to perform in the activity in
31+
* order to get transportable (encoded) links
32+
*/
33+
static public function encode_content_links($content) {
34+
return $content;
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* Define all the backup steps that will be used by the backup_vmchecker_activ
5+
*
6+
* @package block_vmchecker
7+
* @copyright 2022 Mihai Baruta <baruta.mihai99@gmail.com>
8+
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 or laterity_task
9+
*/
10+
class backup_vmchecker_activity_structure_step extends backup_activity_structure_step {
11+
12+
protected function define_structure() {
13+
return null;
14+
}
15+
}

block_vmchecker.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,40 @@ private function process_submit_form(block_vmchecker\form\submit_form $form) {
161161
return true;
162162
}
163163

164+
165+
/**
166+
* Loads the assignment of the current block.
167+
* @param int $assignmentid
168+
* @param int $courseid
169+
* @return \assign|null
170+
*/
171+
private function load_assignment(int $assignmentid, int $courseid): \assign|null {
172+
if ($assignmentid == null) {
173+
return null;
174+
}
175+
176+
$context = null;
177+
try {
178+
$course_info = get_fast_modinfo($courseid);
179+
foreach ($course_info->instances['assign'] as $instance) {
180+
if ($instance->instance != $assignmentid) {
181+
continue;
182+
}
183+
184+
$context = \context_module::instance($instance->id);
185+
break;
186+
}
187+
} catch (dml_missing_record_exception | dml_multiple_records_exception $e) {
188+
return null;
189+
}
190+
191+
if (!$context) {
192+
return null;
193+
}
194+
195+
return new \assign($context, null, null);
196+
}
197+
164198
/**
165199
* Get the HTML content for the block.
166200
* @return string
@@ -178,27 +212,12 @@ public function get_content() {
178212

179213
$this->content = new stdClass();
180214

181-
if ($this->config->assignment == null) {
215+
$assign = $this->load_assignment($this->config->assignment, $this->page->course->id);
216+
if ($assign == null) {
182217
$this->content->text = get_string('no_assignment_selected', 'block_vmchecker');
183218
return $this->content;
184219
}
185220

186-
try {
187-
$course_info = get_fast_modinfo($this->page->course->id);
188-
foreach ($course_info->instances['assign'] as $instance) {
189-
if ($instance->instance == $this->config->assignment) {
190-
$cm = $instance;
191-
break;
192-
}
193-
}
194-
} catch (dml_missing_record_exception | dml_multiple_records_exception $e) {
195-
$this->content->text = get_string('no_assignment_selected', 'block_vmchecker');
196-
return $this->content;
197-
}
198-
$context = \context_module::instance($cm->id);
199-
200-
$assign = new \assign($context, null, null);
201-
202221
$this->set_title($assign);
203222
$backendurl = get_config('block_vmchecker', 'backend');
204223
$api = new \block_vmchecker\backend\api($backendurl);
@@ -316,6 +335,10 @@ public function instance_create() {
316335
public function instance_config_save($data, $nolongerused = false) {
317336
global $DB;
318337

338+
if (!$DB->record_exists('block_vmchecker_options', array('blockinstanceid' => $this->instance->id))) {
339+
$this->instance_create();
340+
}
341+
319342
parent::instance_config_save($data, $nolongerused);
320343
$DB->update_record('block_vmchecker_options', [
321344
'id' => $DB->get_record('block_vmchecker_options', array('blockinstanceid' => $this->instance->id), 'id')->id,

0 commit comments

Comments
 (0)