Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Restore instructions for the seb (Safe Exam Browser) quiz access subplugin.
19
 *
20
 * @package    quizaccess_seb
21
 * @category   backup
22
 * @author     Andrew Madden <andrewmadden@catalyst-au.net>
23
 * @copyright  2020 Catalyst IT
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
use quizaccess_seb\seb_quiz_settings;
1441 ariadna 28
use quizaccess_seb\settings_provider;
29
use quizaccess_seb\template;
1 efrain 30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
require_once($CFG->dirroot . '/mod/quiz/backup/moodle2/restore_mod_quiz_access_subplugin.class.php');
34
 
35
/**
36
 * Restore instructions for the seb (Safe Exam Browser) quiz access subplugin.
37
 *
38
 * @copyright  2020 Catalyst IT
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class restore_quizaccess_seb_subplugin extends restore_mod_quiz_access_subplugin {
42
 
43
    /**
44
     * Provides path structure required to restore data for seb quiz access plugin.
45
     *
46
     * @return array
47
     */
48
    protected function define_quiz_subplugin_structure() {
49
        $paths = [];
50
 
51
        // Quiz settings.
52
        $path = $this->get_pathfor('/quizaccess_seb_quizsettings'); // Subplugin root path.
53
        $paths[] = new restore_path_element('quizaccess_seb_quizsettings', $path);
54
 
55
        // Template settings.
56
        $path = $this->get_pathfor('/quizaccess_seb_quizsettings/quizaccess_seb_template');
57
        $paths[] = new restore_path_element('quizaccess_seb_template', $path);
58
 
59
        return $paths;
60
    }
61
 
62
    /**
63
     * Process the restored data for the quizaccess_seb_quizsettings table.
64
     *
65
     * @param stdClass $data Data for quizaccess_seb_quizsettings retrieved from backup xml.
66
     */
67
    public function process_quizaccess_seb_quizsettings($data) {
68
        global $DB, $USER;
69
 
70
        // Process quizsettings.
71
        $data = (object) $data;
72
        $data->quizid = $this->get_new_parentid('quiz'); // Update quizid with new reference.
73
        $data->cmid = $this->task->get_moduleid();
74
 
75
        unset($data->id);
76
        $data->timecreated = $data->timemodified = time();
77
        $data->usermodified = $USER->id;
78
 
1441 ariadna 79
        // Do not use template if it is no longer enabled.
80
        if ($data->requiresafeexambrowser == settings_provider::USE_SEB_TEMPLATE &&
81
                !$DB->record_exists(template::TABLE, ['id' => $data->templateid, 'enabled' => '1'])) {
82
            $data->templateid = 0;
83
            $data->requiresafeexambrowser = settings_provider::USE_SEB_NO;
84
        }
85
 
86
        $DB->insert_record(seb_quiz_settings::TABLE, $data);
87
 
1 efrain 88
        // Process attached files.
89
        $this->add_related_files('quizaccess_seb', 'filemanager_sebconfigfile', null);
90
    }
91
 
92
    /**
93
     * Process the restored data for the quizaccess_seb_template table.
94
     *
95
     * @param stdClass $data Data for quizaccess_seb_template retrieved from backup xml.
96
     */
97
    public function process_quizaccess_seb_template($data) {
98
        global $DB;
99
 
100
        $data = (object) $data;
101
 
102
        $quizid = $this->get_new_parentid('quiz');
103
 
104
        $template = null;
105
        if ($this->task->is_samesite()) {
106
            $template = \quizaccess_seb\template::get_record(['id' => $data->id]);
107
        } else {
108
            // In a different site, try to find existing template with the same name and content.
109
            $candidates = \quizaccess_seb\template::get_records(['name' => $data->name]);
110
            foreach ($candidates as $candidate) {
111
                if ($candidate->get('content') == $data->content) {
112
                    $template = $candidate;
113
                    break;
114
                }
115
            }
116
        }
117
 
118
        if (empty($template)) {
119
            unset($data->id);
120
            $template = new \quizaccess_seb\template(0, $data);
121
            $template->save();
122
        }
123
 
124
        // Update the restored quiz settings to use restored template.
1441 ariadna 125
        // Check if template is enabled before using it.
126
        if ($template->get('enabled')) {
127
            $DB->set_field(seb_quiz_settings::TABLE, 'templateid', $template->get('id'), ['quizid' => $quizid]);
128
        }
1 efrain 129
    }
130
 
131
}
132