<?php 
_e('Drag & Drop unit elements here', 'cp');
?>
                            </div>

                            <div id="unit-pages">
                                <ul class="sidebar-name unit-pages-navigation">
                                    <li class="unit-pages-title"><span><?php 
_e('Unit Page(s)', 'cp');
?>
</span></li>
									<?php 
if ($unit_pagination) {
    $unit_pages = coursepress_unit_pages($unit_id, $unit_pagination);
} else {
    $unit_pages = coursepress_unit_pages($unit_id);
}
if ($unit_id == 0) {
    $unit_pages = 1;
}
for ($i = 1; $i <= $unit_pages; $i++) {
    ?>
										<li><a href="#unit-page-<?php 
    echo $i;
    ?>
"><?php 
    echo $i;
    ?>
</a><span class="arrow-down"></span></li>
									<?php 
}
        /**
         * Shows the course structure.
         *
         * @since 1.0.0
         */
        function course_structure($atts)
        {
            extract(shortcode_atts(array('course_id' => in_the_loop() ? get_the_ID() : '', 'course' => false, 'free_text' => __('Free', 'cp'), 'free_show' => 'true', 'show_title' => 'no', 'show_label' => 'no', 'label_delimeter' => ': ', 'label_tag' => 'h2', 'show_divider' => 'yes', 'label' => __('Course Structure', 'cp'), 'class' => ''), $atts, 'course_structure'));
            $course_id = (int) $course_id;
            $free_text = sanitize_text_field($free_text);
            $free_show = sanitize_text_field($free_show);
            $free_show = 'true' == $free_show ? true : false;
            $show_title = sanitize_html_class($show_title);
            $show_label = sanitize_html_class($show_label);
            $label_delimeter = sanitize_html_class($label_delimeter);
            $label_tag = sanitize_html_class($label_tag);
            $show_divider = sanitize_html_class($show_divider);
            $label = sanitize_text_field($label);
            $class = sanitize_html_class($class);
            // Saves some overhead by not loading the post again if we don't need to.
            $course = empty($course) ? new Course($course_id) : object_decode($course, 'Course');
            $class = sanitize_html_class($class);
            $label_tag = sanitize_html_class($label_tag);
            $label_delimeter = sanitize_html_class($label_delimeter);
            if ($course->details->course_structure_options == 'on') {
                $content = '';
                $student = new Student(get_current_user_id());
                $existing_student = $student->has_access_to_course($course_id);
                $show_unit = $course->details->show_unit_boxes;
                $preview_unit = $course->details->preview_unit_boxes;
                $show_page = $course->details->show_page_boxes;
                $preview_page = $course->details->preview_page_boxes;
                $current_time = date('Y-m-d', current_time('timestamp', 0));
                $course_start_date = $course->details->course_start_date;
                $enable_links = false;
                if ($current_time >= $course_start_date) {
                    $enable_links = true;
                }
                $units = $course->get_units();
                $content .= '<div class="course-structure-block course-structure-block-' . $course_id . '">';
                if (!empty($label)) {
                    $content .= '<' . $label_tag . ' class="label">' . $label . $label_delimeter . '</' . $label_tag . '>';
                }
                $content .= 'yes' == $show_title ? '<label>' . $this->details->post_title . '</label>' : '';
                if ($units) {
                    ob_start();
                    ?>
					<ul class="tree">
						<li>
							<ul>
								<?php 
                    foreach ($units as $unit) {
                        $unit_class = new Unit($unit->ID);
                        $unit_pagination = cp_unit_uses_new_pagination($unit->ID);
                        if ($unit_pagination) {
                            $unit_pages = coursepress_unit_pages($unit->ID, $unit_pagination);
                        } else {
                            $unit_pages = coursepress_unit_pages($unit->ID);
                        }
                        //$unit_pages	 = $unit_class->get_number_of_unit_pages();
                        //									$modules = Unit_Module::get_modules( $unit->ID );
                        $unit_permalink = Unit::get_permalink($unit->ID);
                        if (isset($show_unit[$unit->ID]) && $show_unit[$unit->ID] == 'on' && $unit->post_status == 'publish') {
                            ?>
										<li>
											<label for="unit_<?php 
                            echo $unit->ID;
                            ?>
" class="course_structure_unit_label <?php 
                            echo $existing_student ? 'single_column' : '';
                            ?>
">
												<?php 
                            $title = '';
                            if ($existing_student && $enable_links) {
                                $title = '<a href="' . $unit_permalink . '">' . $unit->post_title . '</a>';
                            } else {
                                $title = $unit->post_title;
                            }
                            ?>
												<div class="tree-unit-left"><?php 
                            echo $title;
                            ?>
</div>
												<div class="tree-unit-right">

													<?php 
                            if ($course->details->course_structure_time_display == 'on') {
                                ?>
														<span><?php 
                                echo $unit_class->get_unit_time_estimation($unit->ID);
                                ?>
</span>
													<?php 
                            }
                            ?>

													<?php 
                            if (isset($preview_unit[$unit->ID]) && $preview_unit[$unit->ID] == 'on' && $unit_permalink && !$existing_student) {
                                ?>
														<a href="<?php 
                                echo $unit_permalink;
                                ?>
?try" class="preview_option"><?php 
                                echo $free_text;
                                ?>
</a>
													<?php 
                            }
                            ?>
												</div>
											</label>

											<ul>
												<?php 
                            for ($i = 1; $i <= $unit_pages; $i++) {
                                if (isset($show_page[$unit->ID . '_' . $i]) && $show_page[$unit->ID . '_' . $i] == 'on') {
                                    ?>
														<li class="course_structure_page_li <?php 
                                    echo $existing_student ? 'single_column' : '';
                                    ?>
">
															<?php 
                                    $pages_num = 1;
                                    $page_title = $unit_class->get_unit_page_name($i);
                                    ?>

															<label for="page_<?php 
                                    echo $unit->ID . '_' . $i;
                                    ?>
">
																<?php 
                                    $title = '';
                                    if ($existing_student && $enable_links) {
                                        $p_title = isset($page_title) && $page_title !== '' ? $page_title : __('Untitled Page', 'cp');
                                        $title = '<a href="' . trailingslashit($unit_permalink) . trailingslashit('page') . trailingslashit($i) . '">' . $p_title . '</a>';
                                    } else {
                                        $title = isset($page_title) && $page_title !== '' ? $page_title : __('Untitled Page', 'cp');
                                    }
                                    ?>

																<div class="tree-page-left">
																	<?php 
                                    echo $title;
                                    ?>
																</div>
																<div class="tree-page-right">

																	<?php 
                                    if ($course->details->course_structure_time_display == 'on') {
                                        ?>
																		<span><?php 
                                        echo $unit_class->get_unit_page_time_estimation($unit->ID, $i);
                                        ?>
</span>
																	<?php 
                                    }
                                    ?>

																	<?php 
                                    if (isset($preview_page[$unit->ID . '_' . $i]) && $preview_page[$unit->ID . '_' . $i] == 'on' && $unit_permalink && !$existing_student) {
                                        ?>
																		<a href="<?php 
                                        echo $unit_permalink;
                                        ?>
page/<?php 
                                        echo $i;
                                        ?>
?try" class="preview_option"><?php 
                                        echo $free_text;
                                        ?>
</a>
																	<?php 
                                    }
                                    ?>

																</div>
															</label>

															<?php 
                                    ?>
														</li>
													<?php 
                                }
                            }
                            //page visible
                            ?>

											</ul>
										</li>
									<?php 
                        }
                        //unit visible
                    }
                    // foreach
                    ?>
							</ul>
						</li>
					</ul>

					<?php 
                    if ($show_divider == 'yes') {
                        ?>
						<div class="divider"></div>
					<?php 
                    }
                    ?>

					<?php 
                    $content .= trim(ob_get_clean());
                } else {
                }
                $content .= '</div>';
                return $content;
            }
        }
        public static function get_modules_front($unit_id = 0)
        {
            global $coursepress, $coursepress_modules, $wp, $paged, $_POST;
            if (isset($_GET['resubmit_nonce']) || isset($_GET['resubmit_nonce']) && wp_verify_nonce($_GET['resubmit_nonce'], 'resubmit_answer')) {
                if (isset($_GET['resubmit_answer'])) {
                    $user_id = get_current_user_id();
                    $course_id = (int) $_GET['c'];
                    $unit_id = (int) $_GET['u'];
                    $module_id = (int) $_GET['m'];
                    $response = get_post((int) $_GET['resubmit_answer']);
                    $response_id = false;
                    if (isset($response) && isset($response->post_author) && $response->post_author == get_current_user_ID()) {
                        $response_id = $response->ID;
                        $resubmitted_response = array('ID' => $response_id, 'post_status' => 'private');
                        wp_update_post($resubmitted_response);
                    }
                    Student_Completion::clear_mandatory_answer($user_id, $course_id, $unit_id, $module_id);
                    wp_redirect(add_query_arg(array('resubmitting' => $module_id, 'previous_response' => $response_id), $_GET['resubmit_redirect_to']) . '#module-' . $module_id);
                    exit;
                } else {
                    if (!empty($_GET['resubmit_cancel'])) {
                        $response_id = $_GET['resubmit_cancel'];
                        $response = get_post((int) $response_id);
                        $user_id = get_current_user_id();
                        $course_id = (int) $_GET['c'];
                        $unit_id = (int) $_GET['u'];
                        $module_id = (int) $_GET['m'];
                        if (isset($response) && isset($response->post_author) && $response->post_author == get_current_user_ID()) {
                            $resubmitted_response = array('ID' => $response_id, 'post_status' => 'publish');
                            wp_update_post($resubmitted_response);
                            Student_Completion::record_mandatory_answer($user_id, $course_id, $unit_id, $module_id);
                            wp_redirect($_GET['resubmit_redirect_to'] . '#module-' . $module_id);
                        }
                    }
                }
            }
            $front_save = false;
            $responses = 0;
            $input_modules = 0;
            $paged = isset($wp->query_vars['paged']) ? absint($wp->query_vars['paged']) : 1;
            $unit_pagination = cp_unit_uses_new_pagination((int) $unit_id);
            $modules = self::get_modules($unit_id, $paged);
            $course_id = do_shortcode('[get_parent_course_id]');
            /**
             * @todo: replace with Student_Completion function soon
             */
            cp_set_visited_unit_page($unit_id, $paged, get_current_user_ID(), $course_id);
            //$unit_module_page_number = isset( $_GET['to_elements_page'] ) ? $_GET['to_elements_page'] : 1;
            if (isset($_POST['submit_modules_data_done']) || isset($_POST['submit_modules_data_no_save_done'])) {
                // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
                if (isset($_POST['submit_modules_data_done'])) {
                    //wp_redirect( cp_full_url( $_SERVER ). '?saved=ok' );
                    if ($_POST['event_origin'] == 'button') {
                        wp_redirect(get_permalink($course_id) . trailingslashit($coursepress->get_units_slug()) . '?saved=ok');
                        exit;
                    } else {
                        wp_redirect(cp_full_url($_SERVER)) . '?saved=ok';
                        exit;
                    }
                } else {
                    if ($_POST['event_origin'] == 'button') {
                        wp_redirect(trailingslashit(get_permalink($course_id)) . trailingslashit($coursepress->get_units_slug()));
                        exit;
                    } else {
                        wp_redirect(cp_full_url($_SERVER));
                        exit;
                    }
                    /* if ( $paged != 1 ) {
                    	  //wp_redirect( cp_full_url( $_SERVER ) );
                    	  wp_redirect(get_permalink($course_id) . trailingslashit($coursepress->get_units_slug()));
                    	  } else {
                    	  wp_redirect(cp_full_url($_SERVER));
                    	  } */
                }
                exit;
            }
            if (isset($_POST['submit_modules_data_save']) || isset($_POST['submit_modules_data_no_save_save'])) {
                // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
                if (isset($_POST['submit_modules_data_save'])) {
                    //wp_redirect( $_SERVER['REQUEST_URI'] . '?saved=ok' );
                    wp_redirect(cp_full_url($_SERVER) . '?saved=ok');
                    exit;
                    //exit;
                } else {
                    //wp_redirect( get_permalink( $unit_id ) . trailingslashit( 'page' ) . trailingslashit( $unit_module_page_number ) );
                }
            }
            if (isset($_POST['save_student_progress_indication'])) {
                wp_redirect(get_permalink($course_id) . trailingslashit($coursepress->get_units_slug()) . '?saved=progress_ok');
                exit;
            }
            ?>

			<form name="modules_form" id="modules_form" enctype="multipart/form-data" method="post" action="<?php 
            echo trailingslashit(get_permalink($unit_id));
            //strtok( $_SERVER["REQUEST_URI"], '?' );
            ?>
" onSubmit="return check_for_mandatory_answers();">
				<!--#submit_bottom-->
				<input type="hidden" id="go_to_page" value=""/>

				<?php 
            if ($unit_pagination) {
                foreach ($modules as $mod) {
                    $class_name = $mod->module_type;
                    if (class_exists($class_name)) {
                        call_user_func($class_name . '::front_main', $mod);
                        if (constant($class_name . '::FRONT_SAVE')) {
                            $front_save = true;
                            if (method_exists($class_name, 'get_response')) {
                                $response = call_user_func($class_name . '::get_response', get_current_user_id(), $mod->ID);
                                if (count($response) > 0) {
                                    $responses++;
                                }
                                $input_modules++;
                            }
                        }
                    }
                }
            } else {
                $pages_num = 1;
                foreach ($modules as $mod) {
                    $class_name = $mod->module_type;
                    if (class_exists($class_name)) {
                        if ($class_name == 'page_break_module') {
                            $pages_num++;
                        } else {
                            if ($pages_num == $paged) {
                                call_user_func($class_name . '::front_main', $mod);
                                if (constant($class_name . '::FRONT_SAVE')) {
                                    $front_save = true;
                                    if (method_exists($class_name, 'get_response')) {
                                        $response = call_user_func($class_name . '::get_response', get_current_user_id(), $mod->ID);
                                        if (count($response) > 0) {
                                            $responses++;
                                        }
                                        $input_modules++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            wp_nonce_field('modules_nonce');
            if ($unit_pagination) {
                $pages_num = coursepress_unit_pages($unit_id, $unit_pagination);
            }
            $is_last_page = coursepress_unit_module_pagination($unit_id, $pages_num, true);
            //check if current unit page is last page
            if (!$coursepress->is_preview($unit_id)) {
                if ($front_save) {
                    if ($input_modules !== $responses) {
                        ?>
							<div class="mandatory_message"><?php 
                        _e('All questions marked with "* Mandatory" require your input.', 'cp');
                        ?>
</div>
							<div class="clearf"></div>
							<input type="hidden" name="unit_id" value="<?php 
                        echo $unit_id;
                        ?>
"/>
							<a id="submit_bottom"></a>
							<?php 
                        if (isset($_POST['submit_modules_data'])) {
                            $form_message = __('The module data has been submitted successfully.', 'coursepress');
                        }
                        if (isset($form_message)) {
                            ?>
<p class="form-info-regular"><?php 
                            echo $form_message;
                            ?>
</p>
							<?php 
                        }
                        ?>


							<input type="submit" class="apply-button-enrolled submit-elements-data-button" name="submit_modules_data_<?php 
                        echo $is_last_page ? 'done' : 'save';
                        ?>
" value="<?php 
                        echo $is_last_page ? __('Done', 'cp') : __('Next', 'cp');
                        ?>
">
							<?php 
                    } else {
                        ?>
							<input type="submit" class="apply-button-enrolled submit-elements-data-button" name="submit_modules_data_no_save_<?php 
                        echo $is_last_page ? 'done' : 'save';
                        ?>
" value="<?php 
                        echo $is_last_page ? __('Done', 'cp') : __('Next', 'cp');
                        ?>
">
							<?php 
                    }
                } else {
                    ?>
						<input type="submit" class="apply-button-enrolled submit-elements-data-button" name="submit_modules_data_no_save_<?php 
                    echo $is_last_page ? 'done' : 'save';
                    ?>
" value="<?php 
                    echo $is_last_page ? __('Done', 'cp') : __('Next', 'cp');
                    ?>
">
						<?php 
                }
            }
            coursepress_unit_module_pagination($unit_id, $pages_num);
            ?>
				<div class="fullbox"></div>
				<?php 
            if (!isset($_GET['try'])) {
                ?>
					<a href="" id="save_student_progress" class="save_progress"><?php 
                _e('Save Progress & Exit', 'cp');
                ?>
</a>
				<?php 
            }
            ?>
			</form>

			<?php 
        }
 function get_unit_page_time_estimation($unit_id, $page_num)
 {
     $unit_pagination = cp_unit_uses_new_pagination($unit_id);
     if ($unit_pagination) {
         $unit_pages = coursepress_unit_pages($unit_id, $unit_pagination);
     } else {
         $unit_pages = coursepress_unit_pages($unit_id);
     }
     //$unit_pages	 = $this->get_number_of_unit_pages();
     $modules = Unit_Module::get_modules($unit_id, $page_num);
     foreach ($modules as $mod) {
         $total_minutes = 0;
         $total_seconds = 0;
         foreach ($modules as $mod) {
             $class_name = $mod->module_type;
             $time_estimation = $mod->time_estimation;
             if (class_exists($class_name)) {
                 if (isset($time_estimation) && $time_estimation !== '') {
                     $estimatation = explode(':', $time_estimation);
                     if (isset($estimatation[0])) {
                         $total_minutes = $total_minutes + intval($estimatation[0]);
                     }
                     if (isset($estimatation[1])) {
                         $total_seconds = $total_seconds + intval($estimatation[1]);
                     }
                 }
             }
         }
         $total_seconds = $total_seconds + $total_minutes * 60;
         //converted everything into minutes for easy conversion back to minutes and seconds
         $minutes = floor($total_seconds / 60);
         $seconds = $total_seconds % 60;
         if ($minutes >= 1 || $seconds >= 1) {
             return apply_filters('coursepress_unit_time_estimation_minutes_and_seconds_format', $minutes . ':' . ($seconds <= 9 ? '0' . $seconds : $seconds) . ' min');
         } else {
             return apply_filters('coursepress_unit_time_estimation_na_format', __('N/A', 'cp'));
         }
     }
 }
																				</ol>
																			<?php 
        } else {
            ?>
																				<ol>
																					<?php 
            // Cheking for inhertited "show" status and forces a save.
            $section_dirty = false;
            foreach ($units as $unit) {
                $unit_class = new Unit($unit->ID);
                $unit_pages = $unit_class->get_number_of_unit_pages();
                $unit_pagination = cp_unit_uses_new_pagination($unit->ID);
                if ($unit_pagination) {
                    $unit_pages = coursepress_unit_pages($unit->ID, $unit_pagination);
                } else {
                    $unit_pages = coursepress_unit_pages($unit->ID);
                }
                $modules = Unit_Module::get_modules($unit->ID);
                ?>

																						<li class="<?php 
                echo $unit->post_status == 'publish' ? 'enabled_unit' : 'disabled_unit';
                ?>
">

																							<label for="unit_<?php 
                echo $unit->ID;
                ?>
">
																								<div class="tree-unit-left"><?php 
                echo $unit->post_status != 'publish' ? __('[draft] ', 'cp') : '';