Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix visibility of the creating form for administrator #141

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public static function onWallEntryControlsInit($event)
{
$object = $event->sender->object;

if(!$object instanceof Poll) {
if (!$object instanceof Poll) {
return;
}

if($object->content->canEdit()) {
if ($object->content->canEdit()) {
$event->sender->addWidget(CloseButton::class, [
'poll' => $object,
]);
}

if($object->isResetAllowed()) {
if ($object->isResetAllowed()) {
$event->sender->addWidget(ResetButton::class, [
'poll' => $object,
]);
Expand All @@ -54,7 +54,7 @@ public static function onWallEntryControlsInit($event)
*/
public static function onUserDelete($event)
{
foreach (PollAnswerUser::findAll(array('created_by' => $event->sender->id)) as $answer) {
foreach (PollAnswerUser::findAll(['created_by' => $event->sender->id]) as $answer) {
$answer->delete();
}

Expand Down
24 changes: 12 additions & 12 deletions controllers/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace humhub\modules\polls\controllers;

use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswer;
use humhub\modules\polls\widgets\WallCreateForm;
use humhub\modules\stream\actions\Stream;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\helpers\Html;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswer;

/**
* PollController handles all poll related actions.
Expand Down Expand Up @@ -43,11 +42,12 @@ public function actionCreateForm()
*/
public function actionCreate()
{
if (!$this->contentContainer->permissionManager->can(new CreatePoll())) {
$poll = new Poll($this->contentContainer, ['scenario' => Poll::SCENARIO_CREATE]);

if (!$poll->content->canEdit()) {
throw new HttpException(400, 'Access denied!');
}

$poll = new Poll(['scenario' => Poll::SCENARIO_CREATE]);
$poll->load(Yii::$app->request->post());
return WallCreateForm::create($poll, $this->contentContainer);
}
Expand All @@ -59,7 +59,7 @@ public function actionReload($id)
{
$model = Poll::findOne(['id' => $id]);

if(!$model) {
if (!$model) {
throw new HttpException(404);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public function actionAnswer()
$answers = Yii::$app->request->post('answers');

// Build array of answer ids
$votes = array();
$votes = [];
if (is_array($answers)) {
foreach ($answers as $answer_id => $flag) {
$votes[] = (int) $answer_id;
Expand Down Expand Up @@ -196,7 +196,7 @@ public function actionUserListResults()
$query->andWhere(['poll_answer_user.poll_answer_id' => $answerId]);
$query->orderBy('poll_answer_user.created_at DESC');

$title = Yii::t('PollsModule.controllers_PollController', "Users voted for: <strong>{answer}</strong>", array('{answer}' => Html::encode($answer->answer)));
$title = Yii::t('PollsModule.controllers_PollController', "Users voted for: <strong>{answer}</strong>", ['{answer}' => Html::encode($answer->answer)]);

return $this->renderAjaxContent(UserListBox::widget(['query' => $query, 'title' => $title]));
}
Expand All @@ -209,7 +209,7 @@ public function actionUserListResults()
*/
private function renderPollOut($question)
{
$json = array();
$json = [];
$json['output'] = $this->renderAjaxContent($question->getWallOut());

return $this->asJson($json);
Expand Down
11 changes: 5 additions & 6 deletions controllers/rest/PollsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use humhub\modules\polls\helpers\RestDefinitions;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswerUser;
use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\rest\components\BaseContentController;
use Yii;

Expand Down Expand Up @@ -57,13 +56,13 @@ public function actionCreate($containerId)
/* @var ContentContainerActiveRecord $container */
$container = $containerRecord->getPolymorphicRelation();

if (! in_array(get_class($container), Yii::$app->getModule('polls')->getContentContainerTypes()) ||
! $container->permissionManager->can([CreatePoll::class])) {
$poll = new Poll($container, ['scenario' => Poll::SCENARIO_CREATE]);

if (!in_array(get_class($container), Yii::$app->getModule('polls')->getContentContainerTypes()) ||
!$poll->content->canEdit()) {
return $this->returnError(403, 'You are not allowed to create a poll!');
}

$poll = new Poll($container, ['scenario' => Poll::SCENARIO_CREATE]);

if ($this->savePoll($poll)) {
return $this->returnContentDefinition(Poll::findOne(['id' => $poll->id]));
}
Expand Down Expand Up @@ -184,7 +183,7 @@ public function actionVote(int $id): array

$answers = Yii::$app->request->post('answers');

$votes = array();
$votes = [];
if (is_array($answers)) {
foreach ($answers as $answerId) {
$votes[] = (int)$answerId;
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.3.6 (Unreleased)
----------------------
- Enh #137: Use PHP CS Fixer
- Fix #141: Fix visibility of the creating form for administrator

1.3.5 (June 18, 2024)
----------------------
Expand Down
12 changes: 6 additions & 6 deletions migrations/m131023_165921_initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ class m131023_165921_initial extends Migration
public function up()
{

$this->createTable('poll', array(
$this->createTable('poll', [
'id' => 'pk',
'question' => 'varchar(255) NOT NULL',
'allow_multiple' => 'tinyint(4) NOT NULL',
'created_at' => 'datetime NOT NULL',
'created_by' => 'int(11) NOT NULL',
'updated_at' => 'datetime NOT NULL',
'updated_by' => 'int(11) NOT NULL',
), '');
], '');

$this->createTable('poll_answer', array(
$this->createTable('poll_answer', [
'id' => 'pk',
'poll_id' => 'int(11) NOT NULL',
'answer' => 'varchar(255) NOT NULL',
'created_at' => 'datetime NOT NULL',
'created_by' => 'int(11) NOT NULL',
'updated_at' => 'datetime NOT NULL',
'updated_by' => 'int(11) NOT NULL',
), '');
], '');

$this->createTable('poll_answer_user', array(
$this->createTable('poll_answer_user', [
'id' => 'pk',
'poll_id' => 'int(11) NOT NULL',
'poll_answer_id' => 'int(11) NOT NULL',
'created_at' => 'datetime NOT NULL',
'created_by' => 'int(11) NOT NULL',
'updated_at' => 'datetime NOT NULL',
'updated_by' => 'int(11) NOT NULL',
), '');
], '');
}

public function down()
Expand Down
22 changes: 11 additions & 11 deletions models/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function scenarios()
*/
public function rules()
{
return array(
return [
[['question'], 'string', 'max' => 255],
[['question'], 'required'],
[['description'], 'string'],
Expand All @@ -87,12 +87,12 @@ public function rules()
[['question'], 'minTwoAnswers', 'on' => self::SCENARIO_EDIT],
[['allow_multiple'], 'integer'],

);
];
}

public function minTwoNewAnswers($attribute)
{
if(count($this->newAnswers) < self::MIN_REQUIRED_ANSWERS) {
if (count($this->newAnswers) < self::MIN_REQUIRED_ANSWERS) {
$this->addError($attribute, Yii::t('PollsModule.models_Poll', "Please specify at least {min} answers!", ["{min}" => self::MIN_REQUIRED_ANSWERS]));
}
}
Expand All @@ -110,7 +110,7 @@ public function minTwoAnswers($attribute)
*/
public function attributeLabels()
{
return array(
return [
'newAnswers' => Yii::t('PollsModule.models_Poll', 'Answers'),
'editAnswers' => Yii::t('PollsModule.models_Poll', 'Answers'),
'question' => Yii::t('PollsModule.models_Poll', 'Question'),
Expand All @@ -119,7 +119,7 @@ public function attributeLabels()
'is_random' => Yii::t('PollsModule.widgets_views_pollForm', 'Display answers in random order?'),
'anonymous' => Yii::t('PollsModule.widgets_views_pollForm', 'Anonymous Votes?'),
'show_result_after_close' => Yii::t('PollsModule.widgets_views_pollForm', 'Hide results until poll is closed?'),
);
];
}

public function getIcon()
Expand Down Expand Up @@ -173,7 +173,7 @@ public function afterSave($insert, $changedAttributes)

RichText::postProcess($this->description, $this);

if($this->scenario === static::SCENARIO_EDIT || $this->scenario === static::SCENARIO_CREATE) {
if ($this->scenario === static::SCENARIO_EDIT || $this->scenario === static::SCENARIO_CREATE) {
if (!$insert) {
$this->updateAnswers();
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public function hasUserVoted($userId = "")
$userId = Yii::$app->user->id;
}

$answer = PollAnswerUser::findOne(array('created_by' => $userId, 'poll_id' => $this->id));
$answer = PollAnswerUser::findOne(['created_by' => $userId, 'poll_id' => $this->id]);

if ($answer == null) {
return false;
Expand All @@ -281,7 +281,7 @@ public function hasUserVoted($userId = "")
return true;
}

public function vote($votes = array())
public function vote($votes = [])
{

if ($this->hasUserVoted()) {
Expand All @@ -291,7 +291,7 @@ public function vote($votes = array())
$voted = false;

foreach ($votes as $answerId) {
$answer = PollAnswer::findOne(array('id' => $answerId, 'poll_id' => $this->id));
$answer = PollAnswer::findOne(['id' => $answerId, 'poll_id' => $this->id]);
if ($answer) {
$userVote = new PollAnswerUser();
$userVote->poll_id = $this->id;
Expand Down Expand Up @@ -321,7 +321,7 @@ public function vote($votes = array())
public function resetAnswer($userId = "")
{

if($this->closed) {
if ($this->closed) {
return;
}

Expand All @@ -331,7 +331,7 @@ public function resetAnswer($userId = "")

if ($this->hasUserVoted($userId)) {

$answers = PollAnswerUser::findAll(array('created_by' => $userId, 'poll_id' => $this->id));
$answers = PollAnswerUser::findAll(['created_by' => $userId, 'poll_id' => $this->id]);
foreach ($answers as $answer) {
$answer->delete();
}
Expand Down
16 changes: 8 additions & 8 deletions models/PollAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public static function tableName()
*/
public function rules()
{
return array(
array(['poll_id', 'answer'], 'required'),
array(['poll_id'], 'integer'),
array(['answer'], 'string', 'max' => 255),
);
return [
[['poll_id', 'answer'], 'required'],
[['poll_id'], 'integer'],
[['answer'], 'string', 'max' => 255],
];
}

public function getPoll()
Expand Down Expand Up @@ -74,7 +74,7 @@ public function beforeDelete()
*/
public function getPercent()
{
$total = PollAnswerUser::find()->where(array('poll_id' => $this->poll_id))->count();
$total = PollAnswerUser::find()->where(['poll_id' => $this->poll_id])->count();
if ($total == 0) {
return 0;
}
Expand All @@ -90,7 +90,7 @@ public function getPercent()
public function getTotal()
{

return PollAnswerUser::find()->where(array('poll_answer_id' => $this->id))->count();
return PollAnswerUser::find()->where(['poll_answer_id' => $this->id])->count();
}

public static function filterValidAnswers($answerArr)
Expand All @@ -101,7 +101,7 @@ public static function filterValidAnswers($answerArr)

$result = [];
foreach ($answerArr as $key => $answerText) {
if($answerText != null && $answerText !== '') {
if ($answerText != null && $answerText !== '') {
$result[$key] = $answerText;
}
}
Expand Down
8 changes: 4 additions & 4 deletions models/PollAnswerUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static function tableName()
*/
public function rules()
{
return array(
array(['poll_answer_id', 'poll_id'], 'required'),
array(['poll_answer_id', 'poll_id'], 'integer'),
);
return [
[['poll_answer_id', 'poll_id'], 'required'],
[['poll_answer_id', 'poll_id'], 'integer'],
];
}

public function getPoll()
Expand Down
3 changes: 1 addition & 2 deletions widgets/WallCreateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use humhub\modules\content\widgets\WallCreateContentForm;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\space\models\Space;
use humhub\modules\ui\form\widgets\ActiveForm;

Expand Down Expand Up @@ -41,7 +40,7 @@ public function renderActiveForm(ActiveForm $form): string
public function run()
{
if ($this->contentContainer instanceof Space) {
if (!$this->contentContainer->permissionManager->can(new CreatePoll())) {
if (!(new Poll($this->contentContainer))->content->canEdit()) {
return '';
}
}
Expand Down