LLMS_Question_Choice
LLMS_Question_Choice model class
Contents
Source Source
File: includes/models/model.llms.question.choice.php
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | class LLMS_Question_Choice { protected $prefix = '_llms_choice_' ; private $id = null; private $data = array (); private $question = null; private $question_id = null; /** * Constructor * * @param int $question_id WP Post ID of the choice's parent LLMS_Question * @param array|string $data_or_id array of choice data or the choice ID string * @since 3.16.0 * @version 3.16.0 */ public function __construct( $question_id , $data_or_id = array () ) { // Ensure the question is valid. if ( $this ->set_question( $question_id ) ) { // If an ID is passed in, load the question data from post meta. if ( ! is_array ( $data_or_id ) ) { $data_or_id = str_replace ( $this ->prefix, '' , $data_or_id ); $data_or_id = get_post_meta( $this ->question_id, $this ->prefix . $data_or_id , true ); } // Hydrate with postmeta data or array of data passed in. if ( is_array ( $data_or_id ) && isset( $data_or_id [ 'id' ] ) ) { $this ->hydrate( $data_or_id ); } } } /** * Creates a new question * * @param array $data question data array * @return self * @since 3.16.0 * @version 3.16.0 */ public function create( $data ) { $this ->id = uniqid(); return $this ->update( $data )->save(); } /** * Delete a choice * * @return boolean * @since 3.16.0 * @version 3.16.0 */ public function delete () { return delete_post_meta( $this ->question_id, $this ->prefix . $this ->id ); } /** * Determine if the choice that's been requested actually exists * * @return boolean * @since 3.16.0 * @version 3.16.0 */ public function exists() { return ( $this ->id ); } /** * Retrieve a piece of choice data by key * * @param string $key name of the data to be retrieved * @param mixed $default default value if key isn't set * @return mixed * @since 3.16.0 * @version 3.16.0 */ public function get( $key , $default = '' ) { if ( isset( $this ->data[ $key ] ) ) { return $this ->data[ $key ]; } return $default ; } /** * Generic choice getter which automatically uses correct functions based on choice type * * @return string * @since 3.16.0 * @version 3.16.0 */ public function get_choice() { if ( 'image' === $this ->get( 'choice_type' ) ) { return $this ->get_image(); } return $this ->get( 'choice' ); } /** * Retrieve an image for picture choices * * @return [type] * @since 3.16.0 * @version 3.16.0 */ public function get_image() { if ( 'image' !== $this ->get( 'choice_type' ) ) { return '' ; } $img = $this ->get( 'choice' ); if ( is_array ( $img ) && isset( $img [ 'id' ] ) ) { return wp_get_attachment_image( $img [ 'id' ], 'full' ); } return '' ; } /** * Retrieve all of the choice data as an array * * @return array * @since 3.16.0 * @version 3.16.0 */ public function get_data() { return $this ->data; } /** * Retrieve an instance of an LLMS_Question for questions parent * * @return obj * @since 3.16.0 * @version 3.16.0 */ public function get_question() { return $this ->question; } /** * Retrieve the question ID for the given choice * * @return int * @since 3.16.0 * @version 3.16.0 */ public function get_question_id() { return $this ->question_id; } /** * Setup the id and data variables * * @param array $data array of question data * @return void * @since 3.16.0 * @version 3.16.0 */ private function hydrate( $data ) { $this ->id = $data [ 'id' ]; $this ->data[ 'id' ] = $this ->id; $this ->update( $data ); } /** * Determine if the choice is correct * * @return bool * @since 3.16.0 * @version 3.16.0 */ public function is_correct() { return filter_var( $this ->get( 'correct' ), FILTER_VALIDATE_BOOLEAN ); } /** * Save $this->data to the postmeta table * * @return void * @since 3.16.0 * @version 3.16.0 */ public function save() { $this ->data[ 'id' ] = $this ->id; // Always ensure the ID is set when saving data. $update = update_post_meta( $this ->question_id, $this ->prefix . $this ->id, $this ->data ); return ( $update ); } /** * Set a piece of data by key. * * @since 3.16.0 * @since 7.4.1 Check `$type['choices']` is an array before trying to access it as such. * * @param string $key Name of the key to set. * @param mixed $val Value to set. * @return self */ public function set( $key , $val ) { // Don't set the ID. if ( 'id' === $key ) { return $this ; } switch ( $key ) { case 'choice_type' : if ( ! in_array( $val , array ( 'text' , 'image' ) ) ) { $val = 'text' ; } break ; case 'correct' : $val = filter_var( $val , FILTER_VALIDATE_BOOLEAN ); break ; case 'marker' : $type = $this ->get_question()->get_question_type(); if ( is_array ( $type [ 'choices' ] ?? false ) ) { $markers = $type [ 'choices' ][ 'markers' ]; if ( ! in_array( $val , $markers ) ) { $val = $markers [0]; } } break ; case 'choice' : default : if ( is_array ( $val ) ) { $val = array_map ( 'sanitize_text_field' , $val ); } else { $val = wp_kses_post( $val ); } break ; } $this ->data[ $key ] = $val ; return $this ; } /** * Sets question-related data from constructor * * @param int $id WP Post ID of the question's parent question * @return boolean * @since 3.16.0 * @version 3.16.0 */ public function set_question( $id ) { $question = llms_get_post( $id ); if ( $question && is_a ( $question , 'LLMS_Question' ) ) { $this ->question = $question ; $this ->question_id = $id ; return true; } return false; } /** * Update multiple data by key=>val pairs * * @param array $data array of data to set * @return self * @since 3.16.0 * @version 3.16.0 */ public function update( $data = array () ) { foreach ( $data as $key => $val ) { $this ->set( $key , $val ); } return $this ; |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- __construct — Constructor
- create — Creates a new question
- delete — Delete a choice
- exists — Determine if the choice that's been requested actually exists
- get — Retrieve a piece of choice data by key
- get_choice — Generic choice getter which automatically uses correct functions based on choice type
- get_data — Retrieve all of the choice data as an array
- get_image — Retrieve an image for picture choices
- get_question — Retrieve an instance of an LLMS_Question for questions parent
- get_question_id — Retrieve the question ID for the given choice
- hydrate — Setup the id and data variables
- is_correct — Determine if the choice is correct
- save — Save $this->data to the postmeta table
- set — Set a piece of data by key
- set_question — Sets question-related data from constructor
- update — Update multiple data by key=>val pairs
Changelog Changelog
Version | Description |
---|---|
3.16.0 | Introduced. |