diff --git a/classes/local/cmi5_connectors.php b/classes/local/cmi5_connectors.php
index 9e0491c..439576c 100755
--- a/classes/local/cmi5_connectors.php
+++ b/classes/local/cmi5_connectors.php
@@ -117,21 +117,6 @@ public function cmi5launch_create_tenant($newtenantname) {
// Build URL for launch URL request.
$url = $playerurl . "/api/v1/tenant";
- echo " ";
- echo "basicname: " . $username;
- echo " ";
-
- echo " ";
- echo "password: " . $password;
- echo " ";
-
- echo " ";
- echo "URL: " . $url;
- echo " ";
-
- echo " ";
- echo "newtenantname: " . $newtenantname;
- echo " ";
// The body of the request must be made as array first.
$data = array(
'code' => $newtenantname);
@@ -152,7 +137,7 @@ public function cmi5launch_create_tenant($newtenantname) {
// Decode returned response into array.
$returnedinfo = json_decode($result, true);
- echo"It worked";
+
// Return an array with tenant name and info.
return $returnedinfo;
} else {
@@ -261,10 +246,25 @@ public function cmi5launch_retrieve_registration_with_post($courseid, $id) {
* @param $audience - the name the of the audience using the token,
* @param #tenantid - the id of the tenant
*/
- public function cmi5launch_retrieve_token($url, $username, $password, $audience, $tenantid) {
+ public function cmi5launch_retrieve_token($audience, $tenantid) {
+
+ // Honestly the params can be rabbbed through settings right? So I thinks we can change this whole func.
+ // but if it is called, will it need to go tooo secret back page?
+ // and can we make it same page, like if pthere is no prompt? which is fdiff then null right? Or maybe another page just to be certain.
+
+ global $CFG, $cmi5launchid;
+
+ $settings = cmi5launch_settings($cmi5launchid);
+ //$actor = $USER->username;
+ $username = $settings['cmi5launchbasicname'];
+ $playerurl = $settings['cmi5launchplayerurl'];
+ $password = $settings['cmi5launchbasepass'];
global $CFG;
+ // Build URL for launch URL request.
+ $url = $playerurl . "/api/v1/auth";
+
// The body of the request must be made as array first.
$data = array(
'tenantId' => $tenantid,
@@ -282,8 +282,10 @@ public function cmi5launch_retrieve_token($url, $username, $password, $audience,
$resulttest = $this->cmi5launch_connectors_error_message($result, "retrieving the token");
if ($resulttest == true) {
-
- return $result;
+ $resultDecoded = json_decode($result, true);
+ $token = $resultDecoded['token'];
+
+ return $token;
} else {
return false;
}
diff --git a/db/install.xml b/db/install.xml
index 106b457..667d885 100755
--- a/db/install.xml
+++ b/db/install.xml
@@ -89,7 +89,7 @@
-
+
@@ -128,7 +128,7 @@
-
+
@@ -146,4 +146,4 @@
-
+
\ No newline at end of file
diff --git a/db/upgrade.php b/db/upgrade.php
index 503c168..283957b 100755
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -42,6 +42,23 @@ function xmldb_cmi5launch_upgrade($oldversion) {
global $DB;
$dbman = $DB->get_manager();
+ // Change masteryscore to number type because of decimal
+ if ($oldversion < 2024061115) {
+
+ // Changing type of field masteryscore on table cmi5launch_sessions to int.
+ $tablesessions = new xmldb_table('cmi5launch_sessions');
+ $tableaus = new xmldb_table('cmi5launch_aus');
+ $field = new xmldb_field('masteryscore', XMLDB_TYPE_NUMBER, '10', null, null, null, null, 'launchmode');
+ $field2 = new xmldb_field('masteryscore', XMLDB_TYPE_NUMBER, '10', null, null, null, null, 'activitytype');
+
+ // Launch change of type for field masteryscore in both tables.
+ $dbman->change_field_type($tablesessions, $field);
+ $dbman->change_field_type($tableaus, $field2);
+ // Cmi5launch savepoint reached.
+ upgrade_mod_savepoint(true, 2024061115, 'cmi5launch');
+ }
+
+
if ($oldversion < 2024032112) {
// Define index lmsid (not unique) to be dropped form cmi5launch_sessions.
@@ -56,11 +73,17 @@ function xmldb_cmi5launch_upgrade($oldversion) {
// Define index lmsid (not unique) to be added to cmi5launch_sessions.
$indexnew = new xmldb_index('sessionid', XMLDB_INDEX_NOTUNIQUE, ['sessionid']);
- // Conditionally launch add index lmsid.
- if (!$dbman->index_exists($table, $indexnew)) {
+ // If table exists
+ // Conditionally launch create table for cmi5launch.
+ if ($dbman->table_exists($table)) {
+
+ // Conditionally launch add index lmsid.
+ if (!$dbman->index_exists($table, $indexnew)) {
$dbman->add_index($table, $indexnew);
}
+ }
+
// Cmi5launch savepoint reached.
upgrade_mod_savepoint(true, 2024032112, 'cmi5launch');
}
@@ -84,10 +107,17 @@ function xmldb_cmi5launch_upgrade($oldversion) {
// Now cycle through array and remove fields.
foreach ($tablestoadd as $table) {
- // Conditionally launch add field moodlecourseid.
- if (!$dbman->field_exists($table, $fieldmcid)) {
- $dbman->add_field($table, $fieldmcid);
- }
+ // If the table exists
+ // Conditionally launch add index lmsid.
+ // Conditionally launch create table for cmi5launch.
+ if ($dbman->table_exists($table)) {
+
+ // Conditionally launch add field moodlecourseid.
+ if (!$dbman->field_exists($table, $fieldmcid)) {
+ $dbman->add_field($table, $fieldmcid);
+ }
+ }
+
}
// Cmi5launch savepoint reached.
@@ -942,4 +972,4 @@ function xmldb_cmi5launch_upgrade($oldversion) {
// Final return of upgrade result (true, all went good) to Moodle.
return true;
-}
+}
\ No newline at end of file
diff --git a/lang/en/cmi5launch.php b/lang/en/cmi5launch.php
index b5b70e2..484c12c 100755
--- a/lang/en/cmi5launch.php
+++ b/lang/en/cmi5launch.php
@@ -99,7 +99,7 @@
$string['cmi5launchbasicname_default'] = '';
$string['cmi5launchtenantname'] = 'cmi5 player: The cmi5 tenant username.';
-$string['cmi5launchtenantname_help'] = ' The tenant name attached to the token. Should only need to be used during initial setup. If for some reason the tenant name is changed a new token will need to be generated.';
+$string['cmi5launchtenantname_help'] = ' The tenant name attached to the token. Should only need to be used during initial setup. If for some reason the tenant name is changed a new bearer token will need to be generated.';
$string['cmi5launchtenantname_default'] = '';
$string['cmi5launchbasepass'] = 'cmi5 player: basic password';
@@ -107,7 +107,7 @@
$string['cmi5launchbasepass_default'] = '';
$string['cmi5launchtenanttoken'] = 'cmi5 player: bearer token';
-$string['cmi5launchtenanttoken_help'] = 'The cmi5 tenant bearer token (should be a long string).';
+$string['cmi5launchtenanttoken_help'] = 'The cmi5 tenant bearer token (should be a long string). Should only need to be used during initial setup. If for some reason the tenant name is changed a new bearer token will need to be generated. This cannot be generated if a tenant name has not been made yet.';
$string['cmi5launchtenanttoken_default'] = '';
// Grading info - MB.
diff --git a/settings.php b/settings.php
index 3e39ba2..7a4e63c 100755
--- a/settings.php
+++ b/settings.php
@@ -56,6 +56,16 @@ function openprompt(){
$('#settingform').submit();
}
+}
+function totokenpage(){
+
+ console.log("To the make a token page");
+
+
+ // Post it.
+ $('#settingformtoken').submit();
+
+
}
//TRy this new func
// Function for popup window
@@ -275,7 +285,7 @@ function openprompt2(){
// Info we need to send?
//$newtenantname;
- $link = "
+ $linktotenant = "
+ ";
+
+ // Maybe we can have a thing here if fromsettings equals a keyword, say token, call token making stuff.
+ // after all it doesn't need input from user. but we should write a catch to warn there is no tenant
+ // because it does need a tenant to make a token.
+
+
+if ($fromsettings != null) {
+
+
+ // Make the new tenant and grab results.
+ $tenant = $createtenant($fromsettings);
+
+ // The return response should be json and have 'id' and 'code'
+ $response = $tenant;
+
+ // Save the code as the tenant name and ID as ID.
+ $name = $response['code'];
+ $id = $response['id'];
+
+ echo "Tenant code: " . $name . " ";
+ echo "Tenant ID: " . $id . " ";
+
+ // check we have a tenant and is, and save them to db for later retrieval (particularly id)
+ if ($name != null && $id != null) {
+
+
+ $result = set_config('cmi5launchtenantname', $name, $plugin = 'cmi5launch');
+
+ // But how can we save the id to DB when this page has no access to cmi5launch
+ // What if te id is saved as a invisible settings?
+ // Is that a thing?
+ // But do we need an id? Probably not cause like...
+ // It saves over itself and we don't need to keep records do we?
+ // But then we would need to look it up with query right? Might be better to just save it someoien
+ // Lets see if we can just make a setting here!
+ $idresult = set_config('cmi5launchtenantid', $id, $plugin = 'cmi5launch');
+
+ if($idresult && $result ){
+
+ // If result is true then redirect back to settings page.
+ $settingurl = new moodle_url($CFG->wwwroot . '/' . 'admin/settings.php', array('section' => 'modsettingcmi5launch'));
+
+ redirect($settingurl, 'Successfully made and saved new tenant', 10);
+
+ } else {
+ echo "Failed to save tenant to DB.";
+ echo " ";
+ echo "Tenant name: " . $name . " failed to save as setting. With result " . $result . " ";
+ //if fail shoudl we freeze and alert user with a window towith error message
+
+ echo $link;
+ }
+ } else {
+
+ echo "Failed to make tenant. Check connection to player and tenant name.";
+
+ echo $link;
+ }
+} else {
+
+ // If there is no tenant name then alert user, when they click to clear take them back to settings page.
+ echo "Tenant name not retrieved or blank. Please try again.";
+
+ echo $link;
+
+}
+?>
+
+
+
+
diff --git a/tokensetup.php b/tokensetup.php
new file mode 100644
index 0000000..789872f
--- /dev/null
+++ b/tokensetup.php
@@ -0,0 +1,129 @@
+.
+
+/**
+ * Page to create tenant behind the scenes.
+ *
+ * @copyright 2023 Megan Bohland
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+use mod_cmi5launch\local\cmi5_connectors;
+
+?>
+
+
+
+libdir.'/tablelib.php');
+require_once($CFG->dirroot.'/mod/cmi5launch/locallib.php');
+require_once($CFG->libdir.'/formslib.php');
+require_once($CFG->dirroot. '/reportbuilder/classes/local/report/column.php');
+
+define('CMI5LAUNCH_REPORT_DEFAULT_PAGE_SIZE', 20);
+define('CMI5LAUNCH_REPORT_ATTEMPTS_ALL_STUDENTS', 0);
+define('CMI5LAUNCH_REPORT_ATTEMPTS_STUDENTS_WITH', 1);
+define('CMI5LAUNCH_REPORT_ATTEMPTS_STUDENTS_WITH_NO', 2);
+$PAGE->requires->jquery();
+
+global $cmi5launch, $CFG;
+
+// External classes and functions.
+$cmi5helper = new cmi5_connectors;
+$gettoken = $cmi5helper->cmi5launch_get_retrieve_token();
+
+
+// Return link/button to settings page.
+ $link = "
+
+
+
+
+ ";
+
+ // Maybe we can have a thing here if fromsettings equals a keyword, say token, call token making stuff.
+ // after all it doesn't need input from user. but we should write a catch to warn there is no tenant
+ // because it does need a tenant to make a token.
+
+ // Before a token can be made, there must be a tenant name and id, so verify these exist, if not throw error.
+
+ // Retrieves the string if there or false if not.
+ $tenantname = get_config('cmi5launch', 'cmi5launchtenantname');
+ $tenantid = get_config('cmi5launch', 'cmi5launchtenantid');
+
+// If niether are false.
+if ($tenantname != null && $tenantid != null) {
+
+ // Make the new tenant and grab results.
+ $token = $gettoken($tenantname, $tenantid);
+
+// If the token is not false it should be what we need
+ if ($token != false) {
+
+ //Save it to the settings.
+ $tokenresult = set_config('cmi5launchtenanttoken', $token, $plugin = 'cmi5launch');
+
+ if ($tokenresult == false) {
+ echo "Failed to save token to settings. Check connection with DB and try again.";
+ echo " ";
+ echo "Save failed. With result " . $tokenresult . " ";
+
+ //if fail shoudl we freeze and alert user with a window towith error message
+ echo $link;
+ }else {
+ // Assumin the tokenresult is not false, it was saved correctly and we cango back to setting pae.
+ // If result is true then redirect back to settings page.
+ $settingurl = new moodle_url($CFG->wwwroot . '/' . 'admin/settings.php', array('section' => 'modsettingcmi5launch'));
+
+ redirect($settingurl, 'Successfully retrieved and saved new bearer token', 10);
+ }
+ }
+ else {
+ echo "Failed to retrieve token from cmi5 player. Check connection with player, ensure tenant name and ID exist, and try again.";
+ echo " ";
+ echo "Token retrieval failed. With result " . $tokenresult . " ";
+
+ //if fail shoudl we freeze and alert user with a window towith error message
+ echo $link;
+ }
+
+} else {
+
+ // If there is no tenant name then alert user, when they click to clear take them back to settings page.
+ echo "Tenant name and/or ID not retrieved or blank. Please create a tenant before trying again.";
+
+ echo $link;
+
+}
+?>
+
+
+
+
diff --git a/version.php b/version.php
index cadec7c..e5b7f56 100755
--- a/version.php
+++ b/version.php
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2024032112; // The current module version (Date: YYYYMMDDXX).
+$plugin->version = 2024061115; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2015111000; // Requires Moodle 3.0 version.
$plugin->cron = 0; // Period for cron to check this module (secs).