Skip to content

Commit

Permalink
Merge pull request #20 from maths/Issue-19-Grade-synchronisation-is-n…
Browse files Browse the repository at this point in the history
…ot-working

Issue 19 grade synchronisation is not working
  • Loading branch information
sangwinc authored Nov 7, 2024
2 parents 55b6608 + cf87f1a commit 52819a0
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 24 deletions.
9 changes: 5 additions & 4 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static function create_username($consumerkey, $ltiuserid) {
$userkey = $consumerkey . ':' . $ltiuserid;
return 'enrol_lticoursetemplate' . sha1($consumerkey . '::' . $userkey);
} else {
throw new moodle_exception('usernamecreateerror', 'enrol_lticoursetemplate');
throw new \moodle_exception('usernamecreateerror', 'enrol_lticoursetemplate');
exit();
}
}
Expand Down Expand Up @@ -325,12 +325,12 @@ public static function get_lti_new_tool(

// Platform key is required.
if (!isset($platform)) {
throw new moodle_exception('invalidplatform', 'enrol_lticoursetemplate');
throw new \moodle_exception('invalidplatform', 'enrol_lticoursetemplate');
}

// Label (shortname) is required.
if (!isset($label)) {
throw new moodle_exception('invalidshortname', 'enrol_lticoursetemplate');
throw new \moodle_exception('invalidshortname', 'enrol_lticoursetemplate');
}

// Construct "unique" shortname.
Expand Down Expand Up @@ -373,7 +373,8 @@ public static function get_lti_new_tool(
'rolelearner' => $oldtool->rolelearner,
'provisioningmodelearner' => $oldtool->provisioningmodelearner,
'provisioningmodeinstructor' => $oldtool->provisioningmodeinstructor,
'membersyncmode' => $oldtool->membersyncmode
'membersyncmode' => $oldtool->membersyncmode,
'gradesync' => $oldtool->gradesync
]);

// Save the course in the plugin table.
Expand Down
22 changes: 16 additions & 6 deletions classes/local/ltiadvantage/lib/http_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct(\curl $curlclient) {
public function request(string $method, string $url, array $options): IHttpResponse {
$this->curlclient->resetHeader();
$this->curlclient->resetopt();

if (isset($options['headers'])) {
$headers = $options['headers'];
array_walk(
Expand All @@ -64,8 +65,11 @@ function(&$val, $key) {
);
$this->curlclient->setHeader($headers);
}

if ($method == 'POST') {
$res = $this->curlclient->post($url, $options['body'] ?? null, ['CURLOPT_HEADER' => 1]);
$body = $options['body'] ?? null;
$body = $body ?? (!empty($options['form_params']) ? http_build_query($options['form_params'], '' , '&') : null);
$res = $this->curlclient->post($url, $body, ['CURLOPT_HEADER' => 1]);
} else if ($method == 'GET') {
$res = $this->curlclient->get($url, [], ['CURLOPT_HEADER' => 1]);
} else {
Expand All @@ -77,28 +81,34 @@ function(&$val, $key) {
if (!$this->curlclient->get_errno() && !$this->curlclient->error) {
// No errors, so format the response.
$headersize = $info['header_size'];
$resheaders = substr($res, 0, $headersize);
$resbody = substr($res, $headersize);
$resheaders = substr($res ?? '', 0, $headersize);
$resbody = substr($res ?? '', $headersize);
$headerlines = array_filter(explode("\r\n", $resheaders));
$parsedresponseheaders = [
'httpstatus' => array_shift($headerlines)
];

foreach ($headerlines as $headerline) {
$headerbits = explode(':', $headerline, 2);

if (count($headerbits) == 2) {
// Only parse headers having colon separation.
$parsedresponseheaders[$headerbits[0]] = $headerbits[1];
}
}

$response = new http_response(['headers' => $parsedresponseheaders, 'body' => $resbody], intval($info['http_code']));

if ($response->getStatusCode() >= 400) {
throw new http_exception($response, "An HTTP error status was received: '{$response->getHeaders()['httpstatus']}'");
throw new http_exception($response, "An HTTP error status was received(orginal): '{$resbody}'");
}

return $response;
}

// The curl client experienced errors, so report that.
throw new \Exception("There was a cURL error when making the request: errno: {$this->curlclient->get_errno()},
error: {$this->curlclient->error}.");
error: {$this->curlclient->error}."
);
}
}
2 changes: 1 addition & 1 deletion classes/local/ltiadvantage/repository/user_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private function user_record_from_user(user $user): \stdClass {
}

/**
* Create the corresponding enrol_lti_user record from a user instance.
* Create the corresponding enrol_ct_users record from a user instance.
*
* @param user $user the user instance.
* @return \stdClass the record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class published_resources_table extends \table_sql {
* @param string $courseid The id of the course.
*/
public function __construct($courseid) {
parent::__construct('enrol_lti_manage_table');
parent::__construct('enrol_ct_manage_table');

$this->define_columns(array(
'name',
Expand Down
2 changes: 1 addition & 1 deletion classes/local/ltiadvantage/task/sync_grades.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected function get_line_item_label(\stdClass $resource, \context $context):
JOIN {enrol} e
ON (e.id = t.enrolid)
JOIN {course} c
ON {c.id} = e.courseid
ON (c.id = e.courseid)
WHERE t.id = :resourceid";
$coursename = $DB->get_field_sql($coursenamesql, ['resourceid' => $resource->id]);
$resourcename = format_string($coursename, true, ['context' => $context->id]);
Expand Down
2 changes: 1 addition & 1 deletion classes/manage_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class manage_table extends \table_sql {
* @param string $courseid The id of the course.
*/
public function __construct($courseid) {
parent::__construct('enrol_lticoursetemplate_manage_table');
parent::__construct('enrol_ct_manage_table');

$this->define_columns(array(
'name',
Expand Down
2 changes: 1 addition & 1 deletion classes/task/sync_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ protected function sync_member_information(stdClass $tool, ToolConsumer $consume

// Check if this user has already been registered in the enrol_ct_users table.
if (!$DB->record_exists('enrol_ct_users', ['toolid' => $tool->id, 'userid' => $user->id])) {
// Create an initial enrol_lti_user record that we can use later when syncing grades and members.
// Create an initial enrol_ct_users record that we can use later when syncing grades and members.
$userlog = new stdClass();
$userlog->userid = $user->id;
$userlog->toolid = $tool->id;
Expand Down
14 changes: 7 additions & 7 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,31 +554,31 @@ function xmldb_enrol_lticoursetemplate_upgrade($oldversion) {
// This applies to any LTI 2.0 user who has launched the tool (i.e. has lastaccess) and fixes a non-functional grade sync
// for LTI 2.0 consumers.
$sql = "SELECT lu.id, lc.secret
FROM {enrol_lti_users} lu
JOIN {enrol_lti_lti2_consumer} lc
FROM {enrol_ct_users} lu
JOIN {enrol_ct_lti2_consumer} lc
ON (" . $DB->sql_compare_text('lu.consumerkey', 255) . " = lc.consumerkey256)
WHERE lc.ltiversion = :ltiversion
AND " . $DB->sql_compare_text('lu.consumersecret') . " != lc.secret
AND lu.lastaccess IS NOT NULL";
$affectedltiusersrs = $DB->get_recordset_sql($sql, ['ltiversion' => 'LTI-2p0']);
foreach ($affectedltiusersrs as $ltiuser) {
$DB->set_field('enrol_lti_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
$DB->set_field('enrol_ct_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
}
$affectedltiusersrs->close();

// Update lti user information for any users missing a consumer secret.
// This applies to any user who has launched the tool (i.e. has lastaccess) but who doesn't have a secret recorded.
// This fixes a bug where enrol_lti_users records are created first during a member sync, and are missing the secret,
// This fixes a bug where enrol_ct_users records are created first during a member sync, and are missing the secret,
// even despite having launched the tool subsequently.
$sql = "SELECT lu.id, lc.secret
FROM {enrol_lti_users} lu
JOIN {enrol_lti_lti2_consumer} lc
FROM {enrol_ct_users} lu
JOIN {enrol_ct_lti2_consumer} lc
ON (" . $DB->sql_compare_text('lu.consumerkey', 255) . " = lc.consumerkey256)
WHERE lu.consumersecret IS NULL
AND lu.lastaccess IS NOT NULL";
$affectedltiusersrs = $DB->get_recordset_sql($sql);
foreach ($affectedltiusersrs as $ltiuser) {
$DB->set_field('enrol_lti_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
$DB->set_field('enrol_ct_users', 'consumersecret', $ltiuser->secret, ['id' => $ltiuser->id]);
}
$affectedltiusersrs->close();

Expand Down
2 changes: 1 addition & 1 deletion tooltemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
// if ($ltiversion === \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1) {
// $dataconnector = new \enrol_lticoursetemplate\data_connector();
// $consumer = new \IMSGlobal\LTI\ToolProvider\ToolConsumer($consumerkey, $dataconnector);
// // Check if the consumer has already been registered to the enrol_lti_lti2_consumer table. Register if necessary.
// // Check if the consumer has already been registered to the enrol_ct_lti2_consumer table. Register if necessary.
// $consumer->ltiVersion = \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1;
// // For LTIv1, set the tool secret as the consumer secret.
// $consumer->secret = $tool->secret;
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023062600; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023080400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2021052500; // Requires this Moodle version.
$plugin->component = 'enrol_lticoursetemplate'; // Full name of the plugin (used for diagnostics).
$plugin->dependencies = [
Expand Down

0 comments on commit 52819a0

Please sign in to comment.