Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class importController extends Controller {
- function __construct() {
- parent::__construct();
- if(!@$_COOKIE['wanting']) {
- echo "...";
- exit();
- }
- }
- /*
- * Creates a theoretical situation in which, three users, work on 6 projects simultaneosly.
- * * Users work 8 hours a day, 5 days a week
- * * Proyects are 'Proyectos', 'Plataforma', 'Filtros', 'Gamelandia', 'Relatividad', 'Portatiles'
- * * Users are 'Pedro Worcel', 'José Martinez' and 'Albert Einstein'
- * * Pedro worcel spends most of his time working at project 'Proyectos', but also works at 'Plataforma', 'Gamelandia', and 'Filtros'
- * * José Martinez works at 'Gamelandia', 'plataforma' and 'portatiles'.
- * * Albert Einstein works at 'Relatividad' and 'Gamelandia'
- */
- function generate_dummy() {
- /*
- * PROJECTS
- */
- $projects = array(
- 'proyectos',
- 'plataforma',
- 'gamelandia',
- 'filtros',
- 'relatividad',
- 'portatiles'
- );
- $this->load->model('projects');
- foreach($projects as $project) {
- $this->projects->name = $project;
- $this->projects->description = generate_string(80);
- $this->projects->date_creation = generate_date("-3 months", '-0 months');
- $this->projects->SaveNew();
- $project_ids[$project] = $this->projects->projectsId;
- }
- /*
- * USERS
- */
- $this->load->model('users');
- $this->users->username = "PedroWorcel";
- $this->users->password = $this->auth->HashPassword('password');
- $this->users->name = "Pedro";
- $this->users->last_name = "Worcel";
- $this->users->role = 1;
- $this->users->date_registration = NOW();
- $this->users->SaveNew();
- $users['pedro_worcel'] = $this->users->usersId;
- $this->users->username = "JoseMartinez";
- $this->users->password = $this->auth->HashPassword('password');
- $this->users->name = "Jose";
- $this->users->last_name = "Martinez";
- $this->users->role = 1;
- $this->users->date_registration = NOW();
- $this->users->SaveNew();
- $users['jose_martinez'] = $this->users->usersId;
- $this->users->username = "AlbertEnstein";
- $this->users->password = $this->auth->HashPassword('password');
- $this->users->name = "Albert";
- $this->users->last_name = "Einstein";
- $this->users->role = 1;
- $this->users->date_registration = NOW();
- $this->users->SaveNew();
- $users['albert_einstein'] = $this->users->usersId;
- /*
- * USERS PROJECTS
- */
- $users_projects['proyectos'] = array(
- $users['pedro_worcel'],
- );
- $users_projects['plataforma'] = array(
- $users['pedro_worcel'],
- $users['jose_martinez']
- );
- $users_projects['filtros'] = array(
- $users['pedro_worcel']
- );
- $users_projects['gamelandia'] = array(
- $users['pedro_worcel'],
- $users['jose_martinez'],
- $users['albert_einstein']
- );
- $users_projects['relatividad'] = array(
- $users['albert_einstein']
- );
- $users_projects['portatiles'] = array(
- $users['jose_martinez']
- );
- $this->load->model('users_projects');
- foreach($users_projects as $project_name => $project_users) {
- $project_id = $project_ids[$project_name];
- foreach($project_users as $project_user) {
- $this->users_projects->usersId = $project_user;
- $this->users_projects->projectsId = $project_id;
- $this->users_projects->SaveNew();
- }
- }
- /*
- * DIVISIONS
- */
- $this->load->model('divisions');
- $divisions = array(
- 'contenido',
- 'ciencia',
- 'juegos'
- );
- foreach($divisions as $division) {
- $this->divisions->name = $division;
- $this->divisions->detail = generate_string();
- $this->divisions->date_creation = NOW();
- $this->divisions->SaveNew();
- $division_ids[$division] = $this->divisions->divisionsId;
- }
- /*
- * PROJECTS DIVISIONS
- */
- $projects_divisions = array(
- 'contenido' => array(
- 'plataforma',
- 'filtros',
- 'portatiles'
- ),
- 'ciencia' => array(
- 'relatividad',
- 'proyectos'
- ),
- 'juegos' => array(
- 'gamelandia'
- )
- );
- $this->load->model('projects_divisions');
- foreach($projects_divisions as $division => $projects) {
- $division_id = $division_ids[$division];
- foreach($projects as $project) {
- $this->projects_divisions->divisionsId = $division_id;
- $this->projects_divisions->projectsId = $project_ids[$project];
- $this->projects_divisions->SaveNew();
- }
- }
- /*
- * USERS PRICES
- */
- $users_prices = array(
- $users["pedro_worcel"] => array(
- '2010-01-01' => '2700',
- '2010-08-01' => '3100'
- ),
- $users['jose_martinez'] => array(
- '2010-01-01' => '2200',
- '2010-08-01' => '2700'
- ),
- $users['albert_einstein'] => array(
- '2010-01-01' => '3200',
- '2010-08-01' => '4000'
- ),
- );
- $this->load->model("users_prices");
- foreach($users_prices as $usersId => $users_price) {
- foreach($users_price as $date => $price) {
- $this->users_prices->usersId = $usersId;
- $this->users_prices->price = $price;
- $this->users_prices->date_updated = $date;
- $this->users_prices->SaveNew();
- }
- }
- /*
- * REPORTS
- */
- /*tendency*/
- function get_tendency($users, $project_ids) {
- foreach($users as $user) {
- global $controller;
- $fcv_where = array(
- array(
- 'usersId',
- '=',
- $user
- )
- );
- $user_projects = $controller->users_projects->GetList($fcv_where);
- $workedHours = 0;
- $x = 1;
- foreach($user_projects as $user_project) {
- if($workedHours > 8) break;
- if($user == $users['pedro_worcel']) {
- if($user_project->projectsId == $project_ids['proyectos']) {
- $rand = mt_rand(4,8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['proyectos']] = $rand;
- } elseif($user_project->projectsId == $project_ids['plataforma']) {
- $rand = mt_rand(0,8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['plataforma']] = $rand;
- } elseif($user_project->projectsId == $project_ids['filtros']) {
- $rand = mt_rand(0,8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['filtros']] = $rand;
- } elseif($user_project->projectsId == $project_ids['gamelandia']) {
- $rand = mt_rand(0,8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['gamelandia']] = $rand;
- }
- } elseif($user == $users['jose_martinez']) {
- if($user_project->projectsId == $project_ids['plataforma']) {
- $rand = mt_rand(4,8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['plataforma']] = $rand;
- } elseif($user_project->projectsId == $project_ids['gamelandia']) {
- $rand = mt_rand(0, 8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['gamelandia']] = $rand;
- } elseif($user_project->projectsId == $project_ids['portatiles']) {
- $rand = mt_rand(0, 8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['portatiles']] = $rand;
- }
- } elseif($user == $users['albert_einstein']) {
- if( $user_project->projectsId == $project_ids['gamelandia']) {
- $rand = mt_rand(0, 8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['gamelandia']] = $rand;
- } elseif($user_project->projectsId == $project_ids['relatividad'] ) {
- $rand = mt_rand(0, 8-$workedHours);
- $workedHours += $rand;
- $tendency[$user][$project_ids['relatividad']] = $rand;
- }
- }
- $x++;
- if($x == count($user_projects) && $workedHours < 8) {
- @$tendency[$user][$project_ids[array_search($user_projects[mt_rand(0, count($user_projects) - 1)]->projectsId, $project_ids)]] += 8 - $workedHours;
- }
- }
- }
- return $tendency;
- }
- $months = array(
- 'january' => range(1,31),
- 'february' => range(1,28),
- 'march' => range(1,31),
- 'april' => range(1,30),
- 'may' => range(1,31),
- 'june' => range(1,30),
- 'july' => range(1,31),
- 'august' => range(1,31),
- 'september' => range(1,date("j"))
- );
- $a = -1;
- $this->load->model('reports');
- foreach($months as $month => $days) {
- foreach($days as $day) {
- $date = date("Y-m-d H:i:s", strtotime("$month $day 2010 16:00"));
- if($a == 0) {
- $isWorkingDay = true;
- } elseif($a == 5) {
- $isWorkingDay = false;
- } elseif($a == 6) {
- $a = -1;
- }
- if(@$isWorkingDay) {
- $projects_worked = get_tendency($users, $project_ids);
- foreach($projects_worked as $user_id => $project) {
- foreach($project as $project_id => $worked_hours) {
- $this->reports->projectsId = $project_id;
- $this->reports->usersId = $user_id;
- $this->reports->date_creation = $date;
- $this->reports->hours_invested = $worked_hours;
- $this->reports->detail = generate_string(mt_rand(0, 120));
- $this->reports->SaveNew();
- }
- }
- }
- $a++;
- }
- }
- }
- function import_csv() {
- error_reporting(E_ALL);
- $csv_handle = fopen(SITE_PATH . "/csv/reportes_diarios.csv", "r");
- /*
- * first parse.
- * Creates and stores
- * divisions
- * projects
- * projects_divisions
- * users
- * users_projects
- */
- $a = 1;
- while(false !== ($line = fgetcsv($csv_handle))) {
- if($a > 2) {
- $date_creation = trim($line[0]);
- $user = trim($line[1]);
- $exploded_user = explode(" ", $user);
- $name = $this->standarize($exploded_user[0]);
- $last_name = $this->standarize(@$exploded_user[1]);
- $division = $this->standarize($line[2]);
- $project = $this->standarize($line[3]);
- $duracion_tarea = trim($line[4]);
- $detalle = trim($line[5]);
- /* divisions */
- if(null === ($divisions[$division] = $this->db->get_var("SELECT divisionsId FROM divisions WHERE name = '$division'"))) {
- $insertArray = array(
- 'name' => $division
- );
- $this->db->insert('divisions', $insertArray);
- $divisions[$division] = mysql_insert_id();
- }
- /* projects */
- if(null === ($projects[$project] = $this->db->get_var("SELECT * FROM projects WHERE name = '$project'"))) {
- $insertArray = array(
- 'name' => $project
- );
- $this->db->insert('projects', $insertArray);
- $projects[$project] = mysql_insert_id();
- }
- /* projects_divisions */
- if(null === ($this->db->get_var("SELECT projects_divisionsId
- FROM projects_divisions
- WHERE projectsId = {$projects[$project]}
- AND divisionsId = {$divisions[$division]}"))) {
- $insertArray = array(
- 'projectsid' => $projects[$project],
- 'divisionsid' => $divisions[$division]
- );
- $this->db->insert('projects_divisions', $insertArray);
- }
- /* users */
- if(null === ($users[$user] = $this->db->get_var("SELECT usersId FROM users WHERE name = '$name' AND last_name = '$last_name'"))) {
- $password = "password";
- $hashed = $this->auth->HashPassword($password);
- $insertArray = array(
- 'username' => $name,
- 'password' => $hashed,
- 'name' => $name,
- 'last_name' => $last_name,
- 'email' => 'invalid_email@hotmail.com',
- 'role' => 1,
- 'date_registration' => NOW()
- );
- $this->db->insert('users', $insertArray);
- $users[$user] = mysql_insert_id();
- }
- /* users_projects */
- if(null == $this->db->get_var("SELECT users_projectsId FROM users_projects WHERE usersId = {$users[$user]} AND projectsId = {$projects[$project]}")) {
- $insertArray = array(
- 'usersId' => $users[$user],
- 'projectsId' => $projects[$project]
- );
- $this->db->insert("users_projects", $insertArray);
- }
- } else {
- $a++;
- }
- }
- rewind($csv_handle);
- /*
- * second parse
- * Creates reports
- */
- $a = 1;
- while(false !== ($line = fgetcsv($csv_handle))) {
- if($a > 2) {
- $date_creation = trim($line[0]);
- $date_exploded = explode("/", $date_creation);
- $new_date = $date_exploded[1] . "/" . $date_exploded[0] . "/" . $date_exploded[2];
- $new_date = date_mysql(strtotime($new_date));
- $user = trim($line[1]);
- $exploded_user = explode(" ", $user);
- $name = $this->standarize($exploded_user[0]);
- $last_name = $this->standarize($exploded_user[1]);
- $division = $this->standarize($line[2]);
- $project = $this->standarize($line[3]);
- $detail = trim($line[5]);
- $duracion_tarea = explode(":", trim($line[4]));
- $hours_invested = $duracion_tarea[0];
- if($duracion_tarea[1] != "00") {
- $converted_minutes = $duracion_tarea[1] / 60;
- $hours_invested += $converted_minutes;
- }
- $hours_invested = round($hours_invested, 2);
- $insertArray = array(
- 'projectsid' => $projects[$project],
- 'usersid' => $users[$user],
- 'date_creation' => $new_date,
- 'hours_invested' => $hours_invested,
- 'detail' => $detail
- );
- $this->db->insert('reports', $insertArray);
- } else {
- $a++;
- }
- }
- if(!@$errors) {
- echo "Este mensaje debería de indicar que todo marchó bien. Pero en realidad no indica nada.";
- }
- }
- function prettify_names() {
- $this->load->model("divisions");
- $divisions = $this->divisions->GetList();
- foreach($divisions as $division) {
- $division->name = str_replace("-", " ", ucfirst($division->name));
- $division->Save();
- }
- $this->load->model("projects");
- $projects = $this->projects->GetList();
- foreach($projects as $project) {
- $project->name = preg_replace_callback("-", " ", ucfirst($project->name));
- $project->Save();
- }
- $this->load->model("users");
- $users = $this->users->GetList();
- foreach($users as $user) {
- $user->name = ucfirst($user->name);
- $user->last_name = ucfirst($user->last_name);
- $user->Save();
- }
- echo "All good.";
- }
- function standarize($text, $encoding = 'utf-8') {
- global $encodign;
- if($encodign) {
- $encoding = $encodign;
- }
- $text = preg_replace ( '~[^\\pL\d]+~u', '-', $text );
- $text = trim ( $text, '-' );
- if (function_exists ( 'iconv' )) {
- $text = iconv ( $encoding, 'us-ascii//TRANSLIT', $text );
- }
- $text = strtolower ( $text );
- // remove unwanted characters
- $text = preg_replace ( '~[^-\w]+~', '', $text );
- if(empty($text)) {
- return false;
- }
- return $text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement