public static function isCoordinator() { $cache_key = 'userIsCoordinator#' . Yii::app()->user->id; $cached = Yii::app()->cache->get($cache_key); if ($cached == null) { $cached = false; $schoolMentor = SchoolMentor::model()->find('user_id=:user_id and coordinator=:coordinator', array(':user_id' => Yii::app()->user->id, ':coordinator' => 1)); if ($schoolMentor != null) { $cached = true; } Yii::app()->cache->set($cache_key, $cached, 600); } return $cached; }
public function actionGet() { $key = Yii::app()->request->getParam('key', null); if ($key != null) { $key = base64_decode($key); $key = str_replace('/', '', $key); $user_id = isset(Yii::app()->user->id) ? Yii::app()->user->id : 0; $dir = dirname(__FILE__) . '/../../shared/' . $user_id . '/'; if (is_dir($dir)) { if (file_exists($dir . '/' . $key)) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Disposition: attachment; filename=\"" . $key . "\";"); header('Content-type: ' . CFileHelper::getMimeType($dir . '/' . $key)); header("Content-Transfer-Encoding: binary"); readfile($dir . '/' . $key); die; } } // list mentor directories $mentors = SchoolMentor::model()->findAll('user_id=:user_id', array(':user_id' => $user_id)); $key = mb_substr($key, 1, mb_strlen($key, 'UTF-8') - 1, 'UTF-8'); foreach ($mentors as $mentor) { $dir = dirname(__FILE__) . '/../../shared/M' . $mentor->id . '/'; if (is_dir($dir)) { if (file_exists($dir . '/' . $key)) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Disposition: attachment; filename=\"" . $key . "\";"); header('Content-type: ' . CFileHelper::getMimeType($dir . '/' . $key)); header("Content-Transfer-Encoding: binary"); readfile($dir . '/' . $key); die; } } } } }
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 = SchoolMentor::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; }
?> <?php echo $form->error($model, 'competition_category_school_id'); ?> </div> <?php $user = User::model()->find('id=:id', array(':id' => Yii::app()->user->id)); if ($user != null && $user->profile->user_role > 5) { ?> <div class="row"> <?php echo $form->labelEx($model, 'user_id'); ?> <?php $data = SchoolMentor::model()->with('user.profile')->with('school')->together()->findAll(array('condition' => 'profile.user_role>=:user_role', 'params' => array(':user_role' => 5), 'order' => 'profile.last_name ASC, profile.first_name ASC, school.name ASC')); $user_list = array(); $schools_per_user = array(); foreach ($data as $dataUser) { if (!isset($user_list[$dataUser->user->id])) { $user_list[$dataUser->user->id] = $dataUser->user->profile->last_name . ' ' . $dataUser->user->profile->first_name; } if (!isset($schools_per_user[$dataUser->user->id])) { $schools_per_user[$dataUser->user->id] = array(); } $schools_per_user[$dataUser->user->id][] = $dataUser->school->name; } foreach ($user_list as $mentor_user_id => $userData) { if (isset($schools_per_user[$mentor_user_id])) { if (count($schools_per_user[$mentor_user_id]) > 0) { $user_list[$mentor_user_id] .= ' (' . implode(', ', $schools_per_user[$mentor_user_id]) . ')';
public function actionLoadSchools() { $userData = Yii::app()->getRequest()->getPost('Profile', array()); $country_id = isset($userData['country_id']) ? $userData['country_id'] : 0; if ($country_id != 0) { $data = School::model()->findAll('country_id=:country_id', array(':country_id' => $country_id)); $listData = array(); foreach ($data as $models) { $value = $models['id']; $coordinator = SchoolMentor::model()->find('school_id=:school_id', array(':school_id' => $value)); if ($coordinator != null || $coordinator != 0) { $name = $coordinator->user->profile->first_name; $lastName = $coordinator->user->profile->last_name; $text = $models['name'] . ' ( ' . Yii::t('app', 'Coordinator') . ': ' . $name . ' ' . $lastName . ' )'; } else { $text = $models['name']; } $listData[$value] = $text; } $data = $listData; // $data = CHtml::listData($data,"id","name"); echo CHtml::tag('option', array('value' => 'choose'), Yii::t('app', 'choose'), true); foreach ($data as $value => $name) { //CHtml::encode($name) echo CHtml::tag('option', array('value' => $value), $name, true); } } else { echo 'Invalid country'; } }