Skip to content

Commit

Permalink
Save a Single Question as csv
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Dec 9, 2023
1 parent 8ec7f06 commit bde8b86
Show file tree
Hide file tree
Showing 7 changed files with 553 additions and 12 deletions.
32 changes: 32 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@
'apiVersion' => 'v2(\.[1-2])?'
]
],
[
'name' => 'api#exportQuestion',
'url' => '/api/{apiVersion}/submissions/exportQuestion/{questionId}',
'verb' => 'GET',
'requirements' => [
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#exportSubmission',
'url' => '/api/{apiVersion}/submissions/exportSubmission/{submissionId}',
'verb' => 'GET',
'requirements' => [
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#exportSubmissionsToCloud',
'url' => '/api/{apiVersion}/submissions/export',
Expand All @@ -299,6 +315,22 @@
'apiVersion' => 'v2(\.[1-2])?'
]
],
[
'name' => 'api#exportQuestionToCLoud',
'url' => '/api/{apiVersion}/submissions/exportQuestion',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#exportSubmissionToCloud',
'url' => '/api/{apiVersion}/submissions/exportSubmission',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2.2'
]
],
[
'name' => 'api#deleteAllSubmissions',
'url' => '/api/{apiVersion}/submissions/{formId}',
Expand Down
53 changes: 53 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,33 @@ Returns all submissions to the form in form of a csv-file.
"jonas","Friday, January 22, 2021 at 12:47:29 AM GMT+0:00","Option 2","Answer"
"jonas","Friday, January 22, 2021 at 12:45:57 AM GMT+0:00","Option 3","NextAnswer"
```
### Get a single Submission as csv (Download)
Returns one submission in form of a csv-file.
- Endpoint: `/api/v2.2/submissions//exportSubmission/{submissionId}`
- Url-Parameter:
| Parameter | Type | Description |
|-----------|---------|-------------|
| _submissionId_ | Integer | Id of the submission |
- Method: `GET`
- Response: A Data Download Response containg the headers `Content-Disposition: attachment; filename="Form 1 (responses).csv"` and `Content-Type: text/csv;charset=UTF-8`. The actual data contains all submissions to the referred form, formatted as comma separated and escaped csv.
```
"User display name","Timestamp","Question 1","Question 2"
"jonas","Friday, January 22, 2021 at 12:47:29 AM GMT+0:00","Option 2","Answer"
```
### Get a single Question as csv (Download)
Returns one question in form of a csv-file.
- Endpoint: `/api/v2.2/submissions//exportQuestion/{questionId}`
- Url-Parameter:
| Parameter | Type | Description |
|-----------|---------|-------------|
| _questionId_ | Integer | Id of the question |
- Method: `GET`
- Response: A Data Download Response containg the headers `Content-Disposition: attachment; filename="Form 1 (responses).csv"` and `Content-Type: text/csv;charset=UTF-8`. The actual data contains all submissions to the referred form, formatted as comma separated and escaped csv.
```
"User display name","Timestamp","Question 1"
"jonas","Friday, January 22, 2021 at 12:47:29 AM GMT+0:00","Option 2"
"jonas","Friday, January 22, 2021 at 12:45:57 AM GMT+0:00","Option 3"
```

### Export Submissions to Cloud (Files-App)
Creates a csv file and stores it to the cloud, resp. Files-App.
Expand All @@ -539,6 +566,32 @@ Creates a csv file and stores it to the cloud, resp. Files-App.
```
"data": "Form 2 (responses).csv"
```
### Export Submission to Cloud (Files-App)
Creates a csv file of one submission and stores it to the cloud, resp. Files-App.
- Endpoint: `/api/v2.2/submissions/exportSubmission`
- Method: `POST`
- Parameters:
| Parameter | Type | Description |
|-----------|---------|-------------|
| _submissionId_ | Integer | Id of the submission |
| _path_ | String | Path within User-Dir, to store the file to |
- Response: Stores the file to the given path and returns the fileName.
```
"data": "Form 2 (responses).csv"
```
### Export Question to Cloud (Files-App)
Creates a csv file of one question answers and stores it to the cloud, resp. Files-App.
- Endpoint: `/api/v2.2/submissions/exportQuestion`
- Method: `POST`
- Parameters:
| Parameter | Type | Description |
|-----------|---------|-------------|
| _questionId_ | Integer | Id of the question |
| _path_ | String | Path within User-Dir, to store the file to |
- Response: Stores the file to the given path and returns the fileName.
```
"data": "Form 2 (responses).csv"
```

### Delete Submissions
Delete all Submissions to a form
Expand Down
155 changes: 154 additions & 1 deletion lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,72 @@ public function exportSubmissions(string $hash): DataDownloadResponse {
$csv = $this->submissionService->getSubmissionsCsv($hash);
return new DataDownloadResponse($csv['data'], $csv['fileName'], 'text/csv');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Export submissions of a specified Question
*
* @param int $questionId of the question
* @return DataDownloadResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function exportQuestion(int $questionId): DataDownloadResponse {
$this->logger->debug('Export submissions for Question: {questionId}', [
'questionId' => $questionId,
]);

try {
$question = $this->questionMapper->findById($questionId);
$formId = $question->getFormId();
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find question');
throw new OCSBadRequestException();
}

if (!$this->formsService->canSeeResults($form)) {
$this->logger->debug('The current user has no permission to get the results for this form');
throw new OCSForbiddenException();
}

$csv = $this->submissionService->getQuestionCsv($formId, $questionId);

Check failure on line 1129 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1129:51: InvalidArgument: Argument 1 of OCA\Forms\Service\SubmissionService::getQuestionCsv expects string, but int provided (see https://psalm.dev/004)

Check failure on line 1129 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1129:60: InvalidArgument: Argument 2 of OCA\Forms\Service\SubmissionService::getQuestionCsv expects string, but int provided (see https://psalm.dev/004)
return new DataDownloadResponse($csv['data'], $csv['fileName'], 'text/csv');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Export a single submission
*
* @param int $submissionId of the submission
* @return DataDownloadResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function exportSubmission(int $submissionId): DataDownloadResponse {
$this->logger->debug('Export submission: {submissionId}', [
'submissionId' => $submissionId,
]);

try {
$submission = $this->submissionMapper->findById($submissionId);
$formId = $submission->getFormId();
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find submission');
throw new OCSBadRequestException();
}

if (!$this->formsService->canSeeResults($form)) {
$this->logger->debug('The current user has no permission to get the results for this form');
throw new OCSForbiddenException();
}

$csv = $this->submissionService->getSubmissionCsv($formId, $submissionId);

Check failure on line 1162 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1162:53: InvalidArgument: Argument 1 of OCA\Forms\Service\SubmissionService::getSubmissionCsv expects string, but int provided (see https://psalm.dev/004)
return new DataDownloadResponse($csv['data'], $csv['fileName'], 'text/csv');
}
/**
* @CORS
* @NoAdminRequired
Expand Down Expand Up @@ -1129,12 +1194,100 @@ public function exportSubmissionsToCloud(string $hash, string $path) {

// Write file to cloud
try {
$fileName = $this->submissionService->writeCsvToCloud($hash, $path);
$csvData = $this->submissionService->getSubmissionsCsv($hash);
$fileName = $this->submissionService->writeCsvToCloud($csvData, $path);
} catch (NotPermittedException $e) {
$this->logger->debug('Failed to export Submissions: Not allowed to write to file');
throw new OCSException('Not allowed to write to file.');
}

return new DataResponse($fileName);
}
/**
* @CORS
* @NoAdminRequired
*
* Export Submission to the Cloud
*
* @param int $submissionId of the submission
* @param string $path The Cloud-Path to export to
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function exportSubmissionToCloud(int $submissionId, string $path) {
$this->logger->debug('Export submission: {submissionId} to Cloud at: /{path}', [
'$submissionId' => $submissionId,
'path' => $path,
]);

try {
$submission = $this->submissionMapper->findById($submissionId);
$formId = $submission->getFormId();
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find submission');
throw new OCSBadRequestException();
}

if (!$this->formsService->canSeeResults($form)) {
$this->logger->debug('The current user has no permission to get the results for this form');
throw new OCSForbiddenException();
}

// Write file to cloud
try {
$csvData = $this->submissionService->getSubmissionCsv($formId, $submissionId);

Check failure on line 1240 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1240:58: InvalidArgument: Argument 1 of OCA\Forms\Service\SubmissionService::getSubmissionCsv expects string, but int provided (see https://psalm.dev/004)
$fileName = $this->submissionService->writeCsvToCloud($csvData, $path);
} catch (NotPermittedException $e) {
$this->logger->debug('Failed to export Submission: Not allowed to write to file');
throw new OCSException('Not allowed to write to file.');
}

return new DataResponse($fileName);
}

/**
* @CORS
* @NoAdminRequired
*
* Export Question to the Cloud
*
* @param int $questionId of the question
* @param string $path The Cloud-Path to export to
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function exportQuestionToCLoud(int $questionId, string $path) {
$this->logger->debug('Export question : {questionId} to Cloud at: /{path}', [
'questionId' => $questionId,
'path' => $path,
]);

try {
$question = $this->questionMapper->findById($questionId);
$formId = $question->getFormId();
$form = $this->formMapper->findById($formId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find submission');
throw new OCSBadRequestException();
}

if (!$this->formsService->canSeeResults($form)) {
$this->logger->debug('The current user has no permission to get the results for this form');
throw new OCSForbiddenException();
}

// Write file to cloud
try {
$csvData = $this->submissionService->getQuestionCsv($formId, $questionId);

Check failure on line 1284 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1284:56: InvalidArgument: Argument 1 of OCA\Forms\Service\SubmissionService::getQuestionCsv expects string, but int provided (see https://psalm.dev/004)

Check failure on line 1284 in lib/Controller/ApiController.php

View workflow job for this annotation

GitHub Actions / Static analysis

InvalidArgument

lib/Controller/ApiController.php:1284:65: InvalidArgument: Argument 2 of OCA\Forms\Service\SubmissionService::getQuestionCsv expects string, but int provided (see https://psalm.dev/004)
$fileName = $this->submissionService->writeCsvToCloud($csvData, $path);
} catch (NotPermittedException $e) {
$this->logger->debug('Failed to export Question: Not allowed to write to file');
throw new OCSException('Not allowed to write to file.');
}

return new DataResponse($fileName);
}
}
23 changes: 23 additions & 0 deletions lib/Db/AnswerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ public function findBySubmission(int $submissionId): array {
return $this->findEntities($qb);
}

/**
* @param int $submissionId
* @param int $questionId
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
* @return Answer[]
*/

public function findBySubmissionAndQuestionId(int $submissionId, int $questionId): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('submission_id', $qb->createNamedParameter($submissionId, IQueryBuilder::PARAM_INT))
)
->andWhere(
$qb->expr()->eq('question_id', $qb->createNamedParameter($questionId, IQueryBuilder::PARAM_INT))

);

return $this->findEntities($qb);
}

/**
* @param int $submissionId
*/
Expand Down
Loading

0 comments on commit bde8b86

Please sign in to comment.