public function actionRender() { $competition = Competition::model()->findByPk($this->iRequest('competition_id')); $template = NewsTemplate::model()->findByPk($this->iRequest('template_id')); if ($competition === null || $template === null) { $this->ajaxOK(null); } $attributes = $template->attributes; $data = $this->generateTemplateData($competition); set_error_handler(function ($errno, $errstr) { throw new Exception($errstr); }); try { foreach ($attributes as $key => $attribute) { $attributes[$key] = preg_replace_callback('|\\{([^}]+)\\}|i', function ($matches) use($data) { $result = $this->evaluateExpression($matches[1], $data); if (is_array($result)) { $result = CHtml::normalizeUrl($result); } return $result; }, $attribute); } } catch (Exception $e) { $this->ajaxOK(null); } $this->ajaxOK($attributes); }
public function actionUpdate() { $competitions = Competition::model()->findAllByAttributes(array('type' => Competition::TYPE_WCA), array('condition' => 'date < unix_timestamp() AND date > unix_timestamp() - 86400 * 20', 'order' => 'date ASC')); $wcaDb = intval(file_get_contents(dirname(__DIR__) . '/config/wcaDb')); $sql = "UPDATE `user` `u`\r\n\t\t\t\tINNER JOIN `registration` `r` ON `u`.`id`=`r`.`user_id`\r\n\t\t\t\tLEFT JOIN `competition` `c` ON `r`.`competition_id`=`c`.`id`\r\n\t\t\t\tLEFT JOIN `wca_{$wcaDb}`.`Results` `rs`\r\n\t\t\t\t\tON `c`.`wca_competition_id`=`rs`.`competitionId`\r\n\t\t\t\t\tAND `rs`.`personName`=CASE WHEN `u`.`name_zh`='' THEN `u`.`name` ELSE CONCAT(`u`.`name`, ' (', `u`.`name_zh`, ')') END\r\n\t\t\t\tSET `u`.`wcaid`=`rs`.`personId`\r\n\t\t\t\tWHERE `u`.`wcaid`='' and `r`.`competition_id`=%id%"; $db = Yii::app()->db; $num = []; foreach ($competitions as $competition) { $num[$competition->id] = $db->createCommand(str_replace('%id%', $competition->id, $sql))->execute(); } echo 'updated wcaid: ', array_sum($num), PHP_EOL; Yii::import('application.statistics.*'); Yii::app()->cache->flush(); $data = Statistics::getData(true); echo 'set results_statistics_data: ', $data ? 1 : 0, PHP_EOL; }
public static function getCompetition($row) { $cacheKey = 'results_competition_data_' . $row['competitionId']; $cache = Yii::app()->cache; if (self::$_competitions === array()) { $competitions = Competition::model()->with('location', 'location.province', 'location.city')->cache(self::CACHE_EXPIRE)->findAll(array('condition' => 'wca_competition_id!=""', 'select' => 't.name, t.name_zh, t.wca_competition_id')); foreach ($competitions as $competition) { self::$_competitions[$competition->wca_competition_id] = array('name' => $competition->name, 'name_zh' => $competition->name_zh, 'city_name' => $competition->isMultiLocation() ? 'Multiple' : (in_array($competition->location[0]->province_id, array(215, 525, 567, 642)) ? $competition->location[0]->province->name : $competition->location[0]->city->name . ', ' . $competition->location[0]->province->name), 'city_name_zh' => $competition->isMultiLocation() ? '多地' : (in_array($competition->location[0]->province_id, array(215, 525, 567, 642)) ? $competition->location[0]->province->name_zh : $competition->location[0]->province->name_zh . $competition->location[0]->city->name_zh), 'url' => array('/results/c', 'id' => $competition->wca_competition_id)); } } if (isset(self::$_competitions[$row['competitionId']])) { $data = self::$_competitions[$row['competitionId']]; } elseif (($data = $cache->get($cacheKey)) === false) { $data['name'] = $data['name_zh'] = $row['cellName']; $data['city_name'] = $data['city_name_zh'] = $row['cityName']; $data['url'] = array('/results/c', 'id' => $row['competitionId']); $cache->set($cacheKey, $data, self::CACHE_EXPIRE); self::$_competitions[$row['competitionId']] = $data; } return array_merge($row, $data); }
public function beforeAction($action) { if (parent::beforeAction($action)) { $criteria = new CDbCriteria(); $criteria->with = array('organizer' => array('together' => true)); $criteria->compare('organizer.organizer_id', Yii::app()->user->id); $criteria->compare('t.status', Competition::STATUS_SHOW); $competitions = Competition::model()->findAll($criteria); foreach ($competitions as $competition) { if (!$competition->isScheduleFinished()) { $this->alerts[] = array('url' => array('/board/competition/edit', 'id' => $competition->id), 'label' => sprintf('"%s"赛程不完整', $competition->name_zh)); } } Yii::app()->language = 'zh_cn'; if (strpos($action->id, 'edit') !== false) { $this->setReferrer(); } return true; } return false; }
public static function canShowAwardField($competition_id) { $superuser = Generic::isSuperAdmin(); if (!$superuser) { // check if visible by competition settings $cache_key = 'CCompetition-mentor-awards-timestamp-' . $competition_id; $cache = Yii::app()->cache->get($cache_key); if ($cache == null) { $competition = Competition::model()->findByPk($competition_id); if ($competition != null) { $cache = $competition->timestamp_mentor_awards == null ? '-' : $competition->timestamp_mentor_awards; } else { $cache = '-'; } Yii::app()->cache->set($cache_key, $cache, 600); } if ($cache == '-') { return false; } else { $timestamp = strtotime($cache); if ($timestamp > time()) { return false; } } } return true; }
public function GetCompetitionNameIdList() { $modelData = Competition::model()->search(true); $list = array(); foreach ($modelData->getData() as $competition) { $competition['name'] = $competition->name; $list[] = $competition; } return $list; }
public function actionToggle() { $id = $this->iRequest('id'); $model = Competition::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'Not found'); } if ($this->user->isOrganizer()) { throw new CHttpException(401, 'Unauthorized'); } $model->formatEvents(); $model->formatDate(); $attribute = $this->sRequest('attribute'); $model->{$attribute} = 1 - $model->{$attribute}; $model->save(); $this->ajaxOk(array('value' => $model->{$attribute})); }
public function actionPreviewNotice() { $id = $this->iGet('id'); $competition = Competition::model()->findByPk($id); if ($competition === null) { throw new CHttpException(404, '未知比赛ID'); } if ($this->user->isOrganizer() && !isset($competition->organizers[$this->user->id])) { throw new CHttpException(403, '权限不足'); } $competition->formatEvents(); $registration = new Registration(); $registration->unsetAttributes(); $registration->competition_id = $id; $model = new SendNoticeForm(); if (isset($_POST['SendNoticeForm'])) { $model->attributes = $_POST['SendNoticeForm']; } echo json_encode($model->getPreview($competition)); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Competition::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.')); } return $model; }
public function importMentorsWithIdsAndCodes($competition_id, $country_id, $csv) { $country = Country::model()->findByPk($country_id); if ($country == null) { echo 'Country does not exist!'; die; } $competition = Competition::model()->findByPk($competition_id); if ($competition == null) { echo 'Competition does not exist!'; die; } header('Content-Type: text/html; charset=utf-8'); if ($country->country == 'Srbija') { // popravek imen kategorij, ki se parsajo $cols = array('Mentor ID' => 'mentor_id', 'kategorija' => 'category_name', 'koda' => 'access_code'); } else { $cols = array('Mentor ID' => 'mentor_id', 'kategorija' => 'category_name', 'koda' => 'access_code'); } $lines = explode("\n", trim($csv)); $header_line = $lines[0]; $header_cols = explode(';', trim($header_line)); $keys = array_keys($cols); $cols_matrix = array(); $index = 0; foreach ($header_cols as $h_cols) { $h_cols = trim($h_cols); if (in_array($h_cols, $keys)) { $cols_matrix[$index] = $cols[$h_cols]; } $index++; } if (count($cols_matrix) != count($cols)) { echo 'One of required header columns is missing. Required columns: ', implode(', ', $keys), "<br />"; die; } // pre_print($cols_matrix); $datas = array(); for ($i = 1; $i < count($lines); ++$i) { $col = explode(';', trim($lines[$i])); $data = array(); $index = 0; foreach ($col as $c) { $data[$cols_matrix[$index]] = trim($c); $index++; } $datas[] = $data; } // pre_print($datas); $categoryCache = array(); $imported = 0; foreach ($datas as $data) { if (isset($data['mentor_id']) && $data['mentor_id'] != '' && mb_substr($data['mentor_id'], 0, 1, 'UTF-8') == 'M') { $mentor_id = trim(ltrim($data['mentor_id'], "M")); $category_name = $data['category_name']; if (isset($categoryCache[$category_name])) { $competition_category_id = $categoryCache[$category_name]; } else { $competitionCategory = CompetitionCategory::model()->find('country_id=:country_id and name=:name', array(':country_id' => $country_id, ':name' => $category_name)); if ($competitionCategory == null) { echo 'Competition Category ', $category_name, ' cannot be found in database.'; die; } else { $categoryCache[$category_name] = $competitionCategory->id; $competition_category_id = $categoryCache[$category_name]; } } $access_code = $data['access_code']; $schoolMentor = SchoolMentor::model()->findByPk($mentor_id); if ($schoolMentor == null) { echo 'School mentor with ID: ', $mentor_id, ' could not be found!'; die; } // check if school already on competition in this category $competitionCategorySchool = CompetitionCategorySchool::model()->find('competition_id=:cid and competition_category_id=:ccid and school_id=:sid', array(':cid' => $competition_id, ':ccid' => $competition_category_id, ':sid' => $schoolMentor->school_id)); if ($competitionCategorySchool == null) { $competitionCategorySchool = new CompetitionCategorySchool(); $competitionCategorySchool->competition_id = $competition_id; $competitionCategorySchool->competition_category_id = $competition_category_id; $competitionCategorySchool->school_id = $schoolMentor->school_id; if (!$competitionCategorySchool->save()) { echo 'Error saving competition category school!'; die; } } // check if access code already in use $competitionCategorySchoolMentor = CompetitionCategorySchoolMentor::model()->find('access_code=:access_code', array(':access_code' => $access_code)); if ($competitionCategorySchoolMentor != null) { if ($competitionCategorySchoolMentor->user_id != $schoolMentor->user_id || $competitionCategorySchoolMentor->competition_category_school_id != $competitionCategorySchool->id) { echo 'Cannot import access code: ', $access_code, ', because is already used by id: ', $competitionCategorySchoolMentor->id; die; } } else { $competitionCategorySchoolMentor = new CompetitionCategorySchoolMentor(); $competitionCategorySchoolMentor->access_code = $access_code; $competitionCategorySchoolMentor->competition_category_school_id = $competitionCategorySchool->id; $competitionCategorySchoolMentor->user_id = $schoolMentor->user_id; if (!$competitionCategorySchoolMentor->save()) { echo 'Error saving CompetitionCategorySchoolMentor for access code: ', $access_code; die; } else { $imported++; } } } else { echo 'Error importing data: <br />'; pre_print($data); die; } } echo '<br />Number of imported access codes: ', $imported; }
public function actionC() { $id = $this->sGet('id'); $type = $this->sGet('type', 'winners'); $types = Competitions::getResultsTypes(); $competition = Competitions::model()->findByAttributes(array('id' => $id)); if ($competition == null) { $this->redirect(array('/results/competition')); } if (!array_key_exists($type, $types)) { $type = 'winners'; } if (($c = Competition::model()->findByAttributes(array('wca_competition_id' => $id))) !== null) { $competition->name = $c->getAttributeValue('name'); $competition->location = $c->isMultiLocation() ? $c->getLocationInfo('venue') : $c->location[0]->getFullAddress(false); $competition->c = $c; } $data = Yii::app()->cache->getData(array('Competitions', 'getResults'), $id); if (empty($data['scrambles']) && $type !== 'scrambles') { unset($types['scrambles']); } $data['competition'] = $competition; $data['type'] = $type; $data['types'] = $types; $this->breadcrumbs = array('Results' => array('/results/index'), 'Competitions' => array('/results/competition'), $competition->name); $this->pageTitle = array($competition->name, 'Competition Results'); $this->title = $competition->name; // $this->setWeiboShareDefaultText($competition->name . '选手的魔方速拧成绩页 - 粗饼·中国魔方赛事网', false); $this->render('c', $data); }