Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 5ece06f

Browse files
author
Dmitry Kochnev
committed
[FEATURE] support url lrnsid for session detail report and submit_url for Items API
1 parent 3edc50e commit 5ece06f

File tree

2 files changed

+184
-138
lines changed

2 files changed

+184
-138
lines changed

classes/Learnosity/Shortcodes/ItemsEmbed.php

Lines changed: 172 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -8,145 +8,180 @@
88
class ItemsEmbed
99
{
1010

11-
private $config;
12-
private $security;
11+
private $config;
12+
private $security;
13+
private $sessionId;
1314

1415
private $student_prefix;
1516

16-
public function __construct($options,$mode)
17-
{
18-
$this->student_prefix = get_option('lrn_student_prefix','student_');
19-
20-
$this->security = array(
21-
'consumer_key' => get_option('lrn_consumer_key'),
22-
'domain' => $_SERVER['SERVER_NAME'],
23-
'timestamp' => gmdate('Ymd-Hi')
24-
);
25-
$defaults = array(
26-
'activityid' => \UUID::generateUuid(),
27-
'autorender' => true,
28-
'name' => 'My Activity',
29-
'rendersubmit' => false,
30-
'sessionid' => \UUID::generateUuid(),
31-
'state' => 'initial',
32-
'studentid' => $this->student_prefix . get_current_user_id(),
33-
'type' => get_option('lrn_default_type','submit_practice')
34-
);
35-
$options = $this->parse_options($options);
36-
$this->config = array_merge($defaults, $options);
37-
38-
//Force ther rendering type based based on mode called
39-
// lrn-items:inline or lrn-assess:assess
40-
$this->config['renderingtype'] = $mode;
41-
42-
}
43-
44-
public function render()
45-
{
46-
ob_start();
47-
$this->render_init_js($this->config);
48-
49-
if($this->config['renderingtype'] == 'inline'){
50-
//In Inline mode
51-
if ($this->config['autorender']) {
52-
$this->render_items(
53-
$this->items_attr_to_array($this->config['items']),
54-
$this->config['rendersubmit']
55-
);
56-
}
57-
}else{
58-
//We are in Assess mode.
59-
if ($this->config['autorender']) {
60-
$this->render_assess(
61-
$this->items_attr_to_array($this->config['items']),
62-
$this->config['rendersubmit']
63-
);
64-
}
65-
66-
}
67-
return ob_get_clean();
68-
}
69-
70-
private function generate_signed_request()
71-
{
72-
$request = array(
73-
'user_id' => $this->config['studentid'],
74-
'rendering_type' => $this->config['renderingtype'],
75-
'name' => $this->config['name'],
76-
'state' => $this->config['state'],
77-
'activity_id' => $this->config['activityid'],
78-
'session_id' => $this->config['sessionid'],
79-
'type' => $this->config['type'],
80-
'config' => array(
81-
'renderSubmitButton' => $this->config['rendersubmit'],
82-
)
83-
);
84-
85-
// If items defined then add them to the request
86-
if($this->config['items']){
87-
$request['items'] = $this->items_attr_to_array($this->config['items']);
88-
}
89-
90-
// If activitytemplateid then add it to the request
91-
if($this->config['activitytemplateid']){
92-
$request['activity_template_id'] = $this->config['activitytemplateid'];
93-
}
94-
95-
96-
$request_helper = new \RequestHelper(
97-
'items',
98-
$this->security,
99-
get_option('lrn_consumer_secret'),
100-
$request
101-
);
102-
$signed_request = $request_helper->generateRequest();
103-
if (isset($this->config['activitytemplateid'])) {
104-
$signed_request = json_decode($signed_request, true);
105-
$ati = $this->config['activitytemplateid'];
106-
$signed_request['request']['activity_template_id'] = $ati;
107-
$signed_request = json_encode($signed_request);
108-
}
109-
return $signed_request;
110-
}
111-
112-
private function items_attr_to_array($items_string)
113-
{
114-
$items_string = preg_replace('/\s+/', '', $items_string);
115-
return explode(',', $items_string);
116-
}
117-
118-
private function parse_boolean($str_val)
119-
{
120-
return $str_val === 'true' ||
121-
($str_val !== 'false' && intval($str_val) > 0);
122-
}
123-
124-
private function parse_options($options)
125-
{
126-
$booleanOptions = ['autorender', 'rendersubmit'];
127-
foreach ($booleanOptions as $i => $option) {
128-
if (isset($options[$option])) {
129-
$str_val = $options[$option];
130-
$options[$option] = $this->parse_boolean($str_val);
131-
}
132-
}
133-
return $options;
134-
}
135-
136-
private function render_init_js()
137-
{
138-
$signed_request = $this->generate_signed_request($this->config);
139-
include(__DIR__ . '/../../../templates/init-items-js.php');
140-
}
141-
142-
private function render_items($references, $should_render_submit)
143-
{
144-
include(__DIR__ . '/../../../templates/items.php');
145-
}
146-
147-
private function render_assess()
148-
{
149-
include(__DIR__ . '/../../../templates/assess.php');
150-
}
17+
public function __construct($options, $mode)
18+
{
19+
$this->student_prefix = get_option('lrn_student_prefix', 'student_');
20+
21+
$this->security = array(
22+
'consumer_key' => get_option('lrn_consumer_key'),
23+
'domain' => $_SERVER['SERVER_NAME'],
24+
'timestamp' => gmdate('Ymd-Hi')
25+
);
26+
27+
$this->sessionId = \UUID::generateUuid();
28+
29+
$defaults = array(
30+
'activityid' => \UUID::generateUuid(),
31+
'autorender' => true,
32+
'name' => 'My Activity',
33+
'rendersubmit' => false,
34+
'sessionid' => $this->sessionId,
35+
'state' => 'initial',
36+
'studentid' => $this->student_prefix . get_current_user_id(),
37+
'type' => get_option('lrn_default_type', 'submit_practice')
38+
);
39+
$options = $this->parse_options($options);
40+
$this->config = array_merge($defaults, $options);
41+
42+
//Force ther rendering type based based on mode called
43+
// lrn-items:inline or lrn-assess:assess
44+
$this->config['renderingtype'] = $mode;
45+
46+
}
47+
48+
public function render()
49+
{
50+
ob_start();
51+
$this->render_init_js($this->config);
52+
53+
if ($this->config['renderingtype'] == 'inline') {
54+
//In Inline mode
55+
if ($this->config['autorender']) {
56+
$this->render_items(
57+
$this->items_attr_to_array($this->config['items']),
58+
$this->config['rendersubmit']
59+
);
60+
}
61+
} else {
62+
//We are in Assess mode.
63+
if ($this->config['autorender']) {
64+
$this->render_assess(
65+
$this->items_attr_to_array($this->config['items']),
66+
$this->config['rendersubmit']
67+
);
68+
}
69+
70+
}
71+
return ob_get_clean();
72+
}
73+
74+
private function generate_signed_request()
75+
{
76+
$request = array(
77+
'user_id' => $this->config['studentid'],
78+
'rendering_type' => $this->config['renderingtype'],
79+
'name' => $this->config['name'],
80+
'state' => $this->config['state'],
81+
'activity_id' => $this->config['activityid'],
82+
'session_id' => $this->config['sessionid'],
83+
'type' => $this->config['type'],
84+
'config' => array(
85+
'renderSubmitButton' => $this->config['rendersubmit'],
86+
)
87+
);
88+
89+
// do not overwrite the value set up for activity_template_id (no else clause)
90+
// config.configuration.onsubmit_redirect_url
91+
if (isset($this->config['onsubmit_redirect_url']) AND !empty($this->config['onsubmit_redirect_url'])) {
92+
$onsubmit_redirect_url = $this->config['onsubmit_redirect_url'];
93+
// parsing URL properly (to respect anchor if exists)
94+
$url_parsed = parse_url($onsubmit_redirect_url);
95+
// if there are parameters in query part (all coming after ? in request)
96+
if (count($url_parsed['query']) > 0) {
97+
// adding learnosity parameter with "&" prefix
98+
// using html_entity_decode as last & is converted to "&"
99+
if (substr(html_entity_decode($url_parsed['query']), -1) != '&') {
100+
$url_parsed['query'] .= '&lrnsid=' . $this->sessionId;
101+
} else {
102+
$url_parsed['query'] .= 'lrnsid=' . $this->sessionId;
103+
}
104+
} else {
105+
// adding just learnosity parameter as the only parameter
106+
$url_parsed['query'] = 'lrnsid=' . $this->sessionId;
107+
}
108+
109+
// building new URL with new 'lrnsid' parameter added
110+
// TODO
111+
// http://php.net/manual/fa/function.http-build-url.php
112+
// right now no support for user/password/port/anchor
113+
$request['config']['configuration']['onsubmit_redirect_url'] = html_entity_decode(
114+
$url_parsed["scheme"] . '://' .
115+
$url_parsed["host"] .
116+
$url_parsed["path"] . '?' .
117+
$url_parsed["query"]);
118+
}
119+
120+
// If items defined then add them to the request
121+
if ($this->config['items']) {
122+
$request['items'] = $this->items_attr_to_array($this->config['items']);
123+
}
124+
125+
// If activitytemplateid then add it to the request
126+
if ($this->config['activitytemplateid']) {
127+
$request['activity_template_id'] = $this->config['activitytemplateid'];
128+
}
129+
130+
131+
$request_helper = new \RequestHelper(
132+
'items',
133+
$this->security,
134+
get_option('lrn_consumer_secret'),
135+
$request
136+
);
137+
$signed_request = $request_helper->generateRequest();
138+
if (isset($this->config['activitytemplateid'])) {
139+
$signed_request = json_decode($signed_request, true);
140+
$ati = $this->config['activitytemplateid'];
141+
$signed_request['request']['activity_template_id'] = $ati;
142+
$signed_request = json_encode($signed_request);
143+
}
144+
return $signed_request;
145+
}
146+
147+
private function items_attr_to_array($items_string)
148+
{
149+
$items_string = preg_replace('/\s+/', '', $items_string);
150+
return explode(',', $items_string);
151+
}
152+
153+
private function parse_boolean($str_val)
154+
{
155+
return $str_val === 'true' ||
156+
($str_val !== 'false' && intval($str_val) > 0);
157+
}
158+
159+
private function parse_options($options)
160+
{
161+
$booleanOptions = ['autorender', 'rendersubmit'];
162+
foreach ($booleanOptions as $i => $option) {
163+
if (isset($options[$option])) {
164+
$str_val = $options[$option];
165+
$options[$option] = $this->parse_boolean($str_val);
166+
}
167+
}
168+
return $options;
169+
}
170+
171+
private function render_init_js()
172+
{
173+
$signed_request = $this->generate_signed_request($this->config);
174+
include(__DIR__ . '/../../../templates/init-items-js.php');
175+
}
176+
177+
private function render_items($references, $should_render_submit)
178+
{
179+
include(__DIR__ . '/../../../templates/items.php');
180+
}
181+
182+
private function render_assess()
183+
{
184+
include(__DIR__ . '/../../../templates/assess.php');
185+
}
151186

152187
}

classes/Learnosity/Shortcodes/ReportEmbed.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function __construct($options)
3333
'timestamp' => gmdate('Ymd-Hi')
3434
);
3535

36+
//Handling URL parameters
37+
$lrnsid = $this->get_url_parameter('lrnsid','');
38+
3639
$defaults = array(
3740
'id' => $this->report_id,
3841
'type' => '',
@@ -48,7 +51,7 @@ public function __construct($options)
4851

4952
//settings for session-detail-by-item
5053
'user_id' => get_current_user_id(),
51-
'session_id' => '',
54+
'session_id' => $lrnsid,
5255
'show_correct_answers' => 'true',
5356
);
5457

@@ -74,6 +77,14 @@ public function render()
7477
return ob_get_clean();
7578
}
7679

80+
private function get_url_parameter($key, $default = '')
81+
{
82+
if (!isset($_GET[$key]) || empty($_GET[$key])) {
83+
return $default;
84+
}
85+
return strip_tags((string) wp_unslash($_GET[$key]));
86+
}
87+
7788
private function get_user_name($user_id)
7889
{
7990
$user_info = get_userdata($user_id);

0 commit comments

Comments
 (0)