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 |
* mod_lti data generator
|
|
|
19 |
*
|
|
|
20 |
* @package mod_lti
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright Copyright (c) 2012 Moodlerooms Inc. (http://www.moodlerooms.com)
|
|
|
23 |
* @author Mark Nielsen
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
global $CFG;
|
|
|
30 |
require_once($CFG->dirroot . '/mod/lti/locallib.php');
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* LTI module data generator class
|
|
|
34 |
*
|
|
|
35 |
* @package mod_lti
|
|
|
36 |
* @category test
|
|
|
37 |
* @copyright Copyright (c) 2012 Moodlerooms Inc. (http://www.moodlerooms.com)
|
|
|
38 |
* @author Mark Nielsen
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class mod_lti_generator extends testing_module_generator {
|
|
|
42 |
|
|
|
43 |
public function create_instance($record = null, array $options = null) {
|
|
|
44 |
$record = (object) (array) $record;
|
|
|
45 |
|
|
|
46 |
if (!isset($record->toolurl)) {
|
|
|
47 |
$record->toolurl = '';
|
|
|
48 |
} else {
|
|
|
49 |
$toolurl = new moodle_url($record->toolurl);
|
|
|
50 |
$record->toolurl = $toolurl->out(false);
|
|
|
51 |
}
|
|
|
52 |
if (!isset($record->resourcekey)) {
|
|
|
53 |
$record->resourcekey = '12345';
|
|
|
54 |
}
|
|
|
55 |
if (!isset($record->password)) {
|
|
|
56 |
$record->password = 'secret';
|
|
|
57 |
}
|
|
|
58 |
if (!isset($record->grade)) {
|
|
|
59 |
$record->grade = 100;
|
|
|
60 |
}
|
|
|
61 |
if (!isset($record->instructorchoicesendname)) {
|
|
|
62 |
$record->instructorchoicesendname = 1;
|
|
|
63 |
}
|
|
|
64 |
if (!isset($record->instructorchoicesendemailaddr)) {
|
|
|
65 |
$record->instructorchoicesendemailaddr = 1;
|
|
|
66 |
}
|
|
|
67 |
if (!isset($record->instructorchoiceacceptgrades)) {
|
|
|
68 |
$record->instructorchoiceacceptgrades = 1;
|
|
|
69 |
}
|
|
|
70 |
if (!isset($record->typeid)) {
|
|
|
71 |
$record->typeid = 0;
|
|
|
72 |
}
|
|
|
73 |
return parent::create_instance($record, (array)$options);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Create a tool proxy.
|
|
|
78 |
*
|
|
|
79 |
* @param array $config
|
|
|
80 |
*/
|
|
|
81 |
public function create_tool_proxies(array $config) {
|
|
|
82 |
if (!isset($config['capabilityoffered'])) {
|
|
|
83 |
$config['capabilityoffered'] = '';
|
|
|
84 |
}
|
|
|
85 |
if (!isset($config['serviceoffered'])) {
|
|
|
86 |
$config['serviceoffered'] = '';
|
|
|
87 |
}
|
|
|
88 |
lti_add_tool_proxy((object) $config);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Split type creation data into 'type' and 'config' components, based on input array key prefixes.
|
|
|
93 |
*
|
|
|
94 |
* The $data array contains both the type data and config data that will be passed to lti_add_type(). This must be split into
|
|
|
95 |
* two params (type, config) based on the array key prefixes ({@see lti_add_type()} for how the two params are handled):
|
|
|
96 |
* - NO prefix: denotes 'type' data.
|
|
|
97 |
* - 'lti_' prefix: denotes 'config' data.
|
|
|
98 |
* - 'ltiservice_' prefix: denotes 'config' data, specifically config for service plugins.
|
|
|
99 |
*
|
|
|
100 |
* @param array $data array of type and config data containing prefixed keys.
|
|
|
101 |
* @return array containing separated objects for type and config data. E.g. ['type' = stdClass, 'config' => stdClass]
|
|
|
102 |
*/
|
|
|
103 |
protected function get_type_and_config_from_data(array $data): array {
|
|
|
104 |
// Grab any non-prefixed fields; these are the type fields. The rest is considered config.
|
|
|
105 |
$type = array_filter(
|
|
|
106 |
$data,
|
|
|
107 |
fn($val, $key) => !str_contains($key, 'lti_') && !str_contains($key, 'ltiservice_'),
|
|
|
108 |
ARRAY_FILTER_USE_BOTH
|
|
|
109 |
);
|
|
|
110 |
$config = array_diff_key($data, $type);
|
|
|
111 |
|
|
|
112 |
return ['type' => (object) $type, 'config' => (object) $config];
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Create a tool type.
|
|
|
117 |
*
|
|
|
118 |
* @param array $data
|
|
|
119 |
* @return int ID of created tool
|
|
|
120 |
*/
|
|
|
121 |
public function create_tool_types(array $data): int {
|
|
|
122 |
if (!isset($data['baseurl'])) {
|
|
|
123 |
throw new coding_exception('Must specify baseurl when creating a LTI tool type.');
|
|
|
124 |
}
|
|
|
125 |
$data['baseurl'] = (new moodle_url($data['baseurl']))->out(false); // Permits relative URLs in behat features.
|
|
|
126 |
|
|
|
127 |
// Sensible defaults permitting the tool type to be used in a launch.
|
|
|
128 |
$data['lti_acceptgrades'] = $data['lti_acceptgrades'] ?? LTI_SETTING_ALWAYS;
|
|
|
129 |
$data['lti_sendname'] = $data['lti_sendname'] ?? LTI_SETTING_ALWAYS;
|
|
|
130 |
$data['lti_sendemailaddr'] = $data['lti_sendname'] ?? LTI_SETTING_ALWAYS;
|
|
|
131 |
$data['lti_launchcontainer'] = $data['lti_launchcontainer'] ?? LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
|
|
|
132 |
|
|
|
133 |
['type' => $type, 'config' => $config] = $this->get_type_and_config_from_data($data);
|
|
|
134 |
|
|
|
135 |
return lti_add_type(type: $type, config: $config);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Create a course tool type.
|
|
|
140 |
*
|
|
|
141 |
* @param array $type the type info.
|
|
|
142 |
* @return int ID of created tool.
|
|
|
143 |
* @throws coding_exception if any required fields are missing.
|
|
|
144 |
*/
|
|
|
145 |
public function create_course_tool_types(array $type): int {
|
|
|
146 |
global $SITE;
|
|
|
147 |
|
|
|
148 |
if (!isset($type['baseurl'])) {
|
|
|
149 |
throw new coding_exception('Must specify baseurl when creating a course tool type.');
|
|
|
150 |
}
|
|
|
151 |
if (!isset($type['course']) || $type['course'] == $SITE->id) {
|
|
|
152 |
throw new coding_exception('Must specify a non-site course when creating a course tool type.');
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
$type['baseurl'] = (new moodle_url($type['baseurl']))->out(false); // Permits relative URLs in behat features.
|
|
|
156 |
$type['coursevisible'] = $type['coursevisible'] ?? LTI_COURSEVISIBLE_ACTIVITYCHOOSER;
|
|
|
157 |
$type['state'] = LTI_TOOL_STATE_CONFIGURED; // The default for course tools.
|
|
|
158 |
|
|
|
159 |
// Sensible defaults permitting the tool type to be used in a launch.
|
|
|
160 |
$type['lti_acceptgrades'] = $type['lti_acceptgrades'] ?? LTI_SETTING_ALWAYS;
|
|
|
161 |
$type['lti_sendname'] = $type['lti_sendname'] ?? LTI_SETTING_ALWAYS;
|
|
|
162 |
$type['lti_sendemailaddr'] = $type['lti_sendemailaddr'] ?? LTI_SETTING_ALWAYS;
|
|
|
163 |
$type['lti_coursevisible'] = $type['coursevisible'] ?? LTI_COURSEVISIBLE_ACTIVITYCHOOSER;
|
|
|
164 |
$type['lti_launchcontainer'] = $type['lti_launchcontainer'] ?? LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS;
|
|
|
165 |
|
|
|
166 |
// Required for cartridge processing support.
|
|
|
167 |
$type['lti_toolurl'] = $type['baseurl'];
|
|
|
168 |
$type['lti_description'] = $type['description'] ?? '';
|
|
|
169 |
$type['lti_icon'] = $type['icon'] ?? '';
|
|
|
170 |
$type['lti_secureicon'] = $type['secureicon'] ?? '';
|
|
|
171 |
if (!empty($type['name'])) {
|
|
|
172 |
$type['lti_typename'] = $type['name'];
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
['type' => $type, 'config' => $config] = $this->get_type_and_config_from_data($type);
|
|
|
176 |
|
|
|
177 |
lti_load_type_if_cartridge($config);
|
|
|
178 |
return lti_add_type(type: $type, config: $config);
|
|
|
179 |
}
|
|
|
180 |
}
|