|
8 | 8 | class ItemsEmbed
|
9 | 9 | {
|
10 | 10 |
|
11 |
| - private $config; |
12 |
| - private $security; |
| 11 | + private $config; |
| 12 | + private $security; |
| 13 | + private $sessionId; |
13 | 14 |
|
14 | 15 | private $student_prefix;
|
15 | 16 |
|
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 | + } |
151 | 186 |
|
152 | 187 | }
|
0 commit comments