| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Admin_Editor_Question |
| 5 |
* |
| 6 |
* @since 3.0.2 |
| 7 |
*/ |
| 8 |
class LP_Admin_Editor_Question extends LP_Admin_Editor { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var LP_Question |
| 12 |
*/ |
| 13 |
protected $question = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var LP_Question_CURD |
| 17 |
*/ |
| 18 |
protected $question_curd = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* LP_Admin_Editor_Question constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
} |
| 25 |
|
| 26 |
public function dispatch() { |
| 27 |
check_ajax_referer( 'learnpress_admin_question_editor', 'nonce' ); |
| 28 |
|
| 29 |
$args = wp_parse_args( |
| 30 |
$_REQUEST, |
| 31 |
array( |
| 32 |
'id' => false, |
| 33 |
'type' => '', |
| 34 |
) |
| 35 |
); |
| 36 |
$question_id = $args['id']; |
| 37 |
$question = LP_Question::get_question( $question_id ); |
| 38 |
|
| 39 |
if ( ! $question ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
$this->question = $question; |
| 44 |
$this->question_curd = new LP_Question_CURD(); |
| 45 |
$this->result = array( 'status' => false ); |
| 46 |
|
| 47 |
$this->call( $args['type'], array( $args ) ); |
| 48 |
|
| 49 |
return $this->get_result(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get question data in admin question editor. |
| 54 |
* |
| 55 |
* @since 3.0.0 |
| 56 |
* |
| 57 |
* @param $question |
| 58 |
* @param $object | if true, input in question object, do not need init LP_Question::get_question() |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function get_question_data_to_question_editor( $question, $object = false ) { |
| 63 |
if ( ! $object ) { |
| 64 |
if ( get_post_type( $question ) !== LP_QUESTION_CPT ) { |
| 65 |
return array(); |
| 66 |
} |
| 67 |
|
| 68 |
$question = LP_Question::get_question( $question ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! $question ) { |
| 72 |
return array(); |
| 73 |
} |
| 74 |
|
| 75 |
$question_id = $question->get_id(); |
| 76 |
|
| 77 |
$data = array( |
| 78 |
'id' => $question_id, |
| 79 |
'open' => false, |
| 80 |
'title' => get_the_title( $question_id ), |
| 81 |
'type' => array( |
| 82 |
'key' => $question->get_type(), |
| 83 |
'label' => $question->get_type_label(), |
| 84 |
), |
| 85 |
'answers' => is_array( $question->get_data( 'answer_options' ) ) ? array_values( $question->get_data( 'answer_options' ) ) : array(), |
| 86 |
); |
| 87 |
|
| 88 |
return $data; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Draft question. |
| 93 |
* |
| 94 |
* @since 3.0.0 |
| 95 |
* |
| 96 |
* @param $question_id |
| 97 |
* @param array $args |
| 98 |
* |
| 99 |
* @return bool|int|LP_Question |
| 100 |
*/ |
| 101 |
private function draft_question( $question_id, $args = array() ) { |
| 102 |
|
| 103 |
if ( get_post_status( $question_id ) != 'auto-draft' ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$curd = new LP_Question_CURD(); |
| 108 |
|
| 109 |
$args = wp_parse_args( |
| 110 |
$args, |
| 111 |
array( |
| 112 |
'id' => $question_id, |
| 113 |
'title' => __( 'New Question', 'learnpress' ), |
| 114 |
'content' => '', |
| 115 |
'status' => 'draft', |
| 116 |
'create_answers' => false, |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
$question = $curd->create( $args ); |
| 121 |
|
| 122 |
if ( ! $question ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
return $question; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param array $args |
| 131 |
* |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
public function change_question_type( $args = array() ) { |
| 135 |
$type = ! empty( $args['question_type'] ) ? $args['question_type'] : false; |
| 136 |
|
| 137 |
if ( ! $type ) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
$question = $this->question; |
| 142 |
$args = $args['draft_question'] ? $args['draft_question'] : ''; |
| 143 |
|
| 144 |
if ( $args ) { |
| 145 |
$args = (array) ( json_decode( wp_unslash( $args ), '' ) ); |
| 146 |
$draft = $this->draft_question( $this->question->get_id(), $args ); |
| 147 |
|
| 148 |
if ( $draft ) { |
| 149 |
$question = $draft; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if ( isset( $question ) ) { |
| 154 |
$this->question_curd->change_question_type( $question, $type ); |
| 155 |
$this->result = $this->get_question_data_to_question_editor( $question, true ); |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @param array $args |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
public function sort_answer( $args = array() ) { |
| 169 |
// answers order |
| 170 |
$order = ! empty( $args['order'] ) ? $args['order'] : false; |
| 171 |
|
| 172 |
if ( ! $order ) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
// sort answers |
| 177 |
$this->question = $this->question_curd->sort_answers( $this->question->get_id(), $order ); |
| 178 |
|
| 179 |
$this->result = array_values( $this->question->get_data( 'answer_options' ) ); |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param array $args |
| 186 |
* |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
public function update_answer_title( $args = array() ) { |
| 190 |
$answer = ! empty( $args['answer'] ) ? $args['answer'] : false; |
| 191 |
$answer = is_string( $answer ) ? json_decode( wp_unslash( $answer ), true ) : $answer; |
| 192 |
|
| 193 |
if ( ! $answer ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
// update answer title |
| 198 |
$this->result = $this->question_curd->update_answer_title( $this->question->get_id(), $answer ); |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @param array $args |
| 205 |
* |
| 206 |
* @return bool |
| 207 |
*/ |
| 208 |
public function change_correct( $args = array() ) { |
| 209 |
$correct = ! empty( $args['correct'] ) ? $args['correct'] : false; |
| 210 |
$correct = json_decode( wp_unslash( $correct ), true ); |
| 211 |
|
| 212 |
if ( ! $correct ) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
$this->question = $this->question_curd->change_correct_answer( $this->question, $correct ); |
| 217 |
|
| 218 |
$this->result = $this->_get_answers(); |
| 219 |
|
| 220 |
return true; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @param array $args |
| 225 |
* |
| 226 |
* @return bool |
| 227 |
*/ |
| 228 |
public function delete_answer( $args = array() ) { |
| 229 |
$answer_id = ! empty( $args['answer_id'] ) ? $args['answer_id'] : false; |
| 230 |
|
| 231 |
if ( ! $answer_id ) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
|
| 235 |
$this->question_curd->delete_answer( $this->question->get_id(), $answer_id ); |
| 236 |
|
| 237 |
$this->result = $this->_get_answers(); |
| 238 |
|
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
public function _get_answers() { |
| 243 |
$answers = $this->question->get_data( 'answer_options' ); |
| 244 |
|
| 245 |
return $answers ? array_values( $answers ) : array(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* @param array $args |
| 250 |
* |
| 251 |
* @return bool |
| 252 |
*/ |
| 253 |
public function new_answer( $args = array() ) { |
| 254 |
$answer = LP_Question::get_default_answer(); |
| 255 |
|
| 256 |
$this->question_curd->new_answer( $this->question->get_id(), $answer ); |
| 257 |
|
| 258 |
$this->result = $this->_get_answers(); |
| 259 |
|
| 260 |
return true; |
| 261 |
} |
| 262 |
} |
| 263 |
|