Skip to content

Commit

Permalink
Created a render function for the form as suggested in Opencast-Moodle#6
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaraGunkel committed Feb 15, 2018
1 parent ff82234 commit 7b36430
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
36 changes: 17 additions & 19 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Use lti to login and retrieve cookie from opencast.
*/
function filter_opencast_login() {
global $CFG, $PAGE, $COURSE, $USER;
global $PAGE;

// Get api url of opencast.
$endpoint = get_config('tool_opencast', 'apiurl');
Expand All @@ -39,6 +39,20 @@ function filter_opencast_login() {
}
$endpoint .= '/lti';

// Create parameters.
$params = filter_opencast_create_parameters($endpoint);

// Render form.
$renderer = $PAGE->get_renderer('filter_opencast');
echo $renderer->render_player($endpoint, $params);

// Submit form.
$PAGE->requires->js_call_amd('filter_opencast/form', 'init');
}

function filter_opencast_create_parameters() {
global $CFG, $COURSE, $USER;

// Get consumerkey and consumersecret.
$consumerkey = get_config('filter_opencast', 'consumerkey');
$consumersecret = get_config('filter_opencast', 'consumersecret');
Expand All @@ -57,7 +71,7 @@ function filter_opencast_login() {
$params['context_id'] = $COURSE->id;
$params['context_label'] = trim($COURSE->shortname);
$params['context_title'] = trim($COURSE->fullname);
$params['resource_link_id'] = 'o' . random_int(1000,9999) . '-' . random_int(1000,9999);
$params['resource_link_id'] = 'o' . random_int(1000, 9999) . '-' . random_int(1000, 9999);
$params['resource_link_title'] = 'Opencast';
$params['context_type'] = ($COURSE->format == 'site') ? 'Group' : 'CourseSection';
$params['lis_person_name_given'] = $USER->firstname;
Expand Down Expand Up @@ -85,21 +99,5 @@ function filter_opencast_login() {
$params['oauth_signature_method'] = 'HMAC-SHA1';
$params['oauth_signature'] = $helper->sign("POST", $endpoint, $params, $consumersecret . '&');

$content = "<form action=\"" . urlencode($endpoint) .
"\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";

// Construct html form for the launch parameters.
foreach ($params as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
$content .= "<input type=\"hidden\" name=\"{$key}\"";
$content .= " value=\"";
$content .= $value;
$content .= "\"/>\n";
}
$content .= "</form>\n";

echo $content;
// Submit form.
$PAGE->requires->js_call_amd('filter_opencast/form', 'init');
return $params;
}
24 changes: 24 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,28 @@ class filter_opencast_renderer extends plugin_renderer_base {
public function render_player($data) {
return $this->render_from_template('filter_opencast/player', $data);
}

/**
* Display the lti form.
*
* @param object $data The prepared variables.
* @return string
*/
public function render_lti_form($endpoint, $params) {
$content = "<form action=\"" . urlencode($endpoint) .
"\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";

// Construct html form for the launch parameters.
foreach ($params as $key => $value) {
$key = htmlspecialchars($key);
$value = htmlspecialchars($value);
$content .= "<input type=\"hidden\" name=\"{$key}\"";
$content .= " value=\"";
$content .= $value;
$content .= "\"/>\n";
}
$content .= "</form>\n";

return $content;
}
}

0 comments on commit 7b36430

Please sign in to comment.