<?php
require '../dao/Admission.php';
require '../dao/Global.php';
include 'Hubtel/Api.php';

//if(isset($_POST['send_sms'])){
echo "sa";
$globals = $global->get_all_globals();
    $a_year_id = $globals[0]['a_year_id'];
    $semester_id = $globals[0]['semester_id'];
    $track_id = $globals[0]['track_id'];
    $no_of_days = $globals[0]['no_of_days'];
function headRemark($total_score){
  $remark = "";
  if ($total_score>=80) {
    $remark = "An excellent performance keep it up";
  }elseif($total_score>=70){
    $remark = "Good , but could still be better";
  }elseif($total_score>=60){
    $remark = "An average performance, should work harder next semester";
  }elseif ($total_score>=50) {
    $remark = "There is more room for improvement";
  }elseif ($total_score>=40) {
    $remark = "Below average performance. Parent should contact the head master for a discussion";
  }elseif ($total_score<40) {
    $remark = "Very poor performance. Parent is invited to see the headmaster";
  }
}

 $grade = "";
 $guardian = '555733180';
function grade($total_score){
  $grade = "";
  if (!$total_score) {
    $grade = "IC";
    
  }
  elseif ($total_score>=80) {
    $grade = "A1";
  }elseif($total_score>=75){
    $grade = "B2";
  }elseif($total_score>=70){
    $grade = "B3";
  }elseif($total_score>=65){
    $grade = "C4";
  }elseif($total_score>=60){
    $grade = "C5";
  }elseif($total_score>=55){
    $grade = "C6";
  }elseif($total_score>=50){
    $grade = "D7";
  }elseif($total_score>=40){
    $grade = "E8";
  }elseif($total_score>=1){
    $grade = "F9";
  }elseif($total_score==0){
    $grade = "IC";
  }
  return $grade;
  }

  function remark($total_score){
  $remark = "";
  if (!$total_score) {
    $remark = "Incomplete";
    
  }
  elseif ($total_score>=80) {
    $remark = "Exellent";
  }elseif($total_score>=75){
    $remark = "Very Good";
  }elseif($total_score>=70){
    $remark = "Good";
  }elseif($total_score>=65){
    $remark = "High Credit";
  }elseif($total_score>=60){
    $remark = "Credit";
  }elseif($total_score>=55){
    $remark = "Credit";
  }elseif($total_score>=50){
    $remark = "Average";
  }elseif($total_score>=40){
    $remark = "Pass";
  }elseif($total_score<=39){
    $remark = "Fail";
  }
  return $remark;
  }


$class_id = 2;
$all_class_students = $admission->get_class($class_id);

$auth = new BasicAuth("rwkelkwk", "giscmswq");

// instance of ApiHost
$apiHost = new ApiHost($auth);

// instance of AccountApi
$accountApi = new AccountApi($apiHost);

// set web console logging to false
$disableConsoleLogging = false;

// Let us try to send some message
$messagingApi = new MessagingApi($apiHost, $disableConsoleLogging);
try {
//     // Send a quick message
//     $a = 0;
//     for ($i = 0; $i<5; $i++) 
// {
//     $a .= mt_rand(0,9);
// }
$holder = "";

    foreach ($all_class_students as $student) {
        $class_name  = $admission->get_all_class_students($student['class']);
$class_name = $class_name[0]['class_name'];
        $position_in_class = $admission->postion_in_class($student['class'],$semester_id,$a_year_id);

        $sms = 'Name: '. $student['student_name']."<br>";
        if ($position_in_class) {
                                            $i=0;
                                            foreach ($position_in_class as $position_in_class) {
                                              $i++;
                                            if ($position_in_class['student_id']==$student['student_id']) {
                                              $roll = $admission->class_noOf_stds($student['class']);
                                             $holder = $i.'/'.$roll;
                                            }
                                          }
                                          }

        $sms .= "Position in Class: ".$holder."<br>";
        $sms .= "Class: ".$class_name."<br>";
         $over_all_position = $admission->overall_position($student['form_id'],$semester_id,$a_year_id);
                                          if ($over_all_position) {
                                            $i=0;
                                            foreach ($over_all_position as $over_all_position) {
                                              $i++;
                                            if ($over_all_position['student_id']==$student['student_id']) {
                                              $no_on_roll = $admission->form_noOf_stds($student['form_id']);
                                             $holder = $i.'/'.$no_on_roll;
                                            }
                                          }
                                          }
        $sms .= "Overall Position: ".$holder."<br>";
        $vacation = $global->get_vacation();
        $sms .= "Closing Date: ".$vacation[0]['end']."<br>";
        $sms .= "Next Term Begins: ".$vacation[0]['next']."<br>"; 
        $over_all_position = $admission->overall_position($student['form_id'],$semester_id,$a_year_id);
                                          if ($over_all_position) {
                                            $i=0;
                                            foreach ($over_all_position as $over_all_position) {
                                              $i++;
                                            if ($over_all_position['student_id']==$student['student_id']) {
                                             $grade = grade(round(($over_all_position['total_score']/900)*100));
                                            }
                                          }
                                          }
        $sms .= "Overall Grade: ".$grade."<br>";

        $student_report_core = $admission->student_report_core($student['student_id'],$student['class'],$semester_id,$a_year_id);
                                             if ($student_report_core) {

                                              foreach ($student_report_core as $key) {
                                                if ($key['core_elective_name']=="CORE") {

                                  $subject_position = $admission->subject_position($key['subject_id'],$student['class'],$semester_id,$a_year_id);
                                  $over_all_subject_position = $admission->over_all_subject_position($key['subject_id'],$student['form_id'],$semester_id,$a_year_id);

                                   $last_sem_subject_position = $admission->subject_position($key['subject_id'],$student['class'],$semester_id,$a_year_id);
                                   $sms .= "Subject   Class Score   Exams Score   Total   Remark";
                                   $sms .= $key['subject_name']."   ".round($key['class_score']*(30/100))."   ".round($key['exam_score']*(70/100))."   ".round($key['total_score'])."   ".remark($key['total_score'])."<br>";
                               }
                           }
                       }

                       $student_report_elective = $admission->student_report_elective($student['student_id'],$student['class'],$semester_id,$a_year_id);
                                             if ($student_report_elective) {
                                              foreach ($student_report_elective as $key) {
                                                if ($key['core_elective_name']=="ELECTIVE") {

                                  $subject_position = $admission->subject_position($key['subject_id'],$student['class'],$semester_id,$a_year_id);
                                  $over_all_subject_position = $admission->over_all_subject_position($key['subject_id'],$student['form_id'],$semester_id,$a_year_id);

                                   $last_sem_subject_position = $admission->subject_position($key['subject_id'],$student['class'],$semester_id,$a_year_id);
                                   $sms .= "Subject   Class Score   Exams Score   Total   Remark";
                                   $sms .= $key['subject_name']."   ".round($key['class_score']*(30/100))."   ".round($key['exam_score']*(70/100))."   ".round($key['total_score'])."   ".remark($key['total_score'])."<br>";

                               }
                           }
                       }

    }


    $messageResponse = $messagingApi->sendQuickMessage("OTSHS/OFFICE OF THE ACADEMIC MASTER", "+2330555733180", $sms);
    echo $sms;

    if ($messageResponse instanceof MessageResponse) {
        // echo $messageResponse->getStatus();
        echo 1;
        // $phone_code->insert(array("code"=>$a,"user_id"=>$id));
    } elseif ($messageResponse instanceof HttpResponse) {
        echo "\nServer Response Status : " . $messageResponse->getStatus();
    }
} catch (Exception $ex) {
    echo $ex->getTraceAsString();
}

//}

