2019-05-14 01:15:45 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
|
|
|
*
|
|
|
|
* @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com>
|
|
|
|
* @author René Gieling <github@dartcafe.de>
|
|
|
|
* @author Inigo Jiron <ijiron@terpmail.umd.edu>
|
|
|
|
* @author Natalie Gilbert
|
|
|
|
* @author Affan Hussain
|
2020-03-20 12:22:07 +01:00
|
|
|
* @author John Molakvoæ <skjnldsv@protonmail.com>
|
2019-05-14 01:15:45 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Forms\Controller;
|
|
|
|
|
2020-03-19 21:15:59 +01:00
|
|
|
use OCA\Forms\AppInfo\Application;
|
2020-03-30 13:48:14 +02:00
|
|
|
use OCA\Forms\Db\Form;
|
|
|
|
use OCA\Forms\Db\FormMapper;
|
|
|
|
use OCA\Forms\Db\Submission;
|
|
|
|
use OCA\Forms\Db\SubmissionMapper;
|
|
|
|
use OCA\Forms\Db\Answer;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCA\Forms\Db\AnswerMapper;
|
2020-03-30 13:48:14 +02:00
|
|
|
|
|
|
|
use OCA\Forms\Db\OptionMapper;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCA\Forms\Db\QuestionMapper;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2020-03-19 21:44:06 +01:00
|
|
|
use OCP\IGroup;
|
2019-05-14 01:15:45 +02:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\User; //To do: replace according to API
|
|
|
|
use OCP\Util;
|
|
|
|
|
|
|
|
class PageController extends Controller {
|
|
|
|
|
|
|
|
private $userId;
|
2020-03-30 13:48:14 +02:00
|
|
|
private $formMapper;
|
|
|
|
private $submissionMapper;
|
|
|
|
private $answerMapper;
|
2019-05-14 01:15:45 +02:00
|
|
|
|
|
|
|
private $questionMapper;
|
2020-03-30 13:48:14 +02:00
|
|
|
private $optionMapper;
|
2019-05-14 01:15:45 +02:00
|
|
|
|
|
|
|
private $urlGenerator;
|
|
|
|
private $userMgr;
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userMgr,
|
|
|
|
IGroupManager $groupManager,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
$userId,
|
2020-03-30 13:48:14 +02:00
|
|
|
FormMapper $formMapper,
|
|
|
|
|
2019-05-14 01:15:45 +02:00
|
|
|
QuestionMapper $questionMapper,
|
2020-03-30 13:48:14 +02:00
|
|
|
OptionMapper $optionMapper,
|
|
|
|
SubmissionMapper $SubmissionMapper,
|
|
|
|
AnswerMapper $AnswerMapper
|
2019-05-14 01:15:45 +02:00
|
|
|
) {
|
2020-03-19 21:15:59 +01:00
|
|
|
parent::__construct(Application::APP_ID, $request);
|
2019-05-14 01:15:45 +02:00
|
|
|
$this->userMgr = $userMgr;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->userId = $userId;
|
2020-03-30 13:48:14 +02:00
|
|
|
$this->formMapper = $formMapper;
|
2019-05-14 01:15:45 +02:00
|
|
|
|
|
|
|
$this->questionMapper = $questionMapper;
|
2020-03-30 13:48:14 +02:00
|
|
|
$this->optionMapper = $optionMapper;
|
|
|
|
$this->submissionMapper = $SubmissionMapper;
|
|
|
|
$this->answerMapper = $AnswerMapper;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-20 12:22:07 +01:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function index(): TemplateResponse {
|
2020-03-20 12:22:07 +01:00
|
|
|
Util::addScript($this->appName, 'forms');
|
|
|
|
Util::addStyle($this->appName, 'icons');
|
|
|
|
return new TemplateResponse($this->appName, 'main');
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-20 12:22:07 +01:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function createForm(): TemplateResponse {
|
2020-03-20 12:22:07 +01:00
|
|
|
Util::addScript($this->appName, 'forms');
|
|
|
|
Util::addStyle($this->appName, 'icons');
|
|
|
|
return new TemplateResponse($this->appName, 'main');
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-20 12:22:07 +01:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function cloneForm(): TemplateResponse {
|
2020-03-20 12:22:07 +01:00
|
|
|
Util::addScript($this->appName, 'forms');
|
|
|
|
Util::addStyle($this->appName, 'icons');
|
|
|
|
return new TemplateResponse($this->appName, 'main');
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2020-03-20 12:22:07 +01:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
|
|
|
public function editForm(): TemplateResponse {
|
|
|
|
Util::addScript($this->appName, 'forms');
|
|
|
|
Util::addStyle($this->appName, 'icons');
|
|
|
|
return new TemplateResponse($this->appName, 'main');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2019-05-14 01:15:45 +02:00
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-20 12:22:07 +01:00
|
|
|
public function getResult(): TemplateResponse {
|
|
|
|
Util::addScript($this->appName, 'forms');
|
|
|
|
Util::addStyle($this->appName, 'icons');
|
|
|
|
return new TemplateResponse($this->appName, 'main');
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
* @param string $hash
|
|
|
|
* @return TemplateResponse
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function gotoForm($hash): ?TemplateResponse {
|
2019-05-14 01:15:45 +02:00
|
|
|
try {
|
2020-03-30 13:48:14 +02:00
|
|
|
$form = $this->formMapper->findByHash($hash);
|
2019-05-14 01:15:45 +02:00
|
|
|
} catch (DoesNotExistException $e) {
|
|
|
|
return new TemplateResponse('forms', 'no.acc.tmpl', []);
|
|
|
|
}
|
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
if ($form->getExpirationDate() === null) {
|
2019-05-14 01:15:45 +02:00
|
|
|
$expired = false;
|
|
|
|
} else {
|
2020-03-30 13:48:14 +02:00
|
|
|
$expired = time() > strtotime($form->getExpirationDate());
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($expired) {
|
|
|
|
return new TemplateResponse('forms', 'expired.tmpl');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasUserAccess($form)) {
|
2019-06-27 19:20:28 +02:00
|
|
|
$renderAs = $this->userId !== null ? 'user' : 'public';
|
2020-03-30 13:48:14 +02:00
|
|
|
$res = new TemplateResponse('forms', 'submit.tmpl', [
|
2019-07-24 00:22:25 +02:00
|
|
|
'form' => $form,
|
|
|
|
'questions' => $this->getQuestions($form->getId()),
|
2019-06-27 19:20:28 +02:00
|
|
|
], $renderAs);
|
2019-07-24 00:22:25 +02:00
|
|
|
$csp = new ContentSecurityPolicy();
|
|
|
|
$csp->allowEvalScript(true);
|
|
|
|
$res->setContentSecurityPolicy($csp);
|
|
|
|
return $res;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
2020-03-19 21:44:06 +01:00
|
|
|
|
|
|
|
User::checkLoggedIn();
|
|
|
|
return new TemplateResponse('forms', 'no.acc.tmpl', []);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 01:21:54 +01:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-03-19 21:44:06 +01:00
|
|
|
public function getQuestions(int $formId): array {
|
|
|
|
$questionList = [];
|
2019-05-14 01:15:45 +02:00
|
|
|
try{
|
2020-03-30 13:48:14 +02:00
|
|
|
$questionEntities = $this->questionMapper->findByForm($formId);
|
|
|
|
foreach ($questionEntities as $questionEntity) {
|
|
|
|
$question = $questionEntity->read();
|
|
|
|
$question['options'] = $this->getOptions($question['id']);
|
|
|
|
$questionList[] = $question;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
} catch (DoesNotExistException $e) {
|
|
|
|
//handle silently
|
|
|
|
}
|
2020-03-19 21:44:06 +01:00
|
|
|
|
|
|
|
return $questionList;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 01:21:54 +01:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2020-03-30 13:48:14 +02:00
|
|
|
public function getOptions(int $questionId): array {
|
|
|
|
$optionList = [];
|
2020-03-19 21:44:06 +01:00
|
|
|
|
2019-05-14 01:15:45 +02:00
|
|
|
try{
|
2020-03-30 13:48:14 +02:00
|
|
|
$optionEntities = $this->optionMapper->findByQuestion($questionId);
|
|
|
|
foreach ($optionEntities as $optionEntity) {
|
|
|
|
$optionList[] = $optionEntity->read();
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DoesNotExistException $e) {
|
|
|
|
//handle silently
|
|
|
|
}
|
2020-03-19 21:44:06 +01:00
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
return $optionList;
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @param int $formId
|
|
|
|
* @return TemplateResponse|RedirectResponse
|
|
|
|
*/
|
|
|
|
public function deleteForm($formId) {
|
2020-03-30 13:48:14 +02:00
|
|
|
$formToDelete = $this->formMapper->find($formId);
|
|
|
|
if ($this->userId !== $formToDelete->getOwnerId() && !$this->groupManager->isAdmin($this->userId)) {
|
2019-05-14 01:15:45 +02:00
|
|
|
return new TemplateResponse('forms', 'no.delete.tmpl');
|
|
|
|
}
|
2020-03-30 13:48:14 +02:00
|
|
|
$form = new Form();
|
2019-05-14 01:15:45 +02:00
|
|
|
$form->setId($formId);
|
2020-03-30 13:48:14 +02:00
|
|
|
$this->submissionMapper->deleteByForm($formId);
|
|
|
|
$this->formMapper->delete($form);
|
2019-05-14 01:15:45 +02:00
|
|
|
$url = $this->urlGenerator->linkToRoute('forms.page.index');
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @PublicPage
|
|
|
|
* @param int $formId
|
|
|
|
* @param string $userId
|
2020-03-30 13:48:14 +02:00
|
|
|
* @param array $answers
|
|
|
|
* @param array $questions
|
2019-05-14 01:15:45 +02:00
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
2020-03-30 13:48:14 +02:00
|
|
|
public function insertSubmission($id, $userId, $answers, $questions) {
|
2019-05-14 09:03:59 +02:00
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
$form = $this->formMapper->find($id);
|
2019-05-14 09:03:59 +02:00
|
|
|
$anonID = "anon-user-". hash('md5', (time() + rand()));
|
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
//Insert Submission
|
|
|
|
$submission = new Submission();
|
|
|
|
$submission->setFormId($id);
|
|
|
|
if($form->getIsAnonymous()){
|
|
|
|
$submission->setUserId($anonID);
|
2019-05-14 09:03:59 +02:00
|
|
|
|
2020-03-30 13:48:14 +02:00
|
|
|
}else{
|
|
|
|
$submission->setUserId($userId);
|
|
|
|
}
|
|
|
|
$submission->setTimestamp(date('Y-m-d H:i:s'));
|
|
|
|
$this->submissionMapper->insert($submission);
|
|
|
|
$submissionId = $submission->getId();
|
|
|
|
|
|
|
|
//Insert Answers
|
|
|
|
foreach($questions as $question) {
|
|
|
|
if($question['type'] === "checkbox"){
|
|
|
|
foreach(($answers[$question['text']]) as $ansText) {
|
|
|
|
$answer = new Answer();
|
|
|
|
$answer->setSubmissionId($submissionId);
|
|
|
|
$answer->setQuestionId($question['id']);
|
|
|
|
$answer->setText($ansText);
|
|
|
|
$this->answerMapper->insert($answer);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-30 13:48:14 +02:00
|
|
|
$answer = new Answer();
|
|
|
|
$answer->setSubmissionId($submissionId);
|
|
|
|
$answer->setQuestionId($question['id']);
|
|
|
|
$answer->setText($answers[$question['text']]);
|
|
|
|
$this->answerMapper->insert($answer);
|
2019-05-14 01:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 13:48:14 +02:00
|
|
|
|
2019-05-14 01:15:45 +02:00
|
|
|
$hash = $form->getHash();
|
|
|
|
$url = $this->urlGenerator->linkToRoute('forms.page.goto_form', ['hash' => $hash]);
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @param string $searchTerm
|
|
|
|
* @param string $groups
|
|
|
|
* @param string $users
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function search($searchTerm, $groups, $users) {
|
|
|
|
return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @param string $searchTerm
|
|
|
|
* @param string $groups
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchForGroups($searchTerm, $groups) {
|
|
|
|
$selectedGroups = json_decode($groups);
|
|
|
|
$groups = $this->groupManager->search($searchTerm);
|
2020-03-19 21:44:06 +01:00
|
|
|
$gids = [];
|
|
|
|
$sgids = [];
|
2019-05-14 01:15:45 +02:00
|
|
|
foreach ($selectedGroups as $sg) {
|
|
|
|
$sgids[] = str_replace('group_', '', $sg);
|
|
|
|
}
|
|
|
|
foreach ($groups as $g) {
|
|
|
|
$gids[] = $g->getGID();
|
|
|
|
}
|
|
|
|
$diffGids = array_diff($gids, $sgids);
|
2020-03-19 21:44:06 +01:00
|
|
|
$gids = [];
|
2019-05-14 01:15:45 +02:00
|
|
|
foreach ($diffGids as $g) {
|
|
|
|
$gids[] = ['gid' => $g, 'isGroup' => true];
|
|
|
|
}
|
|
|
|
return $gids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @param string $searchTerm
|
|
|
|
* @param string $users
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchForUsers($searchTerm, $users) {
|
|
|
|
$selectedUsers = json_decode($users);
|
|
|
|
Util::writeLog('forms', print_r($selectedUsers, true), Util::ERROR);
|
|
|
|
$userNames = $this->userMgr->searchDisplayName($searchTerm);
|
2020-03-19 21:44:06 +01:00
|
|
|
$users = [];
|
|
|
|
$sUsers = [];
|
2019-05-14 01:15:45 +02:00
|
|
|
foreach ($selectedUsers as $su) {
|
|
|
|
$sUsers[] = str_replace('user_', '', $su);
|
|
|
|
}
|
|
|
|
foreach ($userNames as $u) {
|
|
|
|
$allreadyAdded = false;
|
|
|
|
foreach ($sUsers as &$su) {
|
|
|
|
if ($su === $u->getUID()) {
|
|
|
|
unset($su);
|
|
|
|
$allreadyAdded = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$allreadyAdded) {
|
2020-03-19 21:44:06 +01:00
|
|
|
$users[] = ['uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false];
|
2019-05-14 01:15:45 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OCP\IGroup[]
|
|
|
|
*/
|
|
|
|
private function getGroups() {
|
|
|
|
$groups = $this->groupManager->getUserGroups(\OC::$server->getUserSession()->getUser());
|
2020-03-19 21:44:06 +01:00
|
|
|
return array_map(function(IGroup $group) {
|
2019-05-14 01:15:45 +02:00
|
|
|
return $group->getGID();
|
|
|
|
}, $groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if user has access to this form
|
|
|
|
*
|
2020-03-30 13:48:14 +02:00
|
|
|
* @param Form $form
|
2019-05-14 01:15:45 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function hasUserAccess($form) {
|
|
|
|
$access = $form->getAccess();
|
2020-03-30 13:48:14 +02:00
|
|
|
$ownerId = $form->getOwnerId();
|
2019-05-14 01:15:45 +02:00
|
|
|
if ($access === 'public' || $access === 'hidden') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($this->userId === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($access === 'registered') {
|
2020-03-30 13:48:14 +02:00
|
|
|
if ($form->getSubmitOnce()) {
|
|
|
|
$participants = $this->submissionMapper->findParticipantsByForm($form->getId());
|
2019-08-01 17:02:45 +02:00
|
|
|
foreach($participants as $participant) {
|
|
|
|
// Don't allow access if user has already taken part
|
|
|
|
if ($participant->getUserId() === $this->userId) return false;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 01:15:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-30 13:48:14 +02:00
|
|
|
if ($ownerId === $this->userId) {
|
2019-05-14 01:15:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Util::writeLog('forms', $this->userId, Util::ERROR);
|
|
|
|
$userGroups = $this->getGroups();
|
|
|
|
$arr = explode(';', $access);
|
|
|
|
foreach ($arr as $item) {
|
|
|
|
if (strpos($item, 'group_') === 0) {
|
|
|
|
$grp = substr($item, 6);
|
|
|
|
foreach ($userGroups as $userGroup) {
|
|
|
|
if ($userGroup === $grp) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strpos($item, 'user_') === 0) {
|
|
|
|
$usr = substr($item, 5);
|
|
|
|
if ($usr === $this->userId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|