diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..c850a85 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/js/* binary diff --git a/.gitignore b/.gitignore index edfd906..4fa09b5 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ cypress/snapshots js/ # Compile-Cache -v8-compile-cache-0/ \ No newline at end of file +v8-compile-cache-0/ + +# php-cs cache +.php_cs.cache \ No newline at end of file diff --git a/.php_cs.dist b/.php_cs.dist new file mode 100644 index 0000000..8a0cb43 --- /dev/null +++ b/.php_cs.dist @@ -0,0 +1,17 @@ +getFinder() + ->notPath('build') + ->notPath('l10n') + ->notPath('src') + ->notPath('vendor') + ->in(__DIR__); +return $config; diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index d7c02af..a2a11bb 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,13 +28,9 @@ declare(strict_types=1); namespace OCA\Forms\AppInfo; use OCP\AppFramework\App; -use OCP\IL10N; -use OCP\INavigationManager; -use OCP\IURLGenerator; class Application extends App { - - const APP_ID = 'forms'; + public const APP_ID = 'forms'; /** * Application constructor. diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 6481d5c..c615b15 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -47,7 +47,6 @@ use OCP\IUserSession; use OCP\Security\ISecureRandom; class ApiController extends Controller { - protected $appName; /** @var SubmissionMapper */ @@ -123,8 +122,8 @@ class ApiController extends Controller { /** * @NoAdminRequired - * - * + * + * * Read all information to edit a Form (form, questions, options, except submissions/answers). */ public function getForm(int $id): Http\JSONResponse { @@ -307,14 +306,14 @@ class ApiController extends Controller { } // Check if array contains duplicates - if ( array_unique($newOrder) !== $newOrder ) { + if (array_unique($newOrder) !== $newOrder) { $this->logger->debug('The given Array contains duplicates.'); return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST); } // Check if all questions are given in Array. $questions = $this->questionMapper->findByForm($formId); - if ( sizeof($questions) !== sizeof($newOrder) ) { + if (sizeof($questions) !== sizeof($newOrder)) { $this->logger->debug('The length of the given array does not match the number of stored questions'); return new Http\JSONResponse([], Http::STATUS_BAD_REQUEST); } @@ -323,8 +322,7 @@ class ApiController extends Controller { $response = []; // Array of ['questionId' => ['order' => newOrder]] // Store array of Question-Entities and check the Questions FormId & old Order. - foreach($newOrder as $arrayKey => $questionId) { - + foreach ($newOrder as $arrayKey => $questionId) { try { $questions[$arrayKey] = $this->questionMapper->findById($questionId); } catch (IMapperException $e) { @@ -344,7 +342,7 @@ class ApiController extends Controller { // Abort if a question is already marked as deleted (order==0) $oldOrder = $questions[$arrayKey]->getOrder(); - if ( $oldOrder === 0) { + if ($oldOrder === 0) { $this->logger->debug('This Question has already been marked as deleted: Id: {id}', [ 'id' => $questions[$arrayKey]->getId() ]); @@ -359,7 +357,7 @@ class ApiController extends Controller { } // Write to Database - foreach($questions as $question) { + foreach ($questions as $question) { $this->questionMapper->update($question); $response[$question->getId()] = [ @@ -443,7 +441,7 @@ class ApiController extends Controller { $formQuestions = $this->questionMapper->findByForm($form->getId()); foreach ($formQuestions as $question) { $questionOrder = $question->getOrder(); - if ( $questionOrder > $deletedOrder ) { + if ($questionOrder > $deletedOrder) { $question->setOrder($questionOrder - 1); $this->questionMapper->update($question); } @@ -489,7 +487,7 @@ class ApiController extends Controller { /** * @NoAdminRequired * Writes the given key-value pairs into Database. - + * * @param int $id OptionId of option to update * @param array $keyValuePairs Array of key=>value pairs to update. */ @@ -589,7 +587,7 @@ class ApiController extends Controller { /** * @NoAdminRequired * @PublicPage - * + * * Process a new submission * @param int $formId * @param array $answers [question_id => arrayOfString] @@ -617,7 +615,7 @@ class ApiController extends Controller { $submission->setTimestamp(time()); // If not logged in or anonymous use anonID - if (!$user || $form->getIsAnonymous()){ + if (!$user || $form->getIsAnonymous()) { $anonID = "anon-user-". hash('md5', (time() + rand())); $submission->setUserId($anonID); } else { @@ -629,7 +627,7 @@ class ApiController extends Controller { $submissionId = $submission->getId(); // Process Answers - foreach($answers as $questionId => $answerArray) { + foreach ($answers as $questionId => $answerArray) { // Search corresponding Question, skip processing if not found $questionIndex = array_search($questionId, array_column($questions, 'id')); if ($questionIndex === false) { @@ -638,11 +636,11 @@ class ApiController extends Controller { $question = $questions[$questionIndex]; } - foreach($answerArray as $answer) { - if($question['type'] === 'multiple' || $question['type'] === 'multiple_unique') { + foreach ($answerArray as $answer) { + if ($question['type'] === 'multiple' || $question['type'] === 'multiple_unique') { // Search corresponding option, skip processing if not found $optionIndex = array_search($answer, array_column($question['options'], 'id')); - if($optionIndex === false) { + if ($optionIndex === false) { continue; } else { $option = $question['options'][$optionIndex]; @@ -650,7 +648,6 @@ class ApiController extends Controller { // Load option-text $answerText = $option['text']; - } else { $answerText = $answer; // Not a multiple-question, answerText is given answer } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 92f3ead..38fb997 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -44,7 +44,6 @@ use OCP\IUserSession; use OCP\Util; class PageController extends Controller { - protected $appName; /** @var FormMapper */ @@ -115,7 +114,7 @@ class PageController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired - * + * * TODO: Implement cloning * * @return TemplateResponse @@ -189,7 +188,7 @@ class PageController extends Controller { /** * @NoAdminRequired * Check if user has access to this form - * + * * @param Form $form * @return boolean */ @@ -215,7 +214,7 @@ class PageController extends Controller { // Refuse access, if SubmitOnce is set and user already has taken part. if ($form->getSubmitOnce()) { $participants = $this->submissionMapper->findParticipantsByForm($form->getId()); - foreach($participants as $participant) { + foreach ($participants as $participant) { if ($participant === $user->getUID()) { return false; } @@ -235,7 +234,7 @@ class PageController extends Controller { // Check if access granted by group. foreach ($access['groups'] as $group) { - if( $this->groupManager->isInGroup($user->getUID(), $group) ) { + if ($this->groupManager->isInGroup($user->getUID(), $group)) { return true; } } diff --git a/lib/Controller/SystemController.php b/lib/Controller/SystemController.php index 68a517d..7af5459 100644 --- a/lib/Controller/SystemController.php +++ b/lib/Controller/SystemController.php @@ -29,13 +29,10 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\IGroupManager; -use OCP\IUser; use OCP\IUserManager; -use OCP\IConfig; use OCP\IRequest; class SystemController extends Controller { - public function __construct( string $appName, IGroupManager $groupManager, @@ -52,9 +49,9 @@ class SystemController extends Controller { * @NoAdminRequired * @return DataResponse */ - public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $skipGroups = array(), $skipUsers = array()) { - $list = array(); - $data = array(); + public function getSiteUsersAndGroups($query = '', $getGroups = true, $getUsers = true, $skipGroups = [], $skipUsers = []) { + $list = []; + $data = []; if ($getGroups) { $groups = $this->groupManager->search($query); foreach ($groups as $group) { diff --git a/lib/Db/Answer.php b/lib/Db/Answer.php index 250697e..ee8e22e 100644 --- a/lib/Db/Answer.php +++ b/lib/Db/Answer.php @@ -59,5 +59,4 @@ class Answer extends Entity { 'text' => htmlspecialchars_decode($this->getText()), ]; } - } diff --git a/lib/Db/AnswerMapper.php b/lib/Db/AnswerMapper.php index 58c18b1..4d4b746 100644 --- a/lib/Db/AnswerMapper.php +++ b/lib/Db/AnswerMapper.php @@ -58,8 +58,8 @@ class AnswerMapper extends QBMapper { } /** - * @param int $submissionId - */ + * @param int $submissionId + */ public function deleteBySubmission(int $submissionId): void { $qb = $this->db->getQueryBuilder(); @@ -70,5 +70,4 @@ class AnswerMapper extends QBMapper { $qb->execute(); } - } diff --git a/lib/Db/Form.php b/lib/Db/Form.php index 81940f9..db6e0e2 100644 --- a/lib/Db/Form.php +++ b/lib/Db/Form.php @@ -50,7 +50,6 @@ use OCP\AppFramework\Db\Entity; * @method void setSubmitOnce(bool $value) */ class Form extends Entity { - protected $hash; protected $title; protected $description; diff --git a/lib/Db/FormMapper.php b/lib/Db/FormMapper.php index f8e7812..75d258c 100644 --- a/lib/Db/FormMapper.php +++ b/lib/Db/FormMapper.php @@ -33,7 +33,7 @@ class FormMapper extends QBMapper { /** * FormMapper constructor. - * + * * @param IDBConnection $db */ public function __construct(IDBConnection $db) { @@ -102,5 +102,4 @@ class FormMapper extends QBMapper { return $this->findEntities($qb); } - } diff --git a/lib/Db/OptionMapper.php b/lib/Db/OptionMapper.php index 6e25fa1..91029ad 100644 --- a/lib/Db/OptionMapper.php +++ b/lib/Db/OptionMapper.php @@ -26,7 +26,6 @@ declare(strict_types=1); namespace OCA\Forms\Db; -use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\AppFramework\Db\QBMapper; @@ -80,5 +79,4 @@ class OptionMapper extends QBMapper { return $this->findEntity($qb); } - } diff --git a/lib/Db/Question.php b/lib/Db/Question.php index 77860ce..090d320 100644 --- a/lib/Db/Question.php +++ b/lib/Db/Question.php @@ -48,7 +48,7 @@ class Question extends Entity { protected $mandatory; protected $text; - const TYPES = [ + public const TYPES = [ 'short', 'long', 'multiple', diff --git a/lib/Db/QuestionMapper.php b/lib/Db/QuestionMapper.php index 83df607..cb0efe4 100644 --- a/lib/Db/QuestionMapper.php +++ b/lib/Db/QuestionMapper.php @@ -30,10 +30,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\AppFramework\Db\QBMapper; -use OCA\Forms\Db\OptionMapper; - class QuestionMapper extends QBMapper { - private $optionMapper; public function __construct(IDBConnection $db, OptionMapper $optionMapper) { @@ -102,5 +99,4 @@ class QuestionMapper extends QBMapper { return $this->findEntity($qb); } - } diff --git a/lib/Db/Submission.php b/lib/Db/Submission.php index 8c639ac..6aaaea9 100644 --- a/lib/Db/Submission.php +++ b/lib/Db/Submission.php @@ -59,5 +59,4 @@ class Submission extends Entity { 'timestamp' => $this->getTimestamp(), ]; } - } diff --git a/lib/Db/SubmissionMapper.php b/lib/Db/SubmissionMapper.php index 32a968a..94a496a 100644 --- a/lib/Db/SubmissionMapper.php +++ b/lib/Db/SubmissionMapper.php @@ -29,10 +29,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\AppFramework\Db\QBMapper; -use OCA\Forms\Db\AnswerMapper; - class SubmissionMapper extends QBMapper { - private $answerMapper; /** @@ -80,7 +77,7 @@ class SubmissionMapper extends QBMapper { $submissionEntities = $this->findEntities($qb); // From array of submissionEntities produce array of userIds. - $userIds = array_map(function($submissionEntity) { + $userIds = array_map(function ($submissionEntity) { return $submissionEntity->getUserId(); }, $submissionEntities); @@ -88,8 +85,8 @@ class SubmissionMapper extends QBMapper { } /** - * @param int $formId - */ + * @param int $formId + */ public function deleteByForm(int $formId): void { $qb = $this->db->getQueryBuilder(); diff --git a/lib/Migration/Version0010Date20190000000007.php b/lib/Migration/Version0010Date20190000000007.php index f30dd84..566f5c0 100644 --- a/lib/Migration/Version0010Date20190000000007.php +++ b/lib/Migration/Version0010Date20190000000007.php @@ -23,11 +23,8 @@ namespace OCA\Forms\Migration; -use Doctrine\DBAL\Exception\TableNotFoundException; -use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Types\Type; use OCP\DB\ISchemaWrapper; -use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\SimpleMigrationStep; diff --git a/lib/Migration/Version010200Date20200323141300.php b/lib/Migration/Version010200Date20200323141300.php index cb63716..aad7c07 100644 --- a/lib/Migration/Version010200Date20200323141300.php +++ b/lib/Migration/Version010200Date20200323141300.php @@ -24,8 +24,6 @@ namespace OCA\Forms\Migration; -use Doctrine\DBAL\Exception\TableNotFoundException; -use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Types\Type; use OCP\DB\ISchemaWrapper; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -214,7 +212,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $schema = $schemaClosure(); // if Database exists. - if( $schema->hasTable('forms_events') ){ + if ($schema->hasTable('forms_events')) { $id_mapping = []; $id_mapping['events'] = []; // Maps oldevent-id => ['newId' => newevent-id, 'nextQuestionOrder' => integer] $id_mapping['questions'] = []; // Maps oldquestion-id => ['newId' => newquestion-id] @@ -227,7 +225,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $qb_fetch->select('id', 'hash', 'title', 'description', 'owner', 'created', 'access', 'expire', 'is_anonymous', 'unique') ->from('forms_events'); $cursor = $qb_fetch->execute(); - while( $event = $cursor->fetch() ){ + while ($event = $cursor->fetch()) { $newAccessJSON = $this->convertAccessList($event['access']); $qb_restore->insert('forms_v2_forms') @@ -257,9 +255,9 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $qb_fetch->select('id', 'form_id', 'form_question_type', 'form_question_text') ->from('forms_questions'); $cursor = $qb_fetch->execute(); - while( $question = $cursor->fetch() ){ + while ($question = $cursor->fetch()) { //In case the old Question would have been longer than current possible length, create a warning and shorten text to avoid Error on upgrade. - if(strlen($question['form_question_text']) > 2048) { + if (strlen($question['form_question_text']) > 2048) { $output->warning("Question-text is too long for new Database: '" . $question['form_question_text'] . "'"); $question['form_question_text'] = substr($question['form_question_text'], 0, 2048); } @@ -283,9 +281,9 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $qb_fetch->select('question_id', 'text') ->from('forms_answers'); $cursor = $qb_fetch->execute(); - while( $answer = $cursor->fetch() ){ + while ($answer = $cursor->fetch()) { //In case the old Answer would have been longer than current possible length, create a warning and shorten text to avoid Error on upgrade. - if(strlen($answer['text']) > 1024) { + if (strlen($answer['text']) > 1024) { $output->warning("Option-text is too long for new Database: '" . $answer['text'] . "'"); $answer['text'] = substr($answer['text'], 0, 1024); } @@ -307,7 +305,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $qb_fetch->select('id') ->from('forms_events'); $cursor = $qb_fetch->execute(); - while( $tmp = $cursor->fetch() ){ + while ($tmp = $cursor->fetch()) { $event_structure[$tmp['id']] = $tmp; } $cursor->closeCursor(); @@ -319,7 +317,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { ->from('forms_questions') ->where($qb_fetch->expr()->eq('form_id', $qb_fetch->createNamedParameter($event['id'], IQueryBuilder::PARAM_INT))); $cursor = $qb_fetch->execute(); - while( $tmp = $cursor->fetch() ) { + while ($tmp = $cursor->fetch()) { $event_structure[$event['id']]['questions'][] = $tmp; } $cursor->closeCursor(); @@ -337,9 +335,9 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $qb_fetch->select('id', 'form_id', 'user_id', 'vote_option_id', 'vote_option_text', 'vote_answer') ->from('forms_votes'); $cursor = $qb_fetch->execute(); - while( $vote = $cursor->fetch() ){ + while ($vote = $cursor->fetch()) { //If the form changed, if the user changed or if vote_option_id became smaller than last one, then a new submission is interpreted. - if ( ($vote['form_id'] != $last_vote['form_id']) || ($vote['user_id'] != $last_vote['user_id']) || ($vote['vote_option_id'] < $last_vote['vote_option_id'])) { + if (($vote['form_id'] != $last_vote['form_id']) || ($vote['user_id'] != $last_vote['user_id']) || ($vote['vote_option_id'] < $last_vote['vote_option_id'])) { $qb_restore->insert('forms_v2_submissions') ->values([ 'form_id' => $qb_restore->createNamedParameter($id_mapping['events'][$vote['form_id']]['newId'], IQueryBuilder::PARAM_INT), @@ -352,7 +350,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $last_vote = $vote; //In case the old Answer would have been longer than current possible length, create a warning and shorten text to avoid Error on upgrade. - if(strlen($vote['vote_answer']) > 2048) { + if (strlen($vote['vote_answer']) > 2048) { $output->warning("Answer-text is too long for new Database: '" . $vote['vote_answer'] . "'"); $vote['vote_answer'] = substr($vote['vote_answer'], 0, 2048); } @@ -396,7 +394,7 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { $accessArray['groups'] = []; $stringExplode = explode(';', $accessString); - foreach($stringExplode as $string) { + foreach ($stringExplode as $string) { if (strpos($string, 'user_') === 0) { $accessArray['users'][] = substr($string, 5); } elseif (strpos($string, 'group_') === 0) { diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index c6e0c38..b28aece 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -54,12 +54,11 @@ class FormsService { public function getOptions(int $questionId): array { $optionList = []; - try{ + try { $optionEntities = $this->optionMapper->findByQuestion($questionId); foreach ($optionEntities as $optionEntity) { $optionList[] = $optionEntity->read(); } - } catch (DoesNotExistException $e) { //handle silently } finally { @@ -69,17 +68,16 @@ class FormsService { public function getQuestions(int $formId): array { $questionList = []; - try{ + try { $questionEntities = $this->questionMapper->findByForm($formId); foreach ($questionEntities as $questionEntity) { $question = $questionEntity->read(); $question['options'] = $this->getOptions($question['id']); $questionList[] = $question; } - } catch (DoesNotExistException $e) { //handle silently - }finally{ + } finally { return $questionList; } } @@ -98,5 +96,4 @@ class FormsService { return $result; } - }