<?php 

/**
 * 
 */
class cp_streams
{
	
	function __construct()
	{
		$this->query = new query;
		$this->columns = [
			'user_id',
			'class_id',
			'subject',
			'teacher_id',
			'message',
			'type',
			'link',
			'status',
			'log_time'
		];
		$this->user = new user;
		$this->status = "active";
		$this->type = "not specified";
		$this->link = "#";
		$this->message = "";
		$this->mail_subject = "CLASSROOM PLUS: NOTIFICATION";
		$this->class_id = "";
		$this->subject = "";
		$this->teacher_id = "";
		$this->students = array();
		$this->teachers = array();
		$this->people = array();
	}

	function init() {
		$sql = "CREATE TABLE IF NOT EXISTS cp_streams (
				id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
				user_id BIGINT(20) NOT NULL,
				class_id BIGINT(20) NOT NULL,
				subject BIGINT(20) NOT NULL,
				teacher_id VARCHAR(100) NOT NULL,
				message TEXT NOT NULL,
				type TEXT NOT NULL,
				link TEXT NOT NULL,
				status TEXT NOT NULL,
				log_time VARCHAR(20) NOT NULL
				)";
		if (! mysqli_query($this->query->connection, $sql)) {
			die ('Unable to create cp_streams table');
		}
	}

	function log() {
		if(!isset($this->user_id)) {
			$session = Session::getInstance();
			$this->user_id = $session->id;
		}
		if(empty($this->teachers)) {
			$this->set_people();
			sleep(3);
		}
		if(is_array($this->teachers)) {
			if(_count($this->teachers)) {
				foreach ($this->teachers as $meta) {
					$this->teacher_id .= "i".$meta['id']."e,";
				}
				$this->teacher_id = substr($this->teacher_id, 0, -1);
			}
		}
		$values = [
			$this->user_id,
			$this->class_id,
			$this->subject,
			$this->teacher_id,
			$this->message,
			$this->type,
			$this->link,
			$this->status,
			time()
		];
		$table = 'cp_streams';
		$this->id = $this->query->insert($table, $this->columns, $values);
	}

	function set_people() {
		$this->students = $this->user->get_user_by_meta_data(['class' => $this->class_id, 'status' => 'active']);
		if(isset($this->subject)) {
			if(!isset($this->teacher_mapping)) {
				$this->teacher_mapping = get_option("teacher_mapping");
			}
			$teachers = get_course_teachers($this->subject, $this->class_id, $this->teacher_mapping);
			if(is_array($teachers)) {
				if(_count($teachers)) {
					foreach ($teachers as $key => $teacher_id) {
						$this->teachers[] = $this->user->get_user_meta($teacher_id); 
					}
				}
			}
		}
		$this->people = array_merge($this->teachers, $this->students);
	}

	function send_communique(...$channels) {
		$home_url = get_option("home_url");
		if(empty($this->teachers) || empty($this->students)) {
			$this->set_people();
			sleep(3);
		}
		inform_users($this->people, $this->message, $this->mail_subject, $home_url.$this->link, ...$channels);
	}

	function get_streams($where = array(), $limit = 0, $offset = 0, $order_by = 'id', $order = 'DESC', $return_key = ARRAY_A) {
		$sql = "SELECT * FROM `cp_streams` ";
		if(is_array($where)) {
			if(_count($where)) {
				$sql .= "WHERE ";
				foreach ($where as $key => $value) {
					if(is_array($value)) {
						if(_count($value)) {
							$sql .= "(";
							foreach ($value as $mv) {
								$sql .= $key." LIKE "."'".$mv."' OR ";
							}
							$sql = substr($sql, 0, -3);
							$sql .= ") AND ";
						}
					} else {
						$sql .= $key." LIKE "."'".$value."' AND ";
					}
				}
				$sql = substr($sql, 0, -4);
			}
		}
		$sql .= "ORDER BY `".$order_by."` ".$order;
		$sql .= ($limit && $limit > 0) ? " LIMIT ".$limit : "";
		$sql .= ($offset && $offset > 0) ? " OFFSET ".$offset : "";
		return $this->query->get_results($sql, $return_key);
	}
}