<?php 
// testing random edit
function _tf_eedu_display_student_class($items, $key, $table_obj) {
	return get_class_name($items['class'] ?? 'all', $items['category'] ?? '');
}

function _tf_eedu_display_term_name($items, $key, $table_obj) {
	return get_term_name($items[$key]);
}

function _tf_eedu_display_session_name($items, $key, $table_obj) {
	return get_session_name($items[$key]);
}

function _tf_eedu_display_full_term_name($items, $key, $table_obj) {
	return get_full_term_name($items[$key]);
}

function _tf_eedu_display_ft_comment($items, $key, $table_obj) {
	return get_session_name($items[$key]);
}

function _tf_eedu_display_phone($items, $key, $table_obj) {
	if($items['role'] == 'student') {
		return (isset($items['n1_phone']) && ($items['n1_phone'] != "")) ? $items['n1_phone'] : '-';
	} else {
		return (isset($items['phone']) && ($items['phone'] != "")) ? $items['phone'] : '-';
	}
}

function _tf_eedu_display_email($items, $key, $table_obj) {
	if($items['role'] == 'student') {
		return (isset($items['n1_email']) && ($items['n1_email'] != "")) ? $items['n1_email'] : '-';
	} else {
		return (isset($items['email']) && ($items['email'] != "")) ? $items['email'] : '-';
	}
}

function _tf_eedu_get_course_name($items, $key, $table_obj) {
	return get_course_name($items[$key]);
}

function _tf_view_print($items, $key, $table_obj) {
	if(!current_user_can("print_results_without_pin") && $items['status'] === "locked") {
		$html = "<div class=\"btn-group-vertical\">";
		$html .= "<a href=\"javaScript:void(0);\" class=\"btn btn-danger ajax-action\" title=\"Click to unlock result\" data-id=".$items['id']." data-prompt='".json_encode(['pin' => 'Result Pin <small>Provided by the school</small>'])."' data-content=\"\" data-action-callback=\"eedu_portal_edit_result_pin\" data-callback=\"refreshPage\">Click To Enter Pin</a>";
		if(current_user_can("edit_form_teacher_comment") || $table_obj->user_id == $items['form_teacher']) {
		$html .= "<a href=\"javaScript:void(0);\" class=\"btn btn-primary ajax-action\" title=\"Click to edit result comment\" data-id=".$items['id']." data-prompt='".json_encode(['comment' => 'New Comment'])."' data-content=\"\" data-action-callback=\"eedu_portal_edit_result_comment\" data-callback=\"click\" data-callbackid=\"#btn-eedu_portal_print_results\">Edit Comment</a>";
		}
		$html .= "</div><br />";
		return $html;
	}
	$link = ($items['type'] === "compiled_mock_result") ? "mock-result-sheet" : "result-sheet";
	if(!isset($table_obj->term[$items['term']])) {
		$post = new post;
		$table_obj->term[$items['term']] = $post->get_post_meta($items['term']);
	}
	$html = "<div class=\"btn-group\">";
	$html .= "<a href=\"".$link."?id=".$items['id']."\" class=\"btn btn-default\" title=\"Click to preview\" target=\"_blank\"><img src=\"contents/plugins/eedu_portal/images/preview.png\" width=\"50px\"></a>";
	$html .= "<a href=\"".$link."?id=".$items['id']."&media=print\" class=\"btn btn-default popmeup\" title=\"Click to print\" data-width=1270 data-height=800><img src=\"contents/plugins/eedu_portal/images/print.png\" width=\"50px\"></a>";
	if(current_user_can("edit_form_teacher_comment") || $table_obj->user_id == $items['form_teacher']) {
		$html .= "<a href=\"javaScript:void(0);\" class=\"btn btn-default ajax-action\" title=\"Click to edit result comment\" data-id=".$items['id']." data-prompt='".json_encode(['comment' => 'New Comment'])."' data-content=\"\" data-action-callback=\"eedu_portal_edit_result_comment\" data-callback=\"click\" data-callbackid=\"#btn-eedu_portal_print_results\"><img src=\"contents/plugins/eedu_portal/images/edit-comment.png\" width=\"50px\"></a>";
	}
	$html .= "</div><br />";
	if($table_obj->term[$items['term']]['order'] === '3') {
		$html .= "<small class=\"blue\">Annual Result: <a class=\"popmeup\" href=\"master-result?class=".$items['class']."&id=".$items['student_id']."&session=".$items['session']."&media=print\" class=\"green\" data-width=1270 data-height=800 title=\"Print Annual Result\">Print</a> |";
		$html .= "<a href=\"master-result?class=".$items['class']."&id=".$items['student_id']."&session=".$items['session']."\" target=\"_blank\" title=\"Preview Annual Result\"> View</a></small>";
	}
	return $html;
}

function _tf_eedu_portal_get_gradebook_input($items, $key, $table_obj) {
	if(!isset($table_obj->totals)) {
		$table_obj->totals = [];
	}
	if(!isset($table_obj->totals[$items['id']])) {
		$table_obj->totals[$items['id']] = 0;
	}
	$html = "<input onclick=\"$(this).select()\" data-student-id=".$items['id']." data-max=".$table_obj->modes[$key]['max']." name=\"result[".$items['id']."][".$key."]\" class=\"form-control numbers-onlyx gradebook-input text-center input-".$items['id']."\" value=\"";
	if(isset($table_obj->results)) {
		if(isset($table_obj->results[$items['id']][$key])) {
			$html .= $table_obj->results[$items['id']][$key];
			$table_obj->totals[$items['id']] += (is_numeric($table_obj->results[$items['id']][$key])) ? $table_obj->results[$items['id']][$key] : 0;
		} else $html .= "x";
	} else $html .= "x";
	$html .= "\" type=\"text\">";
	return $html;
}

function _tf_eedu_portal_get_gradebook_total($items, $key, $table_obj) {
	$html = "<div id=\"total-".$items['id']."\" class=\"text-center\">";
	$html .= $table_obj->totals[$items['id']];
	$html .= "</div>";
	return $html;
}

function _tf_eedu_portal_get_gradebook_grade($items, $key, $table_obj) {
	$html = "<div id=\"grade-".$items['id']."\" class=\"text-center\" data-grades='".json_encode($table_obj->grades)."'>";
	foreach ($table_obj->grades as $grade) {
		if($table_obj->totals[$items['id']] >= $grade['min'] && $table_obj->totals[$items['id']] <= $grade['max']) {
			$html .= $grade['grade'];
		}
	}
	$html .= "</div>";
	return $html;
}

function _tf_eedu_portal_po_must_pass($items, $key, $table_obj) {
	$html = "<input class=\"form-control\" type=\"radio\" id=\"must-pass-".$items['id']."\" name=\"condition[".$items['id']."]\" value=\"must_pass\"";
	if($table_obj->promotion_order) {
		if(isset($table_obj->promotion_order['condition'][$items['id']]) && $table_obj->promotion_order['condition'][$items['id']] === "must_pass") {
			$html .= " checked ";
		}
	}
	$html .= ">";
	return $html;
}

function _tf_eedu_portal_po_optional1($items, $key, $table_obj) {
	$html = "<input class=\"form-control\" type=\"radio\" id=\"optional1-".$items['id']."\" name=\"condition[".$items['id']."]\" value=\"optional1\"";
	if($table_obj->promotion_order) {
		if(isset($table_obj->promotion_order['condition'][$items['id']]) && $table_obj->promotion_order['condition'][$items['id']] === "optional1") {
			$html .= " checked ";
		}
	}
	$html .= ">";
	return $html;
}

function _tf_eedu_portal_po_optional2($items, $key, $table_obj) {
	$html = "<input class=\"form-control\" type=\"radio\" id=\"optional2-".$items['id']."\" name=\"condition[".$items['id']."]\" value=\"optional2\"";
	if($table_obj->promotion_order) {
		if(isset($table_obj->promotion_order['condition'][$items['id']]) && $table_obj->promotion_order['condition'][$items['id']] === "optional2") {
			$html .= " checked ";
		}
	}
	$html .= ">";
	return $html;
}

function _tf_eedu_portal_po_optional3($items, $key, $table_obj) {
	$html = "<input class=\"form-control\" type=\"radio\" id=\"optional3-".$items['id']."\" name=\"condition[".$items['id']."]\" value=\"optional3\"";
	if($table_obj->promotion_order) {
		if(isset($table_obj->promotion_order['condition'][$items['id']]) && $table_obj->promotion_order['condition'][$items['id']] === "optional3") {
			$html .= " checked ";
		}
	}
	$html .= ">";
	return $html;
}

function _tf_eedu_portal_po_remove($items, $key, $table_obj) {
	$html = "<button class=\"btn btn-default btn-block\" onclick=\"document.getElementById('must-pass-".$items['id']."').checked = false; document.getElementById('optional1-".$items['id']."').checked = false; document.getElementById('optional2-".$items['id']."').checked = false; document.getElementById('optional3-".$items['id']."').checked = false; return false;\">Unset</button>";
	return $html;
}