Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forms): Adding request type category and related question type #16984

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"),
};
}
}
138 changes: 138 additions & 0 deletions src/Form/QuestionType/QuestionTypeRequestType.php
@@ -0,0 +1,138 @@
<?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;

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(
question.getEndUserInputName(),
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/units/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
9 changes: 9 additions & 0 deletions tests/units/Glpi/Form/QuestionType/QuestionTypesManager.php
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