@@ -242,6 +242,39 @@ public static function getUsersInCategory($categoryId)
242242 return Database::store_result ($ result );
243243 }
244244
245+ /**
246+ * Returns the list of category IDs assigned to a user.
247+ *
248+ * @param int $userId
249+ * @param int $projectId
250+ *
251+ * @return int[]
252+ */
253+ public static function getCategoryIdsByUser ($ userId , $ projectId = 0 )
254+ {
255+ $ tableRel = Database::get_main_table (TABLE_TICKET_CATEGORY_REL_USER );
256+ $ tableCat = Database::get_main_table (TABLE_TICKET_CATEGORY );
257+ $ userId = (int ) $ userId ;
258+ $ projectId = (int ) $ projectId ;
259+
260+ $ sql = "SELECT rel.category_id
261+ FROM $ tableRel rel
262+ INNER JOIN $ tableCat cat ON (rel.category_id = cat.id)
263+ WHERE rel.user_id = $ userId " ;
264+
265+ if (!empty ($ projectId )) {
266+ $ sql .= " AND cat.project_id = $ projectId " ;
267+ }
268+
269+ $ result = Database::query ($ sql );
270+ $ categories = [];
271+ while ($ row = Database::fetch_array ($ result )) {
272+ $ categories [] = (int ) $ row ['category_id ' ];
273+ }
274+
275+ return $ categories ;
276+ }
277+
245278 /**
246279 * @param int $categoryId
247280 */
@@ -902,7 +935,14 @@ public static function getTicketsByCurrentUser(
902935
903936 // Check if a role was set to the project
904937 if ($ userIsAllowInProject == false ) {
905- $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId ) " ;
938+ $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
939+ $ categoryCondition = '' ;
940+ if (!empty ($ categoryList )) {
941+ $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
942+ $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
943+ }
944+
945+ $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
906946 }
907947
908948 // Search simple
@@ -1094,11 +1134,25 @@ public static function getTotalTicketsCurrentUser()
10941134 // Check if a role was set to the project
10951135 if (!empty ($ allowRoleList ) && is_array ($ allowRoleList )) {
10961136 if (!in_array ($ userInfo ['status ' ], $ allowRoleList )) {
1097- $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId ) " ;
1137+ $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
1138+ $ categoryCondition = '' ;
1139+ if (!empty ($ categoryList )) {
1140+ $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
1141+ $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
1142+ }
1143+
1144+ $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
10981145 }
10991146 } else {
11001147 if (!api_is_platform_admin ()) {
1101- $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId ) " ;
1148+ $ categoryList = self ::getCategoryIdsByUser ($ userId , $ projectId );
1149+ $ categoryCondition = '' ;
1150+ if (!empty ($ categoryList )) {
1151+ $ categoryIds = implode (', ' , array_map ('intval ' , $ categoryList ));
1152+ $ categoryCondition = " OR ticket.category_id IN ( $ categoryIds) " ;
1153+ }
1154+
1155+ $ sql .= " AND (ticket.assigned_last_user = $ userId OR ticket.sys_insert_user_id = $ userId " .$ categoryCondition .") " ;
11021156 }
11031157 }
11041158
@@ -2588,4 +2642,4 @@ public static function notifiyTicketUpdated(int $ticketId, int $categoryId, stri
25882642 }
25892643 }
25902644 }
2591- }
2645+ }
0 commit comments