Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement saving quiz and questions via the REST API #3972

Merged
merged 17 commits into from Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions includes/class-sensei-quiz.php
Expand Up @@ -1473,6 +1473,70 @@ public function get_questions( $quiz_id, $post_status = 'any', $orderby = 'meta_

return $questions_query->posts;
}

/**
* Sets the questions of a quiz. It handles all related quiz and question meta.
*
* @param int $quiz_id The quiz id.
* @param array $question_ids The array of questions ids.
*/
public function set_questions( int $quiz_id, array $question_ids ) {
$old_question_order = get_post_meta( $quiz_id, '_question_order', true );
$old_question_order = empty( $old_question_order ) ? [] : array_map( 'intval', $old_question_order );

if ( $question_ids === $old_question_order ) {
return;
}

$added_questions = array_diff( $question_ids, $old_question_order );
$removed_questions = array_diff( $old_question_order, $question_ids );

// Delete question meta from the questions that were removed from the quiz.
if ( ! empty( $removed_questions ) ) {
$this->delete_quiz_question_meta( $quiz_id, $removed_questions );
}

if ( empty( $question_ids ) ) {
delete_post_meta( $this->get_lesson_id( $quiz_id ), '_quiz_has_questions' );
delete_post_meta( $quiz_id, '_question_order' );

return;
}

$question_count = 1;
foreach ( $question_ids as $question_id ) {
update_post_meta( $question_id, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $question_count );
$question_count++;
}

foreach ( $added_questions as $added_question ) {
add_post_meta( $added_question, '_quiz_id', $quiz_id, false );
}

update_post_meta( $this->get_lesson_id( $quiz_id ), '_quiz_has_questions', '1' );
update_post_meta( $quiz_id, '_question_order', array_map( 'strval', $question_ids ) );
}

/**
* Helper method to delete all related meta of quiz's questions.
*
* @param int $quiz_id The quiz id.
* @param array $question_ids A list of quiz ids to remove the meta from.
*/
private function delete_quiz_question_meta( $quiz_id, $question_ids = null ) {
if ( null === $question_ids ) {
$question_ids = get_post_meta( $quiz_id, '_question_order', true );
}

if ( empty( $question_ids ) ) {
return;
}

foreach ( $question_ids as $question_id ) {
delete_post_meta( $question_id, '_quiz_id', $quiz_id );
delete_post_meta( $question_id, '_quiz_question_order' . $quiz_id );
}
}
}


Expand Down
57 changes: 1 addition & 56 deletions includes/data-port/models/class-sensei-import-lesson-model.php
Expand Up @@ -186,14 +186,6 @@ private function set_quiz_questions( $quiz_id ) {
return;
}

if ( empty( $questions ) ) {
$this->delete_quiz_question_meta( $quiz_id );
delete_post_meta( $this->get_post_id(), '_quiz_has_questions' );
delete_post_meta( $quiz_id, '_question_order' );

return;
}

$question_import_ids = array_unique( Sensei_Data_Port_Utilities::split_list_safely( $questions, true ) );
$question_ids = [];

Expand All @@ -215,54 +207,7 @@ private function set_quiz_questions( $quiz_id ) {
$question_ids[] = $question_id;
}

$old_question_order = get_post_meta( $quiz_id, '_question_order', true );
$old_question_order = empty( $old_question_order ) ? [] : array_map( 'intval', $old_question_order );

if ( $question_ids === $old_question_order ) {
return;
}

$added_questions = array_diff( $question_ids, $old_question_order );
$removed_questions = array_diff( $old_question_order, $question_ids );

// Delete question meta from the questions that were removed from the quiz.
if ( ! empty( $removed_questions ) ) {
$this->delete_quiz_question_meta( $quiz_id, $removed_questions );
}

$question_count = 1;
foreach ( $question_ids as $question_id ) {
update_post_meta( $question_id, '_quiz_question_order' . $quiz_id, $quiz_id . '000' . $question_count );
$question_count++;
}

foreach ( $added_questions as $added_question ) {
add_post_meta( $added_question, '_quiz_id', $quiz_id, false );
}

update_post_meta( $this->get_post_id(), '_quiz_has_questions', '1' );
update_post_meta( $quiz_id, '_question_order', array_map( 'strval', $question_ids ) );
}

/**
* Helper method to delete all related meta of quiz's questions.
*
* @param int $quiz_id The quiz id.
* @param array $question_ids A list of quiz ids to remove the meta from.
*/
private function delete_quiz_question_meta( $quiz_id, $question_ids = null ) {
if ( null === $question_ids ) {
$question_ids = get_post_meta( $quiz_id, '_question_order', true );
}

if ( empty( $question_ids ) ) {
return;
}

foreach ( $question_ids as $question_id ) {
delete_post_meta( $question_id, '_quiz_id', $quiz_id );
delete_post_meta( $question_id, '_quiz_question_order' . $quiz_id );
}
Sensei()->quiz->set_questions( $quiz_id, $question_ids );
}

/**
Expand Down