Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
LLMS_Admin_Builder::update_questions( array $questions, LLMS_Quiz|LLMS_Question $parent, int $course_id )
Update quiz questions from heartbeat data
Parameters Parameters
- $questions
-
(array) (Required) Question data array.
- $parent
-
(LLMS_Quiz|LLMS_Question) (Required) Instance of an LLMS_Quiz or LLMS_Question (group).
- $course_id
-
(int) (Required) WP_Post ID of the authorized builder course.
Return Return
(array)
Source Source
File: includes/admin/class.llms.admin.builder.php
private static function update_questions( $questions, $parent, $course_id = 0 ) {
$res = array();
foreach ( $questions as $q_data ) {
$ret = array_merge(
$q_data,
array(
'orig_id' => $q_data['id'],
)
);
// Remove temp id if we have one so we'll create a new question.
if ( self::is_temp_id( $q_data['id'] ) ) {
unset( $q_data['id'] );
} else {
// Verify the question belongs to this course.
$q_parent = $course_id ? self::get_object_parent_course_id( absint( $q_data['id'] ) ) : 0;
if ( $q_parent && absint( $q_parent ) !== absint( $course_id ) ) {
// Translators: %s = Question post id.
$ret['error'] = sprintf( esc_html__( 'Unable to update question "%s". Invalid question ID.', 'lifterlms' ), $q_data['id'] );
array_push( $res, $ret );
continue;
}
if ( $course_id && ! $q_parent && ! current_user_can( 'edit_post', absint( $q_data['id'] ) ) ) {
// Translators: %s = Question post id.
$ret['error'] = sprintf( esc_html__( 'Unable to update question "%s". Invalid question ID.', 'lifterlms' ), $q_data['id'] );
array_push( $res, $ret );
continue;
}
}
// Remove choices because we'll add them individually after creation.
$choices = ( isset( $q_data['choices'] ) && is_array( $q_data['choices'] ) ) ? $q_data['choices'] : false;
unset( $q_data['choices'] );
// Remove child questions if it's a question group.
$questions = ( isset( $q_data['questions'] ) && is_array( $q_data['questions'] ) ) ? $q_data['questions'] : false;
unset( $q_data['questions'] );
$question_id = $parent->questions()->update_question( $q_data );
if ( ! $question_id ) {
// Translators: %s = Question post id.
$ret['error'] = sprintf( esc_html__( 'Unable to update question "%s". Invalid question ID.', 'lifterlms' ), $q_data['id'] );
} else {
$ret['id'] = $question_id;
$question = $parent->questions()->get_question( $question_id );
/**
* When saving a question, make sure that it has a question type set
* otherwise set it by default to `'choice'`.
*/
if ( ! $question->get( 'question_type', true ) ) {
$question->set( 'question_type', 'choice' );
}
if ( $choices ) {
$ret['choices'] = array();
foreach ( $choices as $c_data ) {
$choice_res = array_merge(
$c_data,
array(
'orig_id' => $c_data['id'],
)
);
unset( $c_data['question_id'] );
// Remove the temp ID so that we create it if it's new.
if ( self::is_temp_id( $c_data['id'] ) ) {
unset( $c_data['id'] );
}
$choice_id = $question->update_choice( $c_data );
if ( ! $choice_id ) {
// Translators: %s = Question choice ID.
$choice_res['error'] = sprintf( esc_html__( 'Unable to update choice "%s". Invalid choice ID.', 'lifterlms' ), $c_data['id'] );
} else {
$choice_res['id'] = $choice_id;
$choice_media_id = isset( $c_data['choice']['id'] ) ? absint( $c_data['choice']['id'] ) : 0;
// Only bind the protected-media meta when the current user is allowed to edit that specific media object.
if ( $choice_media_id && current_user_can( 'edit_post', $choice_media_id ) ) {
// The quiz IDs are needed for later verification of access by the protected media filters.
$quiz_ids = get_post_meta( $choice_media_id, '_llms_quiz_id', true );
if ( ! is_array( $quiz_ids ) ) {
$quiz_ids = array();
}
$quiz_id = $parent->get( 'parent_id' ) ? $parent->get( 'parent_id' ) : $parent->get( 'id' );
if ( ! in_array( $quiz_id, $quiz_ids ) ) {
$quiz_ids[] = $quiz_id;
}
update_post_meta( $choice_media_id, '_llms_quiz_id', $quiz_ids );
}
}
array_push( $ret['choices'], $choice_res );
}
} elseif ( $questions ) {
$ret['questions'] = self::update_questions( $questions, $question, $course_id );
}
}
array_push( $res, $ret );
}
return $res;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 3.38.2 | Make sure that a question as a type set, otherwise set it by default to 'choice'. |
| 3.16.11 | Unknown. |
| 3.16.0 | Introduced. |