Created
April 9, 2017 21:10
-
-
Save makoru-hikage/97e61122be775f65e89f77281e89c574 to your computer and use it in GitHub Desktop.
Just some random gist, a demonstration of "return $this" pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DeltaX\Aegis\ModelServices\GradesRegistry; | |
use DeltaX\Aegis\MenuServices\Checklist; | |
use Carbon\Carbon; | |
use DeltaX\Utilities as U; | |
class StudentChecklist extends Checklist { | |
/** | |
* $student | |
* @var \DeltaX\Models\Student | |
*/ | |
protected $student; | |
/** | |
* columns | |
* @var array | |
*/ | |
protected $columns = [ | |
'courses.code', | |
'courses.name', | |
'courses.credits_in_lecture', | |
'courses.credits_in_lab', | |
'curricula.lec_contact_hrs', | |
'curricula.lab_contact_hrs', | |
'curricula.is_nth_yr_standing', | |
'curriculum_terms.year_level', | |
'instructor.first_name', | |
'instructor.last_name', | |
'semester_attended.date_start', | |
'semester_attended.part_number', | |
'semester_attended.division_type', | |
'enrolled_courses.final_grade', | |
'remarks.remark' | |
]; | |
/** | |
* setDegree | |
* @param string $code | |
* @return self | |
*/ | |
public function setStudent(string $studentNumber) { | |
$student = $this->repoInvoker->find('student', $studentNumber); | |
if (empty($student)){ | |
throw new InvalidStudentNumberException; | |
} | |
$this->student = $student; | |
$this->searchFilter['student_id'] = ['enrolled_courses.student_id', '=', $this->student->id]; | |
return $this; | |
} | |
/** | |
* runQuery | |
* @return \Illuminate\Database\Eloquent\Collection|null | |
*/ | |
protected function prepareQuery() { | |
$this->repoInvoker->entity('curricula', function($entity){ | |
return $entity | |
->selectRaw('GROUP_CONCAT(prerequisite_courses.code) AS prerequisite') | |
->join('courses', 'curricula.course_id', '=', 'courses.id') | |
->join('curriculum_terms', 'curricula.curriculum_term_id', '=', 'curriculum_terms.id') | |
->leftJoin('curriculum_prerequisites', 'curriculum_prerequisites.dependent_id', '=', 'curricula.id') | |
->leftJoin('curricula AS prerequisites', 'curriculum_prerequisites.prerequisite_id', '=', 'prerequisites.id') | |
->join('courses AS prerequisite_courses','curricula.course_id', '=', 'prerequisite_courses.id') | |
->leftJoin('enrolled_courses', 'enrolled_courses.curriculum_id', '=', 'curricula.id') | |
->join('term_attendance', 'enrolled_courses.term_attendance_id', '=', 'term_attendance.id') | |
->join('school_calendar AS semester_attended', 'term_attendace.school_calendar_id', '=', 'semester_attended.id') | |
->leftJoin('remarks', 'enrolled_courses.remark_id', '=', 'remarks.id') | |
->leftJoin('course_sessions', 'enrolled_courses.course_session_id', '=', 'course_sessions.id') | |
->leftJoin('employees', 'course_sessions.employee_id', '=', 'employees.id') | |
->leftJoin('users AS instructors', 'employees.user_id', '=', 'instructors.id') | |
->groupBy('curricula.id'); | |
})->where($params); | |
return $this; | |
} | |
protected function runQuery(){ | |
$this->outputData = $this | |
->prepareTermFilter() | |
->prepareYearLevelFilter() | |
->repoInvoker | |
->get(); | |
return $this; | |
} | |
protected function createInstructorColumn($collection){ | |
$this->outputData = $collection->map(function ($item){ | |
$initial = substr($item['first_name'], 0, 1) . '.'; | |
$item['instructor'] = $initial . ' ' . $item['last_name']; | |
unset($item['first_name']); | |
unset($item['last_name']); | |
return $item; | |
}); | |
return $this; | |
} | |
protected function createTermTakenColumn($collection){ | |
$termTypes = [ | |
'Semester' => 6, | |
'Trimester' => 4, | |
'Quarter' => 3 | |
]; | |
$this->outputData = $collection->map(function ($item) use ($termTypes) { | |
$partNumber = $item['part_number']; | |
$numberOfMonths = $termTypes[$item['division_type']]; | |
$subtrahend = ($part_number == 1) ? | |
0 : ($partNumber * $numberOfMonths); | |
$yearStart = Carbon::parse($item['date_start']) | |
->subMonth($subtrahend)->year; | |
$yearEnd = $yearStart + 1; | |
$nthTerm = U\ordinal($item['part_number']); | |
//(e.g. 2nd, 2012-2013) | |
$item['term_taken'] = $nthTerm . ', ' . $yearStart . '-' .$yearEnd; | |
unset($item['part_number']); | |
unset($item['division_type']); | |
unset($item['date_start']); | |
return $item; | |
}); | |
return $this; | |
} | |
public function getData(){ | |
$this | |
->createPrerequisiteColumn() | |
->createInstructorColumn() | |
->createTermTakenColumn(); | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment