Skip to content

Commit a2dbed9

Browse files
committed
WR455936: Simplify getting WS connection details
1 parent 0279193 commit a2dbed9

File tree

7 files changed

+147
-252
lines changed

7 files changed

+147
-252
lines changed

classes/helper.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525

2626
namespace assignsubmission_maharaws;
2727

28+
use assign;
2829
use stdClass;
2930

31+
defined('MOODLE_INTERNAL') || die();
32+
require_once($CFG->dirroot . "/mod/assign/locallib.php");
33+
3034
/**
3135
* Helper class.
3236
*/
@@ -110,6 +114,7 @@ public static function create_mahara_groups(stdClass $data): void {
110114
*
111115
* @param int $userid User to send the notification to.
112116
* @param stdClass $course Course the notification is associated with.
117+
* @param string $taskname Name of the task that failed.
113118
* @param string $messagebody The message.
114119
*/
115120
public static function send_notification(int $userid, stdClass $course, string $taskname, string $messagebody) {
@@ -149,4 +154,76 @@ public static function send_notification(int $userid, stdClass $course, string $
149154
}
150155
}
151156
}
157+
158+
/**
159+
* Helper function to get config values.
160+
*
161+
* @param ?assign $assignment
162+
* @param ?stdClass $maharagroup
163+
* @return stdClass
164+
*/
165+
public static function get_config_defaults(?assign $assignment = null, ?stdClass $maharagroup = null): stdClass {
166+
global $DB;
167+
// Get the global config.
168+
$config = get_config('assignsubmission_maharaws');
169+
170+
// If global settings are forced, return those.
171+
if (!empty($globalconfig->force_global_credentials)) {
172+
return $config;
173+
}
174+
175+
// Group sync tasks aren't linked to an assignment, so we try to find one based on the group being synced.
176+
if ($maharagroup && !$assignment) {
177+
// Try and work out the connection details.
178+
// We know the Mahara group therefore we have the institution, so we should be able to find an assignment from
179+
// the same course that has the same institution.
180+
$likevalue = $DB->sql_like('value', ':value');
181+
$result = $DB->get_records_sql(
182+
"SELECT assignment
183+
FROM {assign_plugin_config}
184+
WHERE name = 'groups'
185+
AND {$likevalue}",
186+
[
187+
'value' => '%' . $maharagroup->moodlegroup . '%',
188+
],
189+
0,
190+
1
191+
);
192+
193+
// If we have a result, get the assignment.
194+
if ($result) {
195+
$assignment = array_keys($result);
196+
[$course, $assignment] = get_course_and_cm_from_instance($assignment[0], 'assign');
197+
$context = \context_module::instance($assignment->id);
198+
$assignment = new assign($context, $assignment, $course);
199+
}
200+
}
201+
202+
// We have an assignment so override the global config with the custom assignment settings.
203+
if ($assignment && $assignment->has_instance()) {
204+
// Check if connection details are set at activity level.
205+
$assignment = $assignment->get_instance();
206+
$connectionvars = ['url', 'key', 'secret', 'institution'];
207+
[$insql, $inparams] = $DB->get_in_or_equal($connectionvars, SQL_PARAMS_NAMED);
208+
$sql = "SELECT name, value
209+
FROM {assign_plugin_config}
210+
WHERE assignment = :assign
211+
AND plugin = 'maharaws'
212+
AND subtype = 'assignsubmission'
213+
AND name $insql";
214+
$params = [
215+
'assign' => $assignment->id,
216+
];
217+
$params += $inparams;
218+
$result = $DB->get_records_sql($sql, $params);
219+
if ($result) {
220+
$config->url = $result['url']->value;
221+
$config->key = $result['key']->value;
222+
$config->secret = $result['secret']->value;
223+
$config->institution = $result['institution']->value;
224+
}
225+
}
226+
227+
return $config;
228+
}
152229
}

classes/observers.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function submission_graded(submission_graded $event) {
7171
$maharasubmission = $DB->get_record('assignsubmission_maharaws', ['submission' => $submission->id]);
7272

7373
// Process further only if we are dealing with mahara submission that is locked.
74-
if ($maharasubmission->viewstatus == assign_submission_maharaws::STATUS_SUBMITTED) {
74+
if ($maharasubmission && $maharasubmission->viewstatus == assign_submission_maharaws::STATUS_SUBMITTED) {
7575
// Check if marking workflow is in place, page unlocking will be handled in
7676
// assignsubmission_maharaws_observers::workflow_state_updated unless .
7777
if ($assign->get_instance()->markingworkflow) {
@@ -115,7 +115,7 @@ public static function workflow_state_updated(workflow_state_updated $event) {
115115
$maharasubmission = $DB->get_record('assignsubmission_maharaws', ['submission' => $submission->id]);
116116

117117
// Process further only if we are dealing with mahara submission that is locked.
118-
if ($maharasubmission->viewstatus == assign_submission_maharaws::STATUS_SUBMITTED) {
118+
if ($maharasubmission && $maharasubmission->viewstatus == assign_submission_maharaws::STATUS_SUBMITTED) {
119119
// Check marking workflow state, only unlock page if marks are released.
120120
if ($eventdata['other']['newstate'] !== ASSIGN_MARKING_WORKFLOW_STATE_RELEASED) {
121121
return;
@@ -170,7 +170,7 @@ public static function mahara_group_update(base $event): void {
170170
$groupname = get_string('groups:groupdeleted:name', 'assignsubmission_maharaws', $data['objectid']);
171171
}
172172

173-
$connection = webservice::get_connection_details($maharagroup);
173+
$config = helper::get_config_defaults(null, $maharagroup);
174174
$params = [
175175
'groups' => [[
176176
'id' => $maharagroup->maharagroup,
@@ -184,7 +184,7 @@ public static function mahara_group_update(base $event): void {
184184
];
185185

186186
try {
187-
webservice::call('mahara_group_update_groups_details', $params, $connection);
187+
webservice::call('mahara_group_update_groups_details', $params, $config);
188188
} catch (Exception $e) {
189189
\core\notification::add(
190190
get_string(

classes/task/create_group.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function execute() {
6565
// If not try and create the group.
6666
if ($maharagroup && $maharagroup->maharagroup == 0) {
6767
$course = get_course($group->courseid);
68-
$connection = webservice::get_connection_details($maharagroup);
68+
$config = helper::get_config_defaults(null, $maharagroup);
6969
$errormessage = '';
7070

7171
try {
@@ -82,7 +82,7 @@ public function execute() {
8282
),
8383
'shortname' => strtolower('moodlemanagedgroup' . $group->id),
8484
'description' => get_string('groups:groupdesc', 'assignsubmission_maharaws'),
85-
'institution' => $connection->institution,
85+
'institution' => $config->institution,
8686
'grouptype' => 'course',
8787
'category' => get_string('groups:category', 'assignsubmission_maharaws'),
8888
'forcecategory' => 1,
@@ -104,7 +104,7 @@ public function execute() {
104104
],
105105
],
106106
],
107-
$connection
107+
$config
108108
);
109109

110110
foreach ($result as $r) {

classes/task/sync_member.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public function execute(): void {
8686
* @return string
8787
*/
8888
public static function member_sync(stdClass $maharagroup, string $username, string $action): string {
89-
$connection = webservice::get_connection_details($maharagroup);
90-
$result = self::check_mahara_user_exists($username, $action, $connection);
89+
$config = helper::get_config_defaults(null, $maharagroup);
90+
$result = self::check_mahara_user_exists($username, $action, $config);
9191
$errormessage = '';
9292

9393
if (is_string($result)) {
@@ -112,7 +112,7 @@ public static function member_sync(stdClass $maharagroup, string $username, stri
112112
try {
113113
mtrace('Updating group membership for: ' . $username .
114114
' | Group: ' . $group->name);
115-
webservice::call('mahara_group_update_group_members', $params, $connection);
115+
webservice::call('mahara_group_update_group_members', $params, $config);
116116
} catch (\Exception $e) {
117117
$errormessage = 'Group membership not updated for ' . $username .
118118
' | Group: ' . $group->name . ' | Error' . strstr($e->getMessage(), ':');

classes/webservice.php

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,116 +25,42 @@
2525

2626
namespace assignsubmission_maharaws;
2727

28+
use assignsubmission_maharaws\helper;
2829
use stdClass;
2930
use Exception;
3031

3132
/**
3233
* Web service class.
3334
*/
3435
class webservice {
35-
/**
36-
* Get the connection details for the webservice.
37-
*
38-
* @param stdClass $moodlegroup The Moodle/Mahara group.
39-
* @return stdClass
40-
*/
41-
public static function get_connection_details(stdClass $moodlegroup): stdClass {
42-
global $DB;
43-
// Try and work out the connection details.
44-
// We know the Mahara group therefore we have the institution, so we should be able to find an assignment from
45-
// the same course that has the same institution.
46-
$institution = $DB->get_record('assignsubmission_maharawsgroup', ['moodlegroup' => $moodlegroup->moodlegroup]);
47-
$config = $DB->get_records_sql(
48-
"SELECT *
49-
FROM {assign_plugin_config}
50-
WHERE name = ?
51-
AND value = ?",
52-
['institution', $institution->institution],
53-
0,
54-
1
55-
);
56-
57-
$connectiondetails = new stdClass();
58-
if ($config) {
59-
// Get the connection details.
60-
$config = array_pop($config);
61-
$connection = $DB->get_records_sql(
62-
"SELECT *
63-
FROM {assign_plugin_config}
64-
WHERE assignment = ?
65-
AND name IN ('institution', 'url', 'secret', 'key')",
66-
[$config->assignment]
67-
);
68-
69-
foreach ($connection as $detail) {
70-
if ($detail->name == 'url') {
71-
$connectiondetails->url = $detail->value;
72-
}
73-
if ($detail->name == 'key') {
74-
$connectiondetails->key = $detail->value;
75-
}
76-
if ($detail->name == 'secret') {
77-
$connectiondetails->secret = $detail->value;
78-
}
79-
if ($detail->name == 'institution') {
80-
$connectiondetails->institution = $detail->value;
81-
}
82-
}
83-
} else {
84-
// Fall back to global config.
85-
$connectiondetails->url = get_config('assignsubmission_maharaws', 'url');
86-
$connectiondetails->key = get_config('assignsubmission_maharaws', 'key');
87-
$connectiondetails->secret = get_config('assignsubmission_maharaws', 'secret');
88-
$connectiondetails->institution = get_config('assignsubmission_maharaws', 'institution');
89-
}
90-
91-
if (empty($connectiondetails->url)) {
92-
throw new Exception("The Mahara URL is not set correctly.");
93-
}
94-
if (empty($connectiondetails->key)) {
95-
throw new Exception("The Mahara Key is not set correctly.");
96-
}
97-
if (empty($connectiondetails->secret)) {
98-
throw new Exception("The Mahara secret is not set correctly.");
99-
}
100-
if (empty($connectiondetails->institution)) {
101-
throw new Exception("The Mahara institution is not set correctly.");
102-
}
103-
104-
return $connectiondetails;
105-
}
106-
10736
/**
10837
* Call the Mahara web service.
10938
*
11039
* @param string $function The Mahara web service function being called.
11140
* @param array $params The data being updated in Mahara.
112-
* @param stdClass $connection The connection details.
41+
* @param stdClass $config Contains connection details.
11342
* @param string $method
11443
* @return mixed
11544
*/
11645
public static function call(
11746
string $function,
11847
array $params,
119-
stdClass $connection,
48+
stdClass $config,
12049
string $method = "POST"
12150
): mixed {
12251
global $CFG;
123-
$url = $connection->url;
124-
$key = $connection->key;
125-
$secret = $connection->secret;
12652

127-
$endpoint = $url .
128-
(preg_match('/\/$/', $url) ? '' : '/') . // Append a trailing slash if $url doesn't already have one.
53+
$endpoint = $config->url .
54+
(preg_match('/\/$/', $config->url) ? '' : '/') . // Append a trailing slash if $url doesn't already have one.
12955
'webservice/rest/server.php';
13056
$args = [
131-
'oauth_consumer_key' => $key,
132-
'oauth_consumer_secret' => $secret,
57+
'oauth_consumer_key' => $config->key,
58+
'oauth_consumer_secret' => $config->secret,
13359
'oauth_callback' => 'about:blank',
13460
'api_root' => $endpoint,
13561
];
13662

137-
$client = new \assignsubmission_maharaws\mahara_oauth($args);
63+
$client = new mahara_oauth($args);
13864
if (!empty($CFG->disablesslchecks)) {
13965
$options = ['CURLOPT_SSL_VERIFYPEER' => 0, 'CURLOPT_SSL_VERIFYHOST' => 0];
14066
$client->setup_oauth_http_options($options);
@@ -150,7 +76,7 @@ public static function call(
15076
$endpoint,
15177
array_merge($params, ['wsfunction' => $function, 'alt' => 'json']),
15278
null,
153-
$secret
79+
$config->secret
15480
);
15581
$data = json_decode($content, true);
15682

launch.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

25+
use assignsubmission_maharaws\helper;
26+
2527
require("../../../../config.php");
2628

2729
require_once($CFG->dirroot . '/mod/lti/lib.php');
@@ -47,10 +49,11 @@
4749
$extuserusername = $USER->username;
4850
$assign = new assign($context, $cm, $course);
4951
$maharasubmission = new assign_submission_maharaws($assign, 'assignsubmission_maharaws');
50-
if (get_config('assignsubmission_maharaws', 'legacy_ext_usr_username')) {
52+
$config = helper::get_config_defaults($assign);
53+
if ($config->legacy_ext_usr_username) {
5154
// Determine the Mahara field and the username value.
52-
$usernameattribute = $maharasubmission->get_config_default('username_attribute');
53-
$remoteuser = $maharasubmission->get_config_default('remoteuser');
55+
$usernameattribute = $config->username_attribute;
56+
$remoteuser = $config->remoteuser;
5457
$username = (!empty($CFG->mahara_test_user) ? $CFG->mahara_test_user : $USER->{$usernameattribute});
5558
$field =
5659
// Now the trump all - we actually want to test against the institutions auth instances remoteuser.
@@ -92,19 +95,15 @@
9295
'wsfunction' => 'module_lti_launch',
9396
];
9497

95-
96-
$endpoint = $maharasubmission->get_config_default('url');
97-
$endpoint = $endpoint . (preg_match('/\/$/', $endpoint) ? '' : '/') . 'webservice/rest/server.php';
98-
$key = $maharasubmission->get_config_default('key');
99-
$secret = $maharasubmission->get_config_default('secret');
100-
101-
$parms = lti_sign_parameters($requestparams, $endpoint, "POST", $key, $secret);
102-
$debuglaunch = $maharasubmission->get_config_default('debug');
103-
if ($debuglaunch) {
104-
$parms['ext_submit'] = 'Launch';
105-
}
98+
$config = helper::get_config_defaults($assign);
99+
$endpoint = $config->url . (preg_match('/\/$/', $config->url) ? '' : '/') . 'webservice/rest/server.php';
100+
$parms = lti_sign_parameters($requestparams, $endpoint, "POST", $config->key, $config->secret);
106101
$endpointurl = new \moodle_url($endpoint);
107102
$endpointparams = $endpointurl->params();
103+
$debuglaunch = false;
104+
if (get_config('assignsubmission_maharaws', 'debuglaunch')) {
105+
$debuglaunch = get_config('assignsubmission_maharaws', 'debuglaunch');
106+
}
108107
$content = lti_post_launch_html($parms, $endpoint, $debuglaunch);
109108

110109
echo $content;

0 commit comments

Comments
 (0)