<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Webinar_model extends MY_Model
{

	public function one($id)
	{
		$result = $this->db->from('webinar')
			->where('id', $id)
			->get()->row_array();
		$result['num_of_applicants'] = $this->db->from('applicant')
			->where('webinar_id', $id)
			->count_all_results();
		return $result;
	}

	public function token_search($token)
	{
		return $this->db->from('webinar')
			->where('webinar_token', $token)
			->get()->row_array();
	}

	public function register($params)
	{
		$this->load->model('User_model', 'user');
		$applicant = $this->user->email_search($params['email']);
		if (! $applicant) {
			$applicant = $this->user->create($params);
		}
		$access_token = $this->random_string(16);
		$webinar = $this->token_search($params['seminar']);
		$data = [
			'user_id' => $applicant['id'],
			'date' => $params['date'],
			'webinar_id' => $webinar['id'],
			'access_token' => $access_token,
		];
		$this->load->model('Applicant_model', 'applicant');
		$this->applicant->create($data);
		return $access_token;
	}
}
