Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * API VK
- * @author k011E (Александр Каплин)
- */
- class API
- {
- public $tocken;
- public $secret_code;
- public $confirmation_code;
- public $data;
- public $tocken_app = '089d42df638b4966278e37cae6d0fd0a30009ff9ec1e90bd4cb993f682d25163c1bd1144e7214038ab332';
- public $group_id;
- function defaultGenarateQuest(){
- global $db;
- $num = $db->query("SELECT * FROM `quests`")->num_rows;
- $rnd = rand(1, $num);
- $quest = $db->query("SELECT * FROM `quests` WHERE `id`='".$rnd."'")->fetch_assoc();
- // Кнопки
- $button1 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '1')), 'label' => '1'), 'color' => 'positive');
- $button2 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '2')), 'label' => '2'), 'color' => 'positive');
- $button3 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '3')), 'label' => '3'), 'color' => 'positive');
- $button4 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '4')), 'label' => '4'), 'color' => 'positive');
- $button2_1 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => 'start_exam')), 'label' => "Начать экзамен"), 'color' => 'primary');
- // Клавиатуры
- $array = array($button1, $button2, $button3, $button4);
- $array2 = array($button2_1);
- $generate_message = 'Вопрос: '.$quest['name'].'
- Варианты ответов:
- 1. '.$quest['answer1'].'
- 2. '.$quest['answer2'];
- if(!empty($quest['answer3'])){
- $generate_message .= '
- 3. '.$quest['answer3'];
- }
- if(!empty($quest['answer4'])){
- $generate_message .= '
- 4. '.$quest['answer4'];
- }
- $this->sendKeyboard($generate_message, $array, $array2, $this->attachImage($quest['img']));
- $this->updateUser($quest['id']);
- }
- function __construct($tocken, $secret_code, $confirmation_code, $group_id, $data)
- {
- $this->tocken = $tocken;
- $this->secret_code = $secret_code;
- $this->confirmation_code = $confirmation_code;
- $this->data = $data;
- $this->group_id = $group_id;
- }
- function getType(){
- return $this->data->type;
- }
- function getPayload(){
- $string = json_decode($this->data->object->payload, true);
- return $string["button"];
- }
- // Проверка paylaod на начало экзамена
- function isStartExam(){
- if($this->getPayload() == "start_exam"){
- return true;
- }else{
- return false;
- }
- }
- function isStopExam(){
- if($this->getPayload() == "stop_exam"){
- return true;
- }else{
- return false;
- }
- }
- // Функция иницилизирующая старт экзамена
- function startExam(){
- global $db;
- $ticket = $db->query("SELECT * FROM `ticket` ORDER BY RAND() LIMIT 1")->fetch_assoc();
- $db->query("INSERT INTO `session_exams` SET `id_user`='".$this->data->object->user_id."', `time_start`='".time()."', `time_end`='".(time() + 1200)."', `ticket`='".$ticket['id']."', `step`='1'");
- $this->sendMessage("Экзамен начат!");
- }
- function stopExam(){
- global $db;
- $session = $this->getSessionUser();
- $this->sendMessage("Вы отменили экзамен. Отвечено на ".$session['step']." вопросов. Ошибок: ".$session['errors']);
- $db->query("DELETE FROM `session_exams` WHERE `id`='".$session['id']."'");
- }
- // Функция проверки существует ли начатый экзамен
- function isHaveStartedExam(){
- global $db;
- if($db->query("SELECT `id` FROM `session_exams` WHERE `id_user`='".$this->data->object->user_id."'")->num_rows!=0){
- return true;
- }else{
- return false;
- }
- }
- // Функция генерации вопроса
- function generateQuest(){
- global $db;
- $session = $this->getSessionUser();
- $quest = $db->query("SELECT * FROM `quests` WHERE `ticket`='".$session['ticket']."' AND `number`='".$session['step']."'")->fetch_assoc();
- $generate_message = 'Времени осталось: '.$this->getTime(time(), $session['time_end']).'
- Вопрос: '.$quest['name'].'
- Варианты ответов:
- 1. '.$quest['answer1'].'
- 2. '.$quest['answer2'];
- if(!empty($quest['answer3'])){
- $generate_message .= '
- 3. '.$quest['answer3'];
- }
- if(!empty($quest['answer4'])){
- $generate_message .= '
- 4. '.$quest['answer4'];
- }
- return $generate_message;
- }
- function sendQuest(){
- global $db;
- $button1 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '1')), 'label' => '1'), 'color' => 'positive');
- $button2 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '2')), 'label' => '2'), 'color' => 'positive');
- $button3 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '3')), 'label' => '3'), 'color' => 'positive');
- $button4 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => '4')), 'label' => '4'), 'color' => 'positive');
- $button2_1 = array('action' => array('type' => 'text', 'payload' => json_encode(array('button' => 'stop_exam')), 'label' => "Закончить экзамен"), 'color' => 'negative');
- // Клавиатуры
- $array = array($button1, $button2, $button3, $button4);
- $array2 = array($button2_1);
- $session = $this->getSessionUser();
- $quest = $db->query("SELECT * FROM `quests` WHERE `ticket`='".$session['ticket']."' AND `number`='".$session['step']."'")->fetch_assoc();
- $this->sendKeyboard($this->generateQuest(), $array, $array2, $this->attachImage($quest['img']));
- }
- function checkEndExam(){
- global $db;
- $session = $this->getSessionUser();
- if($session['time_end'] <= time()){
- $this->sendMessage("Время вышло. Количество ошибок: ".$session['errors']);
- $db->query("DELETE FROM `session_exams` WHERE `id`='".$session['id']."'");
- if($session['errors'] > 2 AND $session['step'] < 20){
- $this->sendMessage("Вы не сдали экзамен. Попробуйте ещё раз.");
- }else{
- $this->sendMessage("Вы успешно сдали экзамен!");
- }
- }
- }
- function checkAnswerExam(){
- global $db;
- $session = $this->getSessionUser();
- $quest = $db->query("SELECT * FROM `quests` WHERE `ticket`='".$session['ticket']."' AND `number`='".$session['step']."'")->fetch_assoc();
- if($this->data->object->body == $quest['correct']){
- }else{
- $this->sendMessage('Вы ответили неправильно!
- Комментарий:
- '.$quest['explanation']);
- $db->query("UPDATE `session_exams` SET `errors`=`errors`+1 WHERE `id`='".$session['id']."'");
- }
- $db->query("UPDATE `session_exams` SET `step`=`step`+1 WHERE `id`='".$session['id']."'");
- }
- function getTime($start, $end){
- $time = $end - $start;
- $m = intval($time / 60);
- $s = $time - $m * 60;
- return $m.":".$s;
- }
- function attachImage($image){
- $uploadServer = $this->getMessagesUploadServer();
- $link = $uploadServer->response->upload_url;
- $img_path = dirname(__FILE__).'/image/'.$image.'.jpg';
- $cfile = curl_file_create($img_path,'image/jpeg',$image.'.jpg');
- $curl=curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_POST => true,
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_URL => $link,
- CURLOPT_POSTFIELDS => array("photo" => $cfile)
- ));
- $resul_arr = json_decode(curl_exec($curl));
- curl_close($curl);
- $resul_photo = stripslashes($resul_arr->photo);
- $ph = $this->saveMessagesPhoto($resul_photo, $resul_arr->server, $resul_arr->hash);
- if(isset($ph->error)){
- //$VK->sendMessage($ph->error->error_msg.'///'.$resul_arr->server);
- }
- return 'photo'.$ph->response[0]->owner_id.'_'.$ph->response[0]->id;
- }
- function getSessionUser(){
- global $db;
- return $db->query("SELECT * FROM `session_exams` WHERE `id_user`='".$this->data->object->user_id."'")->fetch_assoc();
- }
- function getConfirmationCode(){
- return $this->confirmation_code;
- }
- function checkSubscribe(){
- $sub = json_decode(file_get_contents("https://api.vk.com/method/groups.isMember?group_id=".$this->group_id."&user_id=".$this->data->object->user_id."&v=5.0&extended=1&access_token=".$this->tocken));
- if($sub->response->member == 0){
- return false;
- }else{
- return true;
- }
- }
- function sendMessage($text, $attachment = NULL, $keyboard = NULL){
- $request_params = array(
- 'message' => $text,
- 'user_id' => $this->data->object->user_id,
- 'access_token' => $this->tocken,
- 'v' => '5.0',
- 'attachment' => $attachment,
- 'keyboard' => $keyboard
- );
- $get_params = http_build_query($request_params);
- file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
- }
- function sendKeyboard($message, $buttons, $buttons2 = NULL, $attachment = NULL){
- $key = $this->generateKeyboard($buttons, $buttons2);
- $request_params = array(
- 'message' => $message,
- 'user_id' => $this->data->object->user_id,
- 'access_token' => $this->tocken,
- 'v' => '5.80',
- 'keyboard' => $key,
- 'attachment' => $attachment
- );
- $get_params = http_build_query($request_params);
- file_get_contents('https://api.vk.com/method/messages.send?'. $get_params);
- }
- function generateKeyboard($buttons, $buttons2 = NULL){
- $array = array('one_time' => false, 'buttons' => array($buttons, $buttons2));
- return json_encode($array, JSON_UNESCAPED_UNICODE);
- }
- function getMessagesUploadServer(){
- $get = json_decode(file_get_contents('https://api.vk.com/method/photos.getMessagesUploadServer?access_token='.$this->tocken.'&v=5.69'));
- return $get;
- }
- function saveMessagesPhoto($photo, $server, $hash){
- $params = array('photo' => $photo,
- 'server' => $server,
- 'hash' => $hash,
- 'access_token' => $this->tocken,
- 'v' => '5.67');
- $get_params = http_build_query($params);
- $photo = json_decode(file_get_contents('https://api.vk.com/method/photos.saveMessagesPhoto?'.$get_params));
- return $photo;
- }
- function getBody(){
- return $this->data->object->body;
- }
- function isAttachement(){
- if(isset($this->data->object->attachments[0]->type)){
- return true;
- }else{
- return false;
- }
- }
- function updateUser($quest){
- global $db;
- if(!$this->isUser()){
- $db->query("INSERT INTO `users` SET `id_user`='".$this->data->object->user_id."', `id_quest`='".$quest."'");
- }else{
- $db->query("UPDATE `users` SET `id_quest`='".$quest."' WHERE `id_user`='".$this->data->object->user_id."'");
- }
- }
- function isUser(){
- global $db;
- if($db->query("SELECT `id` FROM `users` WHERE `id_user`='".$this->data->object->user_id."'")->num_rows!=0){
- return true;
- }else{
- return false;
- }
- }
- function checkAnswer(){
- global $db;
- $user = $db->query("SELECT * FROM `users` WHERE `id_user`='".$this->data->object->user_id."'")->fetch_assoc();
- $quest = $db->query("SELECT * FROM `quests` WHERE `id`='".$user['id_quest']."'")->fetch_assoc();
- if($this->data->object->body == $quest['correct']){
- $this->sendMessage('Вы ответили правильно!');
- }else{
- $this->sendMessage('Вы ответили неправильно!
- Комментарий:
- '.$quest['explanation']);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement