Completed
Pull Request — master (#173)
by Corey
02:55
created

CheckinForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace site\models;
3
4
use Yii;
5
use yii\base\Model;
6
use yii\db\Expression;
7
8
/**
9
 * Checkin form
10
 */
11
class CheckinForm extends Model
12
{
13
  public $behaviors1;
14
  public $behaviors2;
15
  public $behaviors3;
16
  public $behaviors4;
17
  public $behaviors5;
18
  public $behaviors6;
19
  public $behaviors7;
20
21
  public $compiled_behaviors;
22
23
  /**
24
   * @inheritdoc
25
   */
26
  public function rules()
27
  {
28
    return [
29
      [
30
        [
31
          'behaviors1',
32
          'behaviors2',
33
          'behaviors3',
34
          'behaviors4',
35
          'behaviors5',
36
          'behaviors6',
37
          'behaviors7'
38
        ],
39
        'validateBehaviors'],
40
    ];
41
  }
42
43
  public function attributeLabels() {
44
    return [
45
      'behaviors1' => 'Restoration',
46
      'behaviors2' => 'Forgetting Priorities',
47
      'behaviors3' => 'Anxiety',
48
      'behaviors4' => 'Speeding Up',
49
      'behaviors5' => 'Ticked Off',
50
      'behaviors6' => 'Exhausted',
51
      'behaviors7' => 'Relapsed/Moral Failure'
52
    ];
53
  }
54
55
  public function setBehaviors($behaviors) {
56
    foreach($behaviors as $category_id => $category_data) {
57
      $attribute = "behaviors$category_id";
58
			$this->$attribute = [];
59
      foreach($category_data['behaviors'] as $behavior) {
60
        $this->{$attribute}[] = $behavior['id'];
61
      }
62
    }   
63
  }
64
65
  public function validateBehaviors($attribute, $params) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
  public function validateBehaviors($attribute, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    if (!$this->hasErrors()) {
67
      foreach($this->$attribute as $behavior) {
68
        if(!is_numeric($behavior)) {
69
          $this->addError($attribute, 'One of your behaviors is not an integer!');
70
        }
71
      }
72
    }
73
  }
74
75
  public function compileBehaviors() {
76
    $behaviors = array_merge((array)$this->behaviors1,
77
                           (array)$this->behaviors2,
78
                           (array)$this->behaviors3,
79
                           (array)$this->behaviors4,
80
                           (array)$this->behaviors5,
81
                           (array)$this->behaviors6,
82
                           (array)$this->behaviors7);
83
84
    return array_filter($behaviors); // strip out false values
85
  }
86
87
  public function deleteToday() {
88
    $time = Yii::$container->get(\common\interfaces\TimeInterface::class);
89
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
90
91
    $date = $time->getLocalDate();
92
    list($start, $end) = $time->getUTCBookends($date);
93
94
    $user_behavior->deleteAll("user_id=:user_id 
95
      AND date > :start_date 
96
      AND date < :end_date", 
97
      [
98
        "user_id" => Yii::$app->user->id, 
99
        ':start_date' => $start, 
100
        ":end_date" => $end
101
      ]
102
    );
103
104
    // delete cached behaviors
105
    array_map(function($period) use ($time) {
106
      $key = "checkins_".Yii::$app->user->id."_{$period}_".$time->getLocalDate();
107
      Yii::$app->cache->delete($key);
108
    }, [30, 90, 180]);
109
  }
110
111
  public function save() {
112
    if(empty($this->compiled_behaviors)) {
113
      $this->commpiled_behaviors = $this->compileBehaviors();
0 ignored issues
show
Bug Best Practice introduced by
The property commpiled_behaviors does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
    }
115
116
    $user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class);
117
118
    $rows = [];
119
    foreach($this->compiled_behaviors as $behavior_id) {
120
      $temp = [
121
        Yii::$app->user->id,
122
        (int)$behavior_id,
123
        new Expression("now()::timestamp")
124
      ];
125
      $rows[] = $temp;
126
    }
127
128
    Yii::$app
129
      ->db
130
      ->createCommand()
131
      ->batchInsert(
132
        $user_behavior->tableName(),
133
        ['user_id', 'behavior_id', 'date'],
134
        $rows
135
      )->execute();
136
137
    // if the user has publicised their check-in graph, create the image
138
    if(Yii::$app->user->identity->expose_graph) {
0 ignored issues
show
Bug introduced by
Accessing expose_graph on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
139
      $checkins_last_month = $user_behavior->getCheckInBreakdown();
140
141
      if($checkins_last_month) {
142
        Yii::$container
143
          ->get(\common\components\Graph::class, [Yii::$app->user->identity])
144
          ->create($checkins_last_month, true);
145
      }
146
    }
147
  }
148
}
149