-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsurveyquestion.class.php
More file actions
403 lines (346 loc) · 13.5 KB
/
surveyquestion.class.php
File metadata and controls
403 lines (346 loc) · 13.5 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/*
* @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
-------------------------------------------------------------------------
satisfaction plugin for GLPI
Copyright (C) 2016-2022 by the satisfaction Development Team.
https://github.com/pluginsglpi/satisfaction
-------------------------------------------------------------------------
LICENSE
This file is part of satisfaction.
satisfaction 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 2 of the License, or
(at your option) any later version.
satisfaction 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 satisfaction. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access directly to this file");
}
/**
* Class PluginSatisfactionSurveyQuestion
*/
class PluginSatisfactionSurveyQuestion extends CommonDBChild {
static $rightname = "plugin_satisfaction";
public $dohistory = true;
// From CommonDBChild
public static $itemtype = 'PluginSatisfactionSurvey';
public static $items_id = 'plugin_satisfaction_surveys_id';
CONST YESNO = 'yesno';
CONST TEXTAREA = 'textarea';
CONST NOTE = 'note';
CONST SEPARATOR = '|==|';
/**
* Return the localized name of the current Type
* Should be overloaded in each new class
*
* @return string
**/
static function getTypeName($nb = 0) {
return _n('Question', 'Questions', $nb, 'satisfaction');
}
/**
* Get Tab Name used for itemtype
*
* NB : Only called for existing object
* Must check right on what will be displayed + template
*
* @since version 0.83
*
* @param CommonDBTM|CommonGLPI $item CommonDBTM object for which the tab need to be displayed
* @param bool|int $withtemplate boolean is a template object ? (default 0)
*
* @return string tab name
*/
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
// can exists for template
if ($item->getType() == 'PluginSatisfactionSurvey') {
if ($_SESSION['glpishow_count_on_tabs']) {
$dbu = new DbUtils();
$table = $dbu->getTableForItemType(__CLASS__);
return self::createTabEntry(self::getTypeName(),
$dbu->countElementsInTable($table,
[self::$items_id => $item->getID()]));
}
return self::getTypeName();
}
return '';
}
/**
* show Tab content
*
* @since version 0.83
*
* @param $item CommonGLPI object for which the tab need to be displayed
* @param $tabnum integer tab number (default 1)
* @param bool|int $withtemplate boolean is a template object ? (default 0)
*
* @return true
*/
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item->getType() == 'PluginSatisfactionSurvey') {
self::showForSurvey($item, $withtemplate);
}
return true;
}
/**
* Question display
*
* @param \PluginSatisfactionSurvey $survey
* @param string $withtemplate
*
* @return bool
*/
public static function showForSurvey(PluginSatisfactionSurvey $survey, $withtemplate = '') {
global $CFG_GLPI;
$squestions_obj = new self();
$sID = $survey->fields['id'];
$rand_survey = mt_rand();
$canadd = Session::haveRight(self::$rightname, CREATE);
$canedit = Session::haveRight(self::$rightname, UPDATE);
$canpurge = Session::haveRight(self::$rightname, PURGE);
//check if answer exists to forbid edition
$answer = new PluginSatisfactionSurveyAnswer;
$found_answer = $answer->find([self::$items_id => $survey->fields['id']]);
if (count($found_answer) > 0) {
echo "<span style='font-weight:bold; color:red'>" . __('You cannot edit the questions when answers exists for this survey. Disable this survey and create a new one !', 'satisfaction') . "</span>";
$canedit = false;
$canadd = false;
$canpurge = false;
}
echo "<div id='viewquestion" . $sID . "$rand_survey'></div>\n";
if ($canadd) {
echo "<script type='text/javascript' >\n";
echo "function viewAddQuestion$sID$rand_survey() {\n";
$params = ['type' => __CLASS__,
'parenttype' => 'PluginSatisfactionSurvey',
self::$items_id => $sID,
'id' => -1];
Ajax::updateItemJsCode("viewquestion$sID$rand_survey",
$CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
echo "};";
echo "</script>\n";
echo "<div class='center'>" .
"<a href='javascript:viewAddQuestion$sID$rand_survey();'>";
echo __('Add a question', 'satisfaction') . "</a></div><br>\n";
}
// Display existing questions
$questions = $squestions_obj->find([self::$items_id => $sID], 'id');
if (count($questions) == 0) {
echo "<table class='tab_cadre_fixe'><tr class='tab_bg_2'>";
echo "<th class='b'>" . __('No questions for this survey', 'satisfaction') . "</th>";
echo "</tr></table>";
} else {
$rand = mt_rand();
if ($canpurge) {
//TODO : Detect delete to update history
Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
$massiveactionparams = ['item' => __CLASS__, 'container' => 'mass' . __CLASS__ . $rand];
Html::showMassiveActions($massiveactionparams);
}
echo "<table class='tab_cadre_fixehov'>";
echo "<tr>";
if ($canpurge) {
echo "<th width='10'>" . Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand) . "</th>";
}
echo "<th>" . self::getTypeName(2) . "</th>";
echo "<th>" . __('Type') . "</th></tr>";
foreach ($questions as $question) {
if ($squestions_obj->getFromDB($question['id'])) {
$squestions_obj->showOne($canedit, $canpurge, $rand_survey);
}
}
echo "</table>";
if ($canpurge) {
$paramsma['ontop'] = false;
Html::showMassiveActions($paramsma);
Html::closeForm();
}
}
}
/**
* @param $ID
* @param array $options
*
* @return bool
*/
function showForm($ID, $options = []) {
global $CFG_GLPI;
if (isset($options['parent']) && !empty($options['parent'])) {
$survey = $options['parent'];
}
$surveyquestion = new self();
if ($ID <= 0) {
$surveyquestion->getEmpty();
} else {
$surveyquestion->getFromDB($ID);
}
if (!$surveyquestion->canView()) {
return false;
}
echo "<form name='form' method='post' action='" . Toolbox::getItemTypeFormURL(self::getType()) . "'>";
echo "<div align='center'><table class='tab_cadre_fixe'>";
echo "<tr><th colspan='4'>" . __('Add a question', 'satisfaction') . "</th></tr>";
echo "<tr class='tab_bg_1'>";
echo "<td>" . self::getTypeName(1) . " :</td>";
echo "<td>";
echo Html::textarea([
'name' => 'name',
'value' => $surveyquestion->fields["name"],
'cols' => '50',
'rows' => '4',
'display' => false,
]);
echo "</td>";
echo Html::hidden(self::$items_id, ['value' =>$surveyquestion->fields[self::$items_id]]);
echo "</td>";
echo "<td rowspan='2'>" . __('Comments') . "</td>";
echo "<td rowspan='2'>";
echo Html::textarea([
'name' => 'comment',
'value' => $surveyquestion->fields["comment"],
'cols' => '60',
'rows' => '6',
'display' => false,
]);
echo "</td></tr>";
echo "<tr class='tab_bg_1'>";
echo "<td>" . __('Type') . "</td>";
echo "<td>";
$array = self::getQuestionTypeList();
Dropdown::showFromArray('type', $array, ['value' => $surveyquestion->fields['type'],
'on_change' => "plugin_satisfaction_loadtype(this.value, \"" . self::NOTE . "\");"]);
$script = "function plugin_satisfaction_loadtype(val, note){";
$script .= "if(val == note) {
$('#show_note').show();
} else {
$('#show_note').hide();
}";
$script .= "};";
echo Html::scriptBlock($script);
$style = ($surveyquestion->fields['type'] == self::NOTE) ? "" : "style='display: none '";
echo "</td>";
echo "</tr>";
echo "<tr class='tab_bg_1' id='show_note' $style>";
echo "<td>";
echo __('Note on', 'satisfaction');
echo "</td>";
echo "<td>";
Dropdown::showNumber('number', ['max' => 10,
'min' => 2,
'value' => $surveyquestion->fields['number'],
'on_change' => "plugin_satisfaction_load_defaultvalue(\"" . Plugin::getWebDir('satisfaction') . "\", this.value);"]);
echo "</td>";
if (!empty($surveyquestion->fields['number'])) {
$max_default_value = $surveyquestion->fields['number'];
} else {
$max_default_value = 2;
}
echo "<td>";
echo __('Default value');
echo "</td>";
echo "<td id='default_value'>";
Dropdown::showNumber('default_value', ['max' => $max_default_value,
'min' => 1,
'value' => $surveyquestion->fields['default_value']]);
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='tab_bg_2 center' colspan='4'>";
if ($ID <= 0) {
echo Html::hidden(self::$items_id, ['value' => $survey->getField('id')]);
echo Html::submit(_sx('button', 'Add'), ['name' => 'add', 'class' => 'btn btn-primary']);
} else {
echo Html::hidden('id', ['value' => $ID]);
echo Html::submit(_sx('button', 'Save'), ['name' => 'update', 'class' => 'btn btn-primary']);
}
echo "</td>";
echo "</tr>";
echo "</table>";
Html::closeForm();
}
/**
* Display line with name & type
*
* @param $canedit
* @param $rand
*/
function showOne($canedit, $canpurge, $rand) {
global $CFG_GLPI;
$style = '';
if ($canedit) {
$style = "style='cursor:pointer' onClick=\"viewEditQuestion" .
$this->fields[self::$items_id] .
$this->fields['id'] . "$rand();\"" .
" id='viewquestion" . $this->fields[self::$items_id] . $this->fields["id"] . "$rand'";
}
echo "<tr class='tab_bg_2' $style>";
if ($canpurge) {
echo "<td width='10'>";
Html::showMassiveActionCheckBox(__CLASS__, $this->fields["id"]);
echo "</td>";
}
if ($canedit) {
echo "\n<script type='text/javascript' >\n";
echo "function viewEditQuestion" . $this->fields[self::$items_id] . $this->fields["id"] . "$rand() {\n";
$params = ['type' => __CLASS__,
'parenttype' => self::$itemtype,
self::$items_id => $this->fields[self::$items_id],
'id' => $this->fields["id"]];
Ajax::updateItemJsCode("viewquestion" . $this->fields[self::$items_id] . "$rand",
$CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
echo "};";
echo "</script>\n";
}
$name = $this->fields["name"];
echo "<td class='left'>" . nl2br($name) . "</td>";
echo "<td class='left'>" . self::getQuestionType($this->fields["type"]) . "</td>";
echo "</tr>";
}
/**
* List of question types
*
* @return array
*/
static function getQuestionTypeList() {
$array = [];
$array[self::YESNO] = __('Yes') . '/' . __('No');
$array[self::TEXTAREA] = __('Text', 'satisfaction');
$array[self::NOTE] = __('Note', 'satisfaction');
return $array;
}
/**
* Return the type
*
* @return array
*/
static function getQuestionType($type) {
switch ($type) {
case self::YESNO :
return __('Yes') . '/' . __('No');
case self::TEXTAREA :
return __('Text', 'satisfaction');
case self::NOTE :
return __('Note', 'satisfaction');
}
return "";
}
/**
* Get the standard massive actions which are forbidden
*
* @since version 0.84
*
* @return an array of massive actions
**/
public function getForbiddenStandardMassiveAction() {
$forbidden = parent::getForbiddenStandardMassiveAction();
$forbidden[] = 'update';
return $forbidden;
}
}