public function actionIndex() { $redirect = false; $competition_user_id = isset(Yii::app()->session['competition_user_id']) ? Yii::app()->session['competition_user_id'] : 0; if ($competition_user_id == 0) { $redirect = true; header('Location: /index.php/StartCompetition'); die; } $competitionUser = CompetitionUser::model()->findByPk($competition_user_id); if ($competitionUser != null) { if ($competitionUser == null) { $competitionUser = new CompetitionUser(); } if ($competitionUser->start_time != null) { $duration = $competitionUser->competition->duration * 60; $starttime = strtotime($competitionUser->start_time); $endtime = $starttime + $duration; if ($competitionUser->finished == 1 || $competitionUser->finish_time != null) { if ($competitionUser->finished == 1 && $competitionUser->finish_time == null) { $competitionUser->finish_time = $endtime; $competitionUser->save(true, array('finish_time')); } else { $competitionUser->finished = 1; $competitionUser->save(true, array('finished')); } $redirect = true; } else { if ($endtime < time()) { $competitionUser->finished = 1; $competitionUser->finish_time = $endtime; $competitionUser->save(true, array('finished', 'finish_time')); $redirect = true; } } } } else { $redirect = true; } if ($redirect) { header('Location: /index.php/StartCompetition'); die; } $this->render('index'); }
public function calculateCompetitionAdvancingToNextLevel($competition_id, $competition_category_id, $number_of_points_needed_for_advancing_to_next_level = 0) { if ($number_of_points_needed_for_advancing_to_next_level == 0) { return false; } // reset advancing_to_next_level to null to all competitiors CompetitionUser::model()->updateAll(array('advancing_to_next_level' => 0), 'competition_id=:competition_id and competition_category_id=:competition_category_id', array(':competition_id' => $competition_id, ':competition_category_id' => $competition_category_id)); $cus = CompetitionUser::model()->findAll('competition_id=:competition_id and competition_category_id=:competition_category_id and disqualified=:disqualified', array(':competition_id' => $competition_id, ':competition_category_id' => $competition_category_id, ':disqualified' => 0)); foreach ($cus as $cu) { if ($cu == null) { $cu = new CompetitionUser(); } $result = $cu->getCompetitionNumericResult(true); if ((int) $result['result'] >= $number_of_points_needed_for_advancing_to_next_level) { $cu->advancing_to_next_level = 1; $cu->save(true, array('advancing_to_next_level')); } } return true; }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { if ($this->CanAccess('create')) { // just statically set the preferred_language for now. Yii::app()->session['preferred_language'] = Yii::app()->params['preferred_language']; // Yii::app()->session['preferred_language'] = 'sl'; $model = new CompetitionUser(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['CompetitionUser'])) { unset(Yii::app()->session['errorMsg']); $model->attributes = $_POST['CompetitionUser']; $search = $model->checkAccessCode(); $isOk = true; if (trim($model->first_name) == '') { $model->addError('first_name', Yii::t('app', 'Missing first name!')); $isOk = false; } if (trim($model->last_name) == '') { $model->addError('last_name', Yii::t('app', 'Missing last name!')); $isOk = false; } if (trim($model->class_numberic) == '') { $model->addError('class', Yii::t('app', 'Missing class!')); $isOk = false; } /* if (trim($model->class) == '') { $model->addError('class', Yii::t('app', 'Missing class!')); $isOk = false; }*/ if ($search == '') { $model->addError('access_code', Yii::t('app', 'Access code is wrong!')); $isOk = false; } if (!in_array($model->gender, array(0, 1))) { $model->addError('gender', Yii::t('app', 'Wrong gender!')); $isOk = false; } if ($search != null && $isOk) { if ($search->competitionCategorySchool->competitionCategory->class_from <= $model->class_numberic && $model->class_numberic <= $search->competitionCategorySchool->competitionCategory->class_to) { // ok } else { $model->addError('access_code', Yii::t('app', 'Access code is invalid for this class!')); $isOk = false; } $competition = $search->competitionCategorySchool->competition; if (!$competition->active) { $model->addError('access_code', Yii::t('app', 'Competition didn\'t start yet!')); $isOk = false; } // check if competition did even start ? /* Commented out so that people can test competiton */ if (strtotime($competition->timestamp_start) > time()) { $model->addError('access_code', Yii::t('app', 'Competition didn\'t start yet!')); $isOk = false; } // check if competition is already complete ? if (strtotime($competition->timestamp_stop) < time()) { $model->addError('access_code', Yii::t('app', 'Competition already finished!')); $isOk = false; } } if ($search != null && $isOk) { $competitionUser = CompetitionUser::model()->find('first_name=:first_name and last_name=:last_name and class=:class and competition_id=:competition_id and competition_category_id=:competition_category_id and school_id=:school_id and competition_category_school_mentor_id=:competition_category_school_mentor_id', array(':first_name' => trim($model->first_name), ':last_name' => trim($model->last_name), ':class' => trim($model->class_numberic . '.' . trim($model->class)), ':competition_id' => $search->competitionCategorySchool->competition_id, ':competition_category_id' => $search->competitionCategorySchool->competition_category_id, ':school_id' => $search->competitionCategorySchool->school_id, ':competition_category_school_mentor_id' => $search->id)); if ($competitionUser != null && $isOk) { if ($competitionUser->finished == 1 || $competitionUser->finish_time != null) { Yii::app()->session['errorMsg'] = Yii::t('app', 'User already finished competition.'); } else { Yii::app()->session['competition_user_id'] = $competitionUser->id; $this->redirect(array('view', 'id' => $competitionUser->id)); } } else { $model->first_name = mb_convert_case(trim($model->first_name), MB_CASE_TITLE, 'UTF-8'); $model->last_name = mb_convert_case(trim($model->last_name), MB_CASE_TITLE, 'UTF-8'); $model->class = trim($model->class_numberic . '.' . trim($model->class)); $model->competition_id = $search->competitionCategorySchool->competition_id; $model->competition_category_id = $search->competitionCategorySchool->competition_category_id; $model->school_id = $search->competitionCategorySchool->school_id; $model->ip_start = isset($_SERVER) && isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '/'; // to ID mentorje, tukaj naj v osnovi linkal z IDijem uporabnika, tekmovalca // $model->user_id = $search->user_id; $model->competition_category_school_mentor_id = $search->id; if ($model->save()) { Yii::app()->session['competition_user_id'] = $model->id; // if AJAX request , we should not redirect the browser if (!Yii::app()->request->isAjaxRequest) { $this->redirect(array('view', 'id' => $model->id)); } else { // UNCOMMENT THIS IF YOU WANT TO RETURN ID OF THE NEWLY CREATED // OBJECT (USEFUL WHEN CREATING NEW OBJECTS VIA AJAX AND INFO ABOUT // THEN NEWLY CREATED OBJECT MUST BE SENT TO THE BROWSER) // echo CJSON::encode(array('error' => '', 'id' => $model->id)); // die(); } } else { Yii::app()->session['errorMsg'] = Yii::t('app', 'Error registering user for competition.'); if (!Yii::app()->request->isAjaxRequest) { } else { throw new CHttpException(400, Yii::t('app', 'Bad request. The request cannot be fulfilled.')); } } } } } if (!Yii::app()->request->isAjaxRequest) { $this->render('create', array('model' => $model)); // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS, // USE THIS LINE AND DELETE THE LINE ABOVE // $this->render('create', array('model' => $model, 'ajaxRendering' => false)); } else { throw new CHttpException(400, Yii::t('app', 'Bad request. The request cannot be fulfilled.')); // IF YOU NEED DIFFERENT RENDERING FOR AJAX AND NON-AJAX CALLS, // USE THIS LINE AND DELETE THE LINE ABOVE // $this->renderPartial('create', array('model' => $model, 'ajaxRendering' => true)); } } else { throw new CHttpException(405, Yii::t('app', 'You do not have permissions to access this page.')); } }
public function actionFinishCompetition() { $competition_user_id = isset(Yii::app()->session['competition_user_id']) ? Yii::app()->session['competition_user_id'] : 0; // demo if ($competition_user_id == 0) { self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'User is not authenticated.'))); } $competitionUser = CompetitionUser::model()->findByPk($competition_user_id); if ($competitionUser != null) { if ($competitionUser == null) { $competitionUser = new CompetitionUser(); } if ($competitionUser->start_time == null) { self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'You cannot finish competition that was never started.'))); } else { if ($competitionUser->finished == 0) { $competitionUser->finished = 1; $duration = $competitionUser->competition->duration * 60; $starttime = strtotime($competitionUser->start_time); $endtime = $starttime + $duration; if ($endtime < time()) { $competitionUser->finish_time = date('Y-m-d H:i:s', $endtime); } else { $competitionUser->finish_time = date('Y-m-d H:i:s'); } $competitionUser->ip_stop = isset($_SERVER) && isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '/'; if ($competitionUser->save(true, array('finished', 'finish_time', 'ip_stop'))) { unset(Yii::app()->session['competition_user_id']); self::reponseJSON(array('success' => true)); } else { self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'Error saving competition finished state!'))); } } else { unset(Yii::app()->session['competition_user_id']); self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'You have already clicked Finish Competition!'))); } } } else { unset(Yii::app()->session['competition_user_id']); self::reponseJSON(array('success' => false, 'error' => Yii::t('app', 'User is not authenticated.'))); } }