mirrored from git://git.moodle.org/moodle.git
/
external.php
189 lines (170 loc) · 7.01 KB
/
external.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?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/>.
/**
* This is the external API for this component.
*
* @package tool_moodlenet
* @copyright 2020 Mathew May {@link https://mathew.solutions}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_moodlenet;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir .'/externallib.php');
require_once($CFG->libdir . '/filelib.php');
require_once(__DIR__ . '/../lib.php');
use core_course\external\course_summary_exporter;
use external_api;
use external_function_parameters;
use external_value;
use external_single_structure;
/**
* This is the external API for this component.
*
* @copyright 2020 Mathew May {@link https://mathew.solutions}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends external_api {
/**
* verify_webfinger parameters
*
* @return external_function_parameters
*/
public static function verify_webfinger_parameters() {
return new external_function_parameters(
array(
'profileurl' => new external_value(PARAM_NOTAGS, 'The profile url that the user has given us', VALUE_REQUIRED),
'course' => new external_value(PARAM_INT, 'The course we are adding to', VALUE_REQUIRED),
'section' => new external_value(PARAM_INT, 'The section within the course we are adding to', VALUE_REQUIRED),
)
);
}
/**
* Figure out if the passed content resolves with a WebFinger account.
*
* @param string $profileurl The profile url that the user states exists
* @param int $course The course we are adding to
* @param int $section The section within the course we are adding to
* @return array Contains the result and domain if any
* @throws \invalid_parameter_exception
*/
public static function verify_webfinger(string $profileurl, int $course, int $section) {
global $USER;
$params = self::validate_parameters(self::verify_webfinger_parameters(), [
'profileurl' => $profileurl,
'section' => $section,
'course' => $course
]
);
try {
$mnetprofile = new moodlenet_user_profile($params['profileurl'], $USER->id);
} catch (\Exception $e) {
return [
'result' => false,
'message' => get_string('profilevalidationfail', 'tool_moodlenet'),
];
}
$userlink = profile_manager::get_moodlenet_profile_link($mnetprofile);
// There were no problems verifying the account so lets store it.
if ($userlink['result'] === true) {
profile_manager::save_moodlenet_user_profile($mnetprofile);
$userlink['domain'] = generate_mnet_endpoint($mnetprofile->get_profile_name(), $course, $section);
}
return $userlink;
}
/**
* verify_webfinger return.
*
* @return \external_description
*/
public static function verify_webfinger_returns() {
return new external_single_structure([
'result' => new external_value(PARAM_BOOL, 'Was the passed content a valid WebFinger?'),
'message' => new external_value(PARAM_TEXT, 'Our message for the user'),
'domain' => new external_value(PARAM_RAW, 'Domain to redirect the user to', VALUE_OPTIONAL),
]);
}
/**
* search_courses_parameters
*
* @return external_function_parameters
*/
public static function search_courses_parameters() {
return new external_function_parameters(
array(
'searchvalue' => new external_value(PARAM_RAW, 'search value'),
)
);
}
/**
* For some given input find and return any course that matches it.
*
* @param string $searchvalue The profile url that the user states exists
* @return array Contains the result set of courses for the value
*/
public static function search_courses(string $searchvalue) {
global $OUTPUT;
$params = self::validate_parameters(
self::search_courses_parameters(),
['searchvalue' => $searchvalue]
);
self::validate_context(\context_system::instance());
$courses = array();
if ($arrcourses = \core_course_category::search_courses(array('search' => $params['searchvalue']))) {
foreach ($arrcourses as $course) {
if (has_capability('moodle/course:manageactivities', \context_course::instance($course->id))) {
$data = new \stdClass();
$data->id = $course->id;
$data->fullname = $course->fullname;
$data->hidden = $course->visible;
$options = [
'course' => $course->id,
];
$viewurl = new \moodle_url('/admin/tool/moodlenet/options.php', $options);
$data->viewurl = $viewurl->out(false);
$category = \core_course_category::get($course->category);
$data->coursecategory = $category->name;
$courseimage = course_summary_exporter::get_course_image($data);
if (!$courseimage) {
$courseimage = $OUTPUT->get_generated_image_for_id($data->id);
}
$data->courseimage = $courseimage;
$courses[] = $data;
}
}
}
return array(
'courses' => $courses
);
}
/**
* search_courses_returns.
*
* @return \external_description
*/
public static function search_courses_returns() {
return new external_single_structure([
'courses' => new \external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'course id'),
'fullname' => new external_value(PARAM_TEXT, 'course full name'),
'hidden' => new external_value(PARAM_INT, 'is the course visible'),
'viewurl' => new external_value(PARAM_URL, 'Next step of import'),
'coursecategory' => new external_value(PARAM_TEXT, 'Category name'),
'courseimage' => new external_value(PARAM_RAW, 'course image'),
]))
]);
}
}