(int) $i['unit_id'], $items ); $item_ids = array_map( static fn( array $i ): int => (int) $i['id'], $items ); $kinds = Kinds::of_units( $unit_ids ); $rules = array() === $item_ids ? array() : Services::curriculum_repo()->items( $item_ids ); foreach ( $items as $i => $row ) { $item = $rules[ (int) $row['id'] ] ?? null; $items[ $i ]['kind'] = $kinds[ (int) $row['unit_id'] ] ?? ( 'quiz' === $row['type'] ? 'quiz' : (string) ( $row['lesson_kind'] ?? 'text' ) ); $items[ $i ] += self::availability( $item ); $items[ $i ]['flags_rev'] = self::flags_rev( $items[ $i ] ); } $data['items'] = $items; $data['picker'] = Kinds::picker( $user_id, false ); return $data; } public static function availability( $item ): array { $rule = null === $item ? UnlockRule::NONE : $item->rules->unlock; return array( 'unlock' => self::rule_slug( $rule ), 'days' => null === $item ? null : $item->rules->days, 'at' => null === $item ? null : $item->rules->at, ); } public static function flags_rev( array $row ): string { return Rev::of( array( (bool) ( $row['required'] ?? false ), (bool) ( $row['preview'] ?? false ), (string) ( $row['unlock'] ?? 'none' ), $row['days'] ?? null, $row['at'] ?? null, ) ); } public static function rule_slug( UnlockRule $rule ): string { return match ( $rule ) { UnlockRule::AFTER_PREVIOUS => 'after_previous', UnlockRule::DAYS_AFTER_ENROLMENT => 'days', UnlockRule::ON_DATE => 'date', default => 'none', }; } public static function rule_of( string $slug ): ?UnlockRule { return match ( $slug ) { 'none' => UnlockRule::NONE, 'after_previous' => UnlockRule::AFTER_PREVIOUS, 'days' => UnlockRule::DAYS_AFTER_ENROLMENT, 'date' => UnlockRule::ON_DATE, default => null, }; } }