function _exec_by_status($status, $folder_id, $user_id, $user_profile_id, $course_id)
 {
     /*
      * We have success by default ;-)
      */
     $this->SetViewVariable('opstat', 'success');
     /*
      * Select the action given by status
      */
     switch ($status) {
         case 'list':
             /*
              * First, we must check our permissions
              */
             if (!$this->obj_data->checkFolderPerms("r", $course_id, $user_profile_id, $folder_id)) {
                 $this->giveControl('main', 'miguel_CMain');
             }
             /*
              * Get/set order list:
              */
             /*
              * Relation among fileds of tables folder and document about order
              */
             $fdOrderRelations = array("id" => array("folder_id", "document.document_id"), "name" => array("folder_name", "document.document_name"), "date" => array("folder_date", "document.date_publish,\r\n\t\t\t\t\t\tdocument.document_name"), "comment" => array("folder_comment", "document.document_comment"), "user_alias" => array("folder_name", "user.user_alias,\r\n\t\t\t\t\t\tdocument.document_name"), "accepted" => array("folder_name", "document.document_accepted,\r\n\t\t\t\t\t\tdocument.document_name"), "mime" => array("folder_name", "document.document_mime,\r\n\t\t\t\t\t\tdocument.document_name"), "actions" => array("folder_perms, foldeer_name", "document.document_name"));
             /*
              * This is somewhat a "hack" but it works and doesn't overload the server.
              * The problem is that we cannot use in_array('orderby', $fdOrderRelations)
              * because..??.. in_array sucks! 
              */
             $fdOrderbyArray = array("id", "name", "date", "comment", "user_alias", "accepted", "mime", "actions");
             $this->setViewVariable('fdOrderbyArray', $fdOrderbyArray);
             if ($this->IsSetVar('orderby', 'orderhow') && in_array($this->getViewVariable('orderby'), $fdOrderbyArray) && ($this->getViewVariable('orderhow') == 0 || $this->getViewVariable('orderhow') == 1)) {
                 $orderby = $this->getViewVariable('orderby');
                 $orderhow = $this->getViewVariable('orderhow');
             } else {
                 $orderby = 'name';
                 $this->setViewVariable('orderby', $orderby);
                 $orderhow = 0;
                 $this->setViewVariable('orderhow', $orderhow);
             }
             /*
              * No real action to execute,
              * but we'll get some data that we'll need in View class
              */
             $this->setViewVariable('FolderList', $this->obj_data->getFolderList($course_id, $folder_id, $user_profile_id, $fdOrderRelations[$orderby][0], $orderhow));
             $this->setViewVariable('FileList', $this->obj_data->getFileList($course_id, $folder_id, $user_profile_id, $user_id, $fdOrderRelations[$orderby][1], $orderhow));
             $this->setViewVariable('FolderProperties', $this->obj_data->getFolderProperties($course_id, $folder_id, $user_profile_id, $user_id));
             $this->setViewVariable('FolderDeeps', $this->obj_data->getFolderDeeps($course_id, $folder_id));
             break;
         case 'send_file':
             /*
              * First, we must check our permissions
              */
             if (!$this->obj_data->checkFilePerms("w", $course_id, $user_id, $user_profile_id, $this->getViewVariable('document_id'))) {
                 $this->giveControl('main', 'miguel_CMain');
             }
             /*
              * We get the junk var of actual file from the BBDD if we're updating
              * instead of creating one
              */
             if ($this->IsSetVar('document_id')) {
                 $this->setViewVariable('document_junk', $this->obj_data->getFileJunk($course_id, $this->getViewVariable('document_id')));
             }
             /*
              * Send a file
              */
             if ($this->IsSetVar('submit') && $_FILES['filename']['tmp_name'] != NULL) {
                 include_once Util::app_Path("filemanager/include/classes/filemanager.class.php");
                 $md5 = md5_file($_FILES['filename']['tmp_name']);
                 if (!$this->getViewVariable('filezip')) {
                     if (!($file_properties = fileManager::uploadFile($_FILES['filename'], $this->getViewVariable('document_junk')))) {
                         $this->SetViewVariable('opstat', 'fserror');
                     }
                     if (!$this->obj_data->insertFile($file_properties['name'], $file_properties['type'], $file_properties['size'], $file_properties['junk'], $md5, $this->getViewVariable('document_comment'), $course_id, $user_id, $folder_id, NULL, $this->getViewVariable('document_id'), $this->getViewVariable('document_accepted'))) {
                         $this->SetViewVariable('opstat', 'dberror');
                     }
                 } else {
                     /*
                      * ZIP files: 
                      */
                     if (!($listUploadFiles = fileManager::uploadFileZip($_FILES['filename']))) {
                         $this->SetViewVariable('opstat', 'fserror');
                     }
                     $listCount = count($listUploadFiles);
                     if (!($folder_zip_id = $this->obj_data->insertFolder($_FILES['filename']['name'], $course_id, $folder_id, $ZipElement['comment'], $this->getViewVariable('shared'), $this->getViewVariable('folder_perms'), NULL))) {
                         $this->SetViewVariable('opstat', 'dberror');
                     }
                     foreach ($listUploadFiles as $ZipElement) {
                         if (!$ZipElement['folder']) {
                             /*
                              * Upload the zip files in the folders they belong to
                              *
                              * $folder_parent_id -> find dirname() in 
                              * $listFolder[ruta_completa] and obtain name and id
                              */
                             $search_folder_name = basename(dirname($ZipElement['stored_filename']));
                             $search_folder_dir = dirname(dirname($ZipElement['stored_filename']));
                             $j = 0;
                             $countList = count($listFolder);
                             $find = false;
                             $folder_parent_id = $folder_zip_id;
                             while ($j < $countList && !$find) {
                                 if ($listFolder[$j][0] == $search_folder_name && $listFolder[$j][1] == $search_folder_dir) {
                                     $folder_parent_id = $listFolder[$j][2];
                                     $find = true;
                                 }
                                 $j++;
                             }
                             $fileJunk = substr(md5(time()), 10, 20);
                             $fileFullPath = Util::app_Path("../var/data/") . basename($ZipElement['stored_filename'] . "-{$fileJunk}");
                             if (!copy($ZipElement['filename'], $fileFullPath)) {
                                 $this->SetViewVariable('opstat', 'fserror');
                             }
                             if (!$this->obj_data->insertFile(basename($ZipElement['stored_filename']), "application/download", $ZipElement['size'], md5_file($fileFullPath), $fileJunk, $ZipElement['comment'], $user_id, $course_id, $folder_id, NULL, NULL, $this->getViewVariable('document_accepted'), 1)) {
                                 $this->SetViewVariable('opstat', 'zipdirdberror');
                             }
                         } else {
                             /*
                              * Recreate the zip dirs structure
                              *
                              * Create a folder list (id, folder_name, folder_full_path)
                              * needed to know the path deepness:
                              * folder_name -> basename(path)
                              * parent_folder_name -> basename(dirname(ruta))
                              */
                             $folder_name = basename($ZipElement['stored_filename']);
                             $folder_dir = dirname($ZipElement['stored_filename']);
                             $folder_parent_id = $folder_zip_id;
                             if ($folder_dir != '.') {
                                 /*
                                  * Find in listFolder if current element
                                  * is contained into other folder.
                                  * In order to do that, it looks for
                                  * folder_name & folder_dir in current
                                  * folder_dir and obtains the id.
                                  */
                                 $search_folder_name = basename($folder_dir);
                                 $search_folder_dir = dirname($folder_dir);
                                 $j = 0;
                                 $countList = count($listFolder);
                                 $find = false;
                                 while ($j < $countList && !$find) {
                                     if ($listFolder[$j][0] == $search_folder_name && $listFolder[$j][1] == $search_folder_dir) {
                                         $folder_parent_id = $listFolder[$j][2];
                                         $find = true;
                                     }
                                     $j++;
                                 }
                             }
                             /*
                              * Finally insert the folder and create a new
                              * element in $listFolder for it
                              */
                             if (!($_folder_id = $this->obj_data->insertFolder($folder_name, $course_id, $folder_parent_id, $ZipElement['comment'], $this->getViewVariable('shared'), $this->getViewVariable('folder_perms'), NULL))) {
                                 $this->SetViewVariable('zipdir', 'error');
                                 $this->SetViewVariable('opstat', 'dberror');
                             }
                             $listFolder[] = array($folder_name, $folder_dir, $_folder_id);
                         }
                     }
                 }
             } else {
                 /*
                  * If form is not being submitted but we're editing a file
                  * then we'll get document vars that we'll need in View
                  */
                 if ($this->IsSetVar('document_id')) {
                     $this->setViewVariable("FileProperties", $this->obj_data->getFileProperties($this->getViewVariable('document_id'), $course_id, $user_profile_id, $user_id));
                 }
             }
             break;
         case 'send_folder':
             /*
              * First, we must check our permissions
              */
             if (!$this->obj_data->checkFolderPerms("w", $course_id, $user_profile_id, $this->getViewVariable('folder_id'))) {
                 $this->giveControl('main', 'miguel_CMain');
             }
             /*
              * Submit a folder (this means either update or create one).
              */
             if ($this->IsSetVar('submit', 'folder_name')) {
                 if (!$this->obj_data->insertFolder($this->getViewVariable('folder_name'), $course_id, $folder_id, $this->getViewVariable('folder_comment'), $this->getViewVariable('shared'), $this->getViewVariable('folder_perms'), $this->getViewVariable('folder_id'))) {
                     $this->SetViewVariable('opstat', 'dberror');
                 }
             } else {
                 /*
                  * If form is not being submitted but we're editing a folder
                  * then we'll get document vars that we'll need in View
                  */
                 if ($this->IsSetVar('folder_id')) {
                     $this->setViewVariable("FolderProperties", $this->obj_data->getFolderProperties($this->getViewVariable('folder_id'), $course_id, $user_profile_id));
                 }
             }
             break;
         case 'send_document':
             /*
              * First, we must check our permissions
              */
             if (!$this->obj_data->checkFilePerms("w", $course_id, $user_id, $user_profile_id, $this->getViewVariable('document_id'))) {
                 $this->giveControl('main', 'miguel_CMain');
             }
             /*
              * We get the junk var of actual file from the BBDD if we're updating
              * instead of creating one
              */
             if ($this->IsSetVar('document_id')) {
                 $this->setViewVariable('document_junk', $this->obj_data->getFileJunk($course_id, $this->getViewVariable('document_id')));
             }
             /*
              * Send a document (that means either create or update a document)
              */
             if ($this->IsSetVar('submit', 'name', 'content', 'type')) {
                 include_once Util::app_Path("filemanager/include/classes/filemanager.class.php");
                 $md5 = md5($this->getViewVariable('document_content'));
                 if (!($file_properties = fileManager::uploadDocument($this->getViewVariable('document_name'), $this->getViewVariable('document_content'), $this->getViewVariable('document_junk')))) {
                     $this->SetViewVariable('opstat', 'fserror');
                 }
                 if (!$this->obj_data->insertFile($file_properties['name'], $file_properties['type'], $file_properties['size'], $file_properties['junk'], $md5, $this->getViewVariable('document_comment'), $course_id, $user_id, $folder_id, $this->getViewVariable('authortool_editable'), $this->getViewVariable('document_id'), $this->getViewVariable('document_accepted'))) {
                     $this->SetViewVariable('opstat', 'dberror');
                 }
             } else {
                 /*
                  * If form is not being submitted but we're editing a file
                  * then we'll get document vars that we'll need in View
                  */
                 if ($this->IsSetVar('document_id')) {
                     $this->setViewVariable("FileProperties", $this->obj_data->getFileProperties($this->getViewVariable('document_id'), $course_id, $user_profile_id, $user_id));
                 }
             }
             break;
         case 'delete':
             /*
              * First we check we have the most inmediate needed var
              */
             if (!$this->IsSetVar('what')) {
                 $this->giveControl('main', 'miguel_CMain');
             } else {
                 switch ($this->getViewVariable('what')) {
                     case 'folder':
                         /*
                          * First, we must check needed var
                          */
                         if (!$this->IsSetVar('folder_id')) {
                             $this->giveControl('main', 'miguel_CMain');
                         }
                         /*
                          * Delete folder
                          */
                         if (!$this->obj_data->deleteFolder($this->getViewVariable('folder_id'), $course_id, $user_profile_id, $user_id)) {
                             $this->SetViewVariable('opstat', 'foldererror');
                         }
                         break;
                     case 'document':
                         /*
                          * First, we must check our permissions
                          */
                         if (!$this->IsSetVar('document_id')) {
                             $this->giveControl('main', 'miguel_CMain');
                         }
                         /*
                          * Delete folder
                          */
                         if (!$this->obj_data->deleteFile($this->getViewVariable('document_id'), $course_id, $user_profile_id, $user_id)) {
                             $this->SetViewVariable('opstat', 'fileerror');
                         }
                         break;
                 }
             }
             break;
     }
 }
 function processPetition()
 {
     /* ----------------- COMPRUEBA EL ACCESO AL MODULO E INICIALIZA --------------- */
     //Se controla que el usuario no tenga acceso.
     $bol_hasaccess = false;
     //Primero comprueba si estamos identificados y si no es asi entonces vamos a ver si es una peticion de autenticacion
     $user_id = $this->getSessionElement('userinfo', 'user_id');
     $course_id = $this->getSessionElement('courseinfo', 'course_id');
     //Para maqueta
     if (empty($course_id)) {
         $course_id = 7;
     }
     if (isset($user_id) && $user_id != '') {
         $bol_hasaccess = true;
         $user = $this->getSessionElement('userinfo', 'user_alias');
     }
     $this->clearNavBarr();
     if ($bol_hasaccess) {
         /* --------------- EXEC FILEMANAGER ACTIONS ----------------- */
         if ($this->issetViewVariable('submit')) {
             /* ----------- NEW FOLDER  -------------- */
             if ($this->issetViewVariable('foldername')) {
                 $this->setViewVariable('folder_id', $this->obj_data->insertFolder($this->getViewVariable('folder_id'), $course_id, $this->getViewVariable('foldername'), $user_id));
             }
             /* -----------  SUBMIT FILE  -------------- */
             if ($_FILES['filename']['tmp_name'] != null) {
                 include_once Util::app_Path("filemanager/include/classes/filemanager.class.php");
                 if (!$this->getViewVariable('filezip')) {
                     fileManager::uploadFile($_FILES['filename']);
                     $this->obj_data->insertFile($_FILES['filename']['name'], $_FILES['filename']['type'], $course_id, $this->getViewVariable('folder_id'), $user_id, $_FILES['filename']['size']);
                 } else {
                     $listUploadFiles = fileManager::uploadFileZip($_FILES['filename']);
                     $listCount = count($listUploadFiles);
                     for ($i = 0; $i < $listCount; $i++) {
                         if (!$listUploadFiles[$i]['folder']) {
                             //$parent_folder_id -> se busca dirname() en $listFolder[ruta_completa] y obtenemos el id y el nombre
                             $this->obj_data->insertFile(basename($listUploadFiles[$i]['stored_filename']), '', $course_id, $this->getViewVariable('folder_id'), $user_id);
                         } else {
                             $this->obj_data->insertFolder($this->getViewVariable('folder_id'), $course_id, $this->getViewVariable('foldername'), $user_id);
                             //Creamos una lista folder - (id - folder_name - folder_ruta_completa), necesaria para mantener la profundidad
                             //folder_name -> basename( ruta ), parent_folder_name -> basename( dirname(ruta) )
                             $listFolder[] = array(basename(dirname($listUploadFiles[$i]['stored_filename'])), $lastIdFolder);
                         }
                     }
                 }
             }
             /* ------------ OTRAS OPERACIONES ARCHIVOS Y DIRECTORIOS CON FORMULARIO INTERMEDIO-------------- */
             /* Eliminar, Renombrar, Comentar, Mover, Hacer visible, Bloquear, Compartir, ¿Actualizar y guardar un log de cambios o control de versiones? */
             if ($this->issetViewVariable('status')) {
                 $status = $this->getViewVariable('status');
                 switch ($status) {
                     case 'rename':
                         if ($this->getViewVariable('tp') == 'f') {
                             $this->obj_data->renameFolder($this->getViewVariable('id'), $this->getViewVariable('newname'));
                         } else {
                             $this->obj_data->renameFile($this->getViewVariable('id'), $this->getViewVariable('newname'));
                         }
                         break;
                     case 'move':
                         if ($this->getViewVariable('tp') == 'f') {
                             $folder_id = $this->getViewVariable('id');
                             $nuevo_destino = $this->getViewVariable('nuevo_destino');
                             if ($folder_id != $nuevo_destino) {
                                 $this->obj_data->moveFolder($folder_id, $nuevo_destino);
                             }
                             $folder_id = null;
                             unset($folder_id);
                             $nuevo_destino = null;
                             unset($nuevo_destino);
                         } else {
                             $this->obj_data->moveFile($course_id, $this->getViewVariable('id'), $this->getViewVariable('folder_id'), $this->getViewVariable('nuevo_destino'));
                         }
                         break;
                 }
                 $status = null;
                 unset($status);
             }
             /* Debería retornar un valor error o estado de la operación y notificarlo en la carga del módulo */
         }
         /* ------------------ OPERACIONES REALIZADAS DIRECTAMENTE ---------*/
         if ($this->issetViewVariable('status')) {
             $status = $this->getViewVariable('status');
             switch ($status) {
                 case 'del':
                     if ($this->getViewVariable('tp') == 'f') {
                         $this->obj_data->deleteFolder($course_id, $this->getViewVariable('id'));
                     } else {
                         $this->obj_data->deleteFile($this->getViewVariable('id'), $this->getViewVariable('folder_id'), $course_id);
                     }
                     break;
                 case 'visible':
                     if ($this->getViewVariable('tp') == 'f') {
                         $this->obj_data->visibleElement($this->getViewVariable('id'), 1, 'folder');
                     } else {
                         $this->obj_data->visibleElement($this->getViewVariable('id'), 1, 'document');
                     }
                     break;
                 case 'invisible':
                     if ($this->getViewVariable('tp') == 'f') {
                         $this->obj_data->visibleElement($this->getViewVariable('id'), 0, 'folder');
                     } else {
                         $this->obj_data->visibleElement($this->getViewVariable('id'), 0, 'document');
                     }
                     break;
                 case 'lock':
                     $this->obj_data->lockDocument($this->getViewVariable('id'), 1);
                     break;
                 case 'unlock':
                     $this->obj_data->lockDocument($this->getViewVariable('id'), 0);
                     break;
                 case 'share':
                     $this->obj_data->shareDocument($this->getViewVariable('id'), 1);
                     break;
                 case 'unshare':
                     $this->obj_data->shareDocument($this->getViewVariable('id'), 0);
                     break;
             }
         }
         /* -------- SET CURRENT FOLDER, IF EMPTY BY DEFAULT FOLDER_ID = 0 ------------- */
         if ($this->issetViewVariable("folder_id") != "") {
             $current_folder_id = $this->getViewVariable("folder_id");
         } else {
             //$current_folder_id = $this->obj_data->getFolderId($course_id);//0;
             $current_folder_id = 0;
         }
         /* ----------- DISPLAY FOLDER CONTENT --------------- */
         $current_folder_info = $this->obj_data->getFolderName($current_folder_id);
         $this->setViewVariable('current_folder_name', $current_folder_info[0]['folder_name']);
         $this->setViewVariable('folder_parent_id', $current_folder_info[0]['folder_parent_id']);
         $this->setViewVariable('arr_files', $this->obj_data->getFileList($course_id, $current_folder_id));
         $this->setViewVariable('arr_folders', $this->obj_data->getFolderList($course_id, $current_folder_id));
         $this->setViewVariable('folder_id', $current_folder_id);
         $operation_id = $this->getViewVariable('operation_id');
         $this->setViewVariable('operation_id', $operation_id);
         if ($operation_id == 'move') {
             $this->obj_data->getFolderTree($course_id, $result, 0, 0);
             $this->setViewVariable('folderTree', $result);
         }
         $this->setViewVariable('user_id', $user_id);
         $this->setViewVariable('profile_id', $this->getSessionElement('userinfo', 'profile_id'));
         $this->setCacheFile("miguel_VFileManager" . $this->getSessionElement("userinfo", "user_id"));
         //$this->setMessage(agt("miguel_fileManager"));
         $this->setPageTitle("miguel_fileManager");
     } else {
         $this->giveControl('main', 'miguel_CMain');
     }
     $this->addNavElement(Util::format_URLPath('filemanager/index.php'), agt("Mis documentos"));
     $this->setCacheFlag(true);
     $this->setHelp("EducFileManager");
 }