| 1 |
<?php |
| 2 |
class LP_Quiz_Results { |
| 3 |
|
| 4 |
/** |
| 5 |
* @var array |
| 6 |
*/ |
| 7 |
protected $results = array(); |
| 8 |
|
| 9 |
/** |
| 10 |
* LP_Quiz_Results constructor. |
| 11 |
* |
| 12 |
* @param $results |
| 13 |
*/ |
| 14 |
public function __construct( $results ) { |
| 15 |
// If $results is user_item_id. |
| 16 |
if ( is_numeric( $results ) ) { |
| 17 |
$this->results = LP_User_Items_Result_DB::instance()->get_result( $results ); |
| 18 |
} else { |
| 19 |
$this->results = $results ? (array) $results : array(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $return |
| 25 |
* |
| 26 |
* @return array|bool|mixed |
| 27 |
*/ |
| 28 |
public function getQuestions( $return = '' ) { |
| 29 |
$questions = $this->offsetGet( 'questions' ); |
| 30 |
|
| 31 |
if ( ! $questions ) { |
| 32 |
$questions = array(); |
| 33 |
} |
| 34 |
$ids = array_keys( $questions ); |
| 35 |
$ids = apply_filters( 'lp-quiz/results/getquestions', $ids ); |
| 36 |
|
| 37 |
return $return === 'ids' ? $ids : $questions; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param int $id |
| 42 |
* |
| 43 |
* @return array|bool |
| 44 |
*/ |
| 45 |
public function getAnswered( $id = 0 ) { |
| 46 |
$questions = $this->getQuestions(); |
| 47 |
|
| 48 |
if ( $id ) { |
| 49 |
return isset( $questions[ $id ] ) ? $questions[ $id ]['answered'] : false; |
| 50 |
} |
| 51 |
|
| 52 |
return wp_list_pluck( $questions, 'answered' ); |
| 53 |
} |
| 54 |
|
| 55 |
public function get( $prop = null, $default = '' ) { |
| 56 |
if ( ! $prop ) { |
| 57 |
return $this->results ? $this->results : $default; |
| 58 |
} |
| 59 |
|
| 60 |
$value = $this->offsetGet( $prop ); |
| 61 |
|
| 62 |
return $value ? $value : $default; |
| 63 |
} |
| 64 |
|
| 65 |
public function offsetUnset( $offset ) { |
| 66 |
if ( isset( $this->results[ $offset ] ) ) { |
| 67 |
unset( $this->results[ $offset ] ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function offsetSet( $offset, $value ) { |
| 72 |
$this->results[ $offset ] = $value; |
| 73 |
} |
| 74 |
|
| 75 |
public function offsetGet( $offset ) { |
| 76 |
return isset( $this->results[ $offset ] ) ? $this->results[ $offset ] : false; |
| 77 |
} |
| 78 |
|
| 79 |
public function offsetExists( $offset ) { |
| 80 |
return array_key_exists( $offset, $this->results ); |
| 81 |
} |
| 82 |
|
| 83 |
public function __toString() { |
| 84 |
return $this->get( 'result' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|