Skip to content

Commit

Permalink
Subfolder support for file uploads into workspace
Browse files Browse the repository at this point in the history
Files can now be uploaded into subfolders of the personal workspace.
  • Loading branch information
disc5 committed Apr 26, 2016
1 parent f7e078d commit cc407e6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ protected function setErrorHandlers() {
});
}

protected function determineDefaultIliasClient() {
require_once("./Services/Init/classes/class.ilIniFile.php");
$ilIliasIniFile = new \ilIniFile("./ilias.ini.php");
$ilIliasIniFile->read();
return $ilIliasIniFile->readVariable("clients","default");
}



/**
* Constructor: RESTController($appDirectory, $userSettings)
Expand Down Expand Up @@ -190,6 +198,10 @@ public function __construct($appDirectory, array $userSettings = array()) {

// Apped useful information to (global) slim-environment
$env = $this->environment();

/*if (defined('CLIENT_ID') == false) {
define('CLIENT_ID', $this->determineDefaultIliasClient());
}*/
$env['client_id'] = CLIENT_ID;
$env['app_directory'] = $appDirectory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public function clone_file_into_repository($user_id, $file_id, $ref_id_repositor
* @param array $file_upload An array containing the file upload parameters of a single file.
* @param int $user_id The reference id of a repository object where the uploaded file will be associated to.
* @param int $owner_id The user_id of the owner of the file if available. Default: anonymous
* @param string $destinationFolderStr name of the subfolder to which the files should be uploaded.
* @return object The response object.
*/
function handleFileUploadIntoMyFileSpace($file_upload, $user_id, $owner_id = 13)
function handleFileUploadIntoMyFileSpace($file_upload, $user_id, $owner_id = 13, $destinationFolderStr = "abc")
{
define('IL_VIRUS_SCANNER', 'None');
// The following constants are normally set by class.ilInitialisation.php->initClientInitFile()
Expand Down Expand Up @@ -129,7 +130,7 @@ function handleFileUploadIntoMyFileSpace($file_upload, $user_id, $owner_id = 13)
$fileObj->setFileType(\ilMimeTypeUtil::getMimeType('', $filename, $type));
$fileObj->setFileSize($size);
$object_id = $fileObj->create();
$this->putObjectInMyFileSpaceTree($fileObj, $user_id);
$this->putObjectInMyFileSpaceTree($fileObj, $user_id, $destinationFolderStr);

// upload file to filesystem
$fileObj->createDirectory();
Expand All @@ -148,14 +149,15 @@ function handleFileUploadIntoMyFileSpace($file_upload, $user_id, $owner_id = 13)
*
* @param ilObject $a_obj
* @param int $user_id
* @param string $destinationFolderStr
*/
protected function putObjectInMyFileSpaceTree($a_obj, $user_id)
protected function putObjectInMyFileSpaceTree($a_obj, $user_id, $destinationFolderStr)
{
Libs\RESTilias::initGlobal('rbacreview', 'ilRbacReview', './Services/AccessControl/classes/class.ilRbacReview.php');
Libs\RESTilias::initGlobal('rbacadmin', 'ilRbacAdmin', './Services/AccessControl/classes/class.ilRbacAdmin.php');
//ilInitialisation::initAccessHandling();
global $rbacreview, $ilUser, $objDefinition;
//global $ilLog;
global $ilLog;

include_once 'Services/PersonalWorkspace/classes/class.ilWorkspaceTree.php';
$tree = new \ilWorkspaceTree($user_id);
Expand All @@ -164,9 +166,46 @@ protected function putObjectInMyFileSpaceTree($a_obj, $user_id)
$tree->createTreeForUser($user_id);
}

$obj_id = $a_obj->getId();
$ref_id = $tree->insertObject($tree->getRootId(),$obj_id);
//$a_obj->setPermissions($a_parent_node_id);
if ($destinationFolderStr!='') { // create folder if necessary
include_once 'Modules/WorkspaceFolder/classes/class.ilObjWorkspaceFolder.php';
include_once 'Services/Object/classes/class.ilObjectFactory.php';
$factory = new \ilObjectFactory();
$aNodeIdsFolders = $tree->getObjectsFromType('wfld');
$destFolderExists = false;
foreach ($aNodeIdsFolders as $nodeId) {
$ilLog->write("Checking personal workspace folders. Found Node_ID ".$nodeId."");
$folder_obj_id = $tree->lookupObjectId($nodeId);
$folder_obj = $factory->getInstanceByObjId($folder_obj_id);
$title = $folder_obj->getTitle();
if ($title == $destinationFolderStr) {
$ilLog->write("Folder $destinationFolderStr found.");
$folder_obj_node_id = $nodeId;
$destFolderExists = true;
break;
}
}

if ($destFolderExists == false) {
// Create new subfolder
$ilLog->write("Folder $destinationFolderStr not found. Creating it ...");
$folderObj = new \ilObjWorkspaceFolder();
$folderObj->setOwner($user_id);
$folderObj->setTitle($destinationFolderStr);
$folderObj->setDescription("");
$folder_obj_id = $folderObj->create();
$folder_obj_node_id = $tree->insertObject($tree->getRootId(),$folder_obj_id);
}

// Add file to the subfolder
$obj_id = $a_obj->getId();
$ref_id = $tree->insertObject($folder_obj_node_id,$obj_id);
} else {
// Add file to the root folder
$obj_id = $a_obj->getId();
$ref_id = $tree->insertObject($tree->getRootId(),$obj_id);
//$a_obj->setPermissions($a_parent_node_id);
}


// BEGIN ChangeEvent: Record save object.
require_once('Services/Tracking/classes/class.ilChangeEvent.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use \RESTController\core\auth as Auth;
use \RESTController\libs as Libs;
use \RESTController\libs\Exceptions as Exceptions;
use \RESTController\extensions\admin as Admin;
use \RESTController\extensions\admin_v1 as Admin;
use \RESTController\extensions\files_v1 as Files;


Expand Down Expand Up @@ -56,7 +56,7 @@
if ($status == false) {
$app->halt(500, 'File could not be copied!');
} else {
$app->success("Moved item from personal file space to repository.");
$app->success(array('msg' => 'Moved item from personal file space to repository.'));
}

} catch(Exceptions\MissingParameter $e) {
Expand Down Expand Up @@ -97,7 +97,7 @@
*/
$app->get('/myfilespaceupload', RESTAuth::checkAccess(RESTAuth::PERMISSION), function() use ($app) {
$app->log->debug('Myfilespace upload via GET');
$app->halt(422, 'Pls use the POST method', 'RESTController\\extensions\\mobile_v1\\MyFileSpaceRoutes::ID_USE_GET');
$app->halt(422, 'Please use the POST method', 'RESTController\\extensions\\mobile_v1\\MyFileSpaceRoutes::ID_USE_GET');
});


Expand All @@ -123,7 +123,7 @@
// Try to upload file
Libs\RESTilias::initAccessHandling();
$model = new Files\PersonalFileSpaceModel();
$resp = $model->handleFileUploadIntoMyFileSpace($_FILES['uploadfile'],$user_id,$user_id);
$resp = $model->handleFileUploadIntoMyFileSpace($_FILES['uploadfile'],$user_id,$user_id, 'Mobile Uploads');

$result = array('id' => $resp->id, 'msg' => "File uploaded to the personal file space.");
$app->success($result);
Expand All @@ -150,7 +150,7 @@
$app->halt(400, $e->getFormatedMessage(), $e::ID);
}

$app->success("Deleted file from personal file space.");
$app->success(array('msg'=>'Deleted file from personal file space.'));
});

});

0 comments on commit cc407e6

Please sign in to comment.