Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added rollback to 2019 artisan file #691

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions app/Console/Commands/RollbackStudentFromInstitutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Builder;

use App\Models\Institution_grade;
use PhpParser\Node\Stmt\TryCatch;

class RollbackStudentFromInstitutes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admission:rollback {institution}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Rollback promoted flag in institution_grades table to 2019';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
/*
* Set back the promoted flag in institution_grades table to 2019.
* 1. Get all the values of promoted field by each record
* 2. Assign all the records to an array
* 3. Loop through all the indexes of the array and check whether the promoted values are equal to 2019
* 4. If not, set them to 2019
*/

try {
/*
* Getting all the records by corresponding institute_id.
*/
$this->info('Fetching all the records...');
$institution_grades = DB::select('select * from institution_grades where institution_id = :id', ['id' => $this->argument('institution')]);

/*
* First check whether the array is not empty
* Then loop though all the records and check the promoted value
* If the promoted value is equal to 2019 keep it as it is otherwise set it back to 2019
*/
if (!empty($institution_grades)) {

$this->info('Fetched ' . count($institution_grades) . ' records');

foreach ($institution_grades as $institute_grade) {
if ($institute_grade->promoted == "2019") {
$this->info('Promoted year have already set to 2019');
} else {
$this->info('Updating record =======================================');
DB::update("update institution_grades set promoted ='2019' where institution_id = ?", [$this->argument('institution')]);
$this->info('Record updated ========================================');
}
}
} else {
$this->info('No results!');
}
} catch (\Exception $e) {
return $e->getMessage();
}
}
}
129 changes: 129 additions & 0 deletions app/Console/Commands/RunAddStudentsToInstitutions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

use App\Models\Institution;

class RunAddStudentsToInstitutions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admission:students {institution}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add approved students data to indtitution_student table';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
DB::enableQueryLog();
// dd('test');
$institution = Institution::where([
'id' => $this->argument('institution')
])->first();

dd($institution);

/*
if (!is_null($institution)) {

//dd($institution);

try {
$this->info('adding missing students to the institution ' . $institution->name);
$approvedstudent = DB::table('institution_student_admission')->select('*')
// ->join('institutions', 'institution_id', '=', 'institutions.id')
// ->leftJoin('institution_students', 'student_id', '=', 'institution_students.student_id')
// ->whereIn('status_id', [121, 122, 123, 124])
// ->where('institutions.id', $institution->id)
->get()->toArray();

dd(DB::getQueryLog());
// $approvedstudent = array_chunk($approvedstudent, 50);
// dd($approvedstudent);
array_walk($approvedstudent, array($this, 'addStudent'));
} catch (\Exception $e) {
Log::error($e);
}
}
*/
}

protected function addStudents($approvedstudent)
{
array_walk($approvedstudent, array($this, 'addStudent'));
}

protected function addStudent($approvedstudent)
{
$output = new \Symfony\Component\Console\Output\ConsoleOutput();
Log::info($approvedstudent);

sleep(1);
if (!(Institution_student::isDuplicated($approvedstudent) > 0)) {
$this->count += 1;
$this->student = $approvedstudent;
try {
Institution_student::insert([
'student_status_id' => 1,
'student_id' => $approvedstudent['student_id'],
'education_grade_id' => $approvedstudent['education_grade_id'],
'academic_period_id' => $approvedstudent['academic_period_id'],
'start_date' => $approvedstudent['start_date'],
'start_year' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['start_date'])->year, // $approvedstudent['start_date']->format('Y'),
'end_date' => $approvedstudent['end_date'],
'end_year' => \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $approvedstudent['end_date'])->year, //$approvedstudent['end_date']->format('Y'),
'institution_id' => $approvedstudent['institution_id'],
'admission_id' => $approvedstudent['admission_id'],
'created_user_id' => $approvedstudent['created_user_id'],
]);

if (!is_null($approvedstudent['institution_class_id'])) {
Institution_class_student::insert([
'student_id' => $approvedstudent['student_id'],
'institution_class_id' => $approvedstudent['institution_class_id'],
'education_grade_id' => $approvedstudent['education_grade_id'],
'academic_period_id' => $approvedstudent['academic_period_id'],
'institution_id' => $approvedstudent['institution_id'],
'student_status_id' => 1,
'created_user_id' => $approvedstudent['created_user_id'],
]);
}
$output->writeln('
####################################################
Total number of students updated : ' . $this->count . '
# #
####################################################');
// $output->writeln();
} catch (\Exception $e) {
// echo $e->getMessage();
$output->writeln($e->getMessage());
}
}
}
}
6 changes: 3 additions & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('import:students')
->cron('* * * * * ');
$schedule->command('import:students')
->cron('* * * * * ');
}

/**
Expand All @@ -36,7 +36,7 @@ protected function schedule(Schedule $schedule)
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
$this->load(__DIR__ . '/Commands');

require base_path('routes/console.php');
}
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
"preferred-install": "dist",
"sort-packages": true,
"github-oauth": {
"github.com": "41148d29c6dfc68853dd304015a3b8371bb09dda"
}

}
},
"extra": {
"laravel": {
Expand Down
Loading