<commit_before><?php
class Call extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->authenticate->redirect_except();
		reset_language(current_language());

		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
	}

	/////////////////////////
	// CRUD-actions
	/////////////////////////

	/**
	 * Specifies the contents of the default page.
	 * @return void
	 */
	public function index()
	{
		create_call_table();
		$data['ajax_source'] = 'call/table/';
		$data['sort_column'] = 5;	// Sort on timestart
		$data['page_title'] = lang('calls');

		$this->load->view('templates/header', $data);
		$this->load->view('templates/list_view', $data);
		$this->load->view('templates/footer');
	}

	/**
	 * Deletes the specified call, and returns to previous page
	 * @param integer $call_id 
	 * @return void
	 */
	public function delete($call_id)
	{
		$this->callModel->delete_call($call_id);
		flashdata(lang('call_deleted'));
		redirect($this->agent->referrer(), 'refresh');
	}

	/////////////////////////
	// Other views
	/////////////////////////

	/**
	 * Gets all calls for a user. 
	 * @param integer $user_id 
	 * @return void
	 */
	public function user($user_id)
	{
		if (!is_admin() && !correct_user($user_id)) return;

		create_call_table(NULL, FALSE);
		$data['ajax_source'] = 'call/table_by_user/' . $user_id;
		$data['sort_column'] = 5;	// Sort on timestart
		$data['page_title'] = lang('calls');

		$this->load->view('templates/header', $data);
		$this->load->view('templates/list_view', $data);
		$this->load->view('templates/footer');
	}

	/////////////////////////
	// Call actions: undo, confirm, no reply, cancel, take over
	/////////////////////////

	/** Undo: deletes call, releases participation lock and returns to the experiment call page (with message) */
	public function undo($call_id)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);

		$this->callModel->delete_call($call_id);
		$this->participationModel->release_lock($participation->id);

		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);
		flashdata(sprintf(lang('part_cancel_call'), name($participant), $experiment->name));

		redirect('/participant/find/' . $experiment->id, 'refresh');
	}

	/** Confirm: confirms the participation with a scheduled date */
	public function confirm($call_id, $appointment = NULL)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);
		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);

		$this->form_validation->set_rules('appointment', lang('appointment'), 'trim|required');
		
		// Run validation
		if (!$this->form_validation->run() && $appointment == NULL)
		{
			// If not succeeded, show form again with error messages
			flashdata(validation_errors(), FALSE);
			redirect($this->agent->referrer(), 'refresh');
		}
		else
		{
			// If succeeded, insert data into database
			if (!$appointment)
			{
				// This is a confirmation of a call, so post the appointment and format it for MySQL.
				$appointment = input_datetime($this->input->post('appointment'));
			}
			else 
			{
				// This is an ad-hoc participation send via url as Unix timestamp, so directly convert to MySQL date.
				$appointment = date('Y-m-d H:i:s', $appointment);
			}

			// End the call, confirm the participation
			$this->callModel->end_call($call_id, CallStatus::Confirmed);
			$this->participationModel->confirm($participation->id, $appointment);
			$this->participationModel->release_lock($participation->id);

			// Fetch the participant's email (or the concept e-mail)
			$flashdata = '';
			$email = $this->input->post('concept') ? TO_EMAIL_OVERRIDE : $participant->email;

			// Send the anamnese (or not, if checkbox is set)
			$testinvite = NULL;
			if ($this->input->post('send_anamnese'))
			{
				$invites = $this->create_test_invitations($participant);
				$flashdata .= br() . $invites[0];
				$testinvites = $invites[1];
				$testinvite = $testinvites[0]; // TODO: this is ugly. there should be only one (Anamnese), but we don't check for that.
			}

			// If there's a combination appointment made, create that participation as well, plus a confirmation e-mail 
			if ($this->input->post('comb_appointment'))
			{
				$comb_experiment = $this->experimentModel->get_experiment_by_id($this->input->post('comb_exp'));
				$comb_participation_id = $this->participationModel->create_participation($comb_experiment, $participant);
				$comb_appointment = input_datetime($this->input->post('comb_appointment'));
				$this->participationModel->confirm($comb_participation_id, $comb_appointment);
				$flashdata .= br() . $this->send_confirmation_email($comb_participation_id, $testinvite, $email, $comb_experiment);
			}
			// Else we can send a simple confirmation e-mail
			else 
			{
				$flashdata .= br() . $this->send_confirmation_email($participation->id, $testinvite, $email);
			}

			// If we send a concept, add that to the confirmation message
			if ($this->input->post('concept')) 
			{
				$flashdata .= br(2) . sprintf(lang('concept_send'), $email);
			}

			// Return to the find participants page with a success message
			flashdata(sprintf(lang('part_confirmed'), name($participant), $experiment->name) . $flashdata);
			redirect('/participant/find/' . $experiment->id, 'refresh');
		}
	}

	/** No-reply: adds a no-reply to the participation, possibly with a message (e.g. voicemail/e-mail) */
	public function no_reply($call_id)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);
		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);

		$this->form_validation->set_rules('message', lang('message'), 'required');

		// Run validation
		if (!$this->form_validation->run())
		{
			// If not succeeded, show form again with error messages
			flashdata(validation_errors(), FALSE);
			redirect($this->agent->referrer(), 'refresh');
		}
		else
		{
			// If succeeded, insert data into database<commit_msg>No confirmation e-mail for ad hoc participations<commit_after><?php
class Call extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->authenticate->redirect_except();
		reset_language(current_language());

		$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
	}

	/////////////////////////
	// CRUD-actions
	/////////////////////////

	/**
	 * Specifies the contents of the default page.
	 * @return void
	 */
	public function index()
	{
		create_call_table();
		$data['ajax_source'] = 'call/table/';
		$data['sort_column'] = 5;	// Sort on timestart
		$data['page_title'] = lang('calls');

		$this->load->view('templates/header', $data);
		$this->load->view('templates/list_view', $data);
		$this->load->view('templates/footer');
	}

	/**
	 * Deletes the specified call, and returns to previous page
	 * @param integer $call_id 
	 * @return void
	 */
	public function delete($call_id)
	{
		$this->callModel->delete_call($call_id);
		flashdata(lang('call_deleted'));
		redirect($this->agent->referrer(), 'refresh');
	}

	/////////////////////////
	// Other views
	/////////////////////////

	/**
	 * Gets all calls for a user. 
	 * @param integer $user_id 
	 * @return void
	 */
	public function user($user_id)
	{
		if (!is_admin() && !correct_user($user_id)) return;

		create_call_table(NULL, FALSE);
		$data['ajax_source'] = 'call/table_by_user/' . $user_id;
		$data['sort_column'] = 5;	// Sort on timestart
		$data['page_title'] = lang('calls');

		$this->load->view('templates/header', $data);
		$this->load->view('templates/list_view', $data);
		$this->load->view('templates/footer');
	}

	/////////////////////////
	// Call actions: undo, confirm, no reply, cancel, take over
	/////////////////////////

	/** Undo: deletes call, releases participation lock and returns to the experiment call page (with message) */
	public function undo($call_id)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);

		$this->callModel->delete_call($call_id);
		$this->participationModel->release_lock($participation->id);

		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);
		flashdata(sprintf(lang('part_cancel_call'), name($participant), $experiment->name));

		redirect('/participant/find/' . $experiment->id, 'refresh');
	}

	/** Confirm: confirms the participation with a scheduled date */
	public function confirm($call_id, $appointment = NULL)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);
		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);

		$this->form_validation->set_rules('appointment', lang('appointment'), 'trim|required');
		
		// Run validation
		if (!$this->form_validation->run() && $appointment == NULL)
		{
			// If not succeeded, show form again with error messages
			flashdata(validation_errors(), FALSE);
			redirect($this->agent->referrer(), 'refresh');
		}
		else
		{
			$ad_hoc = FALSE;

			// If succeeded, insert data into database
			if (!$appointment)
			{
				// This is a confirmation of a call, so post the appointment and format it for MySQL.
				$appointment = input_datetime($this->input->post('appointment'));
			}
			else 
			{
				// This is an ad-hoc participation send via url as Unix timestamp, so directly convert to MySQL date.
				$appointment = date('Y-m-d H:i:s', $appointment);
				// Set a flag to not send a confirmation email / return to participations overview
				$ad_hoc = TRUE;
			}

			// End the call, confirm the participation
			$this->callModel->end_call($call_id, CallStatus::Confirmed);
			$this->participationModel->confirm($participation->id, $appointment);
			$this->participationModel->release_lock($participation->id);

			// Fetch the participant's email (or the concept e-mail)
			$flashdata = '';
			$email = $this->input->post('concept') ? TO_EMAIL_OVERRIDE : $participant->email;

			// Send the anamnese (or not, if checkbox is set)
			$testinvite = NULL;
			if ($this->input->post('send_anamnese'))
			{
				$invites = $this->create_test_invitations($participant);
				$flashdata .= br() . $invites[0];
				$testinvites = $invites[1];
				$testinvite = $testinvites[0]; // TODO: this is ugly. there should be only one (Anamnese), but we don't check for that.
			}

			// If there's a combination appointment made, create that participation as well, plus a confirmation e-mail 
			if ($this->input->post('comb_appointment'))
			{
				$comb_experiment = $this->experimentModel->get_experiment_by_id($this->input->post('comb_exp'));
				$comb_participation_id = $this->participationModel->create_participation($comb_experiment, $participant);
				$comb_appointment = input_datetime($this->input->post('comb_appointment'));
				$this->participationModel->confirm($comb_participation_id, $comb_appointment);
				$flashdata .= br() . $this->send_confirmation_email($comb_participation_id, $testinvite, $email, $comb_experiment);
			}
			// Else we can send a simple confirmation e-mail
			else 
			{
				if (!$ad_hoc)
				{
					$flashdata .= br() . $this->send_confirmation_email($participation->id, $testinvite, $email);
				}
			}

			// If we send a concept, add that to the confirmation message
			if ($this->input->post('concept')) 
			{
				$flashdata .= br(2) . sprintf(lang('concept_send'), $email);
			}

			// Return to the find participants page with a success message
			flashdata(sprintf(lang('part_confirmed'), name($participant), $experiment->name) . $flashdata);
			redirect($ad_hoc ? '/participation/' : '/participant/find/' . $experiment->id, 'refresh');
		}
	}

	/** No-reply: adds a no-reply to the participation, possibly with a message (e.g. voicemail/e-mail) */
	public function no_reply($call_id)
	{
		$this->check_integrity($call_id);
		
		$participation = $this->callModel->get_participation_by_call($call_id);
		$participant = $this->participationModel->get_participant_by_participation($participation->id);
		$experiment = $this->participationModel->get_experiment_by_participation($participation->id);

		$this->form_validation->set_rules('message', lang('message'), 'required');

		// Run validation
		if (!$this->form_validation->run())
		{
			// If not succeeded, show form again with error messages
			flashdata(validation_errors(), FALSE);
			redirect($this->agent->referrer(), 'refresh');
		}
		else
		{
			// If succeeded, insert data into database
{'fraction_non_alphanumeric': 0.17651950408224978, 'fraction_numerical': 0.0009071666162685213, 'mean_word_length': 3.990192380233874, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 209, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6865373', 'n_tokens_mistral': 4669, 'n_tokens_neox': 4062, 'n_words': 876}