| Conditions | 13 |
| Paths | 578 |
| Total Lines | 99 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function indexQuestion(CQuizQuestion $question): ?int |
||
| 38 | { |
||
| 39 | $resourceNode = $question->getResourceNode(); |
||
| 40 | |||
| 41 | // Global feature toggle |
||
| 42 | $enabled = (string) $this->settingsManager->getSetting('search.search_enabled', true); |
||
| 43 | if ($enabled !== 'true') { |
||
| 44 | return null; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!$resourceNode instanceof ResourceNode) { |
||
| 48 | // Question without a resource node cannot be indexed |
||
| 49 | return null; |
||
| 50 | } |
||
| 51 | |||
| 52 | // Quiz context for the question (first quiz + all quizzes) |
||
| 53 | [$primaryQuizId, $allQuizIds] = $this->getQuizContext($question); |
||
| 54 | |||
| 55 | // Resolve course and session from the question resource node |
||
| 56 | [$courseId, $sessionId] = $this->resolveCourseAndSession($resourceNode); |
||
| 57 | |||
| 58 | $title = (string) $question->getQuestion(); |
||
| 59 | $description = (string) ($question->getDescription() ?? ''); |
||
| 60 | |||
| 61 | // Index only the question itself (title + description), not the answers |
||
| 62 | $content = trim($title.' '.$description); |
||
| 63 | |||
| 64 | // IMPORTANT: keep field names consistent with the quiz indexer: |
||
| 65 | // - kind |
||
| 66 | // - tool |
||
| 67 | // - quiz_id (here we store the primary quiz of the question) |
||
| 68 | $fields = [ |
||
| 69 | 'kind' => 'question', |
||
| 70 | 'tool' => 'quiz_question', |
||
| 71 | 'title' => $title, |
||
| 72 | 'description' => $description, |
||
| 73 | 'content' => $content, |
||
| 74 | 'resource_node_id' => (string) $resourceNode->getId(), |
||
| 75 | 'question_id' => (string) $question->getIid(), |
||
| 76 | // This is what Twig will use to build the link for questions |
||
| 77 | 'quiz_id' => $primaryQuizId !== null ? (string) $primaryQuizId : '', |
||
| 78 | 'course_id' => $courseId !== null ? (string) $courseId : '', |
||
| 79 | 'session_id' => $sessionId !== null ? (string) $sessionId : '', |
||
| 80 | 'xapian_data' => json_encode([ |
||
| 81 | 'type' => 'exercise_question', |
||
| 82 | 'question_id' => (int) $question->getIid(), |
||
| 83 | 'quiz_ids' => $allQuizIds, |
||
| 84 | 'primary_quiz_id' => $primaryQuizId, |
||
| 85 | 'course_id' => $courseId, |
||
| 86 | 'session_id' => $sessionId, |
||
| 87 | ]), |
||
| 88 | ]; |
||
| 89 | |||
| 90 | // Terms for filtering |
||
| 91 | $terms = ['Tquiz_question', 'Tquestion']; |
||
| 92 | if ($courseId !== null) { |
||
| 93 | $terms[] = 'C'.$courseId; |
||
| 94 | } |
||
| 95 | if ($sessionId !== null) { |
||
| 96 | $terms[] = 'S'.$sessionId; |
||
| 97 | } |
||
| 98 | if ($primaryQuizId !== null) { |
||
| 99 | $terms[] = 'Q'.$primaryQuizId; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Look for an existing SearchEngineRef for this resource node |
||
| 103 | /** @var SearchEngineRef|null $existingRef */ |
||
| 104 | $existingRef = $this->em |
||
| 105 | ->getRepository(SearchEngineRef::class) |
||
| 106 | ->findOneBy(['resourceNodeId' => $resourceNode->getId()]); |
||
| 107 | |||
| 108 | $existingDocId = $existingRef?->getSearchDid(); |
||
| 109 | |||
| 110 | if ($existingDocId !== null) { |
||
| 111 | try { |
||
| 112 | $this->xapianIndexService->deleteDocument($existingDocId); |
||
| 113 | } catch (\Throwable) { |
||
| 114 | // Best-effort delete: ignore errors here |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | try { |
||
| 119 | $docId = $this->xapianIndexService->indexDocument($fields, $terms); |
||
| 120 | } catch (\Throwable) { |
||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($existingRef instanceof SearchEngineRef) { |
||
| 125 | $existingRef->setSearchDid($docId); |
||
| 126 | } else { |
||
| 127 | $existingRef = new SearchEngineRef(); |
||
| 128 | $existingRef->setResourceNodeId((int) $resourceNode->getId()); |
||
| 129 | $existingRef->setSearchDid($docId); |
||
| 130 | $this->em->persist($existingRef); |
||
| 131 | } |
||
| 132 | |||
| 133 | $this->em->flush(); |
||
| 134 | |||
| 135 | return $docId; |
||
| 136 | } |
||
| 235 |