LLMS_AJAX::query_quiz_questions()

Retrieve Quiz Questions


Description Description

Used by Select2 AJAX functions to load paginated quiz questions Also allows querying by question title


Return Return

(json)


Top ↑

Source Source

File: includes/class.llms.ajax.php

	public function query_quiz_questions() {

		// grab the search term if it exists
		$term = array_key_exists( 'term', $_REQUEST ) ? $_REQUEST['term'] : '';

		$page = array_key_exists( 'page', $_REQUEST ) ? $_REQUEST['page'] : 0;

		global $wpdb;

		$limit = 30;
		$start = $limit * $page;

		if ( $term ) {
			$like = " AND post_title LIKE '%s'";
			$vars = array( '%' . $term . '%', $start, $limit );
		} else {
			$like = '';
			$vars = array( $start, $limit );
		}

		$questions = $wpdb->get_results( $wpdb->prepare(
			"SELECT ID, post_title
			 FROM $wpdb->posts
			 WHERE
			 	    post_type = 'llms_question'
			 	AND post_status = 'publish'
			 	$like
			 ORDER BY post_title
			 LIMIT %d, %d
			",
			$vars
		) );

		$r = array();
		foreach ( $questions as $q ) {

			$r[] = array(
				'id' => $q->ID,
				'name' => $q->post_title . ' (' . $q->ID . ')',
			);

		}

		echo json_encode( array(
			'items' => $r,
			'more' => count( $r ) === $limit,
			'success' => true,
		) );

		wp_die();

	}

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.