Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2020-04-29 11:50:03 +02:00
Родитель 50d9e83ed1
Коммит fcae747c5f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 60C25B8C072916CF
19 изменённых файлов: 67 добавлений и 79 удалений

1
.gitattributes поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
/js/* binary

5
.gitignore поставляемый
Просмотреть файл

@ -26,4 +26,7 @@ cypress/snapshots
js/
# Compile-Cache
v8-compile-cache-0/
v8-compile-cache-0/
# php-cs cache
.php_cs.cache

17
.php_cs.dist Normal file
Просмотреть файл

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
require_once './vendor/autoload.php';
use Nextcloud\CodingStandard\Config;
$config = new Config();
$config
->getFinder()
->notPath('build')
->notPath('l10n')
->notPath('src')
->notPath('vendor')
->in(__DIR__);
return $config;

Просмотреть файл

@ -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.

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -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) {

Просмотреть файл

@ -59,5 +59,4 @@ class Answer extends Entity {
'text' => htmlspecialchars_decode($this->getText()),
];
}
}

Просмотреть файл

@ -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();
}
}

Просмотреть файл

@ -50,7 +50,6 @@ use OCP\AppFramework\Db\Entity;
* @method void setSubmitOnce(bool $value)
*/
class Form extends Entity {
protected $hash;
protected $title;
protected $description;

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -48,7 +48,7 @@ class Question extends Entity {
protected $mandatory;
protected $text;
const TYPES = [
public const TYPES = [
'short',
'long',
'multiple',

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -59,5 +59,4 @@ class Submission extends Entity {
'timestamp' => $this->getTimestamp(),
];
}
}

Просмотреть файл

@ -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();

Просмотреть файл

@ -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;

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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;
}
}