| Conditions | 12 |
| Paths | 194 |
| Total Lines | 102 |
| Code Lines | 60 |
| 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 |
||
| 33 | public function indexQuiz(CQuiz $quiz): ?int |
||
| 34 | { |
||
| 35 | $resourceNode = $quiz->getResourceNode(); |
||
| 36 | |||
| 37 | error_log('[Xapian] indexQuiz: start for quiz id='.(string) $quiz->getIid() |
||
| 38 | .', resource_node_id='.($resourceNode ? $resourceNode->getId() : 'null') |
||
| 39 | ); |
||
| 40 | |||
| 41 | // Check global setting |
||
| 42 | $enabled = (string) $this->settingsManager->getSetting('search.search_enabled', true); |
||
| 43 | if ($enabled !== 'true') { |
||
| 44 | error_log('[Xapian] indexQuiz: search is disabled, skipping'); |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (!$resourceNode instanceof ResourceNode) { |
||
| 49 | error_log('[Xapian] indexQuiz: missing ResourceNode, skipping'); |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Resolve course and session from resource links |
||
| 54 | [$courseId, $sessionId] = $this->resolveCourseAndSession($resourceNode); |
||
| 55 | |||
| 56 | $title = (string) $quiz->getTitle(); |
||
| 57 | $description = (string) ($quiz->getDescription() ?? ''); |
||
| 58 | |||
| 59 | // For now: description only. If later you implement "specific fields" |
||
| 60 | // for quizzes in C2, you can concatenate them here like in v1. |
||
| 61 | $content = \trim($description); |
||
| 62 | |||
| 63 | // Data that will appear in search results under "data" |
||
| 64 | $fields = [ |
||
| 65 | 'kind' => 'quiz', |
||
| 66 | 'tool' => 'quiz', |
||
| 67 | 'title' => $title, |
||
| 68 | 'description' => $description, |
||
| 69 | 'content' => $content, |
||
| 70 | 'resource_node_id' => (string) $resourceNode->getId(), |
||
| 71 | 'quiz_id' => (string) $quiz->getIid(), |
||
| 72 | 'course_id' => $courseId !== null ? (string) $courseId : '', |
||
| 73 | 'session_id' => $sessionId !== null ? (string) $sessionId : '', |
||
| 74 | // Legacy-like metadata, if you want them later: |
||
| 75 | 'xapian_data' => json_encode([ |
||
| 76 | 'type' => 'exercise', |
||
| 77 | 'exercise_id' => (int) $quiz->getIid(), |
||
| 78 | 'course_id' => $courseId, |
||
| 79 | ]), |
||
| 80 | ]; |
||
| 81 | |||
| 82 | // Terms: allow filtering by kind, course, session |
||
| 83 | $terms = ['Tquiz']; |
||
| 84 | if ($courseId !== null) { |
||
| 85 | $terms[] = 'C'.$courseId; |
||
| 86 | } |
||
| 87 | if ($sessionId !== null) { |
||
| 88 | $terms[] = 'S'.$sessionId; |
||
| 89 | } |
||
| 90 | |||
| 91 | // Look for existing SearchEngineRef for this resource node |
||
| 92 | /** @var SearchEngineRef|null $existingRef */ |
||
| 93 | $existingRef = $this->em |
||
| 94 | ->getRepository(SearchEngineRef::class) |
||
| 95 | ->findOneBy(['resourceNodeId' => $resourceNode->getId()]); |
||
| 96 | |||
| 97 | $existingDocId = $existingRef?->getSearchDid(); |
||
| 98 | |||
| 99 | if ($existingDocId !== null) { |
||
| 100 | try { |
||
| 101 | $this->xapianIndexService->deleteDocument($existingDocId); |
||
| 102 | error_log('[Xapian] indexQuiz: deleted previous docId='.(string) $existingDocId); |
||
| 103 | } catch (\Throwable $e) { |
||
| 104 | error_log('[Xapian] indexQuiz: failed to delete previous docId=' |
||
| 105 | .(string) $existingDocId.' error='.$e->getMessage() |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // Index in Xapian |
||
| 111 | try { |
||
| 112 | $docId = $this->xapianIndexService->indexDocument($fields, $terms); |
||
| 113 | } catch (\Throwable $e) { |
||
| 114 | error_log('[Xapian] indexQuiz: indexDocument() failed: '.$e->getMessage()); |
||
| 115 | return null; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Update mapping |
||
| 119 | if ($existingRef instanceof SearchEngineRef) { |
||
| 120 | $existingRef->setSearchDid($docId); |
||
| 121 | } else { |
||
| 122 | $existingRef = new SearchEngineRef(); |
||
| 123 | $existingRef->setResourceNodeId((int) $resourceNode->getId()); |
||
| 124 | $existingRef->setSearchDid($docId); |
||
| 125 | $this->em->persist($existingRef); |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->em->flush(); |
||
| 129 | |||
| 130 | error_log('[Xapian] indexQuiz: completed with docId='.(string) $docId |
||
| 131 | .', search_engine_ref_id='.$existingRef->getId() |
||
| 132 | ); |
||
| 133 | |||
| 134 | return $docId; |
||
| 135 | } |
||
| 197 |