/**
  * Function to send documents to Ephorus
  */
 function handInService()
 {
     $handin_code = DLEApi::getSetting('handin_code');
     if ($handin_code == '') {
         $this->log('Handin code not found');
         $this->trace('... No hand-in code found. Sending of documents terminated.');
         return;
     }
     $this->trace('... Hand-in code: ' . $handin_code);
     $handin_address = DLEApi::getSetting('handin_address');
     if ($handin_address == '') {
         $this->log('Handin address not found');
         $this->trace('... No hand-in address found. Sending of documents terminated.');
         return;
     }
     $this->trace('... Hand-in address: ' . $handin_address);
     $unsent_documents = DLEApi::getUnsentDocuments();
     if (count($unsent_documents) == 0) {
         $this->trace('... No documents to be send.');
         return;
     }
     $this->trace('... ' . count($unsent_documents) . ' Documents found. Start sending documents');
     $soap_client = $this->initSoapClient($handin_address, 'handin');
     if ($soap_client == false) {
         $this->trace('... Connection problem.');
         return;
     }
     foreach ($unsent_documents as $document) {
         // Make sure the document has enough time to be sent to Ephorus.
         set_time_limit(600);
         $this->log('Started hand-in document - ' . $document->filename);
         if (!$this->isSupportedFiletype($document->filename)) {
             $this->log('Cancelled hand-in document - Wrong filetype.');
             $this->trace('... Error with document: ' . $document->filename . ' (' . $document->id . ')');
             DLEApi::setHandinErrorToDocument($document->id, 'unsupported_file_type');
             continue;
         }
         $handin_parameters = DLEApi::getHandinParameters($document);
         if (!$handin_parameters) {
             $this->log('Cancelled hand-in document - Wrong parameters.');
             continue;
         }
         $this->trace('... POST DOCUMENT (' . $document->id . '): ' . $document->filename . ' with processtype ' . $document->processtype);
         try {
             $result = $soap_client->UploadDocument($handin_parameters);
             if ($result == false) {
                 $this->log('Something went wrong while sending document');
                 $this->trace('... Something went wrong while sending');
             } else {
                 $guid = $result->UploadDocumentResult;
                 $this->log('Document (' . $document->id . ') sent successfully - Guid: ' . $guid);
                 $this->trace('- Succes! GUID: ' . $guid);
                 // Update the documents table with the GUID.
                 DLEApi::setGUIDtoDocument($document->id, $guid);
             }
         } catch (SoapFault $result) {
             $this->log('Document (' . $document->id . ') sent. error received - Error: ' . $result->getMessage());
             $this->trace('- Error. ' . $result->getMessage());
             if (strpos($result->getMessage(), 'Wrong file format.') !== false) {
                 DLEApi::setHandinErrorToDocument($document->id, 'unsupported_file_type');
             } else {
                 if (strpos($result->getMessage(), 'Hand In code not found') === false) {
                     DLEApi::setHandinErrorToDocument($document->id, 'unknown_file_error');
                 }
             }
         }
         $this->log('Finished hand-in document (' . $document->id . ') - ' . $document->filename);
         unset($document);
         unset($result);
     }
     $this->trace('... All documents have been sent.');
 }