Skip to content

Commit

Permalink
Token can now be auto generated and saved, will test further
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Jun 13, 2024
1 parent 4ce5d1c commit 1f7b25f
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 253 deletions.
40 changes: 21 additions & 19 deletions classes/local/cmi5_connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,6 @@ public function cmi5launch_create_tenant($newtenantname) {
// Build URL for launch URL request.
$url = $playerurl . "/api/v1/tenant";

echo "<br>";
echo "basicname: " . $username;
echo "<br>";

echo "<br>";
echo "password: " . $password;
echo "<br>";

echo "<br>";
echo "URL: " . $url;
echo "<br>";

echo "<br>";
echo "newtenantname: " . $newtenantname;
echo "<br>";
// The body of the request must be made as array first.
$data = array(
'code' => $newtenantname);
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<FIELD NAME="launchtokenid" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Launchtoken assigned by CMI5 player"/>
<FIELD NAME="lastrequesttime" TYPE="char" LENGTH="30" NOTNULL="false" SEQUENCE="false" COMMENT="Time a session was last requested. String as that is what is returned from player."/>
<FIELD NAME="launchmode" TYPE="char" LENGTH="25" NOTNULL="false" SEQUENCE="false" COMMENT="Launch mode, separate from launchmethod. It can be 'Normal', 'Browse', or 'Review"/>
<FIELD NAME="masteryscore" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The amount toward mastery score."/>
<FIELD NAME="masteryscore" TYPE="number" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The amount toward mastery score."/>
<FIELD NAME="score" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The score of session (returned from 'result' parameter in statements from LRS)."/>
<FIELD NAME="islaunched" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Has session been launched?."/>
<FIELD NAME="isinitialized" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Has session been intialized?."/>
Expand Down Expand Up @@ -128,7 +128,7 @@
<FIELD NAME="objectives" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="AUs objectives."/>
<FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="AUs description."/>
<FIELD NAME="activitytype" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Activity type of AU."/>
<FIELD NAME="masteryscore" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The amount it counts toward mastery score."/>
<FIELD NAME="masteryscore" TYPE="number" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The amount it counts toward mastery score."/>
<FIELD NAME="completed" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Whether an AU has met completed criteria (0 if false, 1 if true)"/>
<FIELD NAME="passed" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Whether an AU has met passed criteria (0 if false, 1 if true)"/>
<FIELD NAME="inprogress" TYPE="int" LENGTH="1" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Whether an AU is in progress or not (been started) (0 if false, 1 if true)"/>
Expand All @@ -146,4 +146,4 @@
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
44 changes: 37 additions & 7 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
}
Expand All @@ -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.
Expand Down Expand Up @@ -942,4 +972,4 @@ function xmldb_cmi5launch_upgrade($oldversion) {

// Final return of upgrade result (true, all went good) to Moodle.
return true;
}
}
4 changes: 2 additions & 2 deletions lang/en/cmi5launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@
$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';
$string['cmi5launchbasepass_help'] = 'The cmi5 base password (secret).';
$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.
Expand Down
75 changes: 27 additions & 48 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,7 +285,7 @@ function openprompt2(){

// Info we need to send?
//$newtenantname;
$link = "</br>
$linktotenant = "</br>
<p id=name >
<div class='input-group rounded'>
<button class='btn btn-secondary' type='reset' name='tenantbutton' onclick='openprompt()'>
Expand All @@ -284,13 +294,21 @@ function openprompt2(){
</div>
</p>
";
$linktotoken = "</br>
<p id=name >
<div class='input-group rounded'>
<button class='btn btn-secondary' type='reset' name='tokenbutton' onclick='totokenpage()'>
<span class='button-label'>Generate bearer token</span>
</button>
</div>
</p>
";
//$link ="<a href='http://www.google.com' target='_parent'><button>Click me !</button></a>";

$setting = new admin_setting_configtext(
'cmi5launch/cmi5launchtenantname',
get_string('cmi5launchtenantname', 'cmi5launch'),
" " . get_string('cmi5launchtenantname_help', 'cmi5launch') . $link,

" " . get_string('cmi5launchtenantname_help', 'cmi5launch') . $linktotenant,
get_string('cmi5launchtenantname_default', 'cmi5launch')
);
$settings->add($setting);
Expand All @@ -306,7 +324,7 @@ function openprompt2(){

$setting = new admin_setting_configtext('cmi5launch/cmi5launchtenanttoken',
get_string('cmi5launchtenanttoken', 'cmi5launch'),
get_string('cmi5launchtenanttoken_help', 'cmi5launch'),
get_string('cmi5launchtenanttoken_help', 'cmi5launch') . $linktotoken,
get_string('cmi5launchtenanttoken_default', 'cmi5launch'));
$settings->add($setting);

Expand Down Expand Up @@ -361,52 +379,13 @@ function openprompt2(){

}

// So what lobals are available to me?
global $DB, $CFG, $OUTPUT, $PAGE, $ADMIN, $USER, $SESSION, $cmi5launch, $cm, $course, $context;
echo "<br>";
echo "What are some of these globals? ";
echo "<br>";
/*
echo "DB: ";
var_dump($DB);
echo "<br>";
echo "CFG: ";
var_dump($CFG);
echo "<br>";
echo "OUTPUT: ";
var_dump($OUTPUT);
echo "<br>";
echo "PAGE: ";
var_dump($PAGE);
echo "<br>";
echo "ADMIN: ";
var_dump($ADMIN);
echo "<br>";
echo "USER: ";
var_dump($USER);
echo "<br>";
echo "SESSION: ";
var_dump($SESSION);
*/
echo "<br>";
echo "cmi5launch: ";
var_dump($cmi5launch);
echo "<br>";
echo "cm: ";
var_dump($cm);
echo "<br>";
echo "course: ";
var_dump($course);
echo "<br>";
echo "context: ";
var_dump($context);
echo "<br>";
echo "<br>";

?>
<form id="settingformtoken" action="../mod/cmi5launch/tokensetup.php" method="get">

</form>


?>
<form id="settingform" action="../mod/cmi5launch/setup.php" method="get">
<form id="settingform" action="../mod/cmi5launch/tenantsetup.php" method="get">

<input id="variableName" name="variableName" type="hidden" value="default">

Expand Down
Loading

0 comments on commit 1f7b25f

Please sign in to comment.