-
Notifications
You must be signed in to change notification settings - Fork 5
/
block_onlinesurvey.php
318 lines (270 loc) · 11.8 KB
/
block_onlinesurvey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* Plugin "Evaluations (evasys)"
*
* @package block_onlinesurvey
* @copyright 2018 Soon Systems GmbH on behalf of evasys GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/lti/locallib.php');
require_once(__DIR__ . '/locallib.php');
/**
* Onlinesurvey block.
*
* @package block_onlinesurvey
* @copyright 2018 Soon Systems GmbH on behalf of evasys GmbH
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_onlinesurvey extends block_base {
/** @var boolean Indicates whether the DEBUG mode is set or not */
private $debugmode;
/** @var boolean Indicates whether the block is minimally configured or not */
private $isconfigured;
/** @var String Error string */
private $error;
/** @var String Type of connection - LTI or SOAP */
private $connectiontype;
/** @var String Path to .wsdl used for SOAP */
private $wsdl;
/** @var Int Id of the current user */
private $moodleuserid;
/**
* Initialises the block.
*
* @return void
*/
public function init() {
$this->title = get_string('pluginname', 'block_onlinesurvey');
// Block settings.
$config = get_config("block_onlinesurvey");
if (isset($config) && !empty($config)) {
// Get block title from block setting.
if (!empty($config->blocktitle)) {
$this->title = format_string($config->blocktitle);
}
$this->connectiontype = (!empty($config->connectiontype)) ? $config->connectiontype : '';
$this->debugmode = (!empty($config->survey_debug)) ? $config->survey_debug : false;
// Session information.
global $USER;
if ($this->moodleuserid = $USER->id) {
if ($this->connectiontype == 'SOAP') {
$this->wsdl = $config->survey_server;
// Quick check of SOAP settings.
if (empty($config->survey_server) || empty($config->survey_login) || empty($config->survey_user) ||
empty($config->survey_pwd)) {
$this->isconfigured = false;
$this->error = get_string('error_soap_settings_error', 'block_onlinesurvey');
$errorinfo = '';
if (empty($config->survey_server)) {
$errorinfo .= get_string('error_survey_server_missing', 'block_onlinesurvey').'<br>';
}
if (empty($config->survey_login)) {
$errorinfo .= get_string('error_survey_login_missing', 'block_onlinesurvey').'<br>';
}
if (empty($config->survey_user)) {
$errorinfo .= get_string('error_survey_user_missing', 'block_onlinesurvey').'<br>';
}
if (empty($config->survey_pwd)) {
$errorinfo .= get_string('error_survey_pwd_missing', 'block_onlinesurvey').'<br>';
}
$context = context_system::instance();
if (has_capability('block/onlinesurvey:view_debugdetails', $context)) {
if (!empty($errorinfo)) {
$this->error .= "<br>".$errorinfo;
}
}
} else {
$this->isconfigured = true;
}
// Parse wsdlnamespace from the wsdl url.
preg_match('/\/([^\/]+\.wsdl)$/', $this->wsdl, $matches);
if (count($matches) == 2) {
$this->isconfigured = true;
} else {
$this->isconfigured = false;
$this->error = "WSDL namespace parse error";
}
} else if ($this->connectiontype == 'LTI' || $this->connectiontype == LTI_VERSION_1P3) {
// Quick check of some LTI settings.
$this->isconfigured = true;
$errorinfo = '';
if (empty($config->lti_url)) {
$errorinfo .= get_string('error_lti_url_missing', 'block_onlinesurvey').'<br>';
}
if (empty($config->lti_password) && $this->connectiontype == 'LTI') {
$errorinfo .= get_string('error_lti_password_missing', 'block_onlinesurvey').'<br>';
}
if (empty($config->lti_learnermapping)) {
$errorinfo .= get_string('error_lti_learnermapping_missing', 'block_onlinesurvey').'<br>';
}
if (!empty($errorinfo)) {
$this->isconfigured = false;
$this->error = get_string('error_lti_settings_error', 'block_onlinesurvey');
$context = context_system::instance();
if (has_capability('block/onlinesurvey:view_debugdetails', $context)) {
if (!empty($errorinfo)) {
$this->error .= "<br>" . $errorinfo;
}
}
}
}
} else {
$this->isconfigured = false;
$this->error = get_string('error_userid_not_found', 'block_onlinesurvey');
}
} else {
$this->error = get_string('error_config_not_accessible', 'block_onlinesurvey');
$this->isconfigured = false;
}
}
/**
* Display the block content.
*
* @return void
*/
public function get_content() {
global $CFG, $USER, $OUTPUT, $SESSION;
if (isset($SESSION->lti_state)) {
block_onlinesurvey_remove_outdated_cookies($SESSION->lti_state);
}
// Block settings.
$config = get_config("block_onlinesurvey");
$context = context_system::instance();
if (! has_capability('block/onlinesurvey:view', $context)) {
$this->content = null;
return $this->content;
}
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$this->content->text = '';
if ($this->moodleuserid && $this->isconfigured) {
$data = [];
$data['showspinner'] = $config->show_spinner;
$context = $this->page->context;
$course = $this->page->course;
$urlparams = 'ctxid='.$context->id.'&cid='.$course->id;
$url = $CFG->wwwroot.'/blocks/onlinesurvey/show_surveys.php?'.$urlparams;
if ($config->connectiontype == 'LTI') {
$data['connectionclass'] = 'block_onlinesurvey_lti';
} else if ($config->connectiontype == 'SOAP') {
$data['connectionclass'] = 'block_onlinesurvey_soap';
}
if ($config->presentation == 'brief') {
$data['presentationclass'] = 'block_onlinesurvey_compact';
} else if ($config->presentation == 'detailed') {
$data['presentationclass'] = 'block_onlinesurvey_detailed';
}
// Testing reveals that the iframe requires the permissions "allow-same-origin allow-scripts",
// hence the sandbox attribute can not be used.
$data['source'] = $config->show_spinner ? 'about:blank' : $url;
$data['detailed'] = $config->presentation == 'detailed';
$data['zoom'] = $config->offer_zoom == true;
// If we are showing detailed mode.
if ($config->presentation == 'detailed' && $config->offer_zoom != true) {
$this->page->requires->css('/blocks/onlinesurvey/style/block_onlinesurvey_offerzoom.css');
}
$this->content->text = $OUTPUT->render_from_template('block_onlinesurvey/block_content', $data);
if ($config->survey_popupinfo_title != '') {
$popupinfotitle = format_string($config->survey_popupinfo_title);
} else {
$popupinfotitle = get_string('setting_survey_popupinfo_title_default', 'block_onlinesurvey');
}
if ($config->survey_popupinfo_content != '') {
$popupinfocontent = format_text($config->survey_popupinfo_content);
} else {
$popupinfocontent = get_string('setting_survey_popupinfo_content_default', 'block_onlinesurvey');
}
$this->page->requires->js_call_amd('block_onlinesurvey/modal-zoom', 'init',
array($popupinfotitle, $popupinfocontent, $USER->currentlogin));
$this->page->requires->css('/blocks/onlinesurvey/style/block_onlinesurvey_modal-zoom.css');
if ($config->show_spinner) {
$this->page->requires->js_call_amd('block_onlinesurvey/spinner', 'init', array($url));
$this->page->requires->css('/blocks/onlinesurvey/style/block_onlinesurvey_spinner.css');
}
if ($config->survey_hide_empty) {
$this->page->requires->css('/blocks/onlinesurvey/style/block_onlinesurvey_hide.css');
}
}
if (!empty($this->error)) {
$this->content->text = get_string('error_occured', 'block_onlinesurvey', $this->error);
}
return $this->content;
}
/**
* Returns true which means that the block has a settings.php file.
*
* @return bool
*/
public function has_config() {
return true;
}
function instance_allow_config() {
return true;
}
/**
* Returns false which means that only one instance is allowed.
*
* @return bool
*/
public function instance_allow_multiple() {
return false;
}
/**
* Returns false which means that the header should be shown.
*
* @return bool
*/
public function hide_header() {
return false;
}
/**
* Returns the locations where the block can be displayed.
*
* @return array
*/
public function applicable_formats() {
return array('my-index' => true, 'my' => true, 'site-index' => true, 'course-view' => true);
}
/**
* Returns the class $title var value.
*
* Intentionally doesn't check if a title is set.
*
* @return string $this->title
*/
public function get_title() {
return $this->title;
}
/**
* Return the plugin config settings for external functions.
*
* @return stdClass the configs for both the block instance and plugin
* @since Moodle 3.8
*/
public function get_config_for_external() {
// Return all settings for all users since it is safe (no private keys, etc..).
$instanceconfigs = !empty($this->config) ? $this->config : new stdClass();
$pluginconfigs = get_config('block_onlinesurvey');
return (object) [
'instance' => $instanceconfigs,
'plugin' => $pluginconfigs,
];
}
}