Repository: uzh-bf/klicker-uzh
Pull request #4648: fix: add direct link between activity template and required answer collection entries
Author: login: sjschlapbach
Labels: name: size:L
description: This PR changes 100-499 lines, ignoring generated files.
Created: 2025-04-29T12:52:33Z | Merged: 2025-04-29T14:50:42Z



<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Activity templates now track and link specific answer collection entries, ensuring more precise resource management when creating or copying templates.
  - Synchronization between activity templates and instances now includes individual answer collection entries for improved consistency.

- **Bug Fixes**
  - Improved validation to check that all required answer collection entries are present before converting activities into templates.
  - Enhanced deletion safeguards to prevent removal of answer collection entries still in use by templates.

- **Database**
  - Introduced a new table to manage connections between activity templates and answer collection entries, enhancing data integrity.
  - Extended data models to support relationships between templates and answer collection entries.

- **Localization**
  - Updated English and German messages to clarify when resources are missing due to removal or modification of required answer options.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->


Diff:
diff --git a/packages/graphql/src/services/questions.ts b/packages/graphql/src/services/questions.ts
index dcc07e5c25..c8289feea5 100644
--- a/packages/graphql/src/services/questions.ts
+++ b/packages/graphql/src/services/questions.ts
@@ -1094,48 +1094,66 @@ export async function updateElementInstances(
               element.type === DB.ElementType.CASE_STUDY) &&
             element.answerCollectionId !== null
           ) {
-            // get all answer collections connected to one of the impacted template activities
+            // get all answer collections and answer collection entries used in any instance in the affected template
             let instanceCollectionIds: number[] = []
+            let instanceCollectionEntryIds: number[] = []
+
             if (typeof liveQuizId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: liveQuizId,
-                    activityType: ActivityType.LIVE_QUIZ,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: liveQuizId,
+                  activityType: ActivityType.LIVE_QUIZ,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof practiceQuizId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: practiceQuizId,
-                    activityType: ActivityType.PRACTICE_QUIZ,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: practiceQuizId,
+                  activityType: ActivityType.PRACTICE_QUIZ,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof microLearningId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: microLearningId,
-                    activityType: ActivityType.MICRO_LEARNING,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: microLearningId,
+                  activityType: ActivityType.MICRO_LEARNING,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof groupActivityId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: groupActivityId,
-                    activityType: ActivityType.GROUP_ACTIVITY,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: groupActivityId,
+                  activityType: ActivityType.GROUP_ACTIVITY,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             }
 
             // fetch the existing template and the contained answer collections
@@ -1145,6 +1163,7 @@ export async function updateElementInstances(
               },
               include: {
                 answerCollections: true,
+                answerCollectionItems: true,
               },
             })
 
@@ -1163,6 +1182,17 @@ export async function updateElementInstances(
               (id) => !templateCollectionIds.includes(id)
             )
 
+            const templateCollectionEntryIds =
+              template.answerCollectionItems.map((collection) => collection.id)
+            const collectionEntriesToDisconnect =
+              templateCollectionEntryIds.filter(
+                (id) => !instanceCollectionEntryIds.includes(id)
+              )
+            const collectionEntriesToConnect =
+              instanceCollectionEntryIds.filter(
+                (id) => !templateCollectionEntryIds.includes(id)
+              )
+
             // add all answer collections that were added or removed to the touched ones for a derived permissions update
             touchedAnswerCollectionIds = touchedAnswerCollectionIds.concat([
               ...collectionsToDisconnect,
@@ -1172,27 +1202,51 @@ export async function updateElementInstances(
             // check if the list of answer collection ids in the template and the ones used in the instance coincide, otherwise update these links
             if (
               collectionsToConnect.length > 0 ||
-              collectionsToDisconnect.length > 0
+              collectionsToDisconnect.length > 0 ||
+              collectionEntriesToConnect.length > 0 ||
+              collectionEntriesToDisconnect.length > 0
             ) {
               await ctx.prisma.activityTemplate.update({
                 where: {
                   id: templateId,
                 },
                 data: {
-                  answerCollections: {
-                    connect:
-                      collectionsToConnect.length > 0
-                        ? collectionsToConnect.map((id) => ({
-                            id,
-                          }))
-                        : [],
-                    disconnect:
-                      collectionsToDisconnect.length > 0
-                        ? collectionsToDisconnect.map((id) => ({
-                            id,
-                          }))
-                        : [],
-                  },
+                  answerCollections:
+                    collectionsToConnect.length > 0 ||
+                    collectionsToDisconnect.length > 0
+                      ? {
+                          connect:
+                            collectionsToConnect.length > 0
+                              ? collectionsToConnect.map((id) => ({
+                                  id,
+                                }))
+                              : [],
+                          disconnect:
+                            collectionsToDisconnect.length > 0
+                              ? collectionsToDisconnect.map((id) => ({
+                                  id,
+                                }))
+                              : [],
+                        }
+                      : undefined,
+                  answerCollectionItems:
+                    collectionEntriesToConnect.length > 0 ||
+                    collectionEntriesToDisconnect.length > 0
+                      ? {
+                          connect:
+                            collectionEntriesToConnect.length > 0
+                              ? collectionEntriesToConnect.map((id) => ({
+                                  id,
+                                }))
+                              : [],
+                          disconnect:
+                            collectionEntriesToDisconnect.length > 0
+                              ? collectionEntriesToDisconnect.map((id) => ({
+                                  id,
+                                }))
+                              : [],
+                        }
+                      : undefined,
                 },
               })
             }
diff --git a/packages/graphql/src/services/resources.ts b/packages/graphql/src/services/resources.ts
index 835f466afc..d8d38da8a1 100644
--- a/packages/graphql/src/services/resources.ts
+++ b/packages/graphql/src/services/resources.ts
@@ -201,7 +201,9 @@ export async function getSingleAnswerCollection(
     where: { id },
     include: {
       entries: {
-        include: { _count: { select: { itemUsages: true } } },
+        include: {
+          _count: { select: { itemUsages: true, templateUsages: true } },
+        },
         orderBy: { value: 'asc' },
       },
       permissions: { where: { userId: ctx.user.sub } },
@@ -228,7 +230,7 @@ export async function getSingleAnswerCollection(
     ...collection,
     entries: collection.entries.map((entry) => ({
       ...entry,
-      numSolutionUsages: entry._count?.itemUsages,
+      numSolutionUsages: entry._count.itemUsages + entry._count.templateUsages,
     })),
     numSharedUsers: isManager
       ? collection._count?.directPermissions
@@ -624,10 +626,14 @@ export async function deleteAnswerCollectionEntry(
   // verify that the answer collection entry is not linked to any elements
   const entry = await ctx.prisma.answerCollectionEntry.findUnique({
     where: { id, collectionId },
-    include: { _count: { select: { itemUsages: true } } },
+    include: { _count: { select: { itemUsages: true, templateUsages: true } } },
   })
 
-  if (!entry || entry._count.itemUsages > 0) {
+  if (
+    !entry ||
+    entry._count.itemUsages > 0 ||
+    entry._count.templateUsages > 0
+  ) {
     return null
   }
 
diff --git a/packages/graphql/src/services/sharing.ts b/packages/graphql/src/services/sharing.ts
index 217f8d3529..4754cef0c5 100644
--- a/packages/graphql/src/services/sharing.ts
+++ b/packages/graphql/src/services/sharing.ts
@@ -4347,9 +4347,7 @@ export async function createUserGroup(
   })
 
   if (userGroup) {
-    console.error(
-      `User group with name "${name}" already exists for user ${ctx.user.sub}.`
-    )
+    // `User group with name "${name}" already exists for user ${ctx.user.sub}.`
     return null
   }
 
@@ -4366,9 +4364,7 @@ export async function createUserGroup(
 
   // if none of the specified members were found, throw an error
   if (users.length === 0) {
-    console.error(
-      `No users found for the specified members: ${JSON.stringify(members)}`
-    )
+    // `No users found for the specified members: ${JSON.stringify(members)}`
     return null
   }
 
diff --git a/packages/graphql/src/services/templates.ts b/packages/graphql/src/services/templates.ts
index ae0049e06d..d33fa2f2d1 100644
--- a/packages/graphql/src/services/templates.ts
+++ b/packages/graphql/src/services/templates.ts
@@ -167,6 +167,7 @@ export async function getActivityAnswerCollectionIds(
     | null
   noInstances: boolean
   answerCollectionIds: number[]
+  answerCollectionEntryIds: number[]
 }> {
   // helper function that finds the ids of all answer collections linked to elements in an activity
   // fetch all element instances included in the activity that should be converted
@@ -192,6 +193,7 @@ export async function getActivityAnswerCollectionIds(
         activity: null,
         noInstances: false,
         answerCollectionIds: [],
+        answerCollectionEntryIds: [],
       }
     }
 
@@ -217,6 +219,7 @@ export async function getActivityAnswerCollectionIds(
         activity: null,
         noInstances: false,
         answerCollectionIds: [],
+        answerCollectionEntryIds: [],
       }
     }
 
@@ -242,6 +245,7 @@ export async function getActivityAnswerCollectionIds(
         activity: null,
         noInstances: false,
         answerCollectionIds: [],
+        answerCollectionEntryIds: [],
       }
     }
 
@@ -269,6 +273,7 @@ export async function getActivityAnswerCollectionIds(
         activity: null,
         noInstances: false,
         answerCollectionIds: [],
+        answerCollectionEntryIds: [],
       }
     }
 
@@ -280,6 +285,7 @@ export async function getActivityAnswerCollectionIds(
       activity: null,
       noInstances: false,
       answerCollectionIds: [],
+      answerCollectionEntryIds: [],
     }
   }
 
@@ -290,6 +296,7 @@ export async function getActivityAnswerCollectionIds(
       activity,
       noInstances: true,
       answerCollectionIds: [],
+      answerCollectionEntryIds: [],
     }
   }
 
@@ -312,7 +319,33 @@ export async function getActivityAnswerCollectionIds(
     )
   )
 
-  return { error: false, activity, noInstances: false, answerCollectionIds }
+  // extract all answer collection entry ids that are used as sample solutions in selection elements or as items in case study elements
+  const answerCollectionEntryIds = Array.from(
+    new Set(
+      instances
+        .filter(
+          (instance) =>
+            instance.elementType === DB.ElementType.SELECTION ||
+            instance.elementType === DB.ElementType.CASE_STUDY
+        )
+        .flatMap((instance) =>
+          instance.elementType === DB.ElementType.SELECTION
+            ? ((instance.elementData as SelectionElementData).options
+                .answerCollectionSolutionIds ?? [])
+            : ((
+                instance.elementData as CaseStudyElementData
+              ).options.items!.map((item) => item.id) ?? [])
+        )
+    )
+  )
+
+  return {
+    error: false,
+    activity,
+    noInstances: false,
+    answerCollectionIds,
+    answerCollectionEntryIds,
+  }
 }
 
 export async function checkTemplateInfoAvailable(
@@ -323,7 +356,7 @@ export async function checkTemplateInfoAvailable(
   ctx: ContextWithUser
 ) {
   // fetch all answer collections linked to elements in the activity
-  const { error, noInstances, answerCollectionIds } =
+  const { error, noInstances, answerCollectionIds, answerCollectionEntryIds } =
     (await getActivityAnswerCollectionIds({ activityId, activityType }, ctx)) ??
     {}
 
@@ -357,8 +390,18 @@ export async function checkTemplateInfoAvailable(
     select: { id: true },
   })
 
-  // check if all required answer collections exist
-  if (answerCollections.length === answerCollectionIds.length) {
+  // check if all required answer collection entries are available to the user
+  const answerCollectionEntries =
+    await ctx.prisma.answerCollectionEntry.findMany({
+      where: { id: { in: answerCollectionEntryIds } },
+      select: { id: true },
+    })
+
+  // check if all required answer collections and answer collection entries exist
+  if (
+    answerCollections.length === answerCollectionIds.length &&
+    answerCollectionEntries.length === answerCollectionEntryIds.length
+  ) {
     return {
       noInstances: false,
       noResourcesRequired: false,
@@ -395,8 +438,13 @@ export async function createActivityTemplate(
   ctx: ContextWithUser
 ) {
   // fetch all answer collections linked to elements in the activity
-  const { error, activity, noInstances, answerCollectionIds } =
-    await getActivityAnswerCollectionIds({ activityId, activityType }, ctx)
+  const {
+    error,
+    activity,
+    noInstances,
+    answerCollectionIds,
+    answerCollectionEntryIds,
+  } = await getActivityAnswerCollectionIds({ activityId, activityType }, ctx)
 
   if (error || noInstances) {
     return false
@@ -468,6 +516,12 @@ export async function createActivityTemplate(
                         connect: answerCollectionIds.map((id) => ({ id })),
                       }
                     : undefined,
+                answerCollectionItems:
+                  answerCollectionEntryIds.length > 0
+                    ? {
+                        connect: answerCollectionEntryIds.map((id) => ({ id })),
+                      }
+                    : undefined,
               },
             },
             // creator of template becomes new owner of the template activity
@@ -553,6 +607,14 @@ export async function createActivityTemplate(
                           connect: answerCollectionIds.map((id) => ({ id })),
                         }
                       : undefined,
+                  answerCollectionItems:
+                    answerCollectionEntryIds.length > 0
+                      ? {
+                          connect: answerCollectionEntryIds.map((id) => ({
+                            id,
+                          })),
+                        }
+                      : undefined,
                 },
               },
               // templates from asynchronous activities are linked to the same course
@@ -639,6 +701,14 @@ export async function createActivityTemplate(
                           connect: answerCollectionIds.map((id) => ({ id })),
                         }
                       : undefined,
+                  answerCollectionItems:
+                    answerCollectionEntryIds.length > 0
+                      ? {
+                          connect: answerCollectionEntryIds.map((id) => ({
+                            id,
+                          })),
+                        }
+                      : undefined,
                 },
               },
               // templates from asynchronous activities are linked to the same course
@@ -734,6 +804,14 @@ export async function createActivityTemplate(
                           connect: answerCollectionIds.map((id) => ({ id })),
                         }
                       : undefined,
+                  answerCollectionItems:
+                    answerCollectionEntryIds.length > 0
+                      ? {
+                          connect: answerCollectionEntryIds.map((id) => ({
+                            id,
+                          })),
+                        }
+                      : undefined,
                 },
               },
               // templates from asynchronous activities are linked to the same course
@@ -792,6 +870,12 @@ export async function createActivityTemplate(
                   connect: answerCollectionIds.map((id) => ({ id })),
                 }
               : undefined,
+          answerCollectionItems:
+            answerCollectionEntryIds.length > 0
+              ? {
+                  connect: answerCollectionEntryIds.map((id) => ({ id })),
+                }
+              : undefined,
         },
       })
 
diff --git a/packages/graphql/test/template.test.ts b/packages/graphql/test/template.test.ts
index 04fe8215af..8fed189276 100644
--- a/packages/graphql/test/template.test.ts
+++ b/packages/graphql/test/template.test.ts
@@ -936,6 +936,7 @@ describe('Unit tests for template service', () => {
       },
       include: {
         answerCollections: true,
+        answerCollectionItems: true,
         liveQuiz: {
           include: {
             blocks: {
@@ -956,6 +957,13 @@ describe('Unit tests for template service', () => {
     expect(answerCollectionIds).toEqual(
       expect.arrayContaining([AC1.id, AC2.id])
     )
+    const answerCollectionItems = template?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionItems).toHaveLength(2)
+    expect(answerCollectionItems).toEqual(
+      expect.arrayContaining([AC2.entries[0]!.id, AC2.entries[1]!.id])
+    )
     expect(template?.liveQuiz?.blocks[0]?.elements[0]?.elementData.name).toBe(
       SEQuestion.name
     )
@@ -998,6 +1006,7 @@ describe('Unit tests for template service', () => {
       CSQuestion.name
     )
 
+    // trigger another element instance update, this time including the activity templates
     await updateElementInstances(
       { elementId: SEQuestion.id, includeTemplates: true },
       userOneCtx
@@ -1010,6 +1019,7 @@ describe('Unit tests for template service', () => {
       },
       include: {
         answerCollections: true,
+        answerCollectionItems: true,
         liveQuiz: {
           include: {
             blocks: {
@@ -1028,6 +1038,13 @@ describe('Unit tests for template service', () => {
     expect(answerCollectionIds3).toEqual(
       expect.arrayContaining([AC1.id, AC2.id])
     )
+    const answerCollectionItems3 = template3?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionItems3).toHaveLength(2)
+    expect(answerCollectionItems3).toEqual(
+      expect.arrayContaining([AC2.entries[0]!.id, AC2.entries[1]!.id])
+    )
     expect(template3?.liveQuiz?.blocks[0]?.elements[0]?.elementData.name).toBe(
       SEQuestion2.name
     )
@@ -1057,6 +1074,7 @@ describe('Unit tests for template service', () => {
       },
       include: {
         answerCollections: true,
+        answerCollectionItems: true,
         liveQuiz: {
           include: {
             blocks: {
@@ -1075,6 +1093,13 @@ describe('Unit tests for template service', () => {
     expect(answerCollectionIds4).toEqual(
       expect.arrayContaining([AC2.id, AC3.id])
     )
+    const answerCollectionItems4 = template4?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionItems4).toHaveLength(2)
+    expect(answerCollectionItems4).toEqual(
+      expect.arrayContaining([AC2.entries[0]!.id, AC2.entries[1]!.id])
+    )
     expect(template4?.liveQuiz?.blocks[0]?.elements[0]?.elementData.name).toBe(
       SEQuestion3.name
     )
@@ -1108,6 +1133,7 @@ describe('Unit tests for template service', () => {
       },
       include: {
         answerCollections: true,
+        answerCollectionItems: true,
         liveQuiz: {
           include: {
             blocks: {
@@ -1124,6 +1150,13 @@ describe('Unit tests for template service', () => {
     )
     expect(answerCollectionIds5).toHaveLength(1)
     expect(answerCollectionIds5).toEqual(expect.arrayContaining([AC3.id]))
+    const answerCollectionItems5 = template5?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionItems5).toHaveLength(2)
+    expect(answerCollectionItems5).toEqual(
+      expect.arrayContaining([AC3.entries[0]!.id, AC3.entries[1]!.id])
+    )
     expect(template5?.liveQuiz?.blocks[0]?.elements[0]?.elementData.name).toBe(
       SEQuestion3.name
     )
@@ -1157,6 +1190,7 @@ describe('Unit tests for template service', () => {
       },
       include: {
         answerCollections: true,
+        answerCollectionItems: true,
         liveQuiz: {
           include: {
             blocks: {
@@ -1175,6 +1209,13 @@ describe('Unit tests for template service', () => {
     expect(answerCollectionIds6).toEqual(
       expect.arrayContaining([AC1.id, AC3.id])
     )
+    const answerCollectionItems6 = template6?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionItems6).toHaveLength(2)
+    expect(answerCollectionItems6).toEqual(
+      expect.arrayContaining([AC1.entries[0]!.id, AC1.entries[1]!.id])
+    )
     expect(template6?.liveQuiz?.blocks[0]?.elements[0]?.elementData.name).toBe(
       SEQuestion3.name
     )
@@ -1182,6 +1223,56 @@ describe('Unit tests for template service', () => {
       CSQuestion3.name
     )
 
+    // modify the answer collection entries used in the case study question (same answer collection, but options 2 & 3 instead of 1 & 2)
+    const CSQuestion4 = await prisma.element.update({
+      where: { id: CSQuestion.id },
+      data: {
+        name: 'Updated Case Study Question 4',
+        answerCollectionItems: {
+          disconnect: [{ id: AC1.entries[0]!.id }],
+          connect: [{ id: AC1.entries[2]!.id }],
+        },
+      },
+    })
+    await updateElementInstances(
+      { elementId: CSQuestion.id, includeTemplates: true },
+      userOneCtx
+    )
+
+    // verify that the update was successful and that the corresponding answer collection entries are now linked to the template
+    const template7 = await prisma.activityTemplate.findUnique({
+      where: {
+        id: templateId,
+      },
+      include: {
+        answerCollections: true,
+        answerCollectionItems: true,
+        liveQuiz: {
+          include: {
+            blocks: {
+              include: {
+                elements: true,
+              },
+            },
+          },
+        },
+      },
+    })
+    const answerCollectionIds7 = template7?.answerCollections.map(
+      (collection) => collection.id
+    )
+    const answerCollectionItems7 = template7?.answerCollectionItems.map(
+      (item) => item.id
+    )
+    expect(answerCollectionIds7).toHaveLength(2)
+    expect(answerCollectionIds7).toEqual(
+      expect.arrayContaining([AC1.id, AC3.id])
+    )
+    expect(answerCollectionItems7).toHaveLength(2)
+    expect(answerCollectionItems7).toEqual(
+      expect.arrayContaining([AC1.entries[1]!.id, AC1.entries[2]!.id])
+    )
+
     // delete the live quiz / template, answer collections and questions
     await prisma.liveQuiz.delete({ where: { id: activityId } })
     await prisma.answerCollection.deleteMany({
diff --git a/packages/i18n/messages/de.ts b/packages/i18n/messages/de.ts
index af6ab2c905..2e10f3b769 100644
--- a/packages/i18n/messages/de.ts
+++ b/packages/i18n/messages/de.ts
@@ -1605,7 +1605,7 @@ Da die KlickerUZH-App noch nicht im iOS-App-Store verfügbar ist, folgen Sie die
       noInstances:
         'Die ausgewählte Aktivität enthält keine Elemente und kann daher nicht in ein Template umgewandelt werden.',
       resourcesRequiredMissing:
-        'Einige Elemente in dieser Aktivität sind von Ressourcen (z.B. Antwort-Sammlungen) abhängig, welche gelöscht oder aus dem Konto entfernt wurden (bei geteiltem Zugriff). Bitte ersetzen Sie diese Elemente, bevor Sie die Aktivität in eine Vorlage umwandeln.',
+        'Einige Elemente in dieser Aktivität sind von Ressourcen (z.B. Antwort-Sammlungen) abhängig, welche gelöscht, verändert (benötigte Antwort-Optionen entfernt) oder aus dem Konto entfernt wurden (bei geteiltem Zugriff). Bitte ersetzen Sie diese Elemente, bevor Sie die Aktivität in eine Vorlage umwandeln.',
       noResourceAccessRequired:
         'Diese Aktivität enthält keine Elemente, die von Ressourcen abhängen.',
       confirmationsTitle: 'Erforderliche Bestätigungen',
@@ -2323,7 +2323,7 @@ Da die KlickerUZH-App noch nicht im iOS-App-Store verfügbar ist, folgen Sie die
       newAnswerCollection: 'Neue Antwort-Sammlung',
       addSharedAnswerCollection: 'Geteilte Antwort-Sammlung hinzufügen',
       answerOptionUsed:
-        'Antwort-Optionen, welche mit dem Warnsymbold gekennzeichnet sind, werden bereits durch Sie oder andere Nutzer (im Fall einer geteilten Sammlung) in einer Frage als korrekte Lösung or Fallstudien-Element verwendet. Bitte beachten Sie dies beim Bearbeiten der Antwort. Die Option kann nicht gelöscht werden.',
+        'Antwort-Optionen, welche mit dem Warnsymbold gekennzeichnet sind, werden bereits durch Sie oder andere Nutzer (im Fall einer geteilten Sammlung) in einer Frage oder einer Aktivitätsvorlage als korrekte Lösung oder Fallstudien-Element verwendet. Bitte beachten Sie dies beim Bearbeiten der Antwort. Die Option kann nicht gelöscht werden.',
       name: 'Name',
       nameTooltip:
         'Wählen Sie einen Namen für Ihre Antwort-Sammlung, damit diese bei der Elementerstellung oder als geteilte Sammlung identifiziert werden kann. Der Name wird den Studierenden nicht angezeigt.',
diff --git a/packages/i18n/messages/en.ts b/packages/i18n/messages/en.ts
index b2c4d67d5c..d8e27da56e 100644
--- a/packages/i18n/messages/en.ts
+++ b/packages/i18n/messages/en.ts
@@ -1589,7 +1589,7 @@ Since the KlickerUZH app is not yet available on the iOS App Store, follow these
       noInstances:
         'The selected activity does not contain any elements and cannot be converted into a template.',
       resourcesRequiredMissing:
-        'Some elements in this activity depend on resources (e.g., answer collections) that have been deleted or removed from the account (in case of shared access). Please replace these elements before converting the activity into a template.',
+        'Some elements in this activity depend on resources (e.g., answer collections) that have been deleted, modified (required answer options removed), or removed from the account (in case of shared access). Please replace these elements before converting the activity into a template.',
       noResourceAccessRequired:
         'This activity does not contain any elements that depend on resources (e.g., answer collections).',
       confirmationsTitle: 'Required Confirmations',
@@ -2293,7 +2293,7 @@ Since the KlickerUZH app is not yet available on the iOS App Store, follow these
       newAnswerCollection: 'New Answer Collection',
       addSharedAnswerCollection: 'Add Shared Answer Collection',
       answerOptionUsed:
-        'Answer options marked with the warning symbol are already used as sample solutions or case study items in a question by you or other users (in case of a shared collection). Please keep this in mind when editing the answer. The option cannot be deleted.',
+        'Answer options marked with the warning symbol are already used as sample solutions or case study items in a question or activity template by you or other users (in case of a shared collection). Please keep this in mind when editing the answer. The option cannot be deleted.',
       name: 'Name',
       nameTooltip:
         'Choose a name for your answer collection so it can be identified when creating elements or as a shared collection. The name will not be shown to students.',
diff --git a/packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql b/packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql
new file mode 100644
index 0000000000..60dcfd1eb8
--- /dev/null
+++ b/packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql
@@ -0,0 +1,16 @@
+-- CreateTable
+CREATE TABLE "_TemplateAnswerCollectionUsedItems" (
+    "A" UUID NOT NULL,
+    "B" INTEGER NOT NULL,
+
+    CONSTRAINT "_TemplateAnswerCollectionUsedItems_AB_pkey" PRIMARY KEY ("A","B")
+);
+
+-- CreateIndex
+CREATE INDEX "_TemplateAnswerCollectionUsedItems_B_index" ON "_TemplateAnswerCollectionUsedItems"("B");
+
+-- AddForeignKey
+ALTER TABLE "_TemplateAnswerCollectionUsedItems" ADD CONSTRAINT "_TemplateAnswerCollectionUsedItems_A_fkey" FOREIGN KEY ("A") REFERENCES "ActivityTemplate"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- AddForeignKey
+ALTER TABLE "_TemplateAnswerCollectionUsedItems" ADD CONSTRAINT "_TemplateAnswerCollectionUsedItems_B_fkey" FOREIGN KEY ("B") REFERENCES "AnswerCollectionEntry"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/packages/prisma/src/prisma/schema/resources.prisma b/packages/prisma/src/prisma/schema/resources.prisma
index fc29b43192..55eb5a38de 100644
--- a/packages/prisma/src/prisma/schema/resources.prisma
+++ b/packages/prisma/src/prisma/schema/resources.prisma
@@ -40,7 +40,8 @@ model AnswerCollectionEntry {
     collectionId Int
 
     // elements where this entry is used as a sample solution -> deletion not allowed
-    itemUsages Element[] @relation("ElementAnswerCollectionUsedItems")
+    itemUsages     Element[]          @relation("ElementAnswerCollectionUsedItems")
+    templateUsages ActivityTemplate[] @relation("TemplateAnswerCollectionUsedItems")
 
     createdAt DateTime @default(now())
     updatedAt DateTime @updatedAt
diff --git a/packages/prisma/src/prisma/schema/sharing.prisma b/packages/prisma/src/prisma/schema/sharing.prisma
index 631d18d801..67fdad1a9e 100644
--- a/packages/prisma/src/prisma/schema/sharing.prisma
+++ b/packages/prisma/src/prisma/schema/sharing.prisma
@@ -258,7 +258,9 @@ model ActivityTemplate {
     groupActivityId String?        @unique @db.Uuid
 
     // optional link to answer collections, if any question in the activity is based on these collections
-    answerCollections AnswerCollection[] @relation("TemplateAnswerCollectionUsages")
+    answerCollections     AnswerCollection[]      @relation("TemplateAnswerCollectionUsages")
+    // optional link to answer collection items, if any question in the activity is based on these items
+    answerCollectionItems AnswerCollectionEntry[] @relation("TemplateAnswerCollectionUsedItems")
 }
 
 // #endregion


Reviews:
user: login: coderabbitai[bot]
body: **Actionable comments posted: 0**

<details>
<summary>🧹 Nitpick comments (2)</summary><blockquote>

<details>
<summary>packages/graphql/src/services/templates.ts (2)</summary><blockquote>

`322-341`: **New logic to extract answer collection entry IDs from element instances.**

The code properly extracts answer collection entry IDs from both selection elements (solution IDs) and case study elements (item IDs).


Consider adding null/undefined checks for `answerCollectionSolutionIds` and `items` to handle edge cases where these properties might not be present:

```diff
- ? ((instance.elementData as SelectionElementData).options
-     .answerCollectionSolutionIds ?? [])
+ ? ((instance.elementData as SelectionElementData).options
+     ?.answerCollectionSolutionIds ?? [])

- : ((
-     instance.elementData as CaseStudyElementData
-   ).options.items!.map((item) => item.id) ?? [])
+ : ((
+     instance.elementData as CaseStudyElementData
+   ).options.items?.map((item) => item.id) ?? [])
```

---

`393-404`: **Verification of answer collection entries existence.**

The code now checks that all required answer collection entries exist in addition to answer collections.


Consider enhancing error reporting by distinguishing between missing answer collections and missing answer collection entries:

```diff
if (
  answerCollections.length === answerCollectionIds.length &&
  answerCollectionEntries.length === answerCollectionEntryIds.length
) {
  return {
    noInstances: false,
    noResourcesRequired: false,
    resourcesRequiredExist: true,
    resourcesRequiredMissing: false,
  }
} else {
+ const missingCollections = answerCollections.length !== answerCollectionIds.length;
+ const missingEntries = answerCollectionEntries.length !== answerCollectionEntryIds.length;
  return {
    noInstances: false,
    noResourcesRequired: false,
    resourcesRequiredExist: false,
    resourcesRequiredMissing: true,
+   missingCollections,
+   missingEntries,
  }
}
```

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 9de41422d298d0e9da0b2fe5fd5da1ebcf87f9fc and 6cb61617fa925e1e17a9bdc9dcb93ae539f4bd21.

</details>

<details>
<summary>📒 Files selected for processing (6)</summary>

* `packages/graphql/src/services/templates.ts` (16 hunks)
* `packages/i18n/messages/de.ts` (1 hunks)
* `packages/i18n/messages/en.ts` (1 hunks)
* `packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql` (1 hunks)
* `packages/prisma/src/prisma/schema/resources.prisma` (1 hunks)
* `packages/prisma/src/prisma/schema/sharing.prisma` (1 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🧠 Learnings (1)</summary>

<details>
<summary>packages/i18n/messages/de.ts (1)</summary>

```
Learnt from: sjschlapbach
PR: uzh-bf/klicker-uzh#4550
File: apps/frontend-manage/src/components/courses/modals/TemplateConversionModal.tsx:132-138
Timestamp: 2025-03-11T15:07:47.117Z
Learning: The translation keys for template conversion error messages (`noInstances`, `resourcesRequiredMissing`, `noResourceAccessRequired`) are properly defined in the translation files (packages/i18n/messages/en.ts and packages/i18n/messages/de.ts) for the KlickerUZH project.
```

</details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (4)</summary>

* GitHub Check: Analyze (javascript)
* GitHub Check: build
* GitHub Check: test
* GitHub Check: cypress-run

</details>

<details>
<summary>🔇 Additional comments (9)</summary><blockquote>

<details>
<summary>packages/prisma/src/prisma/schema/resources.prisma (1)</summary>

`43-44`: **Great addition of bidirectional relation with templates**

This change adds a new relation field `templateUsages` to track which `ActivityTemplate` objects use this answer collection entry. This complements the existing `itemUsages` relation that tracks usage in elements, creating a more complete dependency tracking system.

The relation name "TemplateAnswerCollectionUsedItems" is clear and consistent with the existing naming pattern.

</details>
<details>
<summary>packages/i18n/messages/en.ts (1)</summary>

`1592-1592`: **Updated error message accurately reflects new functionality**

The error message now correctly indicates that resources could be missing not only when they've been deleted or removed from the account, but also when they've been modified by removing required answer options.

This change aligns with the schema update that now tracks specific answer collection entries used in templates, providing better user feedback when dependencies are broken.

</details>
<details>
<summary>packages/i18n/messages/de.ts (1)</summary>

`1608-1608`: **Appropriately updated translation for resource dependency error message.**

The German translation for the `resourcesRequiredMissing` error message has been expanded to include the scenario where answer options have been removed from a resource. This change aligns with the pull request's focus on tracking and linking specific answer collection entries and provides clearer guidance to users.

</details>
<details>
<summary>packages/prisma/src/prisma/schema/sharing.prisma (1)</summary>

`261-263`: **Relationship added for tracking answer collection entries used in templates.**

The new `answerCollectionItems` relation field properly establishes a many-to-many relationship between activity templates and answer collection entries, complementing the existing `answerCollections` relation.

</details>
<details>
<summary>packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql (1)</summary>

`1-16`: **Well-structured migration for the new many-to-many relationship.**

The migration correctly creates the join table with appropriate foreign key constraints and an index on the "B" column for optimized lookups. The CASCADE behavior on DELETE and UPDATE ensures proper referential integrity.

</details>
<details>
<summary>packages/graphql/src/services/templates.ts (4)</summary>

`170-171`: **Update to function signature to include answer collection entry IDs.**

The return type now includes `answerCollectionEntryIds` which aligns with the PR objective of tracking specific answer collection entries.

---

`196-197`: **Return statements consistently updated for error handling.**

All return statements are properly updated to include empty arrays for `answerCollectionEntryIds` in error cases.



Also applies to: 222-223, 248-249, 275-277, 288-289, 299-300

---

`441-447`: **Updated destructuring to capture answer collection entry IDs.**

The `createActivityTemplate` function now properly captures the answer collection entry IDs from the result of `getActivityAnswerCollectionIds`.

---

`519-524`: **Consistent implementation of answer collection item connections across activity types.**

The code consistently adds logic to connect answer collection items for all activity types when creating or copying templates.



Also applies to: 610-617, 704-711, 807-814, 873-878

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-29T12:58:23Z
user: login: coderabbitai[bot]
body: **Actionable comments posted: 2**

<details>
<summary>🔭 Outside diff range comments (1)</summary><blockquote>

<details>
<summary>packages/graphql/test/template.test.ts (1)</summary><blockquote>

`939-966`: _🛠️ Refactor suggestion_

**Leverage `include` projection only once instead of re-querying; eliminate fragile optional-chaining pattern**

1. The additional `answerCollectionItems` include is 👍, but we subsequently dereference the field via  
   `template?.answerCollectionItems.map(...)`.  
   If the query unexpectedly returns `null` (`template === null`) the optional-chain stops at `template`, but the plain `.` after it will still try to access `.map` on `undefined` and crash the test runner.

   ```ts
   // current
   const answerCollectionItems = template?.answerCollectionItems.map((item) => item.id)

   // safer
   const answerCollectionItems = template?.answerCollectionItems?.map((item) => item.id) ?? []
   ```

2. The same dereference occurs in all following occurrences (`template3`, `template4`, …).

Adopting the defensive `?.` chain will make the assertions resilient to intermittent DB race conditions in CI.

</blockquote></details>

</blockquote></details>

<details>
<summary>🧹 Nitpick comments (4)</summary><blockquote>

<details>
<summary>packages/graphql/test/template.test.ts (2)</summary><blockquote>

`1009-1013`: **Turn comment into expect-style assertion or remove**

The comment describing a “trigger another element instance update” does not add executable value.  
If the scenario is important, transform the intent into a concrete `expect` that asserts the update call succeeded (e.g. compare `updatedAt` timestamps), otherwise delete the comment to avoid drift.

---

`1022-1047`: **Avoid duplicated test logic via helper**

The 6-line assertion block for `answerCollectionItems` is duplicated several times (lines 960-966, 1041-1046, 1096-1102, 1153-1159, 1212-1218, 1261-1274). Create a helper:

```ts
function expectTemplateItems(template: ActivityTemplate | null, expected: number[]) {
  const items = template?.answerCollectionItems?.map((i) => i.id) ?? []
  expect(items).toHaveLength(expected.length)
  expect(items).toEqual(expect.arrayContaining(expected))
}
```

This reduces boilerplate and keeps future changes consistent.

</blockquote></details>
<details>
<summary>packages/graphql/src/services/questions.ts (2)</summary><blockquote>

`1196-1250`: **Race condition: concurrent template updates may overwrite each other**

Multiple instances belonging to the same template are updated in parallel (`Promise.allSettled`).  
They each issue an `activityTemplate.update` that writes full lists of `connect`/`disconnect`.  
When two such transactions overlap the second one may re-apply stale state produced by the first, dropping links.

Options:

1. Serialize template updates with a per-template mutex / `p-limit(1)`.
2. Push the connect/disconnect aggregation **outside** the per-instance loop: compute the final delta per template, then perform a single update.

Failing to address this could leave templates in an inconsistent state.  


Would you like a concrete implementation sketch for approach #2?

---

`1293-1303`: **Derived permissions are not updated for entry items**

After modifying `answerCollectionItems`, only collection-level permissions are recomputed.  
If entry-level permissions are referenced elsewhere (`templateUsages`), their derived permissions (or usage counters) should also be refreshed to keep ACL & metrics consistent.

Consider:

```ts
for (const entryId of uniqueTouchedAnswerCollectionEntryIds) {
  await recomputeDerivedPermissions({ answerCollectionEntryId: entryId }, ctx.prisma)
}
```

(or whichever helper exists).

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: CodeRabbit UI**
**Review profile: CHILL**
**Plan: Pro**

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between e39400d69f4ee17b19d647f16e237627eb2db834 and 8ed6eb59135cae5f5abc74d6c56a2c6e9ee69c4b.

</details>

<details>
<summary>📒 Files selected for processing (3)</summary>

* `packages/graphql/src/services/questions.ts` (4 hunks)
* `packages/graphql/src/services/sharing.ts` (2 hunks)
* `packages/graphql/test/template.test.ts` (11 hunks)

</details>

<details>
<summary>✅ Files skipped from review due to trivial changes (1)</summary>

* packages/graphql/src/services/sharing.ts

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (5)</summary>

* GitHub Check: SonarCloud
* GitHub Check: check
* GitHub Check: Analyze (javascript)
* GitHub Check: cypress-run
* GitHub Check: test

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-29T13:42:00Z
user: login: sjschlapbach
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-29T13:44:30Z
user: login: coderabbitai[bot]
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-29T13:45:17Z
user: login: sjschlapbach
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-29T14:50:06Z
user: login: coderabbitai[bot]
state: COMMENTED
author_association: NONE
submitted_at: 2025-04-29T14:54:23Z

Review comments:
diff_hunk: @@ -1163,6 +1182,17 @@ export async function updateElementInstances(
               (id) => !templateCollectionIds.includes(id)
             )
 
+            const templateCollectionEntryIds =
+              template.answerCollectionItems.map((collection) => collection.id)
+            const collectionEntriesToDisconnect =
+              templateCollectionEntryIds.filter(
+                (id) => !instanceCollectionEntryIds.includes(id)
+              )
+            const collectionEntriesToConnect =
+              instanceCollectionEntryIds.filter(
+                (id) => !templateCollectionEntryIds.includes(id)
+              )
+
path: packages/graphql/src/services/questions.ts
user: login: coderabbitai[bot]
body: _⚠️ Potential issue_

**Possible duplicate IDs – de-duplicate before connect/disconnect**

`collectionEntriesToConnect` / `collectionEntriesToDisconnect` are derived from array filters but are not `Set`-deduplicated.  
Passing duplicate IDs to Prisma’s `connect`/`disconnect` causes `P2002 Unique constraint` errors.

Add `Array.from(new Set(...))` before writing to the mutation, or reuse `uniqueBy` from `remeda`.

<details>
<summary>🤖 Prompt for AI Agents (early access)</summary>

```
In packages/graphql/src/services/questions.ts around lines 1185 to 1195, the arrays collectionEntriesToConnect and collectionEntriesToDisconnect may contain duplicate IDs, which can cause Prisma P2002 unique constraint errors when used in connect/disconnect mutations. To fix this, convert these arrays to sets to remove duplicates by wrapping them with Array.from(new Set(...)) or use the uniqueBy function from remeda before passing them to the mutation.
```

</details>

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-29T13:41:59Z
author_association: NONE
diff_hunk: @@ -1094,48 +1094,66 @@
               element.type === DB.ElementType.CASE_STUDY) &&
             element.answerCollectionId !== null
           ) {
-            // get all answer collections connected to one of the impacted template activities
+            // get all answer collections and answer collection entries used in any instance in the affected template
             let instanceCollectionIds: number[] = []
+            let instanceCollectionEntryIds: number[] = []
+
             if (typeof liveQuizId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: liveQuizId,
-                    activityType: ActivityType.LIVE_QUIZ,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: liveQuizId,
+                  activityType: ActivityType.LIVE_QUIZ,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof practiceQuizId !== 'undefined') {
path: packages/graphql/src/services/questions.ts
user: login: coderabbitai[bot]
body: _🛠️ Refactor suggestion_

_⚠️ Potential issue_

**N × M query explosion – cache activity ↔ collection look-ups**

`getActivityAnswerCollectionIds` is invoked once per **instance** even though many instances reference the **same activity**.  
For a template containing 30 questions the code issues 30 identical DB queries, degrading performance.

Refactor by collecting a set of `(activityId, activityType)` pairs first, fetching their IDs once, and re-using the result inside the Promise loop.

```diff
- // inside per-instance loop
- const { answerCollectionIds: ids, answerCollectionEntryIds: entryIds } =
-   await getActivityAnswerCollectionIds(...)

+ // outside loop
+ const activityCache = new Map<string /*activityId*/, {ids:number[], entryIds:number[]}>()
+
+ ...
+ if (!activityCache.has(liveQuizId)) {
+   activityCache.set(
+     liveQuizId,
+     await getActivityAnswerCollectionIds({ activityId: liveQuizId, activityType: ActivityType.LIVE_QUIZ }, ctx)
+   )
+ }
+ const { ids, entryIds } = activityCache.get(liveQuizId)!
```

This single change cuts DB round-trips from O(instances) to O(distinct activities).  




> Committable suggestion skipped: line range outside the PR's diff.

<details>
<summary>🤖 Prompt for AI Agents (early access)</summary>

```
In packages/graphql/src/services/questions.ts around lines 1098 to 1115, the code calls getActivityAnswerCollectionIds separately for each instance, causing redundant database queries for the same activity. Refactor by first collecting all distinct (activityId, activityType) pairs from the instances, then fetch their answerCollectionIds and answerCollectionEntryIds once in a batch. Store these results in a map keyed by (activityId, activityType) and reuse them inside the Promise loop to assign instanceCollectionIds and instanceCollectionEntryIds, reducing DB queries from O(instances) to O(distinct activities).
```

</details>

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-29T13:41:59Z
author_association: NONE
diff_hunk: @@ -1094,48 +1094,66 @@
               element.type === DB.ElementType.CASE_STUDY) &&
             element.answerCollectionId !== null
           ) {
-            // get all answer collections connected to one of the impacted template activities
+            // get all answer collections and answer collection entries used in any instance in the affected template
             let instanceCollectionIds: number[] = []
+            let instanceCollectionEntryIds: number[] = []
+
             if (typeof liveQuizId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: liveQuizId,
-                    activityType: ActivityType.LIVE_QUIZ,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: liveQuizId,
+                  activityType: ActivityType.LIVE_QUIZ,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof practiceQuizId !== 'undefined') {
path: packages/graphql/src/services/questions.ts
user: login: sjschlapbach
body: The same element is typically only contained once in a corresponding activity, meaning that only a single instance will be found, making this an N x 1 complexity, which should not be problematic
created_at: 2025-04-29T13:44:30Z
author_association: MEMBER
diff_hunk: @@ -1094,48 +1094,66 @@
               element.type === DB.ElementType.CASE_STUDY) &&
             element.answerCollectionId !== null
           ) {
-            // get all answer collections connected to one of the impacted template activities
+            // get all answer collections and answer collection entries used in any instance in the affected template
             let instanceCollectionIds: number[] = []
+            let instanceCollectionEntryIds: number[] = []
+
             if (typeof liveQuizId !== 'undefined') {
-              const { answerCollectionIds: ids } =
-                await getActivityAnswerCollectionIds(
-                  {
-                    activityId: liveQuizId,
-                    activityType: ActivityType.LIVE_QUIZ,
-                  },
-                  ctx
-                )
+              const {
+                answerCollectionIds: ids,
+                answerCollectionEntryIds: entryIds,
+              } = await getActivityAnswerCollectionIds(
+                {
+                  activityId: liveQuizId,
+                  activityType: ActivityType.LIVE_QUIZ,
+                },
+                ctx
+              )
+
               instanceCollectionIds = ids
+              instanceCollectionEntryIds = entryIds
             } else if (typeof practiceQuizId !== 'undefined') {
path: packages/graphql/src/services/questions.ts
user: login: coderabbitai[bot]
body: @sjschlapbach Thank you for the clarification. That makes perfect sense - if each element typically appears only once in an activity, then we're indeed looking at an N x 1 situation rather than N x M. In that case, the caching optimization wouldn't provide significant benefits since there aren't multiple redundant queries for the same activity. I appreciate you sharing this context about the typical usage patterns in your system.

---

<details>
<summary>✏️ Learnings added</summary>

```
Learnt from: sjschlapbach
PR: uzh-bf/klicker-uzh#4648
File: packages/graphql/src/services/questions.ts:1098-1115
Timestamp: 2025-04-29T13:45:04.743Z
Learning: In the KlickerUZH system, elements are typically contained only once in a corresponding activity, meaning a single element instance will usually be found per activity, resulting in N x 1 complexity for operations that query by activity and element.
```

> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>




<!-- <agentComponentType>chat-answer</agentComponentType> -->

<!-- <rawResChunk><answer>@sjschlapbach Thank you for the clarification. That makes perfect sense - if each element typically appears only once in an activity, then we're indeed looking at an N x 1 situation rather than N x M. In that case, the caching optimization wouldn't provide significant benefits since there aren't multiple redundant queries for the same activity. I appreciate you sharing this context about the typical usage patterns in your system.

---

<details>
<summary>✏️ Learnings added</summary>

```
Learnt from: sjschlapbach
PR: uzh-bf/klicker-uzh#4648
File: packages/graphql/src/services/questions.ts:1098-1115
Timestamp: 2025-04-29T13:45:04.743Z
Learning: In the KlickerUZH system, elements are typically contained only once in a corresponding activity, meaning a single element instance will usually be found per activity, resulting in N x 1 complexity for operations that query by activity and element.
```

> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>

</answer></rawResChunk> -->

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-29T13:45:17Z
author_association: NONE
diff_hunk: @@ -1163,6 +1182,17 @@ export async function updateElementInstances(
               (id) => !templateCollectionIds.includes(id)
             )
 
+            const templateCollectionEntryIds =
+              template.answerCollectionItems.map((collection) => collection.id)
+            const collectionEntriesToDisconnect =
+              templateCollectionEntryIds.filter(
+                (id) => !instanceCollectionEntryIds.includes(id)
+              )
+            const collectionEntriesToConnect =
+              instanceCollectionEntryIds.filter(
+                (id) => !templateCollectionEntryIds.includes(id)
+              )
+
path: packages/graphql/src/services/questions.ts
user: login: sjschlapbach
body: deduplication happens in a different place
created_at: 2025-04-29T14:50:06Z
author_association: MEMBER
diff_hunk: @@ -1163,6 +1182,17 @@ export async function updateElementInstances(
               (id) => !templateCollectionIds.includes(id)
             )
 
+            const templateCollectionEntryIds =
+              template.answerCollectionItems.map((collection) => collection.id)
+            const collectionEntriesToDisconnect =
+              templateCollectionEntryIds.filter(
+                (id) => !instanceCollectionEntryIds.includes(id)
+              )
+            const collectionEntriesToConnect =
+              instanceCollectionEntryIds.filter(
+                (id) => !templateCollectionEntryIds.includes(id)
+              )
+
path: packages/graphql/src/services/questions.ts
user: login: coderabbitai[bot]
body: <details>
<summary>🧩 Analysis chain</summary>

🏁 Script executed:
```
#!/bin/bash
# Look at the code following the highlighted section to see how these arrays are used
cat packages/graphql/src/services/questions.ts | grep -A 60 "collectionEntriesToConnect ="
```

Length of output: 2770

---



</details>

@sjschlapbach You're correct. Looking at the source code, I can see that deduplication happens upstream in the `getActivityAnswerCollectionIds` function where both `answerCollectionIds` and `answerCollectionEntryIds` are already deduplicated using `Array.from(new Set(...))` before they're passed down to the code in question. This ensures the arrays are free of duplicates before they're used to compute `collectionEntriesToConnect` and `collectionEntriesToDisconnect`.

---

<details>
<summary>🧬 Code Graph Analysis Results</summary>

### Relevant snippets from `packages/graphql/src/services/resources.ts` (lines 108-194)
```ts
export async function getAnswerCollectionsElements(
  { templateId }: { templateId?: string | null },
  ctx: PrismaTransactionContextWithUser
) {
  // fetch all answer collections, which are available to be included in elements
  const user = await ctx.prisma.user.findUnique({
    where: { id: ctx.user.sub },
    include: {
      objects: {
        where: { answerCollectionId: { not: null } },
        include: {
          answerCollection: {
            include: {
              owner: { select: { shortname: true } },
              entries: { orderBy: { value: 'asc' } },
            },
          },
        },
      },
    },
  })

  if (!user) {
    return []
  }

  // include answer collections used in any element of the template (required for preview / editing)
  // in case the instances were not modified, the user would get access to them either way
  let templateAnswerCollections: (DB.AnswerCollection & {
    entries: DB.AnswerCollectionEntry[]
  })[] = []
  if (templateId) {
    // verify that the user has access to the template activity
    const { accessible } = await validateTemplateAccessible({ templateId }, ctx)
    if (accessible) {
      const template = await ctx.prisma.activityTemplate.findUnique({
        where: {
          id: templateId,
        },
        include: {
          answerCollections: {
            include: {
              entries: {
                orderBy: {
                  value: 'asc',
                },
              },
            },
            orderBy: {
              name: 'asc',
            },
          },
        },
      })

      templateAnswerCollections = template?.answerCollections ?? []
    } else {
      templateAnswerCollections = []
    }
  }

  const combinedAnswerCollections = [
    ...user.objects.flatMap((object) =>
      object.answerCollection
        ? {
            ...object.answerCollection,
            isShared: object.permissionLevel !== DB.PermissionLevel.OWNER,
          }
        : []
    ),
    ...templateAnswerCollections.map((collection) => ({
      ...collection,
      isShared: false,
    })),
  ]

  // return deduplicated list of answer collections (based on id)
  return combinedAnswerCollections.reduce<
    (DB.AnswerCollection & { entries: DB.AnswerCollectionEntry[] })[]
  >((acc, collection) => {
    if (!acc.some((c) => c.id === collection.id)) {
      acc.push(collection)
    }

    return acc
  }, [])
}
```
- This function fetches answer collections available to the user, including those linked to a template if provided.
- It merges user-owned and template answer collections and returns a deduplicated list by collection id.
- Deduplication is explicitly done here by filtering out duplicates based on id.

---

### Relevant snippets from `packages/graphql/src/services/templates.ts` (lines 144-349)
```ts
export async function getActivityAnswerCollectionIds(
  {
    activityId,
    activityType,
  }: { activityId: string; activityType: ActivityType },
  ctx: PrismaTransactionContextWithUser
): Promise<{
  error: boolean
  activity:
    | (DB.LiveQuiz & {
        blocks: (DB.ElementBlock & { elements: DB.ElementInstance[] })[]
      })
    | (DB.PracticeQuiz & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
      })
    | (DB.MicroLearning & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
      })
    | (DB.GroupActivity & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
        parameters: DB.GroupActivityParameter[]
        clues: DB.GroupActivityClue[]
      })
    | null
  noInstances: boolean
  answerCollectionIds: number[]
  answerCollectionEntryIds: number[]
}> {
  // helper function that finds the ids of all answer collections linked to elements in an activity
  // fetch all element instances included in the activity that should be converted
  let instances: DB.ElementInstance[] = []
  let activity
  if (activityType === ActivityType.LIVE_QUIZ) {
    const liveQuiz = await ctx.prisma.liveQuiz.findUnique({
      where: {
        id: activityId,
      },
      include: {
        blocks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!liveQuiz) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = liveQuiz
    instances = liveQuiz.blocks.flatMap((block) => block.elements)
  } else if (activityType === ActivityType.PRACTICE_QUIZ) {
    const practiceQuiz = await ctx.prisma.practiceQuiz.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!practiceQuiz) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = practiceQuiz
    instances = practiceQuiz.stacks.flatMap((stack) => stack.elements)
  } else if (activityType === ActivityType.MICRO_LEARNING) {
    const microLearning = await ctx.prisma.microLearning.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!microLearning) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = microLearning
    instances = microLearning.stacks.flatMap((stack) => stack.elements)
  } else if (activityType === ActivityType.GROUP_ACTIVITY) {
    const groupActivity = await ctx.prisma.groupActivity.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
        parameters: true,
        clues: true,
      },
    })

    if (!groupActivity) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = groupActivity
    instances = groupActivity.stacks.flatMap((stack) => stack.elements)
  } else {
    return {
      error: true,
      activity: null,
      noInstances: false,
      answerCollectionIds: [],
      answerCollectionEntryIds: [],
    }
  }

  // if no instances are found, return this
  if (instances.length === 0) {
    return {
      error: false,
      activity,
      noInstances: true,
      answerCollectionIds: [],
      answerCollectionEntryIds: [],
    }
  }

  // extract all answer collection ids (and potential other resources once supported)
  const answerCollectionIds = Array.from(
    new Set(
      instances
        .filter(
          (instance) =>
            instance.elementType === DB.ElementType.SELECTION ||
            instance.elementType === DB.ElementType.CASE_STUDY
        )
        .map((instance) =>
          instance.elementType === DB.ElementType.SELECTION
            ? (instance.elementData as SelectionElementData).options
                .answerCollection!.id
            : (instance.elementData as CaseStudyElementData).options
                .answerCollectionId!
        )
    )
  )

  // extract all answer collection entry ids that are used as sample solutions in selection elements or as items in case study elements
  const answerCollectionEntryIds = Array.from(
    new Set(
      instances
        .filter(
          (instance) =>
            instance.elementType === DB.ElementType.SELECTION ||
            instance.elementType === DB.ElementType.CASE_STUDY
        )
        .flatMap((instance) =>
          instance.elementType === DB.ElementType.SELECTION
            ? ((instance.elementData as SelectionElementData).options
                .answerCollectionSolutionIds ?? [])
            : ((
                instance.elementData as CaseStudyElementData
              ).options.items!.map((item) => item.id) ?? [])
        )
    )
  )

  return {
    error: false,
    activity,
    noInstances: false,
    answerCollectionIds,
    answerCollectionEntryIds,
  }
}
```
- This function collects all answer collection IDs and entry IDs linked to elements in a given activity.
- It uses `Set` to deduplicate the IDs before returning them.

---

### Relevant snippets from `packages/util/src/permissions.ts` (lines 39-135)
```ts
export async function recomputeDerivedPermissions(
  {
    // object ids - exactly one must be defined
    catalogCollectionId,
    answerCollectionId,
    elementId,
    courseId,
    liveQuizId,
    practiceQuizId,
    microLearningId,
    groupActivityId,
    // optional user to limit the required recomputation
    userId,
  }: {
    catalogCollectionId?: string
    answerCollectionId?: number
    elementId?: number
    courseId?: string
    liveQuizId?: string
    practiceQuizId?: string
    microLearningId?: string
    groupActivityId?: string
    userId?: string
  } & (
    | { catalogCollectionId: string }
    | { answerCollectionId: number }
    | { elementId: number }
    | { courseId: string }
    | { liveQuizId: string }
    | { practiceQuizId: string }
    | { microLearningId: string }
    | { groupActivityId: string }
  ),
  prisma: PrismaTransactionClient
) {
  if (typeof catalogCollectionId !== 'undefined') {
    await recomputeCatalogCollectionPermissions(
      { id: catalogCollectionId, userId },
      prisma
    )
  } else if (typeof answerCollectionId !== 'undefined') {
    await recomputeAnswerCollectionPermissions(
      { id: answerCollectionId, userId },
      prisma
    )
  } else if (typeof elementId !== 'undefined') {
    await recomputeElementPermissions(
      {
        id: elementId,
        userId,
      },
      prisma
    )
  } else if (typeof liveQuizId !== 'undefined') {
    await recomputeLiveQuizPermissions(
      {
        id: liveQuizId,
        userId,
      },
      prisma
    )
  } else if (typeof practiceQuizId !== 'undefined') {
    await recomputePracticeQuizPermissions(
      {
        id: practiceQuizId,
        userId,
      },
      prisma
    )
  } else if (typeof microLearningId !== 'undefined') {
    await recomputeMicroLearningPermissions(
      {
        id: microLearningId,
        userId,
      },
      prisma
    )
  } else if (typeof groupActivityId !== 'undefined') {
    await recomputeGroupActivityPermissions(
      {
        id: groupActivityId,
        userId,
      },
      prisma
    )
  } else if (typeof courseId !== 'undefined') {
    await recomputeCoursePermissions(
      {
        id: courseId,
        userId,
      },
      prisma
    )
  } else {
    throw new Error('No object id defined')
  }
}
```
- This function triggers recomputation of derived permissions for various resource types.
- It requires exactly one resource identifier and optionally a user ID.
- It calls the appropriate recomputation function based on the resource type.

---

### Relevant snippets from `packages/graphql/src/services/sharing.ts` (lines 3898-4071)
```ts
export async function checkAccess(
  checks: (
    | {
        catalogCollectionId: string
        minimumPermissionLevel: DB.PermissionLevel
      }
    | {
        answerCollectionId: number
        minimumPermissionLevel: DB.PermissionLevel
      }
    | { elementId: number; minimumPermissionLevel: DB.PermissionLevel }
    | { liveQuizId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { practiceQuizId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { microLearningId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { groupActivityId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { courseId: string; minimumPermissionLevel: DB.PermissionLevel }
  )[],
  ctx: PrismaTransactionContextWithUser
) {
  for (const check of checks) {
    if (
      'catalogCollectionId' in check &&
      typeof check.catalogCollectionId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          catalogCollectionId_userId: {
            catalogCollectionId: check.catalogCollectionId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'answerCollectionId' in check &&
      typeof check.answerCollectionId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          answerCollectionId_userId: {
            answerCollectionId: check.answerCollectionId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if ('elementId' in check && typeof check.elementId !== 'undefined') {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          elementId_userId: {
            elementId: check.elementId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'liveQuizId' in check &&
      typeof check.liveQuizId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          liveQuizId_userId: {
            liveQuizId: check.liveQuizId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'practiceQuizId' in check &&
      typeof check.practiceQuizId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          practiceQuizId_userId: {
            practiceQuizId: check.practiceQuizId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'microLearningId' in check &&
      typeof check.microLearningId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          microLearningId_userId: {
            microLearningId: check.microLearningId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'groupActivityId' in check &&
      typeof check.groupActivityId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          groupActivityId_userId: {
            groupActivityId: check.groupActivityId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if ('courseId' in check && typeof check.courseId !== 'undefined') {
      const coursePermission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          courseId_userId: {
            courseId: check.courseId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!coursePermission) {
        return false
      }
    } else {
      // ? encountered unsupported element type
      return false
    }
  }

  return true
}
```
- This function checks if the current user has at least the minimum required permission level for a list of resource checks.
- It queries the `derivedPermission` table for each resource and returns false if any check fails.
- Returns true only if all checks pass.

---

### Relevant snippets from `packages/util/src/elements.ts` (lines 54-184)
```ts
export function processElementData(
  element: ElementWithAnswerCollection
): ElementData {
  if (element.type === PrismaElementType.FLASHCARD) {
    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (
    element.type === PrismaElementType.SC ||
    element.type === PrismaElementType.MC ||
    element.type === PrismaElementType.KPRIM
  ) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsChoices,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.NUMERICAL) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsNumerical,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.FREE_TEXT) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsFreeText,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.CONTENT) {
    return {
      ...pick(element, CONTENT_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (
    element.type === PrismaElementType.SELECTION &&
    'hasSampleSolution' in element.options &&
    'numberOfInputs' in element.options
  ) {
    if (
      !element.answerCollection?.entries ||
      (element.options.hasSampleSolution && !element.answerCollectionItems)
    ) {
      throw new Error(
        'Answer collection or solutions missing for selection element'
      )
    }

    // formulate answer collection in the format as it will be required in the element data options
    const answerCollectionOptions = {
      id: element.answerCollection.id,
      entries: element.answerCollection.entries.map((entry) => ({
        id: entry.id,
        value: entry.value,
      })),
    }

    // extract the ids of the correct solution options
    const answerCollectionSolutionIds = element.options.hasSampleSolution
      ? element.answerCollectionItems!.map((entry) => entry.id)
      : []

    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
      options: {
        hasSampleSolution: element.options.hasSampleSolution,
        numberOfInputs: element.options.numberOfInputs,
        answerCollection: answerCollectionOptions,
        answerCollectionSolutionIds,
      } as ElementOptionsSelection,
    }
  } else if (element.type === PrismaElementType.CASE_STUDY) {
    // make sure that answer collection and selected items were passed to the function
    if (
      !element.answerCollection ||
      !element.answerCollection.entries ||
      element.answerCollection.entries.length === 0 ||
      !element.answerCollectionItems ||
      element.answerCollectionItems.length === 0
    ) {
      throw new Error(
        'Answer collection or selected items missing for case study element'
      )
    }

    // extract selected items from collection (store only relevant information on instance)
    const selectedItemIds = element.answerCollectionItems.map((item) => item.id)
    const caseStudyItems = element.answerCollection.entries.flatMap(
      (entry: AnswerCollectionEntry) => {
        if (selectedItemIds?.includes(entry.id)) {
          return {
            id: entry.id,
            value: entry.value,
          }
        }

        return []
      }
    )

    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
      options: {
        ...element.options,
        answerCollectionId: element.answerCollection.id,
        items: caseStudyItems,
      } as ElementOptionsCaseStudy,
    }
  } else {
    throw new Error(
      'Invalid element type encountered during element data processing'
    )
  }
}
```
- This function processes an element and returns a structured `ElementData` object.
- For SELECTION and CASE_STUDY element types, it ensures answer collections and solution items are present.
- It formats answer collections and solution IDs appropriately for element data options.

---

### Relevant snippets from `packages/graphql/src/services/questions.ts` (selected excerpts related to deduplication and permissions)
- In `getAnswerCollectionsElements` function call inside `manipulateQuestion`:
```ts
const availableAnswerCollections = await getAnswerCollectionsElements(
  { templateId },
  ctx
)
```
- In `manipulateQuestion`, permissions are checked for answer collections linked to questions:
```ts
const validAccess = await checkAccess(
  [
    {
      answerCollectionId: options.answerCollection,
      minimumPermissionLevel: DB.PermissionLevel.READ,
    },
  ],
  ctx
)
```
- In `manipulateQuestion`, after upsert, derived permissions are recomputed for the element and possibly for the old answer collection:
```ts
await recomputeDerivedPermissions(
  { elementId: question.id, userId: ctx.user.sub },
  ctx.prisma
)

if (
  questionPrev?.answerCollectionId !== null &&
  typeof questionPrev?.answerCollectionId !== 'undefined' &&
  question.answerCollectionId !== questionPrev.answerCollectionId
) {
  await recomputeDerivedPermissions(
    { answerCollectionId: questionPrev.answerCollectionId },
    ctx.prisma
  )
}
```
- In `updateElementInstances`, after updating instances, derived permissions are recomputed for touched answer collections:
```ts
if (includeTemplates) {
  const uniqueTouchedAnswerCollectionIds = [
    ...new Set(touchedAnswerCollectionIds),
  ]

  for (const id of uniqueTouchedAnswerCollectionIds) {
    await recomputeDerivedPermissions({ answerCollectionId: id }, ctx.prisma)
  }
}
```

---

These snippets provide context on where deduplication of answer collections happens (notably in `getAnswerCollectionsElements`), how permissions are checked (`checkAccess`), and how derived permissions are recomputed (`recomputeDerivedPermissions`). This is relevant to understanding the comment "deduplication happens in a different place" in the `questions.ts` file.

</details>



---

<details>
<summary>✏️ Learnings added</summary>

```
Learnt from: sjschlapbach
PR: uzh-bf/klicker-uzh#4648
File: packages/graphql/src/services/questions.ts:1185-1195
Timestamp: 2025-04-29T14:54:21.935Z
Learning: In the `updateElementInstances` function of `packages/graphql/src/services/questions.ts`, the arrays `collectionEntriesToConnect` and `collectionEntriesToDisconnect` do not need additional deduplication because they are derived from `instanceCollectionEntryIds` which is already deduplicated in the `getActivityAnswerCollectionIds` function using `Array.from(new Set(...))`.
```

> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>




<!-- <agentComponentType>chat-answer</agentComponentType> -->

<!-- <rawResChunk><answer><details>
<summary>🧩 Analysis chain</summary>

🏁 Script executed:
```
#!/bin/bash
# Look at the code following the highlighted section to see how these arrays are used
cat packages/graphql/src/services/questions.ts | grep -A 60 "collectionEntriesToConnect ="
```

Length of output: 2770

---



</details>

@sjschlapbach You're correct. Looking at the source code, I can see that deduplication happens upstream in the `getActivityAnswerCollectionIds` function where both `answerCollectionIds` and `answerCollectionEntryIds` are already deduplicated using `Array.from(new Set(...))` before they're passed down to the code in question. This ensures the arrays are free of duplicates before they're used to compute `collectionEntriesToConnect` and `collectionEntriesToDisconnect`.

---

<details>
<summary>🧬 Code Graph Analysis Results</summary>

### Relevant snippets from `packages/graphql/src/services/resources.ts` (lines 108-194)
```ts
export async function getAnswerCollectionsElements(
  { templateId }: { templateId?: string | null },
  ctx: PrismaTransactionContextWithUser
) {
  // fetch all answer collections, which are available to be included in elements
  const user = await ctx.prisma.user.findUnique({
    where: { id: ctx.user.sub },
    include: {
      objects: {
        where: { answerCollectionId: { not: null } },
        include: {
          answerCollection: {
            include: {
              owner: { select: { shortname: true } },
              entries: { orderBy: { value: 'asc' } },
            },
          },
        },
      },
    },
  })

  if (!user) {
    return []
  }

  // include answer collections used in any element of the template (required for preview / editing)
  // in case the instances were not modified, the user would get access to them either way
  let templateAnswerCollections: (DB.AnswerCollection & {
    entries: DB.AnswerCollectionEntry[]
  })[] = []
  if (templateId) {
    // verify that the user has access to the template activity
    const { accessible } = await validateTemplateAccessible({ templateId }, ctx)
    if (accessible) {
      const template = await ctx.prisma.activityTemplate.findUnique({
        where: {
          id: templateId,
        },
        include: {
          answerCollections: {
            include: {
              entries: {
                orderBy: {
                  value: 'asc',
                },
              },
            },
            orderBy: {
              name: 'asc',
            },
          },
        },
      })

      templateAnswerCollections = template?.answerCollections ?? []
    } else {
      templateAnswerCollections = []
    }
  }

  const combinedAnswerCollections = [
    ...user.objects.flatMap((object) =>
      object.answerCollection
        ? {
            ...object.answerCollection,
            isShared: object.permissionLevel !== DB.PermissionLevel.OWNER,
          }
        : []
    ),
    ...templateAnswerCollections.map((collection) => ({
      ...collection,
      isShared: false,
    })),
  ]

  // return deduplicated list of answer collections (based on id)
  return combinedAnswerCollections.reduce<
    (DB.AnswerCollection & { entries: DB.AnswerCollectionEntry[] })[]
  >((acc, collection) => {
    if (!acc.some((c) => c.id === collection.id)) {
      acc.push(collection)
    }

    return acc
  }, [])
}
```
- This function fetches answer collections available to the user, including those linked to a template if provided.
- It merges user-owned and template answer collections and returns a deduplicated list by collection id.
- Deduplication is explicitly done here by filtering out duplicates based on id.

---

### Relevant snippets from `packages/graphql/src/services/templates.ts` (lines 144-349)
```ts
export async function getActivityAnswerCollectionIds(
  {
    activityId,
    activityType,
  }: { activityId: string; activityType: ActivityType },
  ctx: PrismaTransactionContextWithUser
): Promise<{
  error: boolean
  activity:
    | (DB.LiveQuiz & {
        blocks: (DB.ElementBlock & { elements: DB.ElementInstance[] })[]
      })
    | (DB.PracticeQuiz & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
      })
    | (DB.MicroLearning & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
      })
    | (DB.GroupActivity & {
        stacks: (DB.ElementStack & { elements: DB.ElementInstance[] })[]
        parameters: DB.GroupActivityParameter[]
        clues: DB.GroupActivityClue[]
      })
    | null
  noInstances: boolean
  answerCollectionIds: number[]
  answerCollectionEntryIds: number[]
}> {
  // helper function that finds the ids of all answer collections linked to elements in an activity
  // fetch all element instances included in the activity that should be converted
  let instances: DB.ElementInstance[] = []
  let activity
  if (activityType === ActivityType.LIVE_QUIZ) {
    const liveQuiz = await ctx.prisma.liveQuiz.findUnique({
      where: {
        id: activityId,
      },
      include: {
        blocks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!liveQuiz) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = liveQuiz
    instances = liveQuiz.blocks.flatMap((block) => block.elements)
  } else if (activityType === ActivityType.PRACTICE_QUIZ) {
    const practiceQuiz = await ctx.prisma.practiceQuiz.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!practiceQuiz) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = practiceQuiz
    instances = practiceQuiz.stacks.flatMap((stack) => stack.elements)
  } else if (activityType === ActivityType.MICRO_LEARNING) {
    const microLearning = await ctx.prisma.microLearning.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
      },
    })

    if (!microLearning) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = microLearning
    instances = microLearning.stacks.flatMap((stack) => stack.elements)
  } else if (activityType === ActivityType.GROUP_ACTIVITY) {
    const groupActivity = await ctx.prisma.groupActivity.findUnique({
      where: {
        id: activityId,
      },
      include: {
        stacks: {
          include: {
            elements: true,
          },
        },
        parameters: true,
        clues: true,
      },
    })

    if (!groupActivity) {
      return {
        error: true,
        activity: null,
        noInstances: false,
        answerCollectionIds: [],
        answerCollectionEntryIds: [],
      }
    }

    activity = groupActivity
    instances = groupActivity.stacks.flatMap((stack) => stack.elements)
  } else {
    return {
      error: true,
      activity: null,
      noInstances: false,
      answerCollectionIds: [],
      answerCollectionEntryIds: [],
    }
  }

  // if no instances are found, return this
  if (instances.length === 0) {
    return {
      error: false,
      activity,
      noInstances: true,
      answerCollectionIds: [],
      answerCollectionEntryIds: [],
    }
  }

  // extract all answer collection ids (and potential other resources once supported)
  const answerCollectionIds = Array.from(
    new Set(
      instances
        .filter(
          (instance) =>
            instance.elementType === DB.ElementType.SELECTION ||
            instance.elementType === DB.ElementType.CASE_STUDY
        )
        .map((instance) =>
          instance.elementType === DB.ElementType.SELECTION
            ? (instance.elementData as SelectionElementData).options
                .answerCollection!.id
            : (instance.elementData as CaseStudyElementData).options
                .answerCollectionId!
        )
    )
  )

  // extract all answer collection entry ids that are used as sample solutions in selection elements or as items in case study elements
  const answerCollectionEntryIds = Array.from(
    new Set(
      instances
        .filter(
          (instance) =>
            instance.elementType === DB.ElementType.SELECTION ||
            instance.elementType === DB.ElementType.CASE_STUDY
        )
        .flatMap((instance) =>
          instance.elementType === DB.ElementType.SELECTION
            ? ((instance.elementData as SelectionElementData).options
                .answerCollectionSolutionIds ?? [])
            : ((
                instance.elementData as CaseStudyElementData
              ).options.items!.map((item) => item.id) ?? [])
        )
    )
  )

  return {
    error: false,
    activity,
    noInstances: false,
    answerCollectionIds,
    answerCollectionEntryIds,
  }
}
```
- This function collects all answer collection IDs and entry IDs linked to elements in a given activity.
- It uses `Set` to deduplicate the IDs before returning them.

---

### Relevant snippets from `packages/util/src/permissions.ts` (lines 39-135)
```ts
export async function recomputeDerivedPermissions(
  {
    // object ids - exactly one must be defined
    catalogCollectionId,
    answerCollectionId,
    elementId,
    courseId,
    liveQuizId,
    practiceQuizId,
    microLearningId,
    groupActivityId,
    // optional user to limit the required recomputation
    userId,
  }: {
    catalogCollectionId?: string
    answerCollectionId?: number
    elementId?: number
    courseId?: string
    liveQuizId?: string
    practiceQuizId?: string
    microLearningId?: string
    groupActivityId?: string
    userId?: string
  } & (
    | { catalogCollectionId: string }
    | { answerCollectionId: number }
    | { elementId: number }
    | { courseId: string }
    | { liveQuizId: string }
    | { practiceQuizId: string }
    | { microLearningId: string }
    | { groupActivityId: string }
  ),
  prisma: PrismaTransactionClient
) {
  if (typeof catalogCollectionId !== 'undefined') {
    await recomputeCatalogCollectionPermissions(
      { id: catalogCollectionId, userId },
      prisma
    )
  } else if (typeof answerCollectionId !== 'undefined') {
    await recomputeAnswerCollectionPermissions(
      { id: answerCollectionId, userId },
      prisma
    )
  } else if (typeof elementId !== 'undefined') {
    await recomputeElementPermissions(
      {
        id: elementId,
        userId,
      },
      prisma
    )
  } else if (typeof liveQuizId !== 'undefined') {
    await recomputeLiveQuizPermissions(
      {
        id: liveQuizId,
        userId,
      },
      prisma
    )
  } else if (typeof practiceQuizId !== 'undefined') {
    await recomputePracticeQuizPermissions(
      {
        id: practiceQuizId,
        userId,
      },
      prisma
    )
  } else if (typeof microLearningId !== 'undefined') {
    await recomputeMicroLearningPermissions(
      {
        id: microLearningId,
        userId,
      },
      prisma
    )
  } else if (typeof groupActivityId !== 'undefined') {
    await recomputeGroupActivityPermissions(
      {
        id: groupActivityId,
        userId,
      },
      prisma
    )
  } else if (typeof courseId !== 'undefined') {
    await recomputeCoursePermissions(
      {
        id: courseId,
        userId,
      },
      prisma
    )
  } else {
    throw new Error('No object id defined')
  }
}
```
- This function triggers recomputation of derived permissions for various resource types.
- It requires exactly one resource identifier and optionally a user ID.
- It calls the appropriate recomputation function based on the resource type.

---

### Relevant snippets from `packages/graphql/src/services/sharing.ts` (lines 3898-4071)
```ts
export async function checkAccess(
  checks: (
    | {
        catalogCollectionId: string
        minimumPermissionLevel: DB.PermissionLevel
      }
    | {
        answerCollectionId: number
        minimumPermissionLevel: DB.PermissionLevel
      }
    | { elementId: number; minimumPermissionLevel: DB.PermissionLevel }
    | { liveQuizId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { practiceQuizId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { microLearningId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { groupActivityId: string; minimumPermissionLevel: DB.PermissionLevel }
    | { courseId: string; minimumPermissionLevel: DB.PermissionLevel }
  )[],
  ctx: PrismaTransactionContextWithUser
) {
  for (const check of checks) {
    if (
      'catalogCollectionId' in check &&
      typeof check.catalogCollectionId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          catalogCollectionId_userId: {
            catalogCollectionId: check.catalogCollectionId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'answerCollectionId' in check &&
      typeof check.answerCollectionId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          answerCollectionId_userId: {
            answerCollectionId: check.answerCollectionId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if ('elementId' in check && typeof check.elementId !== 'undefined') {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          elementId_userId: {
            elementId: check.elementId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'liveQuizId' in check &&
      typeof check.liveQuizId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          liveQuizId_userId: {
            liveQuizId: check.liveQuizId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'practiceQuizId' in check &&
      typeof check.practiceQuizId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          practiceQuizId_userId: {
            practiceQuizId: check.practiceQuizId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'microLearningId' in check &&
      typeof check.microLearningId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          microLearningId_userId: {
            microLearningId: check.microLearningId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if (
      'groupActivityId' in check &&
      typeof check.groupActivityId !== 'undefined'
    ) {
      const permission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          groupActivityId_userId: {
            groupActivityId: check.groupActivityId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!permission) {
        return false
      }
    } else if ('courseId' in check && typeof check.courseId !== 'undefined') {
      const coursePermission = await ctx.prisma.derivedPermission.findUnique({
        where: {
          courseId_userId: {
            courseId: check.courseId,
            userId: ctx.user.sub,
          },
          permissionLevel: {
            in: acceptedPermissionLevels[check.minimumPermissionLevel],
          },
        },
      })

      if (!coursePermission) {
        return false
      }
    } else {
      // ? encountered unsupported element type
      return false
    }
  }

  return true
}
```
- This function checks if the current user has at least the minimum required permission level for a list of resource checks.
- It queries the `derivedPermission` table for each resource and returns false if any check fails.
- Returns true only if all checks pass.

---

### Relevant snippets from `packages/util/src/elements.ts` (lines 54-184)
```ts
export function processElementData(
  element: ElementWithAnswerCollection
): ElementData {
  if (element.type === PrismaElementType.FLASHCARD) {
    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (
    element.type === PrismaElementType.SC ||
    element.type === PrismaElementType.MC ||
    element.type === PrismaElementType.KPRIM
  ) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsChoices,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.NUMERICAL) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsNumerical,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.FREE_TEXT) {
    return {
      ...pick(element, QUESTION_KEYS),
      type: element.type,
      options: element.options as ElementOptionsFreeText,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (element.type === PrismaElementType.CONTENT) {
    return {
      ...pick(element, CONTENT_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
    }
  } else if (
    element.type === PrismaElementType.SELECTION &&
    'hasSampleSolution' in element.options &&
    'numberOfInputs' in element.options
  ) {
    if (
      !element.answerCollection?.entries ||
      (element.options.hasSampleSolution && !element.answerCollectionItems)
    ) {
      throw new Error(
        'Answer collection or solutions missing for selection element'
      )
    }

    // formulate answer collection in the format as it will be required in the element data options
    const answerCollectionOptions = {
      id: element.answerCollection.id,
      entries: element.answerCollection.entries.map((entry) => ({
        id: entry.id,
        value: entry.value,
      })),
    }

    // extract the ids of the correct solution options
    const answerCollectionSolutionIds = element.options.hasSampleSolution
      ? element.answerCollectionItems!.map((entry) => entry.id)
      : []

    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
      options: {
        hasSampleSolution: element.options.hasSampleSolution,
        numberOfInputs: element.options.numberOfInputs,
        answerCollection: answerCollectionOptions,
        answerCollectionSolutionIds,
      } as ElementOptionsSelection,
    }
  } else if (element.type === PrismaElementType.CASE_STUDY) {
    // make sure that answer collection and selected items were passed to the function
    if (
      !element.answerCollection ||
      !element.answerCollection.entries ||
      element.answerCollection.entries.length === 0 ||
      !element.answerCollectionItems ||
      element.answerCollectionItems.length === 0
    ) {
      throw new Error(
        'Answer collection or selected items missing for case study element'
      )
    }

    // extract selected items from collection (store only relevant information on instance)
    const selectedItemIds = element.answerCollectionItems.map((item) => item.id)
    const caseStudyItems = element.answerCollection.entries.flatMap(
      (entry: AnswerCollectionEntry) => {
        if (selectedItemIds?.includes(entry.id)) {
          return {
            id: entry.id,
            value: entry.value,
          }
        }

        return []
      }
    )

    return {
      ...pick(element, NO_OPTIONS_KEYS),
      type: element.type,
      id: `${element.id}-v${element.version}`,
      elementId: element.id,
      options: {
        ...element.options,
        answerCollectionId: element.answerCollection.id,
        items: caseStudyItems,
      } as ElementOptionsCaseStudy,
    }
  } else {
    throw new Error(
      'Invalid element type encountered during element data processing'
    )
  }
}
```
- This function processes an element and returns a structured `ElementData` object.
- For SELECTION and CASE_STUDY element types, it ensures answer collections and solution items are present.
- It formats answer collections and solution IDs appropriately for element data options.

---

### Relevant snippets from `packages/graphql/src/services/questions.ts` (selected excerpts related to deduplication and permissions)
- In `getAnswerCollectionsElements` function call inside `manipulateQuestion`:
```ts
const availableAnswerCollections = await getAnswerCollectionsElements(
  { templateId },
  ctx
)
```
- In `manipulateQuestion`, permissions are checked for answer collections linked to questions:
```ts
const validAccess = await checkAccess(
  [
    {
      answerCollectionId: options.answerCollection,
      minimumPermissionLevel: DB.PermissionLevel.READ,
    },
  ],
  ctx
)
```
- In `manipulateQuestion`, after upsert, derived permissions are recomputed for the element and possibly for the old answer collection:
```ts
await recomputeDerivedPermissions(
  { elementId: question.id, userId: ctx.user.sub },
  ctx.prisma
)

if (
  questionPrev?.answerCollectionId !== null &&
  typeof questionPrev?.answerCollectionId !== 'undefined' &&
  question.answerCollectionId !== questionPrev.answerCollectionId
) {
  await recomputeDerivedPermissions(
    { answerCollectionId: questionPrev.answerCollectionId },
    ctx.prisma
  )
}
```
- In `updateElementInstances`, after updating instances, derived permissions are recomputed for touched answer collections:
```ts
if (includeTemplates) {
  const uniqueTouchedAnswerCollectionIds = [
    ...new Set(touchedAnswerCollectionIds),
  ]

  for (const id of uniqueTouchedAnswerCollectionIds) {
    await recomputeDerivedPermissions({ answerCollectionId: id }, ctx.prisma)
  }
}
```

---

These snippets provide context on where deduplication of answer collections happens (notably in `getAnswerCollectionsElements`), how permissions are checked (`checkAccess`), and how derived permissions are recomputed (`recomputeDerivedPermissions`). This is relevant to understanding the comment "deduplication happens in a different place" in the `questions.ts` file.

</details>



---

<details>
<summary>✏️ Learnings added</summary>

```
Learnt from: sjschlapbach
PR: uzh-bf/klicker-uzh#4648
File: packages/graphql/src/services/questions.ts:1185-1195
Timestamp: 2025-04-29T14:54:21.935Z
Learning: In the `updateElementInstances` function of `packages/graphql/src/services/questions.ts`, the arrays `collectionEntriesToConnect` and `collectionEntriesToDisconnect` do not need additional deduplication because they are derived from `instanceCollectionEntryIds` which is already deduplicated in the `getActivityAnswerCollectionIds` function using `Array.from(new Set(...))`.
```

> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>

</answer></rawResChunk> -->

<!-- This is an auto-generated reply by CodeRabbit -->
created_at: 2025-04-29T14:54:23Z
author_association: NONE

PR comments:
user: login: coderabbitai[bot]
created_at: 2025-04-29T12:52:39Z
body: <!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- walkthrough_start -->

<details>
<summary>📝 Walkthrough</summary>

"""
## Walkthrough

This change introduces the tracking and management of answer collection entry IDs (items) in addition to answer collection IDs within activity templates. The backend logic is updated to extract, verify, and associate these entry IDs when creating or copying templates. Database schema changes add a new join table and relations to support linking activity templates with specific answer collection entries. Localization strings are updated in both English and German to clarify resource dependency errors, reflecting the new logic that considers removed or modified answer options as missing resources.

## Changes

| File(s)                                                                                                   | Change Summary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `packages/graphql/src/services/templates.ts`                                                              | Extended logic to extract, check, and link `answerCollectionEntryIds` (answer collection items) in addition to `answerCollectionIds` when handling activity templates. Updated function signatures and template creation logic to support these changes.                                                                                                                                                                                                                                                                                                                                                                                |
| `packages/graphql/src/services/questions.ts`                                                              | Enhanced `updateElementInstances` to synchronize answer collection entries alongside collections between activity templates and instances, updating connect/disconnect logic for both.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `packages/graphql/src/services/resources.ts`                                                              | Updated queries to include `templateUsages` alongside `itemUsages` when counting answer collection entry usages. Modified deletion logic to prevent removing entries used by activity templates.                                                                                                                                                                                                                                                                                                                                                                                               |
| `packages/graphql/src/services/sharing.ts`                                                                | Removed two `console.error` calls in `createUserGroup` function, replacing them with commented-out strings and returning `null` without logging errors.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `packages/graphql/test/template.test.ts`                                                                   | Extended test coverage to verify linking and updating of `answerCollectionItems` in activity templates alongside `answerCollections`. Added tests for modifying entry links in case study questions and verifying template updates accordingly.                                                                                                                                                                                                                                                                                                                                                                             |
| `packages/prisma/src/prisma/migrations/20250429121311_template_answer_collection_entry_link/migration.sql` | Introduced a new join table `_TemplateAnswerCollectionUsedItems` with foreign key constraints linking `ActivityTemplate` and `AnswerCollectionEntry`. Added a composite primary key and index for efficient lookups.                                                                                                                                                                                                                                                                                                                                                                            |
| `packages/prisma/src/prisma/schema/resources.prisma`                                                      | Added a new relation field `templateUsages` to the `AnswerCollectionEntry` model, enabling tracking of entry usage in activity templates.                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
| `packages/prisma/src/prisma/schema/sharing.prisma`                                                        | Added an optional relation field `answerCollectionItems` to the `ActivityTemplate` model, allowing direct association with answer collection entries. Reformatted the existing `answerCollections` field for readability.                                                                                                                                                                                                                                                                                                                                                                      |
| `packages/i18n/messages/en.ts`, `packages/i18n/messages/de.ts`                                            | Updated the `resourcesRequiredMissing` localization string in English and German to clarify that missing resources may be due to deleted, removed, or modified (answer options removed) resources. Clarified the `answerOptionUsed` warning message to include usage in activity templates. No changes to exported entities.                                                                                                                                                                                                                                                                     |

## Sequence Diagram(s)

```mermaid
sequenceDiagram
    participant User
    participant API
    participant DB

    User->>API: Request to create/copy activity template
    API->>API: Extract answerCollectionIds and answerCollectionEntryIds
    API->>DB: Check existence of answerCollections and answerCollectionEntries
    DB-->>API: Return existence status
    API->>DB: Create ActivityTemplate with linked answerCollections and answerCollectionItems
    DB-->>API: Confirmation of template creation
    API-->>User: Respond with result
```

## Possibly related PRs

- #4594: Related extension of `getActivityAnswerCollectionIds` and template creation logic focusing on typing and export improvements without entry ID handling.
- #4550: Foundational PR introducing database structures and relations for activity templates and answer collections, which this PR builds upon by adding entry-level linking and checks.
"""

</details>

<!-- walkthrough_end -->
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEejqANiS4AzeAA8uaWvVrwKo3JAvwMAa0gBElwAdxIydDFJdXkaZm4LahJ0DHoPAEdsdzoUxDC+JgsrKPwsdgp4aQMAOWxmIIouABYANiaADgMAVURKLkQhRAZYRO4BNGGjfWNwKDJ6fBscAmIyZRp6JlZ2Ll5+YU9JaUD5JiUqVXUtHWmTKDhUVExlwlJyKg2FbYxOSCoQyCIOrMNAUeRyBTnFRqTTaXRgQwzUwGbgTPxoUiIAD0RCo3Fg6QsWMQFAYxMoEngDGkWLiCSSiA0uEQHAMACIOQYAMRcyAAQQAkqt3kl6EDWKD5ItGLBMJizLBkjZsBgSlgAAakXB8qKU3CyPkYPKUADC+CKh1KAtoiHVkBCaGQJHsNFSOQI6AsiHwCgtYhSaWC2AoGswxooZr94lKAFEfmDrbbPaUiIh4EpILhFZBnUhxBgiJB1WH8pHitGMIn1RoYIrejn4/IBQARZCg5LO3BUMQ5GwUFg5qxsH4oI24TDU5DS/XcZIAZRjABkYyboAKAPLVfh8E18hcAfTn0C6zYAmidIERqIqKgXAea8PBSpAW8g+wPeuWn1h8NwK1O+HUEhmBfVtIHfECGEdZJEFwbBaClP9v0ZWtkg8OCQ0zWRZwDT0LD+INMNgpJh2Ze1KGSbBuFoUVIiYCg3ALCxYh9XwGAseDkizVByABUEqFkDQjAFDVhlEPxoGA+kaBEmx8D5CRtESAQrHVAAaTNs3Q4NyAWA5/QgostR1cQ9QNI1S3NL8rRtO0MHwAE2I4pRkGLCz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DlwHiJtH3FkcqDCFoH3GIiHVb6L8gBaAYAEBaGcWn4AHYbA0BKdScSBnEV/Z/F+BBaAGBKct+BBKc6c0ASASc6dKcbAmhN/adR/2+SAT+mhCdCdaAWhT+mhwhnFZ+BBnEd+2h5+Z+SB8c6dZ+LQfHLPxIACB8ctAAQO0DpxNAr+swSAO0DoAtBQBJOanHThJxQQj+NgEnCoAYCz8mgj/BgCThaBoB8cDAJAZTnCBP8GA5/UfgYHb4Xt1A3fOon3xEJ0AuceUfQEAA== -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details open="true">
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=uzh-bf/klicker-uzh&utm_content=4648):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `Open a follow-up GitHub issue for this discussion.`
- Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples:
  - `@coderabbitai generate unit testing code for this file.`
  -	`@coderabbitai modularize this function.`
- PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
  - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.`
  - `@coderabbitai read src/utils.ts and generate unit testing code.`
  - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.`
  - `@coderabbitai help me debug CodeRabbit configuration file.`

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

### CodeRabbit Commands (Invoked using PR comments)

- `@coderabbitai pause` to pause the reviews on a PR.
- `@coderabbitai resume` to resume the paused reviews.
- `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
- `@coderabbitai full review` to do a full review from scratch and review all the files again.
- `@coderabbitai summary` to regenerate the summary of the PR.
- `@coderabbitai generate docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this PR.
- `@coderabbitai generate sequence diagram` to generate a sequence diagram of the changes in this PR.
- `@coderabbitai resolve` resolve all the CodeRabbit review comments.
- `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository.
- `@coderabbitai help` to get help.

### Other keywords and placeholders

- Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed.
- Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description.
- Add `@coderabbitai` anywhere in the PR title to generate the title automatically.

### CodeRabbit Configuration File (`.coderabbit.yaml`)

- You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository.
- Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json`

### Documentation and Community

- Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->
author_association: NONE
user: login: cypress[bot]
created_at: 2025-04-29T13:35:16Z
body: 
<h3>
  <a href="https://cloud.cypress.io/projects/y436dx?utm_source=github&utm_medium=PASSED&utm_campaign=project">klicker-uzh</a>&nbsp;
  <img src="https://assets.cypress.io/github-pr-comment-icons/chevron.svg" alt="" height="14" width="14" />&nbsp;
  <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">Run #4969</a>
</h3>

<details>
  <summary>
    <strong>Run Properties:</strong>&nbsp;
    <code><img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="status check passed" height="10" width="10" />&nbsp;Passed&nbsp;<a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">#4969</a></code> &nbsp;&bull;&nbsp;
    <code><img src="https://assets.cypress.io/github-pr-comment-icons/git-commit.svg" alt="git commit" width="10" height="10" /> <a href="https://github.com/uzh-bf/klicker-uzh/commit/9de41422d298d0e9da0b2fe5fd5da1ebcf87f9fc">9de41422d2</a>: fix: ensure that in addition to user courses, the one linked to an activity is f...</code>
  </summary><br />

  <table width="744">
    <tbody>
      <tr />
      <tr height="48">
        <td width="224">
          <strong>Project</strong>
        </td>
        <td width="520">
          <code><a href="https://cloud.cypress.io/projects/y436dx?utm_source=github&utm_medium=PASSED&utm_campaign=project">klicker-uzh</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td width="224">
          <strong>Branch Review</strong>
        </th>
        <td>
          <code><a href="https://cloud.cypress.io/projects/y436dx/branches/v3-derived-permissions/review?utm_source=github&utm_medium=PASSED&utm_campaign=review%20changes&changeRequest=4648">v3-derived-permissions</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Run status</strong></td>
        <td>
          <code><img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="status check passed" height="10" width="10" />&nbsp;Passed&nbsp;<a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">#4969</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Run duration</strong></td>
        <td><code>35m 16s</code></td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Commit</strong></td>
        <td>
          <code><img src="https://assets.cypress.io/github-pr-comment-icons/git-commit.svg" alt="git commit" width="10" height="10" /> <a href="https://github.com/uzh-bf/klicker-uzh/commit/9de41422d298d0e9da0b2fe5fd5da1ebcf87f9fc">9de41422d2</a>: fix: ensure that in addition to user courses, the one linked to an activity is f...</code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Committer</strong></td>
        <td><code>Julius Schlapbach</code></td>
      </tr>
    </tbody>
    <tfoot>
      <tr />
      <tr height="48">
        <td colspan="2">
          <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/properties?utm_source=github&utm_medium=PASSED&utm_campaign=properties">View all properties for this run ↗︎</a>
        </td>
      </tr>
    </tfoot>
  </table>
  <hr />
</details>

<table width="744">
  <thead>
    <tr />
    <tr height="48">
      <th colspan="2" align="left" width="744">Test results</th>
    </tr>
  </thead>
  <tbody>
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/failed.svg" alt="Tests that failed" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?reviewViewBy=FAILED&utm_source=github&utm_medium=PASSED&utm_campaign=failed%20tests">
          <strong>Failures</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/flaky-v3.svg" alt="Tests that were flaky" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?reviewViewBy=FLAKY&utm_source=github&utm_medium=PASSED&utm_campaign=flaky%20tests">
          <strong>Flaky</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/pending.svg" alt="Tests that did not run due to a developer annotating a test with .skip" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/test-results?utm_source=github&utm_medium=PASSED&utm_campaign=pending%20tests&statuses=%5B%7B%22value%22%3A%22PENDING%22%2C%22label%22%3A%22PENDING%22%7D%5D">
          <strong>Pending</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/skipped-v3.svg" alt="Tests that did not run due to a failure in a mocha hook" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/test-results?utm_source=github&utm_medium=PASSED&utm_campaign=skipped%20tests&statuses=%5B%7B%22value%22%3A%22SKIPPED%22%2C%22label%22%3A%22SKIPPED%22%7D%5D">
          <strong>Skipped</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="Tests that passed" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4969/overview?utm_source=github&utm_medium=PASSED&utm_campaign=passed%20tests">
          <strong>Passing</strong>
        </a>
      </td>
      <td>
        <code><strong>509</strong></code>
      </td>
    </tr><tfoot>
    <tr><td colspan="2"><b>⚠️ You've recorded test results over your free plan limit.<br /><a href="https://cloud.cypress.io/organizations/1a5729a2-8fed-493d-8ac1-0fcf1628e4fa/billing?utm_source=github&utm_medium=PASSED&utm_campaign=upgrade%20plan">Upgrade your plan to view test results</a>.</b></td></tr>
    <tr />
    <tr height="48"><td colspan="2"><a href="https://cloud.cypress.io/projects/y436dx/branches/v3-derived-permissions/review?utm_source=github&utm_medium=PASSED&utm_campaign=review%20changes&changeRequest=4648">View all changes introduced in this branch ↗︎</a></td></tr></tfoot>
  </tbody>
</table>






author_association: NONE
user: login: cypress[bot]
created_at: 2025-04-29T13:37:37Z
body: 
<h3>
  <a href="https://cloud.cypress.io/projects/y436dx?utm_source=github&utm_medium=PASSED&utm_campaign=project">klicker-uzh</a>&nbsp;
  <img src="https://assets.cypress.io/github-pr-comment-icons/chevron.svg" alt="" height="14" width="14" />&nbsp;
  <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">Run #4974</a>
</h3>

<details>
  <summary>
    <strong>Run Properties:</strong>&nbsp;
    <code><img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="status check passed" height="10" width="10" />&nbsp;Passed&nbsp;<a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">#4974</a></code> &nbsp;&bull;&nbsp;
    <code><img src="https://assets.cypress.io/github-pr-comment-icons/git-commit.svg" alt="git commit" width="10" height="10" /> <a href="https://github.com/uzh-bf/klicker-uzh/commit/e81667bd2ca428101f80592222a90126bbbc8937">e81667bd2c <span style="cursor:help;" title="Your CI provider generated this commit by merging this branch into the target branch.">ℹ️</span></a>: Merge 8ed6eb59135cae5f5abc74d6c56a2c6e9ee69c4b into 9de41422d298d0e9da0b2fe5fd5d...</code>
  </summary><br />

  <table width="744">
    <tbody>
      <tr />
      <tr height="48">
        <td width="224">
          <strong>Project</strong>
        </td>
        <td width="520">
          <code><a href="https://cloud.cypress.io/projects/y436dx?utm_source=github&utm_medium=PASSED&utm_campaign=project">klicker-uzh</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td width="224">
          <strong>Branch Review</strong>
        </th>
        <td>
          <code><a href="https://cloud.cypress.io/projects/y436dx/branches/template-answer-collection-entry-link/review?utm_source=github&utm_medium=PASSED&utm_campaign=review%20changes&changeRequest=4648">template-answer-collection-entry-link</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Run status</strong></td>
        <td>
          <code><img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="status check passed" height="10" width="10" />&nbsp;Passed&nbsp;<a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?utm_source=github&utm_medium=PASSED&utm_campaign=run%20number">#4974</a></code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Run duration</strong></td>
        <td><code>35m 03s</code></td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Commit</strong></td>
        <td>
          <code><img src="https://assets.cypress.io/github-pr-comment-icons/git-commit.svg" alt="git commit" width="10" height="10" /> <a href="https://github.com/uzh-bf/klicker-uzh/commit/e81667bd2ca428101f80592222a90126bbbc8937">e81667bd2c <span style="cursor:help;" title="Your CI provider generated this commit by merging this branch into the target branch.">ℹ️</span></a>: Merge 8ed6eb59135cae5f5abc74d6c56a2c6e9ee69c4b into 9de41422d298d0e9da0b2fe5fd5d...</code>
        </td>
      </tr>
      <tr />
      <tr height="48">
        <td><strong>Committer</strong></td>
        <td><code>Julius Schlapbach</code></td>
      </tr>
    </tbody>
    <tfoot>
      <tr />
      <tr height="48">
        <td colspan="2">
          <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/properties?utm_source=github&utm_medium=PASSED&utm_campaign=properties">View all properties for this run ↗︎</a>
        </td>
      </tr>
    </tfoot>
  </table>
  <hr />
</details>

<table width="744">
  <thead>
    <tr />
    <tr height="48">
      <th colspan="2" align="left" width="744">Test results</th>
    </tr>
  </thead>
  <tbody>
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/failed.svg" alt="Tests that failed" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?reviewViewBy=FAILED&utm_source=github&utm_medium=PASSED&utm_campaign=failed%20tests">
          <strong>Failures</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/flaky-v3.svg" alt="Tests that were flaky" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?reviewViewBy=FLAKY&utm_source=github&utm_medium=PASSED&utm_campaign=flaky%20tests">
          <strong>Flaky</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/pending.svg" alt="Tests that did not run due to a developer annotating a test with .skip" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/test-results?utm_source=github&utm_medium=PASSED&utm_campaign=pending%20tests&statuses=%5B%7B%22value%22%3A%22PENDING%22%2C%22label%22%3A%22PENDING%22%7D%5D">
          <strong>Pending</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/skipped-v3.svg" alt="Tests that did not run due to a failure in a mocha hook" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/test-results?utm_source=github&utm_medium=PASSED&utm_campaign=skipped%20tests&statuses=%5B%7B%22value%22%3A%22SKIPPED%22%2C%22label%22%3A%22SKIPPED%22%7D%5D">
          <strong>Skipped</strong>
        </a>
      </td>
      <td>
        <code><strong>0</strong></code>
      </td>
    </tr>
    <tr />
    <tr height="48">
      <td width="224">
        <img src="https://assets.cypress.io/github-pr-comment-icons/passed.svg" alt="Tests that passed" width="10" height="10" />&nbsp;
        <a href="https://cloud.cypress.io/projects/y436dx/runs/4974/overview?utm_source=github&utm_medium=PASSED&utm_campaign=passed%20tests">
          <strong>Passing</strong>
        </a>
      </td>
      <td>
        <code><strong>509</strong></code>
      </td>
    </tr><tfoot>
    <tr><td colspan="2"><b>⚠️ You've recorded test results over your free plan limit.<br /><a href="https://cloud.cypress.io/organizations/1a5729a2-8fed-493d-8ac1-0fcf1628e4fa/billing?utm_source=github&utm_medium=PASSED&utm_campaign=upgrade%20plan">Upgrade your plan to view test results</a>.</b></td></tr>
    <tr />
    <tr height="48"><td colspan="2"><a href="https://cloud.cypress.io/projects/y436dx/branches/template-answer-collection-entry-link/review?utm_source=github&utm_medium=PASSED&utm_campaign=review%20changes&changeRequest=4648">View all changes introduced in this branch ↗︎</a></td></tr></tfoot>
  </tbody>
</table>






author_association: NONE
user: login: sonarqubecloud[bot]
created_at: 2025-04-29T13:39:38Z
body: ## [![Quality Gate Failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-failed-20px.png 'Quality Gate Failed')](https://sonarcloud.io/dashboard?id=uzh-bf_klicker-uzh&pullRequest=4648) **Quality Gate failed**  
Failed conditions  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/failed-16px.png '') [11.1% Duplication on New Code](https://sonarcloud.io/component_measures?id=uzh-bf_klicker-uzh&pullRequest=4648&metric=new_duplicated_lines_density&view=list) (required ≤ 3%)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=uzh-bf_klicker-uzh&pullRequest=4648)


author_association: NONE