<?php 

function upload_external_exam_cb() {
	global $post_meta;
	$user = new user;
	$post = new post;
	do_action('display_ext_result_filter');
	
    if(!isset($_POST['class'])) return;
	
    if($_POST['class'] !== 'all') {
        $students = $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active', 'class' => $_POST['class'], 'category' => $_POST['category']]);
    } else {
        $students = $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active']);
    }

	$exam = strtolower($_POST['ext_exam']);
	$class = $_POST['class'];
	$category = $_POST['category'];
	$classname = get_class_name($_POST['class'], $_POST['category']);
	
	$sorting = new sorting;
	$output = $sorting->sort_by_meta_value($students, 'surname', 'asc');
	?>

	<div class="row">
		<div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <strong class="card-title">Upload <?php echo ucwords($exam) .' Result for '. $classname ?> Students</strong>
                </div>
                <div class="card-body">
          <table id="bootstrap-data-table" class="table table-striped table-bordered">
            <thead>
              <tr>
                <th>SN</th>
				<th>Name</th>
				<th>Student ID</th>
				<th>Image</th>
				<th>Gender</th>
				<th>Class</th>
				<th></th>
              </tr>
            </thead>
            <tbody>
<?php
$i = 1;
if(_count($output) > 0) : 
	foreach ($output as $key => $value) {
		$has_res = (isset($value[$exam]) && $value[$exam] != '') ? true : false;
		if($has_res) {
			$res_meta = $post->get_post_meta($value[$exam]);
		}
	 ?>
	 			<tr>
	 				<td><?=$i ?></td>
	 				<td><?=$value['surname'].' '.$value['other_names']; ?></td>
	 				<td><i class="fa fa-user"></i>&nbsp;<?=strtoupper($value['username']);?></td>
	 				<td><img class="img img-72" src="<?php echo get_user_image($value['id']); ?>"></td>
	 				<td><?=$value['gender'];?></td>
	 				<td><?=($classname != '') ? $classname : get_class_name($value['class'], $value['category']); ?></td>
	 				<td>
	 					<form id="upload-form-<?= $value['id'] ?>">
	 						<input type="hidden" class="action" name="action" value="upload_external_exam">
	 						<input type="hidden" class="exam" name="exam" value="<?php echo $exam; ?>">
	 						<input type="hidden" class="class" name="class" value="<?php echo $class; ?>">
	 						<input type="hidden" class="category" name="category" value="<?php echo $category; ?>">
	 						<input type="hidden" class="student_id" name="student_id" value="<?php echo $value['id']; ?>">
							<input type="file" name="upload-file" class="upload-file" accept="image/jpeg,image/gif,image/png,application/pdf,image/x-eps" style="display: none">
							<div class="btn-group" role="group" aria-label="Basic example">
								<a href="<?php echo ($has_res) ? $res_meta['file'] : '#'; ?>" class="viewlink btn btn-info" target="_blank" style="display: <?php echo ($has_res) ? '' : 'none'; ?>;">View</a>
		 						<button data-parent="upload-form-<?= $value['id'] ?>" class="upload-button btn btn-primary">Upload <?php echo ($has_res) ? '' : 'Result'; ?></button>
	 							<a href="#" data-parent="upload-form-<?= $value['id'] ?>" data-exam="<?= $exam ?>" data-studentid="<?= $value['id'] ?>" class="delete error" style="display: <?php echo ($has_res) ? '' : 'none'; ?>;">Delete</a>
		 					</div>
		 					
	 						<br>
	 						<span class="msg" style="display: none;"><em>error message</em></span>
	 					</form>
	 				</td>
	 			</tr>

	<?php $i++; } 
endif;
			?>
            </tbody>
          </table>
                </div>
            </div>
        </div>
    </div>
	<?php
	$post_meta['plugin_name'] = external_examination::$plugin_name;
	add_page_javascript('contents/plugins/external_examination/js/upload.js');
}