insert_row( Tables::name( 'events' ), array( 'session_id' => $session_id, 'candidate_id' => $candidate_id, 'event_type' => Helpers::truncate( $type, 45 ), 'description' => Helpers::truncate( $description, 190 ), 'actor_id' => $actor_id, 'created_at' => gmdate( 'Y-m-d H:i:s' ), ) ); } /** * A page of events, newest first. * * @since 0.1.0 * * @param int $session_id Session id (0 for all sessions). * @param array $args page, per_page. * @return array{items: object[], total: int} */ public function query( int $session_id, array $args ): array { list( , $per_page, $offset ) = $this->paging( $args ); $events = Tables::name( 'events' ); $candidates = Tables::name( 'candidates' ); $clause = $session_id ? 'e.session_id = %d' : '1=1'; $params = $session_id ? array( $session_id ) : array(); $total = (int) $this->value( "SELECT COUNT(*) FROM {$events} e WHERE {$clause}", $params ); $items = $this->rows( "SELECT e.*, c.full_name FROM {$events} e LEFT JOIN {$candidates} c ON c.id = e.candidate_id WHERE {$clause} ORDER BY e.id DESC LIMIT %d OFFSET %d", array_merge( $params, array( $per_page, $offset ) ) ); return array( 'items' => $items, 'total' => $total, ); } }