Skip to content

Commit

Permalink
Merge branch 'tenant-and-token-addition'
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Jul 1, 2024
2 parents 23531c6 + 74c5452 commit cd59aa3
Show file tree
Hide file tree
Showing 18 changed files with 791 additions and 248 deletions.
Empty file added .clj-kondo/.cache/v1/lock
Empty file.
1 change: 1 addition & 0 deletions .lsp/.cache/db.transit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["^ ","~:classpath",["~#set",[]],"~:project-hash","","~:project-root","/var/www/html/moodle/mod/cmi5launch","~:kondo-config-hash","56f27dae6e4a876734bf42298a7d3dc91d05917f53c9d98543cfd410bd88afe0","~:dependency-scheme","jar","~:analysis",null,"~:analysis-checksums",["^ "],"~:project-analysis-type","~:project-and-full-dependencies","~:version",12,"~:stubs-generation-namespaces",["^1",[]]]
Empty file added amd/src/tenant.js
Empty file.
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
44 changes: 30 additions & 14 deletions classes/local/progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,30 @@ public function cmi5launch_request_statements_from_lrs($registrationid, $session
'since' => $session->createdat,
);

$statements = $this->cmi5launch_send_request_to_lrs($data, $session->id);
// Try and retrieve statements
try {
$statements = $this->cmi5launch_send_request_to_lrs($data, $session->id);
/*
if ($statements === false || $statements == null) {
throw new \Exception ("No statements found.");
}
*/
// The results come back as nested array under more then statements. We only want statements, and we want them unique.
$statement = array_chunk($statements["statements"], 1);

// The results come back as nested array under more then statements. We only want statements, and we want them unique.
$statement = array_chunk($statements["statements"], 1);
$length = count($statement);

$length = count($statement);
for ($i = 0; $i < $length; $i++) {

for ($i = 0; $i < $length; $i++) {
// This separates the larger statement into the separate sessions and verbs.
$current = ($statement[$i]);
array_push($result, array($registrationid => $current));
}

// This separates the larger statement into the separate sessions and verbs.
$current = ($statement[$i]);
array_push($result, array($registrationid => $current));
return $result;
} catch (\Throwable $e) {
echo 'Trouble retrieving statements from LRS. Caught exception: ', $e->getMessage(), "\n";
}

return $result;
}

/**
Expand Down Expand Up @@ -109,11 +118,18 @@ public function cmi5launch_send_request_to_lrs($data, $id) {

// Sends the stream to the specified URL and stores results.
// The false is use_include_path, which we dont want in this case, we want to go to the url.
$result = file_get_contents($url, false, $context);

$resultdecoded = json_decode($result, true);
try {
$result = file_get_contents($url, false, $context);

$resultdecoded = json_decode($result, true);
return $resultdecoded;
} catch (\Throwable $e) {
echo 'Unable to communicate with LRS. Caught exception: ', $e->getMessage(), "\n";
echo "<br>";
echo " Check LRS is up, username and password are correct, and LRS endpoint is correct.";
}

return $resultdecoded;

}

/**
Expand Down
76 changes: 76 additions & 0 deletions classes/local/setup_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form for cmi5 connection, namely basic info - player url, user name and password.
*
* @copyright 2023 Megan Bohland
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// Moodleform is defined in formslib.php.
require_once("$CFG->libdir/formslib.php");

class setup_cmi5 extends moodleform {
// Add elements to form.
public function definition() {
// A reference to the form is stored in $this->form.
// A common convention is to store it in a variable, such as `$mform`.
$mform = $this->_form; // Don't forget the underscore!

// Add elements to your form. Second arg is the name of element
// Player url.
$mform->addElement('text', 'cmi5url', get_string('cmi5launchplayerurl', 'cmi5launch'));
// Set type of element.
$mform->setType('cmi5url', PARAM_NOTAGS);
// Default value.
$mform->setDefault('cmi5url', get_string('cmi5launchplayerurl_default', 'cmi5launch')); // The second arg here is the default value and appears in the text box.
// Add a rule to make this field required.
$mform->addRule('cmi5url', 'This is needed to connect to player', 'required');
// Add a help button with a help message.
$mform->addHelpButton('cmi5url', 'cmi5launchplayerurl', 'cmi5launch');

// User name.
$mform->addElement('text', 'cmi5name', get_string('cmi5launchbasicname', 'cmi5launch'));
// Set type of element.
$mform->setType('cmi5name', PARAM_NOTAGS);
// Default value.
$mform->setDefault('cmi5name', get_string('cmi5launchbasicname_default', 'cmi5launch')); // The second arg here is the default value and appears in the text box.
// Add a rule to make this field required.
$mform->addRule('cmi5name', 'This is needed to connect to player', 'required');
// Add a help button with a help message.
$mform->addHelpButton('cmi5name', 'cmi5launchbasicname', 'cmi5launch');

// Password.
// Add elements to your form. Second arg is the name of element
$mform->addElement('text', 'cmi5password', get_string('cmi5launchbasepass', 'cmi5launch'));
// Set type of element.
$mform->setType('cmi5password', PARAM_NOTAGS);
// Default value.
$mform->setDefault('cmi5password', get_string('cmi5launchbasepass_default', 'cmi5launch')); // The second arg here is the default value and appears in the text box.
// Add a rule to make this field required.
$mform->addRule('cmi5password', 'This is needed to connect to player', 'required');
// Below is the help button, it sucks you have to push it to see the help text, but it is there
$mform->addHelpButton('cmi5password', 'cmi5launchbasepass', 'cmi5launch');

$this->add_action_buttons();
}

// Custom validation should be added here.
function validation($data, $files) {
return [];
}
}
57 changes: 57 additions & 0 deletions classes/local/tenant_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form for cmi5 connection, to enter tenant name.
*
* @copyright 2023 Megan Bohland
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");

class setup_tenant extends moodleform {

// Add elements to form.
public function definition() {
// A reference to the form is stored in $this->form.
// A common convention is to store it in a variable, such as `$mform`.
$mform = $this->_form; // Don't forget the underscore!

// Add elements to your form. Second arg is the name of element
$mform->addElement('text', 'cmi5tenant', get_string('cmi5launchtenantnamesetup', 'cmi5launch'));
// Add a help button with a help message.
$mform->addHelpButton('cmi5tenant', 'cmi5launchtenantnamesetup', 'cmi5launch');

// Set type of element.
$mform->setType('cmi5tenant', PARAM_NOTAGS);
// Default value.
$mform->setDefault('cmi5tenant', get_string('cmi5launchtenantname_default', 'cmi5launch')); // The second arg here is the default value and appears in the text box.
// Add a rule to make this field required.
$mform->addRule('cmi5tenant', 'This is needed to connect to player', 'required');

// $mform->addElement('header', 'cmi5instructions', 'Please enter a tenant name. When submitted it will create a tenant in the cmi5 player and automatically retrieve and save a bearer token for it as well.');
$mform->addElement('html', '<p>Please enter a tenant name. When submitted it will create a tenant in the cmi5 player and automatically retrieve and save a bearer token for it as well</p>');

$this->add_action_buttons();
}

// Custom validation should be added here.
function validation($data, $files) {
return [];
}
}
56 changes: 56 additions & 0 deletions classes/local/token_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Form for cmi5 connection, and tenant and token.
*
* @copyright 2023 Megan Bohland
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");


class setup_token extends moodleform {
// Add elements to form.
public function definition() {
// A reference to the form is stored in $this->form.
// A common convention is to store it in a variable, such as `$mform`.
$mform = $this->_form; // Don't forget the underscore!

// Elements are diff inputs in form


// Add elements to your form. Second arg is the name of element
$mform->addElement('text', 'cmi5token', get_string('cmi5launchtenanttoken', 'cmi5launch'));
// Set type of element.
$mform->setType('cmi5token', PARAM_NOTAGS);
// Default value.
$mform->setDefault('cmi5token', get_string('cmi5launchtenanttoken_default', 'cmi5launch')); // The second arg here is the default value and appears in the text box.
// These three go together for making one eleme
// $mform->addElement('button', 'generatetoken', 'Generate Token');
// Add a rule to make this field required.
$mform->addRule('cmi5token', 'This is needed to connect to player', 'required');

$this->add_action_buttons();
}

// Custom validation should be added here.
function validation($data, $files) {
return [];
}
}
Loading

0 comments on commit cd59aa3

Please sign in to comment.