From 0da1c90570b13ceae053ed11af735b7b64368bc9 Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Wed, 19 Jul 2023 13:21:29 +0200 Subject: [PATCH 1/6] Update table names --- .../ltiadvantage/repository/user_repository.php | 2 +- .../table/published_resources_table.php | 2 +- classes/manage_table.php | 2 +- classes/task/sync_members.php | 2 +- db/upgrade.php | 14 +++++++------- tooltemplate.php | 2 +- version.php | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/classes/local/ltiadvantage/repository/user_repository.php b/classes/local/ltiadvantage/repository/user_repository.php index 70065cd..7c31a09 100644 --- a/classes/local/ltiadvantage/repository/user_repository.php +++ b/classes/local/ltiadvantage/repository/user_repository.php @@ -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. diff --git a/classes/local/ltiadvantage/table/published_resources_table.php b/classes/local/ltiadvantage/table/published_resources_table.php index 0c31d99..112a6fa 100644 --- a/classes/local/ltiadvantage/table/published_resources_table.php +++ b/classes/local/ltiadvantage/table/published_resources_table.php @@ -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', diff --git a/classes/manage_table.php b/classes/manage_table.php index 058a5dd..074c815 100644 --- a/classes/manage_table.php +++ b/classes/manage_table.php @@ -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', diff --git a/classes/task/sync_members.php b/classes/task/sync_members.php index 43e8c35..3b5e313 100644 --- a/classes/task/sync_members.php +++ b/classes/task/sync_members.php @@ -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; diff --git a/db/upgrade.php b/db/upgrade.php index 0b60429..e481c66 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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(); diff --git a/tooltemplate.php b/tooltemplate.php index 1db0ad4..44f4e4c 100644 --- a/tooltemplate.php +++ b/tooltemplate.php @@ -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; diff --git a/version.php b/version.php index 11becf9..6f110bd 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023062600; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2023071900; // 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 = [ From 0c7231d5766d57e1f51f6fe50d3638ea5368120b Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Fri, 4 Aug 2023 15:49:44 +0200 Subject: [PATCH 2/6] Fix the issue, missing grant type added --- classes/local/ltiadvantage/lib/http_client.php | 12 ++++++------ classes/local/ltiadvantage/task/sync_grades.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/classes/local/ltiadvantage/lib/http_client.php b/classes/local/ltiadvantage/lib/http_client.php index d7288aa..3736c86 100644 --- a/classes/local/ltiadvantage/lib/http_client.php +++ b/classes/local/ltiadvantage/lib/http_client.php @@ -65,7 +65,9 @@ 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 { @@ -73,12 +75,11 @@ function(&$val, $key) { } $info = $this->curlclient->get_info(); - 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) @@ -92,11 +93,10 @@ function(&$val, $key) { } $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}."); diff --git a/classes/local/ltiadvantage/task/sync_grades.php b/classes/local/ltiadvantage/task/sync_grades.php index 2eac5ae..e93fc6b 100644 --- a/classes/local/ltiadvantage/task/sync_grades.php +++ b/classes/local/ltiadvantage/task/sync_grades.php @@ -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]); From a6fba178ca569f1fad82d259e9a7fc3edfa21764 Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Fri, 4 Aug 2023 16:11:35 +0200 Subject: [PATCH 3/6] Update helper.php --- classes/helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/helper.php b/classes/helper.php index 30df1e0..f8bb016 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -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(); } } @@ -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. From 2d81696f8581d1ba1c8ead95ac9b190cb24ff45d Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Fri, 4 Aug 2023 16:21:52 +0200 Subject: [PATCH 4/6] Update helper.php --- classes/helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/helper.php b/classes/helper.php index f8bb016..69e20de 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -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. From e9cb55c1719cbc3033eaee786f648b41528103bc Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Fri, 4 Aug 2023 16:29:04 +0200 Subject: [PATCH 5/6] Update http_client.php --- classes/local/ltiadvantage/lib/http_client.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/classes/local/ltiadvantage/lib/http_client.php b/classes/local/ltiadvantage/lib/http_client.php index 3736c86..68afefa 100644 --- a/classes/local/ltiadvantage/lib/http_client.php +++ b/classes/local/ltiadvantage/lib/http_client.php @@ -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( @@ -64,6 +65,7 @@ function(&$val, $key) { ); $this->curlclient->setHeader($headers); } + if ($method == 'POST') { $body = $options['body'] ?? null; $body = $body ?? (!empty($options['form_params']) ? http_build_query($options['form_params'], '' , '&') : null); @@ -75,6 +77,7 @@ function(&$val, $key) { } $info = $this->curlclient->get_info(); + if (!$this->curlclient->get_errno() && !$this->curlclient->error) { // No errors, so format the response. $headersize = $info['header_size']; @@ -84,21 +87,28 @@ function(&$val, $key) { $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(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}." + ); } } From cf87f1aa3c3c65866c92fd4c4f61a83be1009f49 Mon Sep 17 00:00:00 2001 From: Arek Juszczyk Date: Fri, 4 Aug 2023 16:46:32 +0200 Subject: [PATCH 6/6] Update version.php --- version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.php b/version.php index 6f110bd..ab331c3 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023071900; // 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 = [