diff --git a/classes/helper.php b/classes/helper.php index 30df1e0..69e20de 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. @@ -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. diff --git a/classes/local/ltiadvantage/lib/http_client.php b/classes/local/ltiadvantage/lib/http_client.php index d7288aa..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,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 { @@ -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}." + ); } } 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/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]); 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..ab331c3 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 = 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 = [