diff --git a/.clj-kondo/.cache/v1/lock b/.clj-kondo/.cache/v1/lock new file mode 100644 index 0000000..e69de29 diff --git a/.lsp/.cache/db.transit.json b/.lsp/.cache/db.transit.json new file mode 100644 index 0000000..52d4536 --- /dev/null +++ b/.lsp/.cache/db.transit.json @@ -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",[]]] \ No newline at end of file diff --git a/amd/src/tenant.js b/amd/src/tenant.js new file mode 100644 index 0000000..e69de29 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/classes/local/progress.php b/classes/local/progress.php index 5163d1c..9394ba0 100755 --- a/classes/local/progress.php +++ b/classes/local/progress.php @@ -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; } /** @@ -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 "
"; + echo " Check LRS is up, username and password are correct, and LRS endpoint is correct."; + } - return $resultdecoded; + } /** diff --git a/classes/local/setup_form.php b/classes/local/setup_form.php new file mode 100644 index 0000000..d95a794 --- /dev/null +++ b/classes/local/setup_form.php @@ -0,0 +1,76 @@ +. + +/** + * 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 []; + } +} diff --git a/classes/local/tenant_form.php b/classes/local/tenant_form.php new file mode 100644 index 0000000..b330bcf --- /dev/null +++ b/classes/local/tenant_form.php @@ -0,0 +1,57 @@ +. + +/** + * 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', '

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

'); + + $this->add_action_buttons(); + } + + // Custom validation should be added here. + function validation($data, $files) { + return []; + } +} diff --git a/classes/local/token_form.php b/classes/local/token_form.php new file mode 100644 index 0000000..c98e7f4 --- /dev/null +++ b/classes/local/token_form.php @@ -0,0 +1,56 @@ +. + +/** + * 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 []; + } +} diff --git a/cmi5setup.php b/cmi5setup.php new file mode 100644 index 0000000..4a7d7cb --- /dev/null +++ b/cmi5setup.php @@ -0,0 +1,78 @@ +. + +/** + * 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 "
"; +echo"Settings are: "; +echo $playerurl . " "; +echo "
"; +echo $playername; +echo "
"; +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(); +?> + + +
+ + +
+ diff --git a/db/install.xml b/db/install.xml index bf944f6..667d885 100755 --- a/db/install.xml +++ b/db/install.xml @@ -146,4 +146,4 @@ - + \ No newline at end of file diff --git a/db/uninstall.php b/db/uninstall.php index 62c4fa0..0261718 100755 --- a/db/uninstall.php +++ b/db/uninstall.php @@ -23,7 +23,6 @@ */ require_once(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php'); -require_login($course, false, $cm); /** * Custom uninstallation procedure */ diff --git a/db/upgrade.php b/db/upgrade.php index b833a77..283957b 100755 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -972,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..b05f872 100755 --- a/lang/en/cmi5launch.php +++ b/lang/en/cmi5launch.php @@ -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.'; @@ -99,7 +102,11 @@ $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['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'; @@ -107,7 +114,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 9d129df..3f98bff 100755 --- a/settings.php +++ b/settings.php @@ -17,7 +17,7 @@ /* For global cmi5 settings */ /** - * Defines the version of cmi5launch + * Cmi5 settings. Ability to update and change * * This code fragment is called by moodle_needs_upgrading() and * /admin/index.php @@ -28,113 +28,24 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - - - defined('MOODLE_INTERNAL') || die; -use mod_cmi5launch\local\cmi5_connectors; ?> dirroot . '/mod/cmi5launch/locallib.php'); require_once($CFG->dirroot . '/mod/cmi5launch/settingslib.php'); - // Varibale to hold answer? -$nameanswer = ""; - - -// Ok let's try to get the answer from the ajax method -if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["variableName"])) { - $receivedVariable = $_POST["variableName"]; - echo "
"; - echo" We got it ! it is ! : ". $receivedVariable; - echo " IGOT IT!"; - // Process the received variable here -// Check for answer : -/////////// maybe come back to htis if (isset($_POST['tenantbutton'])) { - - // Get the answer - // $.get('myFile.php', js_answer: answer); - // $nameanswer = $_POST['cmi5launchtenantname']; - // echo it - // echo $_POST["name"]; - $sessionhelper = new cmi5_connectors; - $maketenant = $sessionhelper->cmi5launch_get_create_tenant(); - - // $pass = $_POST['tenantbutton']; - - echo " What is recieved var : "; - echo $receivedVariable; - $newtenantname = $maketenant($receivedVariable); - - echo $nameanswer; -} -// If it's null dont use it, if its not call func -if ($nameanswer != null) { - // Call func - echo" We got it ! it is ! : ". $nameanswer; - // $maketenant = $sessionhelper->cmi5launch_get_create_tenant(); -} - // MB // From scorm grading stuff. $yesno = array(0 => get_string('no'), 1 => get_string('yes')); - + // Default display settings. $settings->add(new admin_setting_heading('cmi5launch/cmi5launchlrsfieldset', get_string('cmi5launchlrsfieldset', 'cmi5launch'), get_string('cmi5launchlrsfieldset_help', 'cmi5launch'))); + // LRS settings + $settings->add(new admin_setting_heading('cmi5launch/cmi5lrssettings', get_string('cmi5lrssettingsheader', 'cmi5launch'), '')); + $settings->add(new admin_setting_configtext_mod_cmi5launch('cmi5launch/cmi5launchlrsendpoint', get_string('cmi5launchlrsendpoint', 'cmi5launch'), get_string('cmi5launchlrsendpoint_help', 'cmi5launch'), @@ -237,92 +114,89 @@ function openprompt2(){ get_string('cmi5launchuseactoremail_help', 'cmi5launch'), 1)); - // LEt's add a new header to separate cmi5 from lrs - $settings->add(new admin_setting_heading('cmi5launch/cmi5launchsettings', get_string('cmi5launchsettingsheader', 'cmi5launch'), '')); - - - $settings->add(new admin_setting_configtext_mod_cmi5launch('cmi5launch/cmi5launchplayerurl', - get_string('cmi5launchplayerurl', 'cmi5launch'), - get_string('cmi5launchplayerurl_help', 'cmi5launch'), - get_string('cmi5launchplayerurl_default', 'cmi5launch'), PARAM_URL)); - /* - $settings->add(new admin_setting_configtext_mod_cmi5launch('cmi5launch/cmi5launchcontenturl', - get_string('cmi5launchcontenturl', 'cmi5launch'), - get_string('cmi5launchcontenturl_help', 'cmi5launch'), - get_string('cmi5launchcontenturl_default', 'cmi5launch'), PARAM_URL)); -*/ - + // The first time user logs in there will be a button for setup. + $showbutton = false; + // If tenantname or id is false, this is a first time setup we'll have the new button. + $tenantid = get_config('cmi5launch', 'cmi5launchtenantid'); + $tenantname = get_config('cmi5launch', 'cmi5launchtenantname'); + if ($tenantid == null || $tenantid == false) { + $showbutton = true; - $setting = new admin_setting_configtext('cmi5launch/cmi5launchbasicname', - get_string('cmi5launchbasicname', 'cmi5launch'), - get_string('cmi5launchbasicname_help', 'cmi5launch'), - get_string('cmi5launchbasicname_default', 'cmi5launch')); - $settings->add($setting); - - $setting = new admin_setting_configtext('cmi5launch/cmi5launchbasepass', - get_string('cmi5launchbasepass', 'cmi5launch'), - get_string('cmi5launchbasepass_help', 'cmi5launch'), - get_string('cmi5launchbasepass_default', 'cmi5launch')); - $settings->add($setting); - - - -$warning = "OHNO"; - // $link = "Empty all results ".$warning.""; - // $clear_url = new moodle_url('/settings.php'); - // For form action we will call new tenant or new token - // Furthe we can set them as to apperar when made either here or in those funcs being called + } + $settings->add(new admin_setting_heading('cmi5launch/cmi5launchsettings', get_string('cmi5launchsettingsheader', 'cmi5launch'), '')); - // Info we need to send? - //$newtenantname; - $link = "
+ // We need to puck a diff thing for this button, we dont want the text baox available +if ($showbutton) { + // show only a button, otherwise reg showing + $linktocmi5 = "

-

"; - //$link =""; - - $setting = new admin_setting_configtext( - 'cmi5launch/cmi5launchtenantname', - get_string('cmi5launchtenantname', 'cmi5launch'), - " " . get_string('cmi5launchtenantname_help', 'cmi5launch') . $link, - get_string('cmi5launchtenantname_default', 'cmi5launch') - ); - $settings->add($setting); -/* - echo"
"; - //echo ""; - //echo"Hey it worked and I cansee receivedVariable " . $receivedVariable; - echo "What is settings? Did we change it>???? "; - $toread = $settings['cmi5launchtenantname']; - var_dump($toread); - echo "
"; -*/ - - $setting = new admin_setting_configtext('cmi5launch/cmi5launchtenanttoken', - get_string('cmi5launchtenanttoken', 'cmi5launch'), - get_string('cmi5launchtenanttoken_help', 'cmi5launch'), - get_string('cmi5launchtenanttoken_default', 'cmi5launch')); - $settings->add($setting); + //use this for the button instead of config text + $setting = new admin_setting_description('cmi5launchsetup', "

First time setup:", $linktocmi5); + $settings->add($setting); + + } else { + + $settings->add( + new admin_setting_configtext_mod_cmi5launch( + 'cmi5launch/cmi5launchplayerurl', + get_string('cmi5launchplayerurl', 'cmi5launch'), + get_string('cmi5launchplayerurl_help', 'cmi5launch'), + get_string('cmi5launchplayerurl_default', 'cmi5launch'), + PARAM_URL + ) + ); + + $setting = new admin_setting_configtext( + 'cmi5launch/cmi5launchbasicname', + get_string('cmi5launchbasicname', 'cmi5launch'), + get_string('cmi5launchbasicname_help', 'cmi5launch'), + get_string('cmi5launchbasicname_default', 'cmi5launch') + ); + $settings->add($setting); + + $setting = new admin_setting_configtext( + 'cmi5launch/cmi5launchbasepass', + get_string('cmi5launchbasepass', 'cmi5launch'), + get_string('cmi5launchbasepass_help', 'cmi5launch'), + get_string('cmi5launchbasepass_default', 'cmi5launch') + ); + $settings->add($setting); + + // Display tenant info + $todisplay = "Tenant name is: " . $tenantname . ". Tenant id is: " . $tenantid . "

The tenant name and ID have been set. They cannot be changed without causing problems with existing cmi5 launch link activities. To change, plugin must be uninstalled and reinstalled.

"; + $setting = new admin_setting_description('cmi5launchtenantmessage', "cmi5launch tenant name and id:", $todisplay); + $settings->add($setting); + + + // Token generate button. + $linktotoken = "
+

+

+ +
+

+ "; + $setting = new admin_setting_configtext( + 'cmi5launch/cmi5launchtenanttoken', + get_string('cmi5launchtenanttoken', 'cmi5launch'), + get_string('cmi5launchtenanttoken_help', 'cmi5launch') . $linktotoken, + get_string('cmi5launchtenanttoken_default', 'cmi5launch') + ); + $settings->add($setting); -/* - $editstring = "I am a button"; + } - $url = new moodle_url("$CFG->wwwroot/my/index.php"); - $button = $OUTPUT->single_button($url, $editstring); - $PAGE->set_button($button); - $editstring2 = "I am another button"; - $url = new moodle_url("$CFG->wwwroot/my/index.php"); - $button = $OUTPUT->single_button($url, $editstring2); - $PAGE->set_button($button); - - */ // MB. // Grade stuff I'm bringing over. // Default grade settings. @@ -361,11 +235,20 @@ function openprompt2(){ } + ?> + +
+ +
-?> -
+ + +
+ + +
-
+ \ No newline at end of file diff --git a/setupform.php b/setupform.php new file mode 100644 index 0000000..c33b0df --- /dev/null +++ b/setupform.php @@ -0,0 +1,83 @@ +. + +/** + * 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'); +// Include our class file +require_once($CFG->dirroot.'/mod/cmi5launch/classes/local/setup_form.php'); +// Tell moodle about our page, tell it what the url is.\\ +$PAGE->set_url('/mod/cmi5launch/setupform.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 Form'); + +// You want to initialze form in PHP before you echo to page. you dont want to rener while doing calcs + +// we want to display a form +$mform = new setup_cmi5(); + +// Form processing and displaying is done here. +if ($mform->is_cancelled()) { + // If cancel was pressed, then redirect back to the settings page. + // in tutorial he goes back to the manage page, we will go back to settins page? + redirect(url: $CFG->wwwroot . '/admin/settings.php'. '?section=modsettingcmi5launch', message: "Cancelled"); + //now he is dire ting to manage page which is equvalent to our cmi5setup page so we should redirect to that? why so many pages I wonder? + // redirect(url: $CFG->wwwroot . '/mod/cmi5launch/cmi5setup.php', message: 'Cancelled'); + +} else if ($fromform = $mform->get_data()) { + // When the form is submitted, and the data is successfully validated, + // the `get_data()` function will return the data posted in the form. + + // REtrieve data from form. + $cmi5url = $fromform->cmi5url; + $cmi5name = $fromform->cmi5name; + $cmi5password = $fromform->cmi5password; + + // Save data to the database, and configure the settings. + $resulturl = set_config('cmi5launchplayerurl', $cmi5url, $plugin = 'cmi5launch'); + $resultname = set_config('cmi5launchbasicname', $cmi5name, $plugin = 'cmi5launch'); + $resultpass = set_config('cmi5launchbasepass', $cmi5password, $plugin = 'cmi5launch'); + + + // As long as they are not null/false we can move on to the next form + if ($resulturl && $resultname && $resultpass){ + //Move to next form + redirect(url: $CFG->wwwroot . '/mod/cmi5launch/tenantsetup.php', message: 'Successfully saved settings.'); + }else{ + // If for some reason they are null or false, then we will redirect back to the form. + redirect(url: $CFG->wwwroot . '/mod/cmi5launch/setupform.php', message: 'Failed to save to database. Please check database is accessable and try again.'); + + } + +}; + +// Display page. +echo $OUTPUT->header(); +// Display the form. +$mform->display(); + +echo $OUTPUT->footer(); + +?> diff --git a/templates/setup.mustache b/templates/setup.mustache new file mode 100644 index 0000000..d90a1d2 --- /dev/null +++ b/templates/setup.mustache @@ -0,0 +1,27 @@ +{{! + 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 . +}} +{{! + @template mod_cmi5launch/setup + + + Example context (json): + { + } +}} + +

This si the setup page

+

{{texttodisplay}}

\ No newline at end of file diff --git a/tenantsetup.php b/tenantsetup.php new file mode 100644 index 0000000..d429cbe --- /dev/null +++ b/tenantsetup.php @@ -0,0 +1,139 @@ +. + +/** + * 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; + + + +require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); +require_once($CFG->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'); +// Include our class file +require_once($CFG->dirroot.'/mod/cmi5launch/classes/local/tenant_form.php'); +// Tell moodle about our page, tell it what the url is.\\ +$PAGE->set_url('/mod/cmi5launch/tenantsetup.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: 'Creating a tenant'); + +$PAGE->requires->jquery(); + +global $cmi5launch, $CFG; + +// External classes and functions. +$cmi5helper = new cmi5_connectors; +$createtenant = $cmi5helper->cmi5launch_get_create_tenant(); + +// Instantiate form. +$mform = new setup_tenant(); + +// Form processing and displaying is done here. +if ($mform->is_cancelled()) { + + // If they cancel, redirect back to the setup page. + redirect(url: $CFG->wwwroot . '/mod/cmi5launch/setupform.php', message: 'Cancelled'); + +} else if ($fromform = $mform->get_data()) { + + // Retrieve username. + $cmi5tenant = $fromform->cmi5tenant; + + if ($cmi5tenant != null) { + + // Make the new tenant and grab results. + // here is an aarea that could fail. Should we try catch or is that covered in the creat tenant call? + //TODO + $tenant = $createtenant($cmi5tenant); + + // 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'); + + $idresult = set_config('cmi5launchtenantid', $id, $plugin = 'cmi5launch'); + + if ($idresult && $result) { + + // If result is true then redirect back to settings page. + // except now we dont want to redirect to settings! We want to go to + // The TOKEN setup form + // Wait, maybe it should do this automatically? Like they don't need to enter it sine we are making this make it for them, and we don't need them to + // press a button on a new form JUST to make a token. Lets do it behind the scenes and they can retrieve it if they want through an + //echo or settings page? +// $settingurl = new moodle_url($CFG->wwwroot . '/' . 'admin/settings.php', array('section' => 'modsettingcmi5launch')); + redirect(url: $CFG->wwwroot . '/mod/cmi5launch/tokensetup.php', message: 'Tenant made and saved successfully'); + + // 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 (cannot reuse old tenant names)."; + + 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; + + } + +} +; +echo $OUTPUT->header(); + +// Display the form. +$mform->display(); +echo $OUTPUT->footer(); +?> + + +
+ + +
+ diff --git a/tokensetup.php b/tokensetup.php new file mode 100644 index 0000000..4ee112d --- /dev/null +++ b/tokensetup.php @@ -0,0 +1,119 @@ +. + +/** + * 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; + + + +require_once(dirname(dirname(dirname(__FILE__))) . '/config.php'); +require_once($CFG->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'); +// Include our class file +require_once($CFG->dirroot.'/mod/cmi5launch/classes/local/token_form.php'); +// Tell moodle about our page, tell it what the url is.\\ +$PAGE->set_url('/mod/cmi5launch/tokensetup.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: 'Creating a tenant'); +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(); + + // 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 token 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 we freeze and alert user with a window with 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 we freeze and alert user with a window with 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; + +} + + +//whenyou want to out put html use the moodle core output rendereer: often overridden in theme +echo $OUTPUT->header(); + +// we want to display a form +$mform = new setup_token(); +echo $OUTPUT->footer(); +?> + + +
+ + +
+