diff --git a/src/Http/Controllers/PollManagerController.php b/src/Http/Controllers/PollManagerController.php index a0bdde9..def7adc 100644 --- a/src/Http/Controllers/PollManagerController.php +++ b/src/Http/Controllers/PollManagerController.php @@ -69,7 +69,10 @@ public function edit(Poll $poll) 'value' => $option->name, ]; })->toArray(); - return view('larapoll::dashboard.edit', compact('poll', 'options')); + + $canChangeOptions = $poll->votes()->count() === 0; + + return view('larapoll::dashboard.edit', compact('poll', 'options', 'canChangeOptions')); } /** diff --git a/src/Option.php b/src/Option.php index c56bebb..48409d1 100644 --- a/src/Option.php +++ b/src/Option.php @@ -9,7 +9,7 @@ class Option extends Model { use Votable; - protected $fillable = ['name']; + protected $guarded = []; protected $table = 'larapoll_options'; /** diff --git a/src/Poll.php b/src/Poll.php index cba15cf..80997e2 100644 --- a/src/Poll.php +++ b/src/Poll.php @@ -60,4 +60,14 @@ public function canGuestVote() { return $this->canVisitorsVote === 1; } + + /** + * Check if the user can change options + * + * @return bool + */ + public function canChangeOptions() + { + return $this->votes()->count() === 0; + } } diff --git a/src/helpers/PollHandler.php b/src/helpers/PollHandler.php index 98f9264..d5e4a38 100644 --- a/src/helpers/PollHandler.php +++ b/src/helpers/PollHandler.php @@ -7,6 +7,7 @@ use Inani\Larapoll\Exceptions\OptionsInvalidNumberProvidedException; use Inani\Larapoll\Exceptions\OptionsNotProvidedException; use Inani\Larapoll\Exceptions\RemoveVotedOptionException; +use Inani\Larapoll\Option; use Inani\Larapoll\Poll; use InvalidArgumentException; @@ -55,6 +56,18 @@ public static function createFromRequest($request) */ public static function modify(Poll $poll, $data) { + if($poll->canChangeOptions()){ + $poll->options()->delete(); + + collect($data['options'])->each(function ($option) use($poll){ + Option::create([ + 'name' => $option, + 'poll_id' => $poll->id, + 'votes' => 0 + ]); + }); + } + if (isset($data['count_check'])) { if ($data['count_check'] < $poll->options()->count()) { $poll->canSelect($data['count_check']); diff --git a/src/traits/PollCreator.php b/src/traits/PollCreator.php index 8e8b9af..fd2e125 100644 --- a/src/traits/PollCreator.php +++ b/src/traits/PollCreator.php @@ -1,153 +1,154 @@ -options_add)) { - $this->options_add[] = $option; - return true; - } - throw new DuplicatedOptionsException(); - } - - /** - * Add new Options - * - * @param $options - * @return $this - * @throws \InvalidArgumentException - */ - public function addOptions($options) - { - if (is_array($options)) { - foreach ($options as $option) { - if (is_string($option)) { - $this->pushOption($option); - } else { - throw new \InvalidArgumentException("Array arguments must be composed of Strings values"); - } - } - return $this; - } - - if (is_string($options)) { - $this->pushOption($options); - return $this; - } - - throw new \InvalidArgumentException("Invalid Argument provided"); - } - - /** - * Select max options to be voted by a user - * - * @param int $number - * @return $this - */ - public function maxSelection($number = 1) - { - if ($number <= 1) { - $number = 1; - } - $this->maxSelection = $number; - - return $this; - } - - /** - * It starts at - * - * @param null $at - * @return $this - */ - public function startsAt($at = null) - { - $this->starts_at = !is_null($at) ? $at : now(); - - return $this; - } - - /** - * It ends at - * - * @param null $at - * @return $this - */ - public function endsAt($at = null) - { - $this->ends_at = !is_null($at) ? $at : now()->addDays(7); - - return $this; - } - - /** - * Generate the poll - * - * @return bool - * @throws CheckedOptionsException - * @throws OptionsInvalidNumberProvidedException - * @throws OptionsNotProvidedException - */ - public function generate() - { - $totalOptions = count($this->options_add); - - // No option add yet - if ($totalOptions == 0) - throw new OptionsNotProvidedException(); - - // There must be 2 options at least - if ($totalOptions == 1) - throw new OptionsInvalidNumberProvidedException(); - - // At least one options should not be selected - if ($totalOptions <= $this->maxSelection) - throw new CheckedOptionsException(); - - // Create Poll && assign options to it - DB::transaction(function () { - $this->maxCheck = $this->maxSelection; - $this->save(); - $this->options() - ->saveMany($this->instantiateOptions()); - }); - - return true; - } - - /** - * Instantiate the options - * - * @return array - */ - private function instantiateOptions() - { - $options = []; - foreach ($this->options_add as $option) { - $options[] = new Option([ - 'name' => $option - ]); - } - - return $options; - } -} +options_add)) { + $this->options_add[] = $option; + return true; + } + throw new DuplicatedOptionsException(); + } + + /** + * Add new Options + * + * @param $options + * @return $this + * @throws \InvalidArgumentException + * @throws DuplicatedOptionsException + */ + public function addOptions($options) + { + if (is_array($options)) { + foreach ($options as $option) { + if (is_string($option)) { + $this->pushOption($option); + } else { + throw new \InvalidArgumentException("Array arguments must be composed of Strings values"); + } + } + return $this; + } + + if (is_string($options)) { + $this->pushOption($options); + return $this; + } + + throw new \InvalidArgumentException("Invalid Argument provided"); + } + + /** + * Select max options to be voted by a user + * + * @param int $number + * @return $this + */ + public function maxSelection($number = 1) + { + if ($number <= 1) { + $number = 1; + } + $this->maxSelection = $number; + + return $this; + } + + /** + * It starts at + * + * @param null $at + * @return $this + */ + public function startsAt($at = null) + { + $this->starts_at = !is_null($at) ? $at : now(); + + return $this; + } + + /** + * It ends at + * + * @param null $at + * @return $this + */ + public function endsAt($at = null) + { + $this->ends_at = !is_null($at) ? $at : now()->addDays(7); + + return $this; + } + + /** + * Generate the poll + * + * @return bool + * @throws CheckedOptionsException + * @throws OptionsInvalidNumberProvidedException + * @throws OptionsNotProvidedException + */ + public function generate() + { + $totalOptions = count($this->options_add); + + // No option add yet + if ($totalOptions == 0) + throw new OptionsNotProvidedException(); + + // There must be 2 options at least + if ($totalOptions == 1) + throw new OptionsInvalidNumberProvidedException(); + + // At least one options should not be selected + if ($totalOptions <= $this->maxSelection) + throw new CheckedOptionsException(); + + // Create Poll && assign options to it + DB::transaction(function () { + $this->maxCheck = $this->maxSelection; + $this->save(); + $this->options() + ->saveMany($this->instantiateOptions()); + }); + + return true; + } + + /** + * Instantiate the options + * + * @return array + */ + private function instantiateOptions() + { + $options = []; + foreach ($this->options_add as $option) { + $options[] = new Option([ + 'name' => $option + ]); + } + + return $options; + } +} diff --git a/src/views/dashboard/edit.blade.php b/src/views/dashboard/edit.blade.php index 67b4d1e..7b89dc4 100644 --- a/src/views/dashboard/edit.blade.php +++ b/src/views/dashboard/edit.blade.php @@ -46,12 +46,12 @@ Options
- -
-
+