/**
  * @param $countryID
  * @return array
  * @throws \CPS_Exception
  * @throws \Exception
  * @throws null
  */
 public function getOnlineUsers($countryID)
 {
     $query = CPS_Term($countryID, 'country');
     $list = array('id' => 'yes', "x" => 'yes', "y" => 'yes', "about" => "yes", "email" => "yes");
     try {
         $documents = $this->cpsSimple->search($query, NULL, NULL, $list);
     } catch (\Exception $e) {
         return -2;
     }
     $results = [];
     foreach ($documents as $id => $document) {
         $tempResult = [];
         $tempResult["x"] = $document->x->__toString();
         $tempResult["y"] = $document->y->__toString();
         $tempResult["description"] = $document->about->__toString();
         $tempResult["calls_count"] = 0;
         $tempResult["receive_calls_count"] = 0;
         $tempResult["rate"] = 0;
         $tempResult["minutes_spoken"] = 0;
         if (isset($document->conversations->conversation)) {
             //-----------------------------SUM CONVERSATION OUTGOING----------------
             $count = 0;
             foreach ($document->conversations->conversation as $conversation) {
                 if ($conversation->is_incoming == 0) {
                     $tempResult["calls_count"]++;
                     $tempResult["rate"] += $conversation->rate;
                 }
                 $tempResult["minutes_spoken"] += $conversation->duration;
                 $count++;
             }
             //-----------------------------SUM CONVERSATION INCOMMING----------------
             $tempResult["receive_calls_count"] = $count - $tempResult["calls_count"];
             //-----------------------------AVG OF RATE CONVERSATION ----------------
             if ($count > 0) {
                 $tempResult["rate"] = $tempResult["rate"] / $count;
             }
         }
         $tempResult["countries_to"] = ["IR"];
         //TODO make it more general
         $tempResult["languages"] = Converter::XML2Array($document->languages)["languages"];
         $results[] = $tempResult;
     }
     return $results;
 }
 public function generatePin()
 {
     $path = storage_path() . "/data/countries.json";
     // ie: /var/www/laravel/app/storage/json/filename.json
     if (!\File::exists($path)) {
         throw new \Exception("Invalid File");
     }
     $file = \File::get($path);
     // string
     $fileArray = json_decode($file);
     foreach ($fileArray as $key => $countries) {
         $temp = [];
         $temp["uuid"] = \Rhumsaa\Uuid\Uuid::uuid4()->toString();
         $temp["full_name"] = "Arsalan";
         $temp["email"] = "*****@*****.**";
         $temp["password"] = 12345678;
         $temp["is_present"] = true;
         $temp["about"] = "I have a bachelors and I love Sports";
         $temp["created_at"] = date("Y-m-d H:i:s");
         $temp["updated_at"] = date("Y-m-d H:i:s");
         $temp["last_activity"] = date("Y-m-d H:i:s");
         $temp["country"] = $countries->{"iso-code"};
         $temp["type"] = "user";
         $temp["languages"] = ["language" => "English"];
         $temp["conversations"] = "";
         $temp["x"] = $countries->x;
         $temp["y"] = $countries->y;
         $fileArray[$key] = $temp;
     }
     $xml = new \SimpleXMLElement('<users/>');
     Libraries\Converter::Array2XML($fileArray, $xml);
     //dd($xml);
     return \Response::make($xml->asXML(), '200')->header('Content-Type', 'text/xml');
     // Verify Validate JSON?
     // Your other Stuff
 }