Submission_Manager::count( int $user_id, int $form_id )

Counts all existing models for this manager.

Description

If the manager supports statuses, individual counts for each status are returned as well.

See also

Parameters

$user_id

(int) (Optional) If provided and the manager supports authors, only models by that user are counted. Default 0 (ignored).

$form_id

(int) (Optional) If provided only submissions for that form are counted. Default 0 (ignored).

Return

(array) Array of <code>$status => $count</code> pairs. In addition, the array always includes a key called '_total', containing the overall count. If the manager does not support statuses, the array only contains the '_total' key.

Source

File: src/db-objects/submissions/submission-manager.php

	public function count( $user_id = 0, $form_id = 0 ) {
		$user_id = absint( $user_id );
		$form_id = absint( $form_id );

		$cache_key = $this->plural_slug;
		if ( $user_id > 0 ) {
			$cache_key .= '-' . $user_id;
		}
		if ( $form_id > 0 ) {
			$cache_key .= '-' . $form_id;
		}

		$counts = $this->cache()->get( $cache_key, 'counts' );
		if ( false !== $counts ) {
			return $counts;
		}

		$where = array();
		$where_args = array();
		if ( $user_id > 0 ) {
			$where[] = 'user_id = %d';
			$where_args[] = $user_id;
		}
		if ( $form_id > 0 ) {
			$where[] = 'form_id = %d';
			$where_args[] = $form_id;
		}

		if ( ! empty( $where ) ) {
			$where = 'WHERE ' . implode( ' AND ', $where );
		} else {
			$where = '';
		}

		$results = $this->db()->get_results( "SELECT status, COUNT( * ) AS num_models FROM %{$this->table_name}% $where GROUP BY status", $where_args );

		$total = 0;
		$counts = array_fill_keys( array( 'completed', 'progressing' ), 0 );
		foreach ( $results as $row ) {
			$counts[ $row->status ] = $row->num_models;
			$total += $row->num_models;
		}

		$counts['_total'] = $total;

		$this->cache()->set( $cache_key, $counts, 'counts' );

		return $counts;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.