diff --git a/front/surveyquestion.form.php b/front/surveyquestion.form.php index c72c9bb..24a32d7 100644 --- a/front/surveyquestion.form.php +++ b/front/surveyquestion.form.php @@ -32,6 +32,8 @@ Session::checkLoginUser(); +global $GLPI_CACHE; + $question = new PluginSatisfactionSurveyQuestion(); if (isset($_POST["add"])) { @@ -42,6 +44,9 @@ } else if (isset($_POST["update"])) { $question->check($_POST['id'], UPDATE); $question->update($_POST); + //Delete cache if exist + $ref_cache_key = sprintf('plugin_satisfaction_question_%d_type', $_POST['id']); + $GLPI_CACHE->delete($ref_cache_key); Html::back(); } else if (isset($_POST["delete"])) { diff --git a/hook.php b/hook.php index a75c801..fb25b70 100644 --- a/hook.php +++ b/hook.php @@ -27,6 +27,8 @@ -------------------------------------------------------------------------- */ +use Glpi\RichText\RichText; + /** * @return bool */ @@ -116,3 +118,139 @@ function plugin_satisfaction_uninstall() { return true; } + +function plugin_satisfaction_giveItem($type, $ID, $data, $num) { + global $DB, $GLPI_CACHE; + $dbu = new DbUtils(); + $searchopt = &Search::getOptions($type); + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; + $html_output = (strpos($_SERVER['REQUEST_URI'], "front/report.dynamic.php") === false); + + $out = ''; + if ( + $type == Ticket::class && + ($table . '.' . $field) == "glpi_plugin_satisfaction_surveyanswers.answer" + ) { + + if (null === $data[$num][0]['name']) { + return ' '; + } elseif ($data[$num][0]['name']) { + + $cellContent = explode(PluginSatisfactionSurveyQuestion::SEPARATOR, $data[$num][0]['name']); + $surveyResponses = $dbu->importArrayFromDB($cellContent[0]); + $response = null; + foreach ($surveyResponses as $surveyResponseKey => $surveyResponseValue) { + if ($surveyResponseKey == $cellContent[2]) { + $response = $surveyResponseValue; + break; + } + } + if ($response === null || $response === "") { + return ' '; + } + + //Get Survey question meta data in database (question type) + $ref_cache_key = sprintf('plugin_satisfaction_question_%d_type', $cellContent[2]); + //Get from cache for performance + $surveyType = "" . $GLPI_CACHE->get($ref_cache_key); + if (!$surveyType) { + $surveyQuestion = new PluginSatisfactionSurveyQuestion(); + $surveyQuestion->getFromDB($cellContent[2]); + $surveyType = $surveyQuestion->fields['type']; + $GLPI_CACHE->set($ref_cache_key, $surveyType, new \DateInterval('P1D')); + } + + switch ($surveyType) { + case 'yesno': + return \Dropdown::getYesNo($response); + + case 'note': + if ($html_output) { + return TicketSatisfaction::displaySatisfaction($response); + } + + default: + return RichText::getTextFromHtml($response, true, true, $html_output); + } + } + } + + return $out; +} + +function plugin_satisfaction_getAddSearchOptionsNew($itemtype) { + global $DB; + + $options = []; + if ($itemtype == Ticket::class) { + + //Get all questions + $query = [ + 'FIELDS' => [ + 'glpi_plugin_satisfaction_surveyquestions' => ['id', 'name'], + 'glpi_plugin_satisfaction_surveys' => ['entities_id', 'is_recursive'] + ], + 'FROM' => 'glpi_plugin_satisfaction_surveyquestions', + 'LEFT JOIN' => [ + 'glpi_plugin_satisfaction_surveys' => [ + 'FKEY' => [ + 'glpi_plugin_satisfaction_surveys' => 'id', + 'glpi_plugin_satisfaction_surveyquestions' => 'plugin_satisfaction_surveys_id' + ] + ] + ], + 'WHERE' => ['is_active' => 1], + 'ORDER' => 'glpi_plugin_satisfaction_surveyquestions.id ASC' + ]; + $iterator = $DB->request($query); + $questionsArray = []; + foreach ($iterator as $data) { + if ( + isset($_SESSION['glpiactive_entity']) && + + ( + ($data['is_recursive'] == 1 && + in_array($_SESSION['glpiactive_entity'], getSonsOf(Entity::getTable(), $data['entities_id'])) + ) + || + ($data['is_recursive'] == 0 && + $_SESSION['glpiactive_entity'] == $data['entities_id']) + ) + ) { + $questionsArray[$data['id']] = $data['name']; + } + + } + + foreach ($questionsArray as $key => $question) { + $options[] = [ + 'id' => sprintf('520%02d', $key), + 'table' => 'glpi_plugin_satisfaction_surveyanswers', + 'field' => 'answer', + 'name' => $question, + 'jointype' => 'child', + 'linkfield' => 'id', + 'nosearch' => true, + 'joinparams' => [ + 'jointype' => 'child', + 'linkfield' => 'ticketsatisfactions_id', + 'beforejoin' => [ + [ + 'table' => 'glpi_ticketsatisfactions', + 'jointype' => '', + ], + ] + ], + 'computation' => + '(CONCAT(answer, "' . PluginSatisfactionSurveyQuestion::SEPARATOR . + '", plugin_satisfaction_surveys_id, "' . PluginSatisfactionSurveyQuestion::SEPARATOR . + '", "' . $key . '"))', + ]; + + + } + + } + return $options; +} diff --git a/inc/surveyquestion.class.php b/inc/surveyquestion.class.php index f66d284..92ddb1e 100644 --- a/inc/surveyquestion.class.php +++ b/inc/surveyquestion.class.php @@ -47,6 +47,8 @@ class PluginSatisfactionSurveyQuestion extends CommonDBChild { CONST TEXTAREA = 'textarea'; CONST NOTE = 'note'; + CONST SEPARATOR = '|==|'; + /** * Return the localized name of the current Type * Should be overloaded in each new class