wpmudev-sls
5/25/2018 - 8:19 AM

[CoursePress 2] - Responses History. Help monitor student responses in a timely manner

[CoursePress 2] - Responses History. Help monitor student responses in a timely manner

<?php
/**
* Plugin Name: [CoursePress 2] - Responses History
* Plugin URI: https://premium.wpmudev.org/
* Description: Responses History
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_CP_Responses_History' ) ) {

    class WPMUDEV_CP_Responses_History {

        private static $_instance 		= null;
        private $gdpr_settings_args 	= array();
        private $items_titles 			= array()       ;
        
		public static function get_instance() {

			if ( is_null( self::$_instance ) ) {
				self::$_instance = new WPMUDEV_CP_Responses_History();
			}

			return self::$_instance;
		}

		private function __construct() {

			if( ! class_exists('CoursePress_Data_Student') ){
				return;
			}

			add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 );
			add_action( 'wp_ajax_wpmudev_cp_fetch_responses_history', array( &$this, 'ajax_fetch_history' ), 10 );

		}

		public function ajax_fetch_history() {
			check_ajax_referer( 'wpmudev_cp_fetch_responses_history', 'nonce' );

			$input = $_POST;
			if( 
				! isset( $input['course_id'] ) || ! isset( $input['user_id'] ) ||
				empty( $input['course_id'] ) || empty( $input['user_id'] )
			 ) {
				wp_send_json(
					array(
					    'success'	=> false,
					    'message'	=> 'No user id or no course selected'
					)
				);
			}

			$student_id 		= (int) $input['user_id'];
			$course_id 			= (int) $input['course_id'];
			$course 			= get_post( $course_id );

			if( 'course' != $course->post_type ){

				wp_send_json(
					array(
					    'success'	=> false,
					    'message'	=> 'The course id you have entered did not match any course'
					)
				);

			}

			if( ! CoursePress_Data_Student::is_enrolled_in_course( $student_id, $course_id ) ){

				wp_send_json(
					array(
					    'success'	=> false,
					    'message'	=> "The student you have entered has not enrolled on the course <em>{$course->post_title}</em>"
					)
				);

			}

			$response_index 	= false;
			$data 				= CoursePress_Data_Student::get_completion_data( $student_id, $course_id );
			$units 				= CoursePress_Data_Course::get_units_with_modules( $course_id );
			$out 				= "<div style='background: #efefef; padding: 20px; border: 5px dashed #ddd;'>";
			$out 				.= "<h2 style='width: 100%; text-align: center; font-size: 34px; color: #666'>{$course->post_title}</h2>";
			$prepare_out 		= array();

			foreach ( $data[ 'units' ] as $unit_id => $unit_response_data ) {

				$unit = get_post( $unit_id );
				if( isset( $unit_response_data['responses'] ) ){

					foreach ( $unit_response_data['responses'] as $module_id => $module_response_data_arr ){

						foreach ( $module_response_data_arr as $key => $module_response_data ) {

							$prepare_out[ $module_response_data['date'] ][ $unit_id ][ $module_id ] = array(
								'response' 	=> $module_response_data['response'],
								'grades' 	=> $module_response_data['grades']
							);
						}

					}

				}
				
			}

			if( ! empty( $prepare_out ) ){

				foreach ( $prepare_out as $date => $response_data ) {

					$out .= "<div>";
					$out .= "<h2>{$date}</h2>";

					$out .= '<section style="display:block; padding-left: 20px;">';
					foreach ( $response_data as $unit_id => $unit_data ) {

						$unit_title = $this->cp_item_title( $unit_id );
						$out .= "<h3><small>UNIT: </small>{$unit_title}</h3>";

						foreach ( $unit_data as $module_id => $module_data ) {

							$module_title = $this->cp_item_title( $module_id );

							$response = $module_data['response'];
							$grades = ( isset( $module_data['grades'] ) && ! empty( $module_data['grades'] ) ) ? $module_data['grades'] : null;
							$response_str = '';
							$grades_str = '';

							if ( is_array( $response ) ){
								$response_str .= '<ul style="padding-left:30px;"">';

								foreach ( $response as $item_key => $_response ) {

									$response_str .= "<li><strong>{$item_key}</strong><li>";

									$response_str .= "<ul style='padding-left:30px;'>";

									if ( is_array( $_response ) ) {
										foreach ( $_response as $_response_key => $_response_value ) {
											$response_str .= "<li>{$_response_key} : <em>{$_response_value}</em></li>";
										}
									}
									else{
										$response_str .= $_response;	
									}
									$response_str .= "</ul>";
								}

								$response_str .= '</ul>';
							}
							else{

								$response_str = "<div style='padding-left:20px;'><em>{$response}</em></div>";
							}
							$out .= '<div style="padding: 20px 0 0 20px;">';
							$out .= "<strong><small>MODULE:</small> {$module_title}</strong>";
							$out .= $response_str;

							$grades_str = '<div style="border: 2px solid #666; background: #b5b5b5; padding: 20px;">';
							if ( ! is_null( $grades ) ) {
								
								foreach ( $grades as $grade_key => $grade ) {
									$grades_str .= "<div>Graded by: {$grade['graded_by']}</div>";
									$grades_str .= "<div>Graded on: {$grade['date']}</div>";
									$grades_str .= "<div>Grade: {$grade['grade']}</div>";
								}
								
							}
							else {
								$grades_str .= '<div "style="text-align: center">Not Graded yet</div>';
							}
							$grades_str .= '</div>';

							$out .= $grades_str;

							$out .= '</div>';

						}


					}

					$out .= '</section>';

					$out .= "</div>";
					$out .= "<hr style='margin:20px;' />";

				}

			}

			$out .= "</div>";

			wp_send_json(
				array(
				    'success'	=> true,
				    'message'	=> $out
				)
			);
		}


		public function cp_item_title( $post_id ) {

			if ( ! isset( $this->items_titles[$post_id] ) ){
				$item = get_post($post_id);
				$this->items_titles[$post_id] = $item->post_title;
			}

			return $this->items_titles[$post_id];
		}


		public function admin_menu() {

			$post_type = CoursePress_Data_Course::get_post_type_name();
			$parent_slug = 'edit.php?post_type=' . $post_type;

		    add_submenu_page( $parent_slug, 
		    	'Reponses History', 
		    	'Reponses History', 
		    	'manage_options', 
		    	'cp-student-replies-history',
		    	array( $this, 'admin_page' ) );
		}

		public function admin_page(){

			?>
			<div class="row" style="clear:both; overflow: hidden;">
				<div class="col col-30" style="width:30%; float: left;">

					<h2>Student info</h2>

					<div>
						<label>
							User ID
							<input type="text" name="user-id" id="cp-user-id-input" style="width:70px;" />
						</label>

						<label>
							Course ID
							<input type="text" name="course-id" id="cp-course-id-input" style="width:70px;" />
						</label>

						<a class="button-primary" id="cp-load-student-replies">Go</a>

					</div>

				</div>

				<div class="col col-70" style="width:70%; float: left;">
					<div id="cp-responses-history"></div>
				</div>
			</div>

			<script type="text/javascript">
				(function($){
					$(document).ready(function(){
						var history_load_btn = $( '#cp-load-student-replies' );

						history_load_btn.on('click',function(){
							let user_id = $('#cp-user-id-input').val(),
								course_id = $('#cp-course-id-input').val()
								response_area = $('#cp-responses-history');

							data = {
								action: 'wpmudev_cp_fetch_responses_history',
								nonce: '<?php echo wp_create_nonce( 'wpmudev_cp_fetch_responses_history' ); ?>',
								user_id: user_id,
								course_id: course_id
							};

							$.post(ajaxurl, data, function(response) {

								response_area.html( '<img src="/wp-admin/images/spinner-2x.gif">' );
								if( response.success ) {
									response_area.html( response.message );
								}
								else {
									response_area.html( response.message );
								}
							});
						});
					});
				})(jQuery);
			</script>
			<?php

		}

	}

	if ( ! function_exists( 'wpmudev_cp_responses_history' ) ) {

		function wpmudev_cp_responses_history() {
			return WPMUDEV_CP_Responses_History::get_instance();
		}

		add_action( 'plugins_loaded', 'wpmudev_cp_responses_history', 10 );

	}

}