Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

<<<<<<< HEAD
### Added

- Credit vouchers can be restricted to incidents and/or requests (both enabled by default)
=======
### Fixed

- Improve performance of the consumed details, the ticket Credit tab and the entity Credit tab, which no longer run SQL queries for each displayed row
- Remove 2 SQL queries from every GLPI page by dropping the plugin's own activation check
- Delete the default voucher options of a ticket when it is purged
>>>>>>> 924d8dc (Fix(Perf): remove per-row SQL queries from voucher views and the init activation check (#280))

### Changed

Expand Down
61 changes: 53 additions & 8 deletions inc/entity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,30 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
];
}

$vouchers = self::getAllForEntity($ID, $sqlfilter);
$consumed = self::getConsumedForCredits(array_keys($vouchers));
$type_links = [];

$entries = [];
foreach (self::getAllForEntity($ID, $sqlfilter) as $data) {
foreach ($vouchers as $data) {
$quantity_sold = (int) $data['quantity'];
if (0 === $quantity_sold) {
$quantity_sold = __('Unlimited');
}

// The row already holds every field getById() would load.
$item = new self();
$item = $item->getById($data['id']);
$item->fields = $data;
$consumed_for_credit = $consumed[$data['id']] ?? 0;

if (!empty($data['plugin_credit_types_id'])) {
$type = new PluginCreditType();
$type = $type->getById($data['plugin_credit_types_id']);
if ($type) {
$data['plugin_credit_types_id'] = $type->getLink();
$types_id = $data['plugin_credit_types_id'];
if (!array_key_exists($types_id, $type_links)) {
$type = PluginCreditType::getById((int) $types_id);
$type_links[$types_id] = $type ? $type->getLink() : null;
}
if ($type_links[$types_id] !== null) {
$data['plugin_credit_types_id'] = $type_links[$types_id];
}
} else {
$data['plugin_credit_types_id'] = '';
Expand All @@ -240,15 +249,15 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
],
);

$link = "<a href='#' data-bs-toggle='modal' data-bs-target='#displaycreditconsumed_{$data["id"]}' title='" . __('Consumed details', 'credit') . "' alt='" . __('Consumed details', 'credit') . "'>" . PluginCreditEntity::getConsumedForCredit($data['id']) . "</a>";
$link = "<a href='#' data-bs-toggle='modal' data-bs-target='#displaycreditconsumed_{$data["id"]}' title='" . __('Consumed details', 'credit') . "' alt='" . __('Consumed details', 'credit') . "'>" . $consumed_for_credit . "</a>";

$entries[] = array_merge($data, [
'name' => $item->getLink(),
'quantity' => $quantity_sold,
'itemtype' => PluginCreditEntity::class,
'low_credit_alert' => $data['low_credit_alert'] == -1 ? __('Disabled') : $data['low_credit_alert'] . '%',
'quantity_consumed' => $modal . $link,
'quantity_remaining' => $data['quantity'] > 0 ? $data['quantity'] - PluginCreditEntity::getConsumedForCredit($data['id']) : 'Unlimited',
'quantity_remaining' => $data['quantity'] > 0 ? $data['quantity'] - $consumed_for_credit : 'Unlimited',
'entities_id' => Entity::badgeCompletenameLink($entity),
]);
}
Expand Down Expand Up @@ -791,4 +800,40 @@ public static function getConsumedForCredit(int $credit_id)

return (int) $ticket_result['consumed_total'];
}

/**
* Get the total consumption of several credit vouchers in one query.
*
* @param int[] $credit_ids IDs of the credit vouchers
*
* @return array<int, int> Total consumption by voucher ID, vouchers without consumption are absent
*/
public static function getConsumedForCredits(array $credit_ids): array
{
/** @var DBmysql $DB */
global $DB;

if ($credit_ids === []) {
return [];
}

$iterator = $DB->request([
'SELECT' => [
'plugin_credit_entities_id',
'SUM' => 'consumed AS consumed_total',
],
'FROM' => 'glpi_plugin_credit_tickets',
'WHERE' => [
'plugin_credit_entities_id' => $credit_ids,
],
'GROUPBY' => 'plugin_credit_entities_id',
]);

$consumed = [];
foreach ($iterator as $row) {
$consumed[$row['plugin_credit_entities_id']] = (int) $row['consumed_total'];
}

return $consumed;
}
}
141 changes: 104 additions & 37 deletions inc/ticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,11 @@

public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
$nb = $item instanceof CommonDBTM ? self::countForItem($item) : 0;
if ($item instanceof Ticket) {
if ($_SESSION['glpishow_count_on_tabs']) {
return self::createTabEntry(self::getTypeName($nb), $nb);
} else {
return self::getTypeName($nb);
}
} else {
return self::getTypeName($nb);
if ($item instanceof Ticket && $_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForItem($item);
return self::createTabEntry(self::getTypeName($nb), $nb);
}
return self::getTypeName(Session::getPluralNumber());
}

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
Expand Down Expand Up @@ -232,9 +227,27 @@
],
];
}
foreach (self::getAllForTicket($ID) as $data) {
$credit_entity = new PluginCreditEntity();
$credit_entity->getFromDB($data['plugin_credit_entities_id']);
$consumptions = self::getAllForTicket($ID);

// Voucher names and user links are loaded once, not once per consumption.
$voucher_names = [];
$voucher_ids = array_unique(array_column($consumptions, 'plugin_credit_entities_id'));
if ($voucher_ids !== []) {
$vouchers = $DB->request([
'SELECT' => ['id', 'name'],
'FROM' => PluginCreditEntity::getTable(),
'WHERE' => ['id' => $voucher_ids],
]);
foreach ($vouchers as $voucher) {
$voucher_names[$voucher['id']] = $voucher['name'];
}
}
$showuserlink = Session::haveRight('user', READ);
$usernames = [];

foreach ($consumptions as $data) {
$users_id = $data['users_id'];
$usernames[$users_id] ??= $showuserlink ? getUserLink($users_id) : getUserName($users_id);

if (!empty($data['plugin_credit_types_id'])) {
$type = new PluginCreditType();
Expand All @@ -248,10 +261,16 @@

$entries[] = array_merge($data, [
'id' => $data['id'],
'name' => $credit_entity->getName(),
'name' => (string) ($voucher_names[$data['plugin_credit_entities_id']] ?? '') !== ''
? $voucher_names[$data['plugin_credit_entities_id']]
: NOT_AVAILABLE,
'plugin_credit_types_id' => $data['plugin_credit_types_id'],
'date_creation' => $data['date_creation'],
<<<<<<< HEAD

Check failure on line 269 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_SL, expecting ',' or ']' or ')' on line 269

Check failure on line 269 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Cannot use empty array elements in arrays on line 269

Check failure on line 269 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_SL, expecting ',' or ']' or ')' on line 269

Check failure on line 269 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Cannot use empty array elements in arrays on line 269
'users_id' => Session::haveRight(User::$rightname, READ) == true ? getUserLink($data["users_id"]) : getUserName($data["users_id"]),

Check failure on line 270 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_STRING, expecting T_VARIABLE or T_ENCAPSED_AND_WHITESPACE or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN on line 270

Check failure on line 270 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_STRING, expecting T_VARIABLE or T_ENCAPSED_AND_WHITESPACE or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN on line 270
=======
'users_id' => $usernames[$users_id],

Check failure on line 272 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 272

Check failure on line 272 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 272
>>>>>>> 924d8dc (Fix(Perf): remove per-row SQL queries from voucher views and the init activation check (#280))
'consumed' => $data['consumed'],
'itemtype' => PluginCreditTicket::class,
]);
Expand All @@ -261,7 +280,7 @@

TemplateRenderer::getInstance()->display('@credit/tickets/form.html.twig', [
'rand' => $rand,
'entity_id' => $ticket->getEntityID(),

Check failure on line 283 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 283

Check failure on line 283 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 283
'type_name' => self::getTypeName(2),
'creditentityclass' => PluginCreditEntity::class,
'form_url' => self::getFormUrl(),
Expand All @@ -271,10 +290,6 @@
'entries' => $entries,
'massiveactionparams' => $massiveactionparams ?? [],
]);

$Entity = new Entity();
$Entity->getFromDB($ticket->fields['entities_id']);
PluginCreditEntity::showForItemtype($Entity, 'Ticket');
}

/**
Expand All @@ -301,7 +316,7 @@
return;
}

if (!$item->isNewItem()) {

Check failure on line 319 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 319

Check failure on line 319 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 319
// Do not display fields in item update form.
return;
}
Expand Down Expand Up @@ -332,16 +347,16 @@
return;
}

$canedit = $ticket->canEdit($ticket->getID());

Check failure on line 350 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 350

Check failure on line 350 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 350
if (
in_array($ticket->fields['status'], Ticket::getSolvedStatusArray())

Check failure on line 352 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 352

Check failure on line 352 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 352
|| in_array($ticket->fields['status'], Ticket::getClosedStatusArray())

Check failure on line 353 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 353

Check failure on line 353 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 353
) {
$canedit = false;
}

$entity_config = new PluginCreditEntityConfig();
$entity_config->getFromDBByCrit(['entities_id' => $ticket->getEntityID()]);

Check failure on line 359 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.5 - mariadb:10.11 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 359

Check failure on line 359 in inc/ticket.class.php

View workflow job for this annotation

GitHub Actions / GLPI 12.0.x - php:8.3 - mysql:8.0 / Continuous integration

Syntax error, unexpected T_ENCAPSED_AND_WHITESPACE on line 359
$consume = false;
if ($item instanceof ITILSolution) {
$consume = $entity_config->fields['consume_voucher_for_solutions'] ?? 0;
Expand Down Expand Up @@ -404,38 +419,90 @@
*/
public static function displayConsumed($ID)
{
/** @var DBmysql $DB */
global $DB;

$consumed_credits = self::getConsumedForCreditEntity($ID);
$tickets_data = [];

if ($consumed_credits > 0) {
foreach (self::getAllForCreditEntity($ID) as $data) {
$Ticket = new Ticket();
$Ticket->getFromDB($data['tickets_id']);
// Tickets and categories come with the consumptions, in a single query.
$iterator = $DB->request([
'SELECT' => [
'glpi_tickets.*',
'glpi_itilcategories.name AS credit_category_name',
'glpi_itilcategories.completename AS credit_category_completename',
self::getTable() . '.date_creation AS credit_date_creation',
self::getTable() . '.users_id AS credit_users_id',
self::getTable() . '.consumed AS credit_consumed',
],
'FROM' => self::getTable(),
'LEFT JOIN' => [
'glpi_tickets' => [
'ON' => [
'glpi_tickets' => 'id',
self::getTable() => 'tickets_id',
],
],
'glpi_itilcategories' => [
'ON' => [
'glpi_itilcategories' => 'id',
'glpi_tickets' => 'itilcategories_id',
],
],
],
'WHERE' => [
self::getTable() . '.plugin_credit_entities_id' => $ID,
],
'ORDER' => [self::getTable() . '.id DESC'],
]);

$itilcat = new ITILCategory();
$category = __('None');
if ($itilcat->getFromDB($Ticket->fields['itilcategories_id'])) {
$category = $itilcat->getName(['comments' => true]);
$showuserlink = Session::haveRight('user', READ);
$usernames = [];

foreach ($iterator as $data) {
$Ticket = new Ticket();
$ticket_link = '';
$status = '';
$type = '';
// The ticket may have been purged since the consumption.
if ($data['id'] !== null) {
$Ticket->fields = $data;
$ticket_link = sprintf(
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
htmlescape($Ticket->getLinkURL()),
htmlescape($Ticket->getNameID()),
);
$status = Ticket::getStatus($data['status']);
$type = Ticket::getTicketTypeName($data['type']);
}

<<<<<<< HEAD
$showuserlink = Session::haveRight(User::$rightname, READ) ? 1 : 0;
=======
$category = __('None');
if ($data['credit_category_name'] !== null) {
$itilcat = new ITILCategory();
$itilcat->fields = [
'id' => $data['itilcategories_id'],
'name' => $data['credit_category_name'],
'completename' => $data['credit_category_completename'],
];
$category = $itilcat->getName();
}
>>>>>>> 924d8dc (Fix(Perf): remove per-row SQL queries from voucher views and the init activation check (#280))

$ticket_url = $Ticket->getLinkURL();
$ticket_name = $Ticket->getNameID();
$username = $showuserlink !== 0 ? getUserLink($data['users_id']) : getUserName($data['users_id']);
$users_id = $data['credit_users_id'];
$usernames[$users_id] ??= $showuserlink ? getUserLink($users_id) : getUserName($users_id);

$tickets_data[] = [
'ticket_link' => sprintf(
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
htmlescape($ticket_url),
htmlescape($ticket_name),
),
'status' => Ticket::getStatus($Ticket->fields['status']),
'type' => Ticket::getTicketTypeName($Ticket->fields['type']),
'ticket_link' => $ticket_link,
'status' => $status,
'type' => $type,
'category' => $category,
'date_creation' => $data["date_creation"],
'username' => $username,
'consumed' => $data['consumed'],
'date_creation' => $data['credit_date_creation'],
'username' => $usernames[$users_id],
'consumed' => $data['credit_consumed'],
];
}
}
Expand Down
9 changes: 9 additions & 0 deletions inc/ticketconfig.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ public static function updateConfig(Ticket $ticket)
}
}

/**
* Remove the default voucher options of a purged ticket.
* Consumptions are kept: they still count against their voucher.
*/
public static function cleanForTicket(Ticket $ticket): void
{
(new self())->deleteByCriteria(['tickets_id' => $ticket->getID()]);
}

public static function install(Migration $migration)
{
/** @var DBmysql $DB */
Expand Down
9 changes: 6 additions & 3 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ function plugin_init_credit()
/** @var array $PLUGIN_HOOKS */
global $PLUGIN_HOOKS;

$plugin = new Plugin();

$PLUGIN_HOOKS['csrf_compliant']['credit'] = true;

if (Session::getLoginUserID() && $plugin->isActivated('credit')) {
// Outside the session check: purges also run from cron and CLI.
$PLUGIN_HOOKS['item_purge']['credit'] = [
'Ticket' => ['PluginCreditTicketConfig', 'cleanForTicket'],
];

if (Session::getLoginUserID()) {
Plugin::registerClass(
'PluginCreditEntity',
[
Expand Down
Loading