type->value, null === $unit->lesson_kind ? null : $unit->lesson_kind->value, null === $session ? null : $session->provider->value ); } public static function of_row( int $unit_type, ?int $lesson_kind, ?int $provider ): string { return match ( UnitType::tryFrom( $unit_type ) ) { UnitType::QUIZ => 'quiz', UnitType::ASSIGNMENT => 'assignment', UnitType::LIVE => ProviderKey::STREAM->value === $provider ? 'stream' : 'live', UnitType::PACKAGE => 'scorm', UnitType::EXTERNAL => 'external', UnitType::H5P => 'h5p', default => ( LessonKind::tryFrom( (int) $lesson_kind ) ?? LessonKind::TEXT )->slug(), }; } public static function label( string $kind ): string { return match ( $kind ) { 'video' => __( 'Video lesson', 'rooster-lms' ), 'audio' => __( 'Audio lesson', 'rooster-lms' ), 'pdf' => __( 'PDF lesson', 'rooster-lms' ), 'stream' => __( 'Live stream', 'rooster-lms' ), 'live' => __( 'Live lesson', 'rooster-lms' ), 'quiz' => __( 'Quiz', 'rooster-lms' ), 'assignment' => __( 'Assignment', 'rooster-lms' ), 'h5p' => __( 'Interactive content', 'rooster-lms' ), 'scorm' => __( 'SCORM package', 'rooster-lms' ), 'external' => __( 'External tool', 'rooster-lms' ), default => __( 'Text lesson', 'rooster-lms' ), }; } public static function of_units( array $unit_ids ): array { $units = array() === $unit_ids ? array() : Services::unit_repo()->get_many( $unit_ids ); $live = array(); foreach ( $units as $unit ) { if ( UnitType::LIVE === $unit->type ) { $live[] = $unit->id; } } $sessions = array() === $live ? array() : Services::live_session_repo()->for_units( $live ); $out = array(); foreach ( $units as $id => $unit ) { $out[ (int) $id ] = self::of( $unit, self::lesson_session( $sessions[ $unit->id ] ?? array() ) ); } return $out; } public static function lesson_session( array $sessions ): ?Session { foreach ( $sessions as $session ) { if ( Placement::LESSON === $session->placement ) { return $session; } } return null; } public static function target( string $kind ): array { return match ( $kind ) { 'quiz' => array( UnitType::QUIZ, LessonKind::TEXT ), 'assignment' => array( UnitType::ASSIGNMENT, LessonKind::TEXT ), 'h5p' => array( UnitType::H5P, LessonKind::TEXT ), 'live', 'stream' => array( UnitType::LIVE, LessonKind::TEXT ), default => array( UnitType::LESSON, LessonKind::from_slug( $kind ) ?? LessonKind::TEXT ), }; } public static function picker( int $user_id, bool $live = true ): array { $registry = Services::live_providers(); $providers = array(); foreach ( self::LIVE_PROVIDERS as $key ) { $provider = $registry->get( $key ); $providers[] = array( 'key' => $key, 'label' => null === $provider ? self::provider_label( $key ) : $provider->label(), 'available' => $live ? null !== $provider && $provider->available() : null, 'installed' => null !== $provider, ); } $out = array( 'providers' => $providers, 'stream' => null !== $registry->get( ProviderKey::STREAM->slug() ), 'pro' => defined( 'ROOSTER_LMS_PRO_VERSION' ), 'lti' => defined( 'ROOSTER_LTI_VERSION' ), 'can_lesson' => self::can_create( 'lesson', $user_id ), 'can_quiz' => self::can_create( 'quiz', $user_id ), 'addon_kinds' => array(), ); $filtered = apply_filters( 'rooster_lms_course_builder_picker', $out, $user_id ); return is_array( $filtered ) ? $filtered + $out : $out; } public static function can_create( string $which, int $user_id ): bool { $type = get_post_type_object( 'quiz' === $which ? \RoosterLMS\Content\PostTypes::QUIZ : \RoosterLMS\Content\PostTypes::LESSON ); $cap = null === $type ? '' : (string) ( $type->cap->create_posts ?? $type->cap->edit_posts ); return '' !== $cap && $user_id > 0 && user_can( $user_id, $cap ); // phpcs:ignore WordPress.WP.Capabilities.Undetermined -- the lesson or quiz post type's create capability. } private static function provider_label( string $key ): string { return match ( $key ) { 'zoom' => __( 'Zoom', 'rooster-lms' ), 'meet' => __( 'Google Meet', 'rooster-lms' ), 'teams' => __( 'Microsoft Teams', 'rooster-lms' ), 'eroom' => __( 'eRoom', 'rooster-lms' ), 'in_person' => __( 'In person', 'rooster-lms' ), default => __( 'Meeting link', 'rooster-lms' ), }; } }