<commit_before><?php
#helper gateway class
require_once('AfricasTalkingGateway.php');
error_reporting(E_ALL);

class Home extends CI_Controller
{
	private $data;

	function __construct()
	{
		parent::__construct();

    #default timezone
    date_default_timezone_set("Africa/Nairobi");

    # initialize quiz_model
    $this->load->model("quiz_model");
	}

	public function index()
	{
		# credentials
		$username = "aisec";
		$apikey = "<KEY>";

		#details from the user
		$phone_number = $this->input->post('from');
		$sender = $this->input->post('to');//shot code(sender)
		$user_message = trim(strtolower($this->input->post('text')));

    $keyword = substr($user_message, 0, 5);
    $succeeding_msg = substr($user_message, 5);

    $current_date_time = date("Y-m-d H:i:s");

    if ($keyword == "flit ")
    {
        #send the user a question
        $this->verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender);
      
    }

    # tester
    $this->verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender);	
	}
  
  public function verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender)
  {

    # @params -> gets message from the user i.e 'flit message comes here'
    # @return -> sends feedback to the user

    //testers
    $phone_number = "+254725332343";
    $succeeding_msg = "aw834k002";


    # check if user is registered
    if($this->quiz_model->is_user_registered($phone_number))
    {
      # registered user
      $quiz_id = $this->quiz_model->get_quiz_count($phone_number);
      $question = $this->quiz_model->get_question($quiz_id);
      $pb_status = $this->quiz_model->get_db_field("phone_number", $phone_number, "probation_count", "members");

      if($pb_status <= 2)
      {
        # user answering for the actual quiz

        echo "You're already registered...<br>";
        echo "Q-> ".$question."<br>";

        $is_correct = $this->quiz_model->is_answer_correct("quiz_id", $quiz_id, $phone_number, $succeeding_msg, "answer", "quest_answer");

        if($is_correct)
        {
          $response = $this->quiz_model->get_db_field("quiz_id", $quiz_id, "right_response", "quest_answer");
          echo "A->> ".$response."<br>";

          # update quiz_count in members table
          $this->quiz_model->update_quiz_count($phone_number, $quiz_id);

          # send next question
          $next_question = $this->quiz_model->get_question($quiz_id + 1);

          echo $next_question;
          $this->send_sms($phone_number, $next_question, $sender);
        }
        else
        {
          # update the probation status
          $pb_count = $this->quiz_model->get_db_field("phone_number", $phone_number, "probation_count", "members");

          if($pb_count == 3)
          {
            # send user to probatio
            # send probation question

            $pb_question = "You are on probation!\n";
            $pb_question .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

            echo "A->> ".$pb_question."<br>";

            # send user the message
            $this->send_sms($phone_number, $pb_question, $sender);
          }
          else
          {
            # update probation count for the member
            $this->quiz_model->update_probation_count($phone_number, $pb_count);

             $response = $this->quiz_model->get_db_field("quiz_id", $quiz_id, "wrong_response", "quest_answer");
             echo "A->> ".$response."<br>";

             #  re-send the question failed
             $this->send_sms($phone_number, $question, $sender);
             echo "A->> ".$question."<br>";
          }

        }
      }
      else
      {
        # user answering for the redemption island quiz

        # update first_time_redemption code
        $pb_id = $this->quiz_model->get_db_field("phone_number", $phone_number, "first_time_probation", "members");

        $this->quiz_model->update_probation_status($phone_number, $pb_id);


        # check if user just got into redepmtion island
        $pb_checker = $this->quiz_model->get_db_field("phone_number", $phone_number, "first_time_probation", "members");

        if($pb_checker == 1)
        {
          # user just got into probation
          # send user the same question
          $pb_question = "You are on probation!\n";
          $pb_question .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

          echo "A->> ".$pb_question."<br>";

          # send user the message
          $this->send_sms($phone_number, $pb_question, $sender);
        }
        else
        {
          # is a 2nd timer i.e he is answering the probation question
          $is_correct = $this->quiz_model->is_answer_correct("quiz_id", $quiz_id, $phone_number, $succeeding_msg, "probation_answer", "quest_answer");

          if($is_correct)
          {
            # got the correct answer
            # reset redemption status

            $this->quiz_model->update_probation_count($phone_number, -1);
            $this->quiz_model->reset_probation_status("phone_number", $phone_number, "probation_count", 0);
            $this->quiz_model->reset_probation_status("phone_number", $phone_number, "first_time_probation", 0);
            

            # send user the same question
            $prob_msg = "Probation code is correct :)\n\n";
            $prob_msg .= $question;

            echo $prob_msg;

            $this->send_sms($phone_number, $prob_msg, $sender);
          }
          else
          {
            # redemption code wrong -> re-send question
            $prob_msg = "WRONG probation code!\n\n";

            $prob_msg .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

            echo $prob_msg;

            $this->send_sms($phone_number, $prob_msg, $sender);
          }
        } 
      }

      
      $this->send_sms($phone_number, $question, $sender);
    }
    else
    {
      # register user first
      $user_registered = $this->quiz_model->register_user($phone_number, strtoupper($succeeding_msg), $current_date_time);

      $question = $user_registered;

      echo "You've been registered...<br>";
      echo "Q-> ".$question."<br>";
      # send question to user
      $this->send_sms($phone_number, $question, $sender);
    }
  }

	public function send_sms($phone_number, $question, $sender)
	{
    # send question to the user using AfricasTalking API<commit_msg>Bug fix on double sms_send() in verify_user_answer() helper<commit_after><?php
#helper gateway class
require_once('AfricasTalkingGateway.php');
error_reporting(E_ALL);

class Home extends CI_Controller
{
	private $data;

	function __construct()
	{
		parent::__construct();

    #default timezone
    date_default_timezone_set("Africa/Nairobi");

    # initialize quiz_model
    $this->load->model("quiz_model");
	}

	public function index()
	{
		# credentials
		$username = "aisec";
		$apikey = "<KEY>";

		#details from the user
		$phone_number = $this->input->post('from');
		$sender = $this->input->post('to');//shot code(sender)
		$user_message = trim(strtolower($this->input->post('text')));

    $keyword = substr($user_message, 0, 5);
    $succeeding_msg = substr($user_message, 5);

    $current_date_time = date("Y-m-d H:i:s");

    if ($keyword == "flit ")
    {
        #send the user a question
        $this->verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender);
      
    }

    # tester
    $this->verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender);	
	}
  
  public function verify_user_answer($phone_number, $succeeding_msg, $current_date_time, $sender)
  {

    # @params -> gets message from the user i.e 'flit message comes here'
    # @return -> sends feedback to the user

    //testers
    $phone_number = "+254725332343";
    $succeeding_msg = "aw834k002";


    # check if user is registered
    if($this->quiz_model->is_user_registered($phone_number))
    {
      # registered user
      $quiz_id = $this->quiz_model->get_quiz_count($phone_number);
      $question = $this->quiz_model->get_question($quiz_id);
      $pb_status = $this->quiz_model->get_db_field("phone_number", $phone_number, "probation_count", "members");

      if($pb_status <= 2)
      {
        # user answering for the actual quiz

        echo "You're already registered...<br>";
        echo "Q-> ".$question."<br>";

        $is_correct = $this->quiz_model->is_answer_correct("quiz_id", $quiz_id, $phone_number, $succeeding_msg, "answer", "quest_answer");

        if($is_correct)
        {
          $response = $this->quiz_model->get_db_field("quiz_id", $quiz_id, "right_response", "quest_answer");
          echo "A->> ".$response."<br>";

          # update quiz_count in members table
          $this->quiz_model->update_quiz_count($phone_number, $quiz_id);

          # send next question
          $next_question = $this->quiz_model->get_question($quiz_id + 1);

          echo $next_question;
          $this->send_sms($phone_number, $next_question, $sender);
        }
        else
        {
          # update the probation status
          $pb_count = $this->quiz_model->get_db_field("phone_number", $phone_number, "probation_count", "members");

          if($pb_count == 3)
          {
            # send user to probatio
            # send probation question

            $pb_question = "You are on probation!\n";
            $pb_question .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

            echo "A->> ".$pb_question."<br>";

            # send user the message
            $this->send_sms($phone_number, $pb_question, $sender);
          }
          else
          {
            # update probation count for the member
            $this->quiz_model->update_probation_count($phone_number, $pb_count);

             $response = $this->quiz_model->get_db_field("quiz_id", $quiz_id, "wrong_response", "quest_answer");
             echo "A->> ".$response."<br>";

             #  re-send the question failed
             $this->send_sms($phone_number, $question, $sender);
             echo "A->> ".$question."<br>";
          }

        }
      }
      else
      {
        # user answering for the redemption island quiz

        # update first_time_redemption code
        $pb_id = $this->quiz_model->get_db_field("phone_number", $phone_number, "first_time_probation", "members");

        $this->quiz_model->update_probation_status($phone_number, $pb_id);


        # check if user just got into redepmtion island
        $pb_checker = $this->quiz_model->get_db_field("phone_number", $phone_number, "first_time_probation", "members");

        if($pb_checker == 1)
        {
          # user just got into probation
          # send user the same question
          $pb_question = "You are on probation!\n";
          $pb_question .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

          echo "A->> ".$pb_question."<br>";

          # send user the message
          $this->send_sms($phone_number, $pb_question, $sender);
        }
        else
        {
          # is a 2nd timer i.e he is answering the probation question
          $is_correct = $this->quiz_model->is_answer_correct("quiz_id", $quiz_id, $phone_number, $succeeding_msg, "probation_answer", "quest_answer");

          if($is_correct)
          {
            # got the correct answer
            # reset redemption status

            $this->quiz_model->update_probation_count($phone_number, -1);
            $this->quiz_model->reset_probation_status("phone_number", $phone_number, "probation_count", 0);
            $this->quiz_model->reset_probation_status("phone_number", $phone_number, "first_time_probation", 0);


            # send user the same question
            $prob_msg = "Probation code is correct :)\n\n";
            $prob_msg .= $question;

            echo $prob_msg;

            $this->send_sms($phone_number, $prob_msg, $sender);
          }
          else
          {
            # redemption code wrong -> re-send question
            $prob_msg = "WRONG probation code!\n\n";

            $prob_msg .= $this->quiz_model->get_db_field("quiz_id", $quiz_id, "probation_quiz", "quest_answer");

            echo $prob_msg;

            $this->send_sms($phone_number, $prob_msg, $sender);
          }
        } 
      }

    }
    else
    {
      # register user first
      $user_registered = $this->quiz_model->register_user($phone_number, strtoupper($succeeding_msg), $current_date_time);

      $question = $user_registered;

      echo "You've been registered...<br>";
      echo "Q-> ".$question."<br>";
      # send question to user
      $this->send_sms($phone_number, $question, $sender);
    }
  }

	public function send_sms($phone_number, $question, $sender)
	{
    # send question to the user using AfricasTalking API
{'fraction_non_alphanumeric': 0.13745096531036075, 'fraction_numerical': 0.004461195292669795, 'mean_word_length': 2.638958858102435, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 176, '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': '7132511', 'n_tokens_mistral': 4319, 'n_tokens_neox': 4076, 'n_words': 813}