The getDynamicTeacherCount
function is generating number-based reports from the table that I have in database and the getDynamicPercentage
function generates percentage for these reports; then I am calling the getReports
function from the related controller and pass the getReport
returned array to the Laravel View and making a HTML table from that array.
The main problem is that with too many parameters needed when calling the getDynamicTeacherCount
and getDynamicPercentage
functions, the functions unreadable and hard to change or maintain.
namespace App;
use Illuminate\Database\Eloquent\Model;
class GeneralCBE extends Model
{
/**
* The database table name used by this model.
* @var String
*/
protected $table = 'general_cbe';
/**
* The attributes that are guarded.
* @var array
*/
protected $guarded = ['id'];
/**
* The constant for all applicants.
* @var Const
*/
const ALL = '';
/**
* The constant for all active applicants.
* @var Const
*/
const ACTIVE = '1';
/**
* The constant for all Dative applicants.
* @var Const
*/
const DEACTIVATE = '2';
/**
* The constant for all male applicants.
* @var Const
*/
const MALE = '1';
/**
* The constant for all female applicants.
* @var Const
*/
const FEMALE = '2';
/**
* The constant for all rejected applicants.
* @var Const
*/
const REJECTED = '6';
/**
* The constant for all those who rejected by reason one.
* @var Const
*/
const REASONONE = '1'; /**
* The constant for all those who rejected by reason tow.
* @var Const
*/
const REASONTOW = '2';
/*
* the constant for decisions
*/
private $GRANTED = array('tier2' => '1', 'etier2' => '2', 'tier3' => '3', 'etier3' => '4', 'tier4' => '5');
/**
* @param string $cbe
* @param string $decision
* @param string $institution
* @param string $province
* @param string $district
* @return array
*/
public static function getReports($cbe = '', $decision = '', $institution = '', $province = '', $district = '')
{
return [
'genallt' => self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::ALL, $province, $district, $institution),
'general_m_c' => self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::MALE, $province, $district, $institution),
'general_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::ALL, self::ALL, self::MALE, $province, $district, $institution),
'general_f_c' => self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::FEMALE, $province, $district, $institution),
'general_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::ALL, self::ALL, self::FEMALE, $province, $district, $institution),
'rejected' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution),
'rejected_male' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution),
'rejected_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution),
'rejected_female' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution),
'rejected_female_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution),
'rejected_percentage' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::ALL, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution),
'rejected_reason1' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason1_m_c' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason1_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '1'), $cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason1_f_c' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason1_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '1'), $cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason1_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '1'),
'rejected_reason2' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '2'),
'rejected_reason2_m_c' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution, 'rejected_reason', '2'),
'rejected_reason2_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '2'), $cbe, self::REJECTED, self::ALL, self::MALE, $province, $district, $institution, 'rejected_reason', '2'),
'rejected_reason2_f_c' => self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution, 'rejected_reason', '2'),
'rejected_reason2_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '2'), $cbe, self::REJECTED, self::ALL, self::FEMALE, $province, $district, $institution, 'rejected_reason', '2'),
'rejected_reason2_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution), $cbe, self::REJECTED, self::ALL, self::ALL, $province, $district, $institution, 'rejected_reason', '2'),
'allt' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution),
'male' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $district),
'malep' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $district), $cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $district),
'fmale' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $district),
'fmalep' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $district), $cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $district),
'edu_ttc' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ttc_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution), $cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ttc_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ttc_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '4'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ttc_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ttc_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '4'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '4'),
'edu_ba' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ba_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution), $cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ba_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ba_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '2'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ba_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ba_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '2'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '2'),
'edu_ma' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '3'),
'edu_ma_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution), $cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '3'),
'edu_ma_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '3'),
'edu_ma_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '3'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'level_of_edu', '3'),
'edu_ma_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '3'),
'edu_ma_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'level_of_edu', '3'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'level_of_edu', '3'),
'edu_prim_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '1'),
'edu_prim_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '1'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '1'),
'edu_prim_m_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '1'),
'edu_prim_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '1'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '1'),
'edu_prim_f_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '1'),
'edu_prim_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '1'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '1'),
'edu_sec_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '2'),
'edu_sec_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '2'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '2'),
'edu_sec_m_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '2'),
'edu_sec_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '2'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '2'),
'edu_sec_f_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '2'),
'edu_sec_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '2'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '2'),
'edu_uper_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '3'),
'edu_uper_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '3'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '3'),
'edu_uper_m_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '3'),
'edu_uper_m_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '3'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_level', '3'),
'edu_uper_f_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '3'),
'edu_uper_f_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_level', '3'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_level', '3'),
'pashto_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '1'),
'pashto_per' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '1'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '1'),
'pashto_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '1'),
'pasto_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '1'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '1'),
'pashto_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '1'),
'pasto_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '1'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '1'),
'dari_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '2'),
'dari_per' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '2'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '2'),
'dari_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '2'),
'dari_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '2'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '2'),
'dari_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '2'),
'dari_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '2'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '2'),
'other_count' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '3'),
'other_per' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '3'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '3'),
'other_male_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '3'),
'other_male_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '3'),$cbe, $decision, self::ACTIVE, self::MALE, $province, $district, $institution, 'teaching_language', '3'),
'other_fmale_c' => self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '3'),
'other_fmale_p' => self::getDynamicPercentage(self::getDynamicTeacherCount($cbe, $decision, self::ACTIVE, self::ALL, $province, $district, $institution, 'teaching_language', '3'),$cbe, $decision, self::ACTIVE, self::FEMALE, $province, $district, $institution, 'teaching_language', '3')
];
}
/**
* give you the percentage of counts that comes from db
* @param $val
* @param $all
* @return float
*/
public static function getPercentage($val, $all)
{
return round($val * 100 / $all);
}
/**
* get all rejected teacher count
* @return mixed
*/
public static function getRejectedTeacherCount()
{
return self::where('decision', 3)->count();
}
/**
* return number of active teacher
* @return mixed
*/
public static function getAllActiveTeacherCount()
{
return self::activeTeacherObj()->count();
}
/**
* returns the dynamic count of teachers base on function arguments.
* many staff is automated in this function for example get raided
* of many where clauses and many if conditions
* @param string $cbe
* @param string $decision
* @param string $active
* @param string $gender
* @param string $province
* @param string $district
* @param string $institution
* @param string $additionFieldKey
* @param string $additionFieldValue
* @return mixed
*/
public static function getDynamicTeacherCount($cbe = '', $decision = '', $active = '', $gender = '', $province = '', $district = '',
$institution = '', $additionFieldKey = '', $additionFieldValue = '')
{
$fields = [
'tier' => $cbe,
'decision' => $decision,
'active' => $active,
'gender' => $gender,
'pro_province' => $province,
'pro_district' => $district,
'institution_where_studied' => $institution,
$additionFieldKey => $additionFieldValue
];
$result = new self;
foreach ($fields as $attr => $value)
{
if(! empty($value))
{
$result = $result->where($attr, $value);
}
}
return number_format($result->count());
}
/**
* returns the dynamic Percentage of teachers base on function arguments.
* many staff is automated in this function for example get raided
* of many where clauses and many if conditions
* @param $yValue
* @param string $cbe
* @param string $decision
* @param string $active
* @param string $gender
* @param string $province
* @param string $district
* @param string $institution
* @param string $additionFieldOne
* @param string $additionFieldTow
* @return mixed
*/
public static function getDynamicPercentage($yValue, $cbe = '', $decision = '', $active = '', $gender = '', $province = '', $district = '',
$institution = '', $additionFieldOne = '', $additionFieldTow = '')
{
$result = self::getDynamicTeacherCount($cbe, $decision, $active, $gender, $province, $district,
$institution, $additionFieldOne, $additionFieldTow);
$intResult = intval(str_replace(',', '', $result));
return self::getPercentage($intResult, intval(str_replace(',', '', $yValue)));
}
/**
* returns all teachers that the are active
* @return static
*/
public static function activeTeacherObj()
{
return self::where('active', 1);
}
}