Team With No Departments Showed Zero Tickets - edmozley/freeitsm GitHub Wiki

A team with no departments showed 0 tickets

Fixed in 3.7.0 · Reported by email by a user · Present in every release from 1.0.0 to 3.6.1 · Module: Tickets


The short version

Somebody set up a new install, created a team and put their analysts in it. They had not created any departments yet. From that moment the ticket folder list showed 0 on All Tickets, on every status, on Unassigned and on every other analyst - while their own folder, My tickets, still said 153.

Nothing was lost and the tickets were all still there. Only the numbers on the folders were wrong.

Being in a team is meant to narrow your folders to that team's departments, plus every ticket with no department. When the team had no departments at all, the code skipped the counting and set everything to 0 - so the tickets with no department, which on this install was all of them, vanished from the numbers too. The emptiest setup hid the most.

It is fixed: a team with no departments now counts the tickets with no department. A department you have deliberately not linked to a team is still left out of that team's folders, exactly as before.


What you saw

  • All Tickets: 0, and every status beneath it (Open, In Progress, On Hold, Awaiting Response, Closed): 0.
  • Unassigned: 0.
  • Every other analyst: 0.
  • My tickets: 153.

That last number is the clue. "My tickets" is not a separate count - it is your own row in the analyst list, renamed. It is the one count that has always included "tickets assigned to me, wherever they are", so it was the only one the bug could not reach.

The ticket list itself was never affected: the inbox does not narrow the list by team, only the folders and their numbers.

What was wrong, in plain English

FreeITSM has two ways of grouping your service desk:

  • Departments sort tickets into areas of work (IT Support, Infrastructure, ...). You create them in Tickets → Settings → Departments.
  • Teams group analysts. Linking a team to departments (System → Teams, the building icon next to the pencil) means "people in this team work these departments".

Once an analyst is in any team, their ticket folders are narrowed to:

  1. tickets in their team's departments,
  2. plus tickets with no department, which everybody in a team sees,
  3. plus, on their own folder, tickets assigned to them.

On this install there were no departments, so every ticket was in group 2. By the rule, joining a team should have changed nothing.

The code had a shortcut for "this person's teams cover no departments": don't bother counting, the answer is 0. That shortcut dropped group 2 as well.

What was wrong, for developers

api/tickets/get_ticket_counts.php builds every number on the folder list. For an analyst in a team it first collects the departments their teams are linked to, then counts within them. The normal branch carries the no-department allowance in the SQL:

$totalSql = "SELECT COUNT(*) as total FROM tickets t
             WHERE (t.department_id IN ($deptIdPlaceholders) OR t.department_id IS NULL)$ttSql";

But an empty department list cannot go into IN (...), so the empty case had its own branch - and the branch answered instead of asking (3.6.1):

if (empty($accessibleDepts)) {
    // No accessible departments - just count unassigned
    $totalCount = 0;
    $departments = [];
    $deptStatusCounts = [];
    $statusCounts = [];
} else {

The comment says "just count unassigned". The code counts nothing. That gap between what the comment promised and what the line did is the whole bug, and it was in the very first commit.

It was four places, not one

The same "empty list, so 0" shape appeared in four separate branches of the same file, one per kind of number:

Branch Numbers it feeds Was
total + statuses All Tickets and every status under it $totalCount = 0; $statusCounts = [];
unassigned-by-analyst Unassigned in the analyst grouping $unassignedAnalystCount = 0;
unassigned-by-analyst statuses the statuses under that Unassigned $unassignedAnalystStatusRows = [];
analyst folders every analyst's folder AND (assigned to me OR owned by me) - no no-department allowance

The last one is why My tickets survived and every other analyst's folder did not: the analyst-folder filter kept the "always count your own" rule, and nothing else.

Two other places that read team departments were checked and are correct as they are:

  • api/tickets/get_my_departments.php decides which department folders you get. A team with no departments rightly gets none - tickets with no department already have their own Unassigned folder.
  • includes/sla_notifications.php emails the teams of a ticket's department. A ticket with no department has no team to email, which is right.

The fix

Each empty branch now runs the same query as its neighbour with the department list removed - the condition becomes just t.department_id IS NULL:

if (empty($accessibleDepts)) {
    // TRAP: never short-circuit an empty department list to 0 - count "t.department_id IS NULL".
    $totalStmt = $conn->prepare("SELECT COUNT(*) FROM tickets t WHERE t.department_id IS NULL$ttSql");

and the analyst-folder filter becomes AND (t.department_id IS NULL OR <assigned to me or owned by me>), matching the non-empty branch's IN (...) OR t.department_id IS NULL OR <own>.

The non-empty branches were not touched, which is what makes the second half of the guarantee hold: a department that is not linked to your team stays out of your folders.

Debug Tool D018 (Ticket folder counts, added in 3.6.1 to diagnose exactly this report) was updated to follow the corrected rule, and now only raises a team with no departments when tickets that are in a department are being left out. On an install with no departments yet, it stays quiet.

System → Teams now warns about a team with members but no departments, and the team's departments window points to Tickets → Settings → Departments when none exist. That is the part that would have saved the reporter the email: the set-up was legitimate, but nothing on screen said what it meant.

Files

File Change
🟥 api/tickets/get_ticket_counts.php the four empty-department branches count department_id IS NULL instead of returning 0; a TRAP: line on the first
🟦 api/system/debug-tools/D018_ticket_folder_counts.php the step-by-step count follows the corrected rule; findings only when department tickets are really left out
🟦 system/teams/index.php warning banner and "Check" badge for a team with members but no departments; the empty departments window links to Tickets → Settings → Departments
🟩 tests/team-counts-no-departments.php new - see below

How it was proved

The real endpoint was run for one analyst (in no team, with the Tickets module) on a development install with 96 live tickets, 38 of them with no department. Three setups, with a throwaway team created and deleted each time:

Setup 3.6.1 Fixed Expected
No team 96 96 - identical response unchanged
Team linked to one department, four left out on purpose 90 90 - identical response (bar the throwaway team's id) unchanged
Team with no departments 0 38 38, counted independently in SQL

The first two rows are the guarantee that nothing else moved; the third is the fix.

That comparison is now a test, tests/team-counts-no-departments.php, which drives the same endpoint through the same three setups. Control: pointed at the 3.6.1 copy of the endpoint, it fails exactly the two "no departments" checks (All Tickets 0 against 38; statuses 0) and passes everything else - including the checks that unlinked departments stay out, which pass on both, because that behaviour did not change. See Developer Tests - Tickets & tasks.

Not tested: the folder list itself in a browser (the numbers it draws come straight from this endpoint, unchanged on the browser side); a multi-company run as an analyst whose active company is not the default; the "no departments yet" message in the departments window, because the development install has departments.

Things this did not change

Two neighbouring behaviours were noticed while fixing this and deliberately left alone, because they are decisions rather than bugs:

  • An inactive team still narrows its members' folders, and so does a leftover membership of a team that has been deleted. The counts only ask "is this analyst in a team?", not whether the team is active or still exists. D018 flags both.
  • A team made only for tasks, on an install that does have departments, narrows its members' ticket folders to tickets with no department. That is the rule working as designed, and it surprises people - which is what the new warning on System → Teams is for.

What this means for you

Update to 3.7.0. If anyone on your install saw 0 on their ticket folders while they were in a team, the numbers come back with no action needed.

If you use teams, open System → Teams: a team with members but no departments is now flagged. Link it to its departments with the building icon, or take the members out of it if you did not mean the team to narrow anything.


Related: System - Debug Tools · Developer Tests - Tickets & tasks · Code traps · Bugs resolved

⚠️ **GitHub.com Fallback** ⚠️