Skip to content

Commit

Permalink
feat(forms): Adding request type category and related question type
Browse files Browse the repository at this point in the history
refactor: QuestionTypeRequestType to extend AbstractQuestionType
  • Loading branch information
ccailly committed Apr 26, 2024
1 parent aaedd26 commit 30bbb97
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Form/QuestionType/QuestionTypeCategory.php
Expand Up @@ -65,6 +65,11 @@ enum QuestionTypeCategory: string
*/
case URGENCY = "urgency";

/**
* Question that expect a request type
*/
case REQUEST_TYPE = "request_type";
/**
* Get category label
* @return string
Expand All @@ -77,6 +82,7 @@ public function getLabel(): string
self::DATE_AND_TIME => __("Date and time"),
self::ACTORS => __("Actors"),
self::URGENCY => __("Urgency"),
self::REQUEST_TYPE => __("Request type"),
};
}
}
141 changes: 141 additions & 0 deletions src/Form/QuestionType/QuestionTypeRequestType.php
@@ -0,0 +1,141 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Form\QuestionType;

use Glpi\Application\View\TemplateRenderer;
use Glpi\Form\Question;
use Override;
use Ticket;

/**
* Long answers are multiple lines inputs used to answer questions with as much details as needed.
*/
final class QuestionTypeRequestType extends AbstractQuestionType
{
/**
* Retrieve the default value for the request type question type
*
* @param Question|null $question The question to retrieve the default value from
* @return int
*/
public function getDefaultValue(?Question $question): int
{
if (
$question !== null
&& isset($question->fields['default_value'])
) {
if (is_int($question->fields['default_value'])) {
return $question->fields['default_value'];
} elseif (is_numeric($question->fields['default_value'])) {
return (int) $question->fields['default_value'];
}
}

return 0;
}

#[Override]
public function renderAdministrationTemplate(?Question $question): string
{
$template = <<<TWIG
{% import 'components/form/fields_macros.html.twig' as fields %}
{{ fields.dropdownArrayField(
'default_value',
value,
request_types,
'',
{
'init' : init,
'no_label' : true,
'display_emptychoice': true
}
) }}
TWIG;

$twig = TemplateRenderer::getInstance();
return $twig->renderFromStringTemplate($template, [
'init' => $question != null,
'value' => $this->getDefaultValue($question),
'request_types' => Ticket::getTypes()
]);
}

#[Override]
public function renderEndUserTemplate(Question $question): string
{
$template = <<<TWIG
{% import 'components/form/fields_macros.html.twig' as fields %}
{{ fields.dropdownArrayField(
'answers[' ~ question.fields.id ~ ']',
value,
request_types,
'',
{
'no_label' : true,
'display_emptychoice': true
}
) }}
TWIG;

$twig = TemplateRenderer::getInstance();
return $twig->renderFromStringTemplate($template, [
'value' => $this->getDefaultValue($question),
'question' => $question,
'request_types' => Ticket::getTypes()
]);
}

#[Override]
public function renderAnswerTemplate($answer): string
{
$template = <<<TWIG
<div class="form-control-plaintext">{{ answer }}</div>
TWIG;

$twig = TemplateRenderer::getInstance();
return $twig->renderFromStringTemplate($template, [
'answer' => Ticket::getTicketTypeName($answer)
]);
}

#[Override]
public function getCategory(): QuestionTypeCategory
{
return QuestionTypeCategory::REQUEST_TYPE;
}
}
5 changes: 4 additions & 1 deletion tests/functional/Glpi/Form/AnswersSet.php
Expand Up @@ -47,6 +47,7 @@
use Glpi\Form\QuestionType\QuestionTypeEmail;
use Glpi\Form\QuestionType\QuestionTypeLongText;
use Glpi\Form\QuestionType\QuestionTypeNumber;
use Glpi\Form\QuestionType\QuestionTypeRequestType;
use Glpi\Form\QuestionType\QuestionTypeShortText;
use Glpi\Form\QuestionType\QuestionTypesManager;
use Glpi\Form\QuestionType\QuestionTypeTime;
Expand Down Expand Up @@ -257,6 +258,7 @@ public function testShowForm(): void
->addQuestion("Observer", QuestionTypeObserver::class)
->addQuestion("Assignee", QuestionTypeAssignee::class)
->addQuestion("Urgency", QuestionTypeUrgency::class)
->addQuestion("Request type", QuestionTypeRequestType::class)
);
$answers_set = $answers_handler->saveAnswers($form, [
$this->getQuestionId($form, "Name") => "Pierre Paul Jacques",
Expand All @@ -279,7 +281,8 @@ public function testShowForm(): void
Group::getForeignKeyField() . '-1',
Supplier::getForeignKeyField() . '-1'
],
$this->getQuestionId($form, "Urgency") => 2
$this->getQuestionId($form, "Urgency") => 2,
$this->getQuestionId($form, "Request type") => 1
], \Session::getLoginUserID());

// Ensure we used every possible questions types
Expand Down
Expand Up @@ -38,6 +38,7 @@
use DbTestCase;
use Glpi\Form\QuestionType\QuestionTypeInterface;
use Glpi\Form\QuestionType\QuestionTypeCategory;
use Glpi\Form\QuestionType\QuestionTypeRequestType;

class QuestionTypesManager extends DbTestCase
{
Expand Down Expand Up @@ -82,6 +83,7 @@ public function testGetCategories(): void
QuestionTypeCategory::DATE_AND_TIME,
QuestionTypeCategory::ACTORS,
QuestionTypeCategory::URGENCY,
QuestionTypeCategory::REQUEST_TYPE
];

// Manual array comparison, `isEqualTo` doesn't seem to work properly
Expand Down Expand Up @@ -137,6 +139,13 @@ protected function testGetTypesForCategoryProvider(): iterable
new \Glpi\Form\QuestionType\QuestionTypeUrgency(),
]
];

yield [
QuestionTypeCategory::REQUEST_TYPE,
[
new \Glpi\Form\QuestionType\QuestionTypeRequestType(),
]
];
}

/**
Expand Down

0 comments on commit 30bbb97

Please sign in to comment.