<?php

$servername = "localhost";
$username = "devknestlms_dbuser";
$password = "Narahari1!";
$dbname = "devknestlms_multi";

date_default_timezone_set('Asia/Kolkata');
$datetime = date("Y-m-d H:i:s");
$date = date("Y-m-d");

//added for sendinblue
require_once('../vendor/autoload.php');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
// Turn autocommit off
$conn->autocommit(FALSE);

try {
    // Log file
    $file_name = "noytify_admin_logo.txt";
    $count = 0;

    $result0 = mysqli_query($conn, "select DISTINCT a.company_id,a.department_id, null as shipped 
	FROM assessment as a,assessment_qp as aq,question_bank as qb,quiz_response qr 
	WHERE not exists(select 1 from quiz_response_marks qrm where qr.offering_id=qrm.offering_id 
	AND qr.assessment_id=qrm.assessment_id AND qr.user_id=qrm.user_id AND qr.attempt_no=qrm.attempt_no 
	AND qr.question_id=qrm.question_id AND qrm.marks is not null) AND qb.question_id=qr.question_id 
	AND aq.question_id=qb.question_id AND a.assessment_id=aq.assessment_id AND qb.question_type_id=7");
    if (mysqli_num_rows($result0) > 0) {
        $fp = fopen($file_name, "a");
        fwrite($fp, "Date:" . $datetime . "\n");

        while ($row0 = mysqli_fetch_array($result0)) {

            $company_id = $row0['company_id'];
            $department_id = $row0['department_id'];
            if ($department_id == null) {
                $company_id = $company_id . " AND r.department_id is null";
            } else {
                $company_id = $company_id . " AND r.department_id=" . $department_id;
            }

            $result1 = mysqli_query($conn, "SELECT ua.email_id,ua.mobile_no,ua.first_name,ua.last_name,ua.user_id  
				FROM user_account as ua,user_roles as ur,roles as r,role_permission as rp,screen_permission as sp 
				WHERE sp.screen_id=6 AND sp.permission_id=15 AND r.company_id=" . $company_id . " AND 
				r.role_id=ur.role_id AND ur.user_id=ua.user_id AND r.role_id=rp.role_id AND 
				rp.screen_permission_id=sp.screen_permission_id");
            if (mysqli_num_rows($result1) > 0) {

                while ($row1 = mysqli_fetch_array($result1)) {
                    $count = $count + 1;
                    $mobile_no = $row1["mobile_no"];
                    $email_id = $row1["email_id"];
                    $first_name = $row1["first_name"];

                    if ($row1["mobile_no"] != null || $mobile_no != "") {

                        $msgbody = urlencode("Dear admin, There are assessment pending for evaluation. Please evaluate at the earliest. - 4EdgeIT Solutions");
//                        file_get_contents('http://trans.kapsystem.com/api/v3/index.php?method=sms&api_key=A7cca971e58ac774dd98d2830c7aea569&to=' . $mobile_no . '&sender=EDGEIT&message=' . $msgbody);
                         file_get_contents('http://www.smsjust.com/blank/sms/user/urlsms.php?username=haniballal01&pass=user@123&senderid=EDGEIT&dest_mobileno=' . $mobile_no . '&message=' . $msgbody.'&dltentityid=1201160450888723221&dlttempid=1607100000000223931&response=Y');
                    }

                    /* $to = $email_id;
                      $subject = "K-nest LMS";
                      $message = "Dear Admin,<br/><br/>
                      There are assessment pending for evaluation. Please evaluate at the earliest.<br/><br/>
                      Regards,<br/>K-nest LMS";
                      $headers = "MIME-Version: 1.0" . "\r\n";
                      $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                      $headers .= 'From: K-nest LMS <donotreply@knestlms.com>' . "\r\n";
                      mail($to, $subject, $message, $headers); */
                    if ($email_id != "") {
                        $website_link = "";
                        $result16 = mysqli_query($conn, "SELECT c.website_link,c.logo_path  
					FROM user_account as ua,company as c WHERE c.company_id=ua.company_id AND ua.user_id=" . $row1['user_id'] . "");
                        if (mysqli_num_rows($result16) > 0) {
                            $row16 = mysqli_fetch_array($result16);
                             $logo_path = "https://learn.knestlms.com/admin/files_cust_comp" . "/" . $row16['logo_path'];
                            if ($row16["website_link"] != null) {
                                $website_link = $row16["website_link"];
                            }
                        }
                        $credentials = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', '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');
                        $apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(new GuzzleHttp\Client(), $credentials);
                        $sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
                            'subject' => 'K-nest LMS',
                            'sender' => ['name' => 'K-nest LMS', 'email' => 'donotreply@knestlms.com'],
                            'to' => [['email' => $email_id]],
                            'htmlContent' => '<div style="background-color:#f3f5f9;width:100%;padding:10px 50px 10px 10px;"><div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:620px;padding:30px;">Dear Admin,<br/><br/>There are assessment pending for evaluation. Please evaluate at the earliest.<br/><br/>Regards,<br/>K-nest LMS Admin</div><div style="padding:30px;"><center><img src=' . $logo_path . ' /><br/>' . $website_link . '</center></div></div></html>'
                        ]);
                        try {
                            $apiInstance->sendTransacEmail($sendSmtpEmail);
                            //print_r($result);
                        } catch (Exception $e) {
                            echo $e->getMessage(), PHP_EOL;
                        }
                    }
                }
            }
        }
        fwrite($fp, $count . "\n");
        fclose($fp);
    }
    $conn->commit();
} catch (Exception $e) {
    $conn->rollback();

    $fp = fopen($file_name, "a");
    fwrite($fp, "Date:" . $datetime . "\n");
    fwrite($fp, "Message: " . $e->getMessage() . "\n");
    fclose($fp);
}

$conn->close();
