where('slug', self::KIND_SLUG)->value('id'); return $id === null ? null : (int) $id; } /** * Ordnet einen Abschnitt seiner Übung zu und gibt ihm die Materialart. Eine bereits gesetzte Materialart bleibt: * Sie ist eine bewusste Einordnung der Redaktion. */ public function mark(ContentElement $element, Composition $exercise): void { $element->origin_composition_id = $exercise->getKey(); if ($element->editorial_content_kind_id === null) { $element->editorial_content_kind_id = $this->kindId(); } $element->save(); } /** * Ordnet vorhandene Texte nur dann zu, wenn ihre Herkunft belegt ist: * * - Ein Abschnitt aus der Übernahme aus Shinkan (`source_ref` = `shinkan::`) gehört zur lokalen * Übung dieser Quelle. * - Ein anderer Text gilt als Abschnitt, wenn er ohne Materialart ist, ausschließlich in genau einer Übung * vorkommt und nirgends in einem Modul oder Programm direkt verwendet wird. * * Ein bereits gesetzter Materialtyp bleibt in jedem Fall erhalten, ebenso unklare Fälle. * * @return int Anzahl der Texte, die jetzt einer Übung zugeordnet sind */ public function backfill(): int { $kindId = $this->kindId(); if ($kindId === null) { return 0; } $assigned = 0; foreach ($this->fromSource() + $this->fromUse() as $elementId => $compositionId) { DB::table('content_elements')->where('id', $elementId)->whereNull('origin_composition_id')->update(['origin_composition_id' => $compositionId]); DB::table('content_elements')->where('id', $elementId)->whereNull('editorial_content_kind_id')->update(['editorial_content_kind_id' => $kindId]); $assigned++; } return $assigned; } /** * @return array Element-ID => Übung */ private function fromSource(): array { $byRef = []; foreach (DB::table('exercise_sources')->whereNotNull('composition_id')->get(['source_id', 'composition_id']) as $source) { $byRef[(string) $source->source_id] = (int) $source->composition_id; } $found = []; foreach (DB::table('content_elements')->where('output_class', 'text')->where('source_ref', 'like', 'shinkan:%')->get(['id', 'source_ref']) as $element) { $parts = explode(':', (string) $element->source_ref); if (count($parts) === 3 && isset($byRef[$parts[1]])) { $found[(int) $element->id] = $byRef[$parts[1]]; } } return $found; } /** * @return array Element-ID => Übung */ private function fromUse(): array { $exerciseKind = CompositionKind::Exercise->value; $uses = DB::table('composition_members as m') ->join('content_element_versions as v', 'v.id', '=', 'm.content_element_version_id') ->join('content_elements as e', 'e.id', '=', 'v.content_element_id') ->join('composition_versions as cv', 'cv.id', '=', 'm.composition_version_id') ->join('compositions as c', 'c.id', '=', 'cv.composition_id') ->where('e.output_class', 'text') ->whereNull('e.editorial_content_kind_id') ->whereNull('e.origin_composition_id') ->select('e.id as element_id', 'c.id as composition_id', 'c.kind as composition_kind') ->get(); $exercises = []; $elsewhere = []; foreach ($uses as $use) { if ($use->composition_kind === $exerciseKind) { $exercises[(int) $use->element_id][(int) $use->composition_id] = true; } else { $elsewhere[(int) $use->element_id] = true; } } $found = []; foreach ($exercises as $elementId => $compositions) { if (count($compositions) === 1 && ! isset($elsewhere[$elementId])) { $found[$elementId] = array_key_first($compositions); } } return $found; } }