Пример #1
0
 public function ussdProgress($user, $message)
 {
     $menu_item_id = $user->menu_item_id;
     // print_r($menu_item_id);
     // exit;
     $step = $user->step;
     $menuModel = new Model_Menu();
     $menu = $menuModel->getMenu($menu_item_id);
     // print_r($menu);
     // exit;
     if ($menu['id'] == 0) {
         $reply = "We could not understand your response";
         $output = $this->mainMenu($user, $user['phoneno']);
         return $reply . PHP_EOL . $output;
     }
     //
     $menuType = $menu->type;
     // print_r($menuType);
     // exit;
     // print_r($step);
     // exit;
     if ($menuType == 2) {
         //no verification but feed it response and update step as you move on
         if ($step > 0) {
             $message = strtolower($message);
             //feed the response to the table
             // print_r($message);
             // exit;
             $valid = 1;
             $valid = $this->validateResponse($menu->id, $message, $step);
             // print_r($valid);
             // exit;
             if (is_array($valid) && $valid['valid'] == 0) {
                 $output = $valid['message'];
                 //print_r($output);
                 return $output;
             }
             $dataa = array('user_id' => $user['id'], 'menu_id' => $menu_item_id, 'step' => $step, 'response' => $message);
             //print_r($dataa);
             //exit;
             $Response_Model = new Model_Response();
             $result = $Response_Model->createResponse($dataa);
             // print_r($result);
             // exit;
             if ($result) {
                 //check if we have another step, if we do, request, if we dont, compile the summary and confirm
                 $next_step = $step + 1;
                 // print_r($step);
                 // exit;
                 $menuItemsModel = new Model_MenuItems();
                 $menuItem = $menuItemsModel->getNextMenuStep($menu_item_id, $next_step);
                 // print_r($menuItem);
                 // exit;
                 if ($menuItem['id'] != 0) {
                     //update user data and next request and send back
                     $userModel = new Model_User();
                     $result = $userModel->updateUserMenuStep($user['id'], $next_step);
                     //$menuItem = $menuItemsModel -> getNextMenuStep($menu_item_id,$step);
                     return $menuItem->description;
                     //exit;
                 } else {
                     //compile summary for confirmation
                     //$menuItemsModel = new Model_MenuItems();
                     $MenuItems = $menuItemsModel->getMenuItems($menu_item_id);
                     //print_r($MenuItems);
                     //exit;
                     //build up the responses
                     $confirmation = "Confirm?: " . $menu->description;
                     foreach ($MenuItems as $key => $value) {
                         // print_r($value);
                         // exit;
                         //print_r($confirmation);
                         //exit;
                         //select the corresponding response
                         if ($value[4] != 'PIN') {
                             $phrase = $value[4];
                             //print_r($value[4]);
                             //$menu_item_id = $value['']
                             $response = $Response_Model->getResponse($user['id'], $menu_item_id, $value[3]);
                             //print_r($response['response']."<br>");
                             if ($value[4] == 'Gender') {
                                 if ($response['response'] == 1) {
                                     $response['response'] = 'Male';
                                 } elseif ($response['response'] == 2) {
                                     $response['response'] = 'Female';
                                 }
                             } elseif ($value[4] == 'Language') {
                                 if ($response['response'] == 1) {
                                     $response['response'] = 'English';
                                 } elseif ($response['response'] == 2) {
                                     $response['response'] = 'Kiswahili';
                                 }
                             } elseif ($value[4] == 'Card') {
                                 if ($response['response'] == 1) {
                                     $response['response'] = 'Bebapay';
                                 } elseif ($response['response'] == 2) {
                                     $response['response'] = 'Abiria';
                                 } elseif ($response['response'] == 3) {
                                     $response['response'] = '1963';
                                 }
                             }
                             $confirmation = $confirmation . PHP_EOL . $value[4] . ": " . $response['response'];
                             //print_r($response['response']);
                             //exit;
                         }
                     }
                     // print_r($confirmation);
                     // exit;
                     //update the status to waiting for confirmation
                     $userModel = new Model_User();
                     $data = array('session' => 2, 'confirm_from' => $menu->id);
                     //print_r($user['id']);
                     //exit;
                     $result = $userModel->updateUserData($data, $user['id']);
                     //print_r($result);
                     //exit;
                     //$result = $userModel -> updateUserSession($user['id'],2);
                     return $confirmation . PHP_EOL . "1. Yes" . PHP_EOL . "2. No";
                     //exit;
                 }
                 //print_r($menuItem);
                 //exit;
             }
         } else {
             //when the user has not started the steps
             $userModel = new Model_User();
             $result = $userModel->updateUserMenuStep($user['id'], 1);
             $menuItemsModel = new Model_MenuItems();
             $menuItem = $menuItemsModel->getNextMenuStep($menu_item_id, 1);
             return $menuItem->description;
             //exit;
         }
     } else {
         //verify response
         $menuItemsModel = new Model_MenuItems();
         $MenuItems = $menuItemsModel->getMenuItems($menu_item_id);
         //now we have the menu items it is time to create the USSD Menu
         // print_r($message);
         // exit;
         if (empty($MenuItems)) {
             $selected_option = $message;
         } else {
             // echo "hapa";
             // exit;
             $message = strtolower($message);
             // print_r($message);
             // exit;
             foreach ($MenuItems as $key => $value) {
                 //check if they are equal
                 // print_r($value);
                 // exit;
                 if (trim(strtolower($key)) == $message) {
                     //foreach ($value as $key1 => $value1) {
                     $selected_option = $key;
                     $next_menu_id = $value[2];
                     //check for the type of menu_item_id
                     if ($value[5] == 3) {
                         //survey
                         $output = $this->takeSurvey($user, $message);
                         return $output;
                     }
                 }
                 if (empty($selected_option)) {
                     foreach ($value as $key1 => $value1) {
                         //print_r($value1);
                         //exit;
                         if (trim(strtolower($value1)) == $message) {
                             $selected_option = $value1;
                         }
                     }
                 }
             }
         }
         // print_r($selected_option);
         // exit;
         //
         if (empty($selected_option)) {
             $selected_option = 0;
         }
         if (empty($next_menu_id)) {
             $next_menu_id = 0;
         }
         //update the progress with the next menu id
         $userModel = new Model_User();
         $no = $user['phoneno'];
         $result = $userModel->updateUser($next_menu_id, $no);
         if ($result) {
             $menuModel = new Model_Menu();
             $reply = "";
             //print_r($user);
             //exit;
             if ($next_menu_id == 0) {
                 //if ($menu['id'] == 0) {
                 $reply = "We could not understand your response";
                 $next_menu_id = $user['menu_item_id'];
                 //$output = $this->mainMenu($user, $user['phoneno']);
                 //return $reply.PHP_EOL.$output;
                 //}
             }
             //print_r($next_menu_id);
             //exit;
             $menu = $menuModel->getMenu($next_menu_id);
             //print_r($menu);
             //exit;
             $menu_id = $menu['id'];
             $menuType = $menu['type'];
             // print_r($step);
             // exit;
             if ($menuType == 2) {
                 //$menu
                 // print_r($menu);
                 // exit;
                 $output = $this->singleProcess($menu_id, $user);
                 //$output = $this->singleProcess($menu_item_id);
                 return $output;
             } else {
                 //get the description
                 $description = $menu->description;
                 //build up the options
                 $menuItemsModel = new Model_MenuItems();
                 $menuOptions = $menuItemsModel->getMenuOptions($menu_id);
                 return $description . PHP_EOL . $menuOptions;
                 //exit;
             }
         }
     }
 }