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 |
namespace mod_data\local\importer;
|
|
|
18 |
|
|
|
19 |
use mod_data\manager;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Data preset importer for existing presets
|
|
|
23 |
* @package mod_data
|
|
|
24 |
* @copyright 2022 Amaia Anabitarte <amaia@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
class preset_existing_importer extends preset_importer {
|
|
|
28 |
|
|
|
29 |
/** @var int user id. */
|
|
|
30 |
protected $userid;
|
|
|
31 |
|
|
|
32 |
/** @var string fullname of the preset. */
|
|
|
33 |
private $fullname;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Constructor
|
|
|
37 |
*
|
|
|
38 |
* @param manager $manager
|
|
|
39 |
* @param string $fullname
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(manager $manager, string $fullname) {
|
|
|
42 |
global $USER;
|
|
|
43 |
|
|
|
44 |
list($userid, $shortname) = explode('/', $fullname, 2);
|
|
|
45 |
$context = $manager->get_context();
|
|
|
46 |
if ($userid &&
|
|
|
47 |
($userid != $USER->id) &&
|
|
|
48 |
!has_capability('mod/data:manageuserpresets', $context) &&
|
|
|
49 |
!has_capability('mod/data:viewalluserpresets', $context)
|
|
|
50 |
) {
|
|
|
51 |
throw new \coding_exception('Invalid preset provided');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$this->userid = intval($userid);
|
|
|
55 |
$this->fullname = $fullname;
|
|
|
56 |
$cm = $manager->get_coursemodule();
|
|
|
57 |
$course = $cm->get_course();
|
|
|
58 |
$filepath = data_preset_path($course, $userid, $shortname);
|
|
|
59 |
parent::__construct($manager, $filepath);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Returns user ID
|
|
|
64 |
*
|
|
|
65 |
* @return int userid
|
|
|
66 |
*/
|
|
|
67 |
public function get_userid(): int {
|
|
|
68 |
return $this->userid;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Returns the information we need to build the importer selector.
|
|
|
73 |
*
|
|
|
74 |
* @return array Value and name for the preset importer selector
|
|
|
75 |
*/
|
|
|
76 |
public function get_preset_selector(): array {
|
|
|
77 |
return ['name' => 'fullname', 'value' => $this->get_userid().'/'.$this->get_directory()];
|
|
|
78 |
}
|
|
|
79 |
}
|