<?php 

function work_result_cb() {
	$session = Session::getInstance();
    $post = new post;
	global $post_meta;

    do_action('performance_filter3');
    if(!isset($_POST['term']))
        return;
    
    if(!isset($_POST['class'])) {
        do_action("_before_compute_result");
        return;
    }

	
	$class = $_POST['class'];
	$course = $_POST['course'];
	$category = $_POST['category'];
	
	$term = $_POST['term'];
    $acterm = $post->get_post_meta($term);
    $acsession = $post->get_post_meta($acterm['acsession']);

	$user = new user;
	$post = new post;
	$grading = new grading;
	$sorting = new sorting;
	$mode = $grading->get_mode_of_scoring();
	$grades = $grading->get_grades();
	$class_meta = $post->get_post_meta($class);
    $category_meta = $post->get_post_meta($category);
	$course_meta = $post->get_post_meta($course);

    // dynamically query students
    if(promotion_done_in_session($acsession)) {
        $students = array();
        $_students = get_students_in_class($acsession, $term, $class, $category);
        if(_count($_students)) {
            foreach ($_students as $key => $student_id) {
                $students[] = $user->get_user_meta($student_id);
            }
        }
    } else {
        $students = $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active', 'class' => $class, 'category' => $category]);
    }
    // dynamically query students

	$students = $sorting->sort_by_meta_value($students, 'surname', 'asc');
	$existing = $post->get_post_by_meta_data(['type' => 'result', 'class' => $class, 'category' => $category, 'course' => $course, 'term' => $acterm['id'], 'session' => $acsession['id']]);
	if(_count($existing) > 0) {
		$existing = array_shift($existing);
		$existing['result'] = unserialize($existing['result']);
        if($existing['status'] != 'in process' && !get_option('auto_verify_results')) {
            echo '<p class="error">You can no longer edit this result sheet.</p>';
            do_action('course_result_sheet', $existing);
            do_action('result_comments', $existing);
            return;
        }
		
	} else {
		$existing = false;
	}
	var_dump($existing);
    return;
	?>
<div class="row">
<div class="col-md-12">
    <a href="<?php echo $_SERVER['HTTP_REFERER'] ?>" class="btn btn-secondary mb-2 mt-2">Go Back</a>
    <div class="card">
        <div class="card-header">
            <strong class="card-title">
                <?= $course_meta['course_name'].' Score Sheet For '.get_class_name($class_meta, $category_meta).' ('.$acterm['term_name'].' '.$acsession['session_name'].') '; ?></strong>
            <a href="#" class="btn btn-warning pull-right" id="forward" style="top: 4px;position: absolute;right: 4px; <?php echo (!$existing or get_option('auto_verify_results')) ? 'display: none' : ''; ?>"><i class="fas fa-mail-forward"></i>Forward for Verification</a>
        </div>
        <div class="card-body">
            <form method="post" action="contents/plugins/eedu_portal/ajax.php" id="result_sheet_form" data-url="?action=<?php echo base64_encode('submit_for_verification') ?>&class=<?php echo $class ?>&category=<?php echo $category ?>&course=<?php echo $course; echo ($existing) ? '&id='.base64_encode($existing['id']) : ''; ?>" data-verify=false>
                <?php 
                		input(['label' => '', 'id' => 'category', 'value' => $category, 'type' => 'hidden']); 
                		input(['label' => '', 'id' => 'class', 'value' => $class, 'type' => 'hidden']); 
                		input(['label' => '', 'id' => 'course', 'value' => $course, 'type' => 'hidden']);
                		input(['label' => '', 'id' => 'term', 'value' => $acterm['id'], 'type' => 'hidden']);
                		input(['label' => '', 'id' => 'session', 'value' => $acsession['id'], 'type' => 'hidden']);
                	?>
                <table class="table">
                    <tr>
                        <th>SN</th>
                        <th>Name of Students</th>
                        <?php 
             				foreach ($mode as $key => $value) {
             					echo '<th><span class="vertical">'.$value['mode_of_scoring'].' ('.$value['max'].'%)</span></th>';
             				}
             				 ?>
                        <th>Total (100%)</th>
                        <th>Grade</th>
                    </tr>
                    <?php 
$n = 1;
foreach ($students as $student_meta) {
    if($student_meta['status'] != 'active')
        continue;
 ?>
                    <tr class="scoredivs">
                        <td>
                            <?=$n?>
                        </td>
                        <td>
                            <?= ucwords($student_meta['surname'].' '.$student_meta['other_names']);?>
                        </td>
                        <?php 
 							foreach ($mode as $key => $value) {
             				?>
                        <td>
                            <?php input(['label' => '', 'id' => 'input'.$key.$student_meta['id'], 'name' => 'result['.$student_meta['id'].']['.$value['mode_of_scoring'].']', 'data-studId' => $student_meta['id'], 'onchange' => 'calculate_total(this)', 'class' => 'form-control numbers-only', 'value' => $existing['result'][$student_meta['id']][$value['mode_of_scoring']]]) ?>
                        </td>
                        <?php } ?>
                        <td>
                            <div class="form-group">
                                <input id='total<?=$student_meta['id']?>' disabled="disabled" data-modes='
                                <?=json_encode($mode)?>' name='total
                                <?=$student_meta['id']?>' class="form-control numbers-only">
                                <div class="error"><em></em></div>
                            </div>
                        </td>
                        <td>
                            <div class="form-group">
                                <input id='grades<?=$student_meta['id']?>' disabled="disabled" data-grades='
                                <?=json_encode($grades)?>' name='grades
                                <?=$student_meta['id']?>' class="form-control">
                                <div class="error"><em></em></div>
                            </div>
                        </td>
                    </tr>

                    <?php $n++; } ?>
                </table>
                <?php alert(); submit_btn('result_sheet', 'Save'); 
                	do_action('_after_compute_result', ['existing' => $existing, 'class' => $class, 'category' => $category, 'course' => $course, 'session' => $acsession['id'], 'term' => $acterm['id']]);
                	?>
            </form>
        </div>
    </div>
    <?php if($existing) do_action('result_comments', $existing); ?>
</div>
</div>
<?php
	add_page_javascript('app/assets/js/page-scripts/koweb.js');
    add_page_javascript('app/assets/js/page-scripts/submit.js');
	add_page_javascript('contents/plugins/eedu_portal/js/work-result.js');
}
