Warning: This method has been deprecated.

LLMS_AJAX::update_syllabus()

Updates course syllabus JSON object


Description Description


Return Return

(array)


Top ↑

Source Source

File: includes/class.llms.ajax.php

	public function update_syllabus() {

		llms_deprecated_function( 'LLMS_AJAX::update_syllabus()', '3.13.0' );

		$post_id  = $_REQUEST['post_id'];

		// Parse section id and create new array for comparison.
		function parse_new_sections( $new_sections_array ) {
			$array = array();

		    foreach ( $new_sections_array as $key => $value ) {
				if ( is_array( $value ) ) {
					foreach ( $value as $keys => $values ) {
						if ( 'section_id' === $keys ) {
							array_push( $array, $values );
						}
					}
					parse_new_sections( $value );
				}
			}
			return $array;
		}

		// Parse section ids returned from DB and create new array for comparison.
		function parse_current_sections( $current_sections_array ) {
		    $array = array();

		    foreach ( $current_sections_array[0] as $key => $value ) {
		    	foreach ( $value as $keys => $values ) {
		    		if ( 'section_id' == $keys ) {
						array_push( $array, $values );
		    		}
		    	}
		    }
		    return $array;
		}

		// Compare arrays and determine if there are any duplicates.
		function array_has_dupes( $new_array ) {
			return count( $new_array ) !== count( array_unique( $new_array ) );
		}

		function delete_lesson_meta( $post_id ) {

			$lesson_ids = array();

			$rd_args = array(
				'post_type' => 'lesson',
				'meta_key' => '_llms_parent_course',
				'meta_value' => $post_id,
			);

			$rd_query = new WP_Query( $rd_args );

			while ( $rd_query->have_posts() ) : $rd_query->the_post();

				array_push( $lesson_ids,  $rd_query->post->ID );

			endwhile;

			wp_reset_postdata();
		}

		if ( isset( $_REQUEST ) ) {

			$success = 'no'; //default response to no.
			$new_sections_array = $_REQUEST['sections'];

			$current_sections_array = get_post_meta( $_REQUEST['post_id'], '_sections' );

			$new_array = parse_new_sections( $new_sections_array );
			$old_array = parse_current_sections( $current_sections_array );

			$result_array = array_intersect_assoc( $new_array, $old_array );

			$new_array_duplicates = array_count_values( $result_array );
			if ( array_has_dupes( $new_array ) ) {
				$success = 'no';
			} else {
				update_post_meta( $_REQUEST['post_id'], '_sections', ( '' === $_REQUEST['sections'] ) ? '' : $_REQUEST['sections'] );
				$success = 'yes';

				//Manage Section _parent_course
				//find all sections that where assigned to the course and delete the metadata
				$section_args = array(
					'post_type' => 'section',
					'meta_key' => '_llms_parent_course',
					'meta_value' => $post_id,
				);

				$section_query = new WP_Query( $section_args );

				while ( $section_query->have_posts() ) : $section_query->the_post();
					//delete all metadata

					//find all lessons that were assigned to sections and delete post_meta data
					$ols_args = array(
						'post_type' => 'lesson',
						'meta_key' => '_llms_parent_section',
						'meta_value' => $section_query->post->ID,
					);

					$ols_query = new WP_Query( $ols_args );

					while ( $ols_query->have_posts() ) : $ols_query->the_post();
						if ( $section_query->post->ID ) {
							foreach ( $new_sections_array as $key => $value ) {
								if ( $section_query->post->ID == $value['section_id'] ) {
									delete_post_meta( $ols_query->post->ID, '_llms_parent_section', $section_query->post->ID );
								}
							}
						}
					endwhile;
					//wp_reset_postdata();

					if ( $post_id ) {
						delete_post_meta( $section_query->post->ID, '_llms_parent_course', $post_id );
					}
				endwhile;
				wp_reset_postdata();

				//find all sections that are currently assigned to the course
				foreach ( $_REQUEST['sections'] as $key => $value ) {
					//update _parent_course for section ids
					update_post_meta( $value['section_id'], '_llms_parent_course', $post_id );
				}

				//Manage lesson _parent_section and _parent_course
				//find all lessons with _parent_course as $post_id and delete the metadata
				$rd_args = array(
					'post_type' => 'lesson',
					'meta_key' => '_llms_parent_course',
					'meta_value' => $post_id,
				);

				$rd_query = new WP_Query( $rd_args );

				while ( $rd_query->have_posts() ) : $rd_query->the_post();
					if ( $post_id ) {
						delete_post_meta( $rd_query->post->ID, '_llms_parent_course', $post_id );
					}
				endwhile;
				wp_reset_postdata();

				foreach ( $_REQUEST['sections'] as $key => $value ) {

					$ls_args = array(
						'post_type' => 'lesson',
						'meta_key' => '_llms_parent_section',
						'meta_value' => $value['section_id'],
					);

					$ls_query = new WP_Query( $ls_args );

					while ( $ls_query->have_posts() ) : $ls_query->the_post();
						if ( $value['section_id'] ) {
							delete_post_meta( $ls_query->post->ID, '_llms_parent_section', $value['section_id'] );
						}
					endwhile;
					wp_reset_postdata();

					foreach ( $value['lessons'] as $keys => $values ) {
						update_post_meta( $values['lesson_id'], '_llms_parent_section', $value['section_id'] );
						update_post_meta( $values['lesson_id'], '_llms_parent_course', $post_id );
					}
				}
			}// End if().
		}// End if().

		//echo json_encode($lesson_ids);
		die();

	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.13.0 This method has been deprecated.
?? Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.





Permalink: