Exemple #1
0
 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();
         }
     }
 }
	/**
	 * Creates a new shift
     * 
     * As a manager, I want to schedule my employees, by creating shifts for any employee.
     * The body can include fields from the Shift Object (REQUIRED: start_time, end_time, employee_id, manager_id)
	 *     @example POST http://api.domain.com/shifts/create --data '{"manager_id":12, "employee_id":3, "start_time":"2015-08-01 01:00:00", "end_time":"2015-08-01 06:00:00"}'
     * 
     * @return array On success the new shift information is returned
     * 
	 */
	public function actionCreate()
	{
        
        // create shift object
        $shift = new WiwShift(['scenario' => WiwShift::SCENARIO_CREATE]);
        $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);
        }
        
	}