Skip to content

Commit

Permalink
Tenant and token creation added. Now separate for first time setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ADLMeganBohland committed Jun 27, 2024
1 parent 786c4f6 commit dd447eb
Show file tree
Hide file tree
Showing 16 changed files with 581 additions and 192 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",[]]]
2 changes: 1 addition & 1 deletion AUview.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

<script>

function key_test(registration) {
function key_tst(registration) {

if (event.keyCode === 13 || event.keyCode === 32) {
mod_cmi5launch_launchexperience(registration);
Expand Down
Empty file added amd/src/tenant.js
Empty file.
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 [];
}
}
78 changes: 78 additions & 0 deletions cmi5setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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/>.

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


// NEeded for moodle pae. sets up loabl
require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');

// Tell moodle about our page, tell it what the url is.\\
$PAGE->set_url('/mod/cmi5launch/setup.php');
// Tell moodle the context, in this case the site context (it's system wide not a course or course page.)
$PAGE->set_context(\context_system::instance());
// Title tells what is on tab
$PAGE->set_title(title: 'CMI5 Setup');

//whenyou want to out put html use the moodle core output rendereer: often overridden in theme
echo $OUTPUT->header();

// Easier tom ake template as objec ttypecast to array
$templatecontext = (object) [
'texttodisplay' => 'This is the setup page for CMI5. Here you can create a new tenant for your CMI5 player. Please enter a name for your tenant below.',
];
// now render the mustache template we made.
// takes template and template context - basically some vairables pasded into template and used to render stuff.
echo $OUTPUT->render_from_template('mod_cmi5launch/setup', $templatecontext);

// When we first come here lets check if there are plugin settings for username, passowrd, and url, there should nt be so display the form.
// If there are, then we should display the tenant form.
// Retrieve the three settings from the database.
$playerurl = get_config('cmi5launch', 'cmi5launchplayerurl');
$playername = get_config('cmi5launch', 'cmi5launchbasicname');
$playerpass = get_config('cmi5launch', 'cmi5launchbasepass');

$playerpass = null;
echo "<br>";
echo"Settings are: ";
echo $playerurl . " ";
echo "<br>";
echo $playername;
echo "<br>";
echo $playerpass;
// If the settings are not set, then display the first form.
if(!$playerurl || !$playername || !$playerpass){

redirect(url: $CFG->wwwroot . '/mod/cmi5launch/setupform.php', message: 'Cancelled');

}



echo $OUTPUT->footer();
?>


<form id="gobackform" action="../../admin/settings.php" method="get">
<input id="section" name="section" type="hidden" value="modsettingcmi5launch">

</form>

1 change: 0 additions & 1 deletion db/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
require_once(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');

require_login($course, false, $cm);
/**
* Custom uninstallation procedure
*/
Expand Down
7 changes: 7 additions & 0 deletions lang/en/cmi5launch.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
$string['modulename_help'] = 'A plug in for Moodle that allows the launch of cmi5 (xAPI) content which is then tracked to a separate LRS.';

// Start Default LRS Admin Settings.
// Header.
$string['cmi5lrssettingsheader'] = 'LRS connection settings';

$string['cmi5launchlrsfieldset'] = 'Default values for cmi5 launch link activity settings';
$string['cmi5launchlrsfieldset_help'] = 'These are site-wide, default values used when creating a new activity. Each activity has the ability to override and provide alternative values.';

Expand Down Expand Up @@ -99,7 +102,11 @@
$string['cmi5launchbasicname_default'] = '';

$string['cmi5launchtenantname'] = 'cmi5 player: The cmi5 tenant username.';
$string['cmi5launchtenantnamesetup'] = 'Please enter a value for cmi5 tenant name.';
$string['cmi5launchtenantname'] = 'cmi5 player: The cmi5 tenant username.';
$string['cmi5launchtenantnamesetup_help'] = ' The tenant name to be used in cmi5 player requests, and attached to the token. Alphanumeric characters and spaces accepted.';
$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. NOTE: changing a tenant name will require cmi5 launch link activites to need to be re-installed. Do not change name mid sessions! Data will be lost.';

$string['cmi5launchtenantname_default'] = '';

$string['cmi5launchbasepass'] = 'cmi5 player: basic password';
Expand Down
Loading

0 comments on commit dd447eb

Please sign in to comment.