public static function generateRandomShifts() { for ($i = 1; $i <= 31; $i++) { // gets 5 random employees $emps = range(1, 10); shuffle($emps); $emp = array_splice($emps, 0, 5); $mgr = rand(11, 15); $month = 8; $day = $i; $year = 2015; foreach ($emps as $key => $emp) { $start = rand(0, 23); $shiftLen = rand(2, 6); if ($start + $shiftLen > 23) { $end = $start; $start = $end - $shiftLen; } else { $end = $start + $shiftLen; } // save to shifts $x = new WiwShift(); $x->attributes = ['manager_id' => $mgr, 'employee_id' => $emp, 'start_time' => "{$year}-{$month}-{$day} {$start}:00:00", 'end_time' => "{$year}-{$month}-{$day} {$end}:00:00", 'created_at' => date("Y-m-d H:i:s")]; $x->save(); } } }
/** * This just deletes the newly create shift to keep redundant clutter out of the schema table */ private function deleteShiftExample($response) { $obj = json_decode($response->getBody()); $shift = WiwShift::findOne($obj->id); $shift->delete(); return true; }
public function getShifts() { return $this->hasMany(WiwShift::className(), ['employee_id' => 'id']); }
/** * Updates and existing shift * * This function handles a few possibilities: * As a manager, I want to be able to change a shift, by updating the time details. * As a manager, I want to be able to assign a shift, by changing the employee that will work a shift. * @example PUT http://api.domain.com/shifts/update/155 --data '{"start_time":"2015-08-01 01:00:00"}' * NOTE: When testing with Postman, I have to use x-www-form-urlencode to submit body data * * @param integer $id The id of the shift to update * * @return array On success the updated shift is returned * */ public function actionUpdate($id) { $shift = WiwShift::findOne($id); $shift->attributes = \Yii::$app->request->getBodyParams(); if($shift->save()){ $shift->refresh(); return $shift; } else { $list = ''; foreach ($shift->errors as $error) { $list .= $error[0]; } throw new \yii\web\HttpException(422, 'Data validation failed. ' .$list); } }