dozent_get_questions( array $args = array(), bool $first = false )

Get all questions by quiz


Description

This function returns all questions under a quiz

Example usage:

$questions = dozent_get_questions( array(
     'quiz_id'   => null,
     'order_by'  => 'ASC',
     'not_in'    => []
 ));

Parameters

$args

(array) (Optional) Arguments for the get_questions search params.

Default value: array()

$first

(bool) (Optional) is return only first item

Default value: false


Return

(DozentDBResponse)


Source

File: includes/quiz-functions.php

	function dozent_get_questions( $args = [], $first = false ) {
		global $wpdb;

		$response = new DozentDBResponse();

		$defaults = apply_filters( 'dozent_get_questions_default_args', [
			'quiz_id'  => null,
			'order_by' => 'ASC',
			'not_in'   => [],
		] );

		$r = wp_parse_args( $args, $defaults );
		extract( $r, EXTR_SKIP );

		//Return empty results if there is no quiz_id
		if ( empty( $quiz_id ) ) {
			return $response;
		}

		$not_in_sql = "";
		if ( ! empty( $not_in ) && dozent_count( $not_in ) ) {
			foreach ( $not_in as $col => $value ){
				$not_in_arr = is_array( $value ) ? $value : explode( ',', $value );
				$not_in_arr = (array) $not_in_arr;
				$prepare_in_sql = dozent_array_to_in_sql( $not_in_arr );

				$not_in_sql .= " AND {$col} NOT IN( {$prepare_in_sql} ) ";
			}
		}

		$order_by_sql = " ORDER BY sort_order ASC ";
		if ( ! empty( $order_by ) ) {
			$order_by = strtolower( $order_by );
			if ( in_array( $order_by, [ 'rand', 'random' ] ) ) {
				$order_by_sql = " ORDER BY RAND() ";
			} else {
				$order_by_sql = " ORDER BY sort_order {$order_by} ";
			}
		}

		$sql
			= "SELECT * FROM {$wpdb->dozent_quiz_questions} WHERE quiz_id = {$quiz_id} {$not_in_sql} {$order_by_sql} ;";

		if ( $first ) {
			$results = $wpdb->get_row( $sql );
			if ( $results ) {
				$response->count = 1;
			}
		} else {
			$results         = $wpdb->get_results( $sql );
			$response->count = dozent_count( $results );
		}

		$response->results = $results;

		return $response;
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.