- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
public function onAnswerPoll()
    {
        $data = request()->all();
        ValidatePollForm::run($data);
        try {
            $options = Option::find($data['option_ids']);
            $log = Crypt::decrypt($data['log']);
            $log['options'] = array_merge(
                array_get($log, 'options', []),
                $options->lists('id')
            );
            $log['comments'] = array_get($log, 'comments', []) + array_get($data, 'comments', []);
            $this->log = Crypt::encrypt($log);
            $this->option = $options->first();
            $this->poll = $this->loadPoll();
            $this->locations = Location::get();
            $this->step = ++$data['step'];
            if ($this->option->is_last) {
                Log::store($this->poll, $log);
                Option::whereIn('id', $log['options'])->get()->each(function ($item) {
                    $item->increment('votes');
                    $item->logs()->create();
                });
            }
        } catch (Exception $e) {
            trace_log($e);
            return response()
                ->json('Something was wrong.', 500);
        }
    }
    /**
     * onLoadDepartments
     */
    public function onLoadDepartments()
    {
        $data = request()->all();
        $validator = Validator::make($data, [
            'location' => 'required|exists:kitsoft_polls_locations,slug',
            'answer_id' => 'required|exists:kitsoft_polls_answers,id'
        ]);
        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
        try {
            $this->departments = Department::make()
                ->whereHas('locations', function ($query) use ($data) {
                    return $query->where('slug', $data['location']);
                })
                ->whereHas('answers', function ($query) use ($data) {
                    return $query->where('id', $data['answer_id']);
                })
                ->get();
        } catch (Exception $e) {
            trace_log($e);
            return response()
                ->json('Something was wrong.', 500);
        }
    }