Completed
Pull Request — master (#339)
by
unknown
02:39
created

oc_player.php ➔ bigbluebuttonbn_create_lti_parameters()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * View for BigBlueButton interaction.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    2021 Farbod Zamani Boroujeni - ELAN e.V.
24
 */
25
26
global $PAGE, $OUTPUT, $CFG;
27
28
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
29
require_once(dirname(__FILE__).'/locallib.php');
30
31
require_once($CFG->dirroot . '/mod/lti/locallib.php');
32
require_once($CFG->dirroot . '/lib/oauthlib.php');
33
34
$identifier = required_param('identifier', PARAM_TEXT);
35
$bn = optional_param('bn', 0, PARAM_INT);
36
37
$bbbviewinstance = bigbluebuttonbn_view_validator(null, $bn);
38
if (!$bbbviewinstance) {
39
    print_error(get_string('view_error_url_missing_parameters', 'bigbluebuttonbn'));
40
}
41
42
$cm = $bbbviewinstance['cm'];
43
$course = $bbbviewinstance['course'];
44
$bigbluebuttonbn = $bbbviewinstance['bigbluebuttonbn'];
45
$context = context_module::instance($cm->id);
46
47
require_login($course, true, $cm);
48
49
// Capability check.
50
require_capability('mod/bigbluebuttonbn:view', $context);
51
52
$baseurl = new moodle_url('/mod/bigbluebuttonbn/oc_player.php', array('identifier' => $identifier, 'bn' => $bn));
53
$PAGE->set_url($baseurl);
54
$PAGE->set_context($context);
55
$PAGE->set_pagelayout('incourse');
56
$PAGE->set_title(format_string($bigbluebuttonbn->name));
57
$PAGE->set_heading($course->fullname);
58
59
// Get endpoint from engageurl setting of filter_opencast plugin or apiurl setting of tool_opencast plugin.
60
$endpoint = get_config('filter_opencast', 'engageurl');
61
if (empty($endpoint)) {
62
    $endpoint = get_config('tool_opencast', 'apiurl');
63
}
64
65
// Get player url either from playerurl setting or default paella.
66
$playerurl = get_config('filter_opencast', 'playerurl');
67
if (empty($playerurl)) {
68
    $playerurl = '/play/' . $identifier;
69
} else {
70
    $playerurl .= '?id=' . $identifier;
71
}
72
73
if (strpos($endpoint, 'http') !== 0) {
74
    $endpoint = 'http://' . $endpoint;
75
}
76
77
$ltiendpoint = rtrim($endpoint, '/') . '/lti';
78
79
// Create parameters.
80
$params = bigbluebuttonbn_create_lti_parameters($ltiendpoint, $playerurl);
81
82
echo $OUTPUT->header();
83
echo $OUTPUT->heading(format_string($bigbluebuttonbn->name));
84
echo render_lti_form($ltiendpoint, $params);
85
86
$PAGE->requires->js_call_amd('mod_bigbluebuttonbn/mod_lti_form_handler', 'init');
87
echo $OUTPUT->footer();
88
89
/**
90
 * Create necessary lti parameters.
91
 * @param string $endpoint of the opencast instance.
92
 * @param string $playerurl the player url to pass as custom_tool in lti params
93
 *
94
 * @return array lti parameters
95
 * @throws dml_exception
96
 * @throws moodle_exception
97
 */
98
function bigbluebuttonbn_create_lti_parameters($endpoint, $playerurl) {
99
    global $CFG, $COURSE, $USER;
100
101
    // Get consumerkey and consumersecret from filter_opencast.
102
    $consumerkey = get_config('filter_opencast', 'consumerkey');
103
    $consumersecret = get_config('filter_opencast', 'consumersecret');
104
105
    $helper = new oauth_helper(array('oauth_consumer_key'    => $consumerkey,
106
                                    'oauth_consumer_secret' => $consumersecret));
107
108
    // Set all necessary parameters.
109
    $params = array();
110
    $params['oauth_version'] = '1.0';
111
    $params['oauth_nonce'] = $helper->get_nonce();
112
    $params['oauth_timestamp'] = $helper->get_timestamp();
113
    $params['oauth_consumer_key'] = $consumerkey;
114
115
    $params['context_id'] = $COURSE->id;
116
    $params['context_label'] = trim($COURSE->shortname);
117
    $params['context_title'] = trim($COURSE->fullname);
118
    $params['resource_link_id'] = 'o' . random_int(1000, 9999) . '-' . random_int(1000, 9999);
119
    $params['resource_link_title'] = 'Opencast';
120
    $params['context_type'] = ($COURSE->format == 'site') ? 'Group' : 'CourseSection';
121
    $params['launch_presentation_locale'] = current_language();
122
    $params['ext_lms'] = 'moodle-2';
123
    $params['tool_consumer_info_product_family_code'] = 'moodle';
124
    $params['tool_consumer_info_version'] = strval($CFG->version);
125
    $params['oauth_callback'] = 'about:blank';
126
    $params['lti_version'] = 'LTI-1p0';
127
    $params['lti_message_type'] = 'basic-lti-launch-request';
128
    $urlparts = parse_url($CFG->wwwroot);
129
    $params['tool_consumer_instance_guid'] = $urlparts['host'];
130
    $params['custom_tool'] = $playerurl;
131
132
    // User data.
133
    $params['user_id'] = $USER->id;
134
    $params['lis_person_name_given'] = $USER->firstname;
135
    $params['lis_person_name_family'] = $USER->lastname;
136
    $params['lis_person_name_full'] = $USER->firstname . ' ' . $USER->lastname;
137
    $params['ext_user_username'] = $USER->username;
138
    $params['lis_person_contact_email_primary'] = $USER->email;
139
    $params['roles'] = lti_get_ims_role($USER, null, $COURSE->id, false);
140
141
    if (!empty($CFG->mod_lti_institution_name)) {
142
        $params['tool_consumer_instance_name'] = trim(html_to_text($CFG->mod_lti_institution_name, 0));
143
    } else {
144
        $params['tool_consumer_instance_name'] = get_site()->shortname;
145
    }
146
147
    $params['launch_presentation_document_target'] = 'iframe';
148
    $params['oauth_signature_method'] = 'HMAC-SHA1';
149
    $signedparams = lti_sign_parameters($params, $endpoint, "POST", $consumerkey, $consumersecret);
150
    $params['oauth_signature'] = $signedparams['oauth_signature'];
151
152
    return $params;
153
}
154
155
/**
156
 * Display the lti form.
157
 *
158
 * @param string $endpoint of the opencast instance.
159
 * @param array $params lti parameters.
160
 * @return string
161
 */
162
function render_lti_form($endpoint, $params) {
163
    $content = "<form action=\"" . $endpoint .
164
        "\" name=\"ltiLaunchForm\" id=\"ltiLaunchForm\" method=\"post\" encType=\"application/x-www-form-urlencoded\">\n";
165
166
    // Construct html form for the launch parameters.
167
    foreach ($params as $key => $value) {
168
        $key = htmlspecialchars($key);
169
        $value = htmlspecialchars($value);
170
        $content .= "<input type=\"hidden\" name=\"{$key}\"";
171
        $content .= " value=\"";
172
        $content .= $value;
173
        $content .= "\"/>\n";
174
    }
175
    $content .= "</form>\n";
176
177
    return $content;
178
}
179