public function importCompetitiors($competition_id, $competition_category_id, $content) { $lines = explode("\n", $content); $header = explode(';', $lines[0]); $competition_questions = CompetitionQuestionCategory::model()->with('competitionQuestion')->with('competitionQuestion.question')->findAll('competition_category_id=:competition_category_id and competitionQuestion.competition_id=:competition_id', array(':competition_category_id' => $competition_category_id, ':competition_id' => $competition_id)); $questions_to_import = array(); foreach ($competition_questions as $cq) { if ($cq == null) { $cq = new CompetitionQuestionCategory(); } $questions_to_import[trim($cq->competitionQuestion->question->title)] = $cq->competition_question_id; } echo 'Question to import:<br />'; pre_print($questions_to_import); // map fields $colQuestionMap = array(); $found_c = array(); for ($i = 0; $i < count($header); ++$i) { $col = explode('(', $header[$i]); $c = trim($col[0]); if (isset($questions_to_import[$c])) { $colQuestionMap[$i] = $questions_to_import[$c]; $found_c[] = $c; } } if (count($colQuestionMap) != count($questions_to_import)) { echo 'Question to import and question in CSV does not match!'; echo 'Question to import:<br />'; pre_print($questions_to_import); echo 'Found questions: <br />'; pre_print($found_c); echo 'CSV header line cols: <br />'; pre_print($header); echo 'Question column mapping found:<br />'; pre_print($colQuestionMap); die; } else { echo 'Question column exist, we can start importing.<br />'; } // check for required columns $required_fields = array('Priimek' => -1, 'Ime' => -1, 'Mentor' => -1, 'Šola' => -1, 'Spol' => -1, 'Razred' => -1); $required_keys = array_keys($required_fields); // not required fields $required_fields['ID1'] = -1; $required_fields['ID2'] = -1; $required_keys2 = array_keys($required_fields); for ($i = 0; $i < count($header); ++$i) { if (in_array(trim($header[$i]), $required_keys2)) { $required_fields[trim($header[$i])] = $i; } } $missing_fields = array(); for ($i = 0; $i < count($required_keys); ++$i) { if ($required_fields[$required_keys[$i]] == -1) { $missing_fields[] = $required_keys[$i]; } } if (count($missing_fields) > 0) { echo 'Some required fields are missing:<br />'; pre_print($missing_fields); die; } $competition = Competition::model()->findByPk($competition_id); if ($competition == null) { echo 'Provided Competition does not exist!'; die; } $competitionCategory = CompetitionCategory::model()->findByPk($competition_category_id); if ($competitionCategory == null) { echo 'Provided Competition Category does not exist!'; die; } for ($i = 1; $i < count($lines); ++$i) { if (trim($lines[$i]) == '') { continue; } $cols = explode(';', $lines[$i]); $cu = new CompetitionUser(); $cu->competition_category_id = $competition_category_id; $cu->competition_id = $competition_id; $cu->first_name = $cols[$required_fields['Ime']]; $cu->last_name = $cols[$required_fields['Priimek']]; $cu->gender = $cols[$required_fields['Spol']] == 'M' ? 0 : 1; $cu->class = $cols[$required_fields['Razred']]; $sc = School::model()->find('name=:name', array(':name' => trim($cols[$required_fields['Šola']]))); if ($sc == null) { echo 'Provided school not found in database! School name: ', $cols[$required_fields['Šola']], '<br />'; die; } $cu->school_id = $sc->id; $scm = SchoolMentor::model()->with('user.profile')->find('school_id=:school_id and CONCAT_WS(\' \', `profile`.`last_name`, `profile`.`first_name`) LIKE :mentor', array(':school_id' => $sc->id, ':mentor' => trim($cols[$required_fields['Mentor']]))); if ($scm == null) { echo 'Provided school mentor not found in database! School name: ', $cols[$required_fields['Šola']], ', School mentor: ', $cols[$required_fields['Mentor']], '<br />'; die; } // ali je šola prijavljena na tekmovanje za to kategorijo $ccs = CompetitionCategorySchool::model()->find('competition_id=:competition_id and competition_category_id=:competition_category_id and school_id=:school_id', array(':competition_id' => $competition_id, ':competition_category_id' => $competition_category_id, ':school_id' => $sc->id)); if ($ccs == null) { echo 'Provided school is not competing in this category! School name: ', $cols[$required_fields['Šola']], ', Competition: ', $competition->name, ', Competition category: ', $competitionCategory->name, '<br />'; die; } // ali je mentor prijavljen na tej šoli za to tekmovalno kategorijo $ccsm = CompetitionCategorySchoolMentor::model()->find('competition_category_school_id=:competition_category_school_id and user_id=:user_id', array(':competition_category_school_id' => $ccs->id, ':user_id' => $scm->user_id)); if ($ccsm == null) { echo 'Provided mentor is not competing on this school on this competition in this category! School mentor: ', $cols[$required_fields['Mentor']], ', School name: ', $cols[$required_fields['Šola']], ', Competition: ', $competition->name, ', Competition category: ', $competitionCategory->name, '<br />'; die; } $cu->competition_category_school_mentor_id = $ccsm->id; $cu->start_time = null; $cu->finish_time = null; $cu->finished = 2; if (isset($cols[$required_fields['ID1']])) { $cut = CompetitionUser::model()->findByPk($cols[$required_fields['ID1']]); if ($cut != null) { if ($cut == null) { $cut = new CompetitionUser(); } $cu->start_time = $cut->start_time; $cu->finish_time = $cut->finish_time; $cut->disqualified_reason = 'Prenos v pravilno kategorijo tekmovanja'; $cut->disqualified_request = 1; $cut->disqualified = 1; $cut->save(); } } if (isset($cols[$required_fields['ID2']])) { $cut = CompetitionUser::model()->findByPk($cols[$required_fields['ID2']]); if ($cut != null) { if ($cut == null) { $cut = new CompetitionUser(); } if ($cu->start_time == null) { $cu->start_time = $cut->start_time; $cu->finish_time = $cut->finish_time; } else { $time_diff1 = strtotime($cu->finish_time) - strtotime($cu->start_time); $time_diff2 = strtotime($cut->finish_time) - strtotime($cut->start_time); if ($time_diff2 > $time_diff1) { $cu->start_time = $cut->start_time; $cu->finish_time = $cut->finish_time; } } $cut->disqualified_reason = 'Prenos v pravilno kategorijo tekmovanja'; $cut->disqualified_request = 1; $cut->disqualified = 1; $cut->save(); } } // ali tekmovalec s temi podatki že obstaja $cuc = CompetitionUser::model()->find('competition_id=:competition_id and competition_category_id=:competition_category_id and competition_category_school_mentor_id=:competition_category_school_mentor_id and school_id=:school_id and first_name=:first_name and last_name=:last_name and gender=:gender and class=:class', array(':competition_id' => $competition_id, ':competition_category_id' => $competition_category_id, ':competition_category_school_mentor_id' => $cu->competition_category_school_mentor_id, ':school_id' => $cu->school_id, ':last_name' => $cu->last_name, ':first_name' => $cu->first_name, ':gender' => $cu->gender, ':class' => $cu->class)); if ($cuc != null) { echo 'Competition User already exists, skipping...'; continue; } $cu->save(); // import answers $question_keys = array_keys($colQuestionMap); for ($k = 0; $k < count($question_keys); ++$k) { $cuq = new CompetitionUserQuestion(); $cuq->competition_user_id = $cu->id; $cuq->competition_question_id = $colQuestionMap[$question_keys[$k]]; $cuq->ordering = $k + 1; $cuq->random_seed = number_format(mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax(), 9, '.', ''); $cuq->last_change = $cu->finish_time; $custom_answer_ex = explode('|', $cols[$question_keys[$k]]); $custom_answer = trim($custom_answer_ex[0]); $cuq->custom_answer = $custom_answer; $cuq->save(); } echo 'Successfully imported user with ID: ', $cu->id, '<br />'; } }
/** * 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 = CompetitionCategory::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.')); } return $model; }
public function GetCompetitionCategoryNameIdList() { $modelData = CompetitionCategory::model()->search(); $list = array(); foreach ($modelData->getData() as $competitionCategory) { $competitionCategory['name'] = $competitionCategory->name; $list[] = $competitionCategory; } return $list; }
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; }
/* echo CHtml::activeDropDownList($model, 'competition_id', CHtml::listData($data, 'id', 'name'), array('disabled' => $edit_all ? '' : 'disabled', 'empty' => Yii::t('app', 'choose'))); */ $dropdownOptions = array('empty' => Yii::t('app', 'choose'), 'onchange' => 'competitionChanged();', 'disabled' => $edit_all ? '' : 'disabled'); echo CustomCHtml::advancedDropDownList($model, 'competition_id', CHtml::listData($data, 'id', 'name'), $dropdownOptions, array('visible' => false), array('visible' => false)); ?> <?php echo $form->error($model, 'competition_id'); ?> </div> <div class="row"> <?php echo $form->label($model, 'competition_category_id'); if (isset($model->id)) { $data = CompetitionCategory::model()->with('competitionQuestionCategories')->with('competitionQuestionCategories.competitionQuestion')->together()->findAll('competitionQuestion.competition_id=:competition_id', array(':competition_id' => $model->competition_id)); } else { $data = array(); } $dropdownOptions = array('empty' => Yii::t('app', 'choose'), 'onchange' => 'competitionCategoryChanged();', 'disabled' => $edit_all ? '' : 'disabled'); echo CustomCHtml::advancedDropDownList($model, 'competition_category_id', CHtml::listData($data, 'id', 'name'), $dropdownOptions, array('visible' => false), array('visible' => false)); unset($data); echo $form->error($model, 'competition_category_id'); ?> <?php /* echo $form->label($model, 'competition_category_id'); $data = CompetitionCategorySchool::model()->GetCompetitionCategoryNameIdList(); echo CHtml::activeDropDownList($model, 'competition_category_id', CHtml::listData($data, 'id', 'name'), array('disabled' => $edit_all ? '' : 'disabled', 'empty' => Yii::t('app', 'choose'))); echo $form->error($model, 'competition_category_id'); */