<?php 

function edit_psychometrics_cb() {
	$session = Session::getInstance();
	global $post_meta;
	$post = new post;
	$user = new user;
	$ftm  = get_option('form_teacher_mapping');

	$my_class = [];
	if($ftm):
		foreach ($ftm as $class => $value) {
			foreach ($value as $category => $ft_id) {
				if($ft_id == $session->id)
					$my_class[] = ['class' => $class, 'category' => $category];
			}
		}
	endif;
	$students = [];
	if(_count($my_class) > 0) {
		foreach($my_class as $class_cat) {
			$students = array_merge($students, $user->get_user_by_meta_data(['role' => 'student', 'status' => 'active', 'class' => $class_cat['class'], 'category' => $class_cat['category']]));
		}
	}
	$psychometrics = trim_array(get_option('behaviours_skills'));
	$rating = trim_array(get_option('behaviour_rating'));
	// var_dump($students);

	?>
<form method="post" action="contents/plugins/eedu_portal/ajax.php" id="form">
	<div class="row">
		<div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <strong class="card-title">Psychometrics</strong>
                    <button class="btn btn-primary pull-right" id="submit" style="top: 4px;position: absolute;right: 4px;">Update Psychometrics</button>
                </div>
                <div class="card-body">
                	<?php alert(); ?> 
          <table id="bootstrap-data-table" class="table table-striped table-bordered">
            <thead>
              <tr>
              	<th class="sorting_asc" tabindex="0" aria-controls="bootstrap-data-table" rowspan="1" colspan="1" aria-sort="ascending" aria-label="SN: activate to sort column descending">SN</th>
              	<th class="sorting_asc" tabindex="0" aria-controls="bootstrap-data-table" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Class: activate to sort column descending">Student</th>
                <th class="sorting_asc" tabindex="0" aria-controls="bootstrap-data-table" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Class: activate to sort column descending">Class</th>
				<th class="sorting_asc" tabindex="0" aria-controls="bootstrap-data-table" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Course Name: activate to sort column descending">Psychometrics</th>
              </tr>
            </thead>
            <tbody>
<?php
$i = 1;
if(_count($students) > 0) : 
	foreach ($students as $key => $value) {
		$class_meta = $post->get_post_meta($value['class']);
		$category_meta = $post->get_post_meta($value['category']);
		$saved_psy = (isset($value['psychometrics'])) ? unserialize($value['psychometrics']) : false;
	 ?>
	 			<tr>
	 				<td><?=$i?></td>
	 				<td><?=$value['surname'].' '.$value['other_names']; ?></td>
	 				<td><?= get_class_name($class_meta, $category_meta); ?></td>
	 				<td><table>
<?php 
if(_count($psychometrics) > 0) {
	foreach ($psychometrics as $psy) {
		?>
				<tr>
					<td><?php echo $psy; ?></td>
					<?php if(_count($rating) > 0) : foreach ($rating as $rkey => $rate) {
						?>
					<td><?php 
					if(!$saved_psy) {
						if(($rkey+1) == 3) {
							input(['label' => '', 'id' => 'rating', 'name' => $value['id'].'['.$psy.']', 'value' => $rate, 'type' => 'radio', 'checked' => 'checked']); 
						} else {
							input(['label' => '', 'id' => 'rating', 'name' => $value['id'].'['.$psy.']', 'value' => $rate, 'type' => 'radio']);
						}
					} else {
						if(($rkey+1) == $saved_psy[$psy]) {
							input(['label' => '', 'id' => 'rating', 'name' => $value['id'].'['.$psy.']', 'value' => $rate, 'type' => 'radio', 'checked' => 'checked']); 
						} else {
							input(['label' => '', 'id' => 'rating', 'name' => $value['id'].'['.$psy.']', 'value' => $rate, 'type' => 'radio']);
						}
					}

					?></td>
					<?php } endif; ?>
				</tr>
		<?php
	}
}

 ?></table>
	 				</td>
	 			</tr>

	<?php $i++; } 
endif;
			?>
            </tbody>
          </table>
                </div>
            </div>
        </div>
    </div>
    <input type="hidden" name="action" id="action" value="edit_psychometrics">
	
</form>
	<?php
	add_page_javascript('app/assets/js/page-scripts/submit.js');
}