<?php 

if($this->app->post_meta['template'] ?? '' === 'classroom plus'):
	define("BOOTSTRAP_CSS", false);

	define("BOOTSTRAP_JS", false);

	define("BOOTSTRAP3_MODIFIER_CSS", true);

	define("KOWEB_FRAMEWORK_CSS", true);

	define("KOWEB_CSS", false);
endif;

require_once("pages.php");

function cp_get_material_ref($lesson_id) {
	$session = Session::getInstance();
	$record_ref = "cp-materialref-".$lesson_id;
	$ref = ($record = get_record($record_ref)) ? $record : "MATERIAL-".$lesson_id."-".time();
	update_record($record_ref, $ref);
	return $ref;
}

function cp_generate_material_ref($lesson_id) {
	$session = Session::getInstance();
	$record_ref = "cp-materialref-".$lesson_id;
	$ref = "MATERIAL-".$lesson_id."-".time();
	update_record($record_ref, $ref);
	return $ref;
}

function cp_get_random_banner() {
	return "contents/plugins/classroom_plus/images/".rand(1, 10).".png";
}

function cp_is_current_week($week, $term_meta) {
	$now = time();
	$end = strtotime("+".$week." week", $term_meta['term_begins']);
	$start = strtotime("-1 week", $end);
	if($start > $term_meta['term_ends']) {
		return false;
	}
	if($now >= $start && $now < $end) {
		return true;
	} else return false;
}

function cp_get_streams(...$args) {
	$stream = new cp_streams;
	return $stream->get_streams(...$args);
}

function cp_get_stream_caption($stream) {
	switch ($stream['type']) {
		case 'lesson':
			return "CR+ LESSON TOPIC";
			break;

		case 'material':
			return "CR+ ATTACHMENT UPLOAD";
			break;

		case 'comment':
			return "CR+ DISCUSSION";
			break;
		
		default:
			return "CR+ NOTIFICATION";
			break;
	}
}

function cp_get_stream_icon($stream) {
	switch ($stream['type']) {
		case 'lesson':
			return "<div class=\"icon icon-shape bg-gradient-green text-white rounded-circle shadow\"><i class=\"fas fa-chalkboard-teacher\"></i></div>";
			break;

		case 'material':
			return "<div class=\"icon icon-shape bg-gradient-info text-white rounded-circle shadow\"><i class=\"fas fa-book-open\"></i></div>";
			break;

		case 'comment':
			return "<div class=\"icon icon-shape bg-gradient-red text-white rounded-circle shadow\"><i class=\"far fa-comments\"></i></div>";
			break;

		case 'quiz':
			return "<div class=\"icon icon-shape bg-gradient-orange text-white rounded-circle shadow\"><i class=\"fas fa-graduation-cap\"></i></div>";
			break;
		
		default:
			return "<div class=\"icon icon-shape bg-gradient-orange text-white rounded-circle shadow\"><i class=\"fas fa-bell\"></i></div>";
			break;
	}
}

function cp_add_quiz_question($quiz_id, $question_id, $compulsory) {
	if(db_post_unique(['type' => "cp_quiz_question", "question" => $question_id, "quiz" => $quiz_id])) {
		// question added
		return ["status" => false, "msg" => "Quiz already mapped"];
	} else {
		$post = new post;
		$response = ["status" => true, "msg" => "Quiz mapped successfully"];
		$args = [
			'type' => "cp_quiz_question", 
			"question" => $question_id,
			"quiz" => $quiz_id,
			"compulsory" => 0
		];
		if($compulsory == 1) {
			$args["compulsory"] = $compulsory;
		}
		$post->add_post($args);
	}
}

function cp_get_quiz_questions($quiz_id, $compulsory = 0) {
	$post = new post;
	return $post->get_post_by_meta_data(['type' => 'cp_quiz_question', 'quiz' => $quiz_id, 'compulsory' => $compulsory]);
}

function cp_get_question_bank($class, $course) {
	$post = new post;
	return $post->get_post_by_meta_data(['type' => 'cp_question_bank', 'class' => $class, 'course' => $course]);
}

function cp_serve_random_questions($quiz_meta, $count = false) {
	if(!$count) {
		$count = $quiz_meta['max_questions'];
	}
	$compulsory = cp_get_quiz_questions($quiz_meta['id'], 1);
	$not_compulsory = cp_get_quiz_questions($quiz_meta['id'], 0);
	$com_count = 0;
	if(is_array($compulsory)) {
		$com_count = _count($compulsory);
	} else $compulsory = array();
	if(is_array($not_compulsory)) {
		$count = ($count >= $com_count) ? $count - $com_count : 0;
		shuffle($not_compulsory);
		$not_compulsory = array_slice($not_compulsory, 0, $count, true);
	} else $not_compulsory = array();
	$questions = array_merge($compulsory, $not_compulsory);
	shuffle($questions);
	return cp_parse_questions($questions);
}

function cp_parse_questions($questions) {
	if(is_array($questions)) {
		if(_count($questions)) {
			foreach ($questions as &$question) {
				$question['question_meta'] = cp_parse_question($question);
			}
		}
	}
	return $questions;
}

function cp_parse_question($question) {
	$post = new post;
	$question_meta = $post->get_post_meta($question['question']);
	if($question_meta['question_type'] == 'objective') {
		$question_meta['option'] = is_serialized($question_meta['option']) ? unserialize($question_meta['option']) : $question_meta['option'];
	}
	return $question_meta;
}