12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
-
- namespace App;
-
- use Exception;
- use Illuminate\Database\Eloquent\Model;
-
- class Semester extends Model
- {
- public $primaryKey = 'code';
- public $incrementing = false;
- public $timestamps = false;
-
- protected $keyType = 'string';
- protected $fillable = [
- 'code',
- 'alpha'
- ];
-
-
- public function courses() {
- return $this->belongsToMany(Course::class, 'sections');
- }
-
- public function sections() {
- return $this->hasMany(Section::class);
- }
-
- public function professors() {
- return $this->belongsToMany(Professor::class)->withPivot('admin_load', 'investigative_load');
- }
-
- public function getIsFutureAttribute() {
- return Semester::orderBy('code', 'desc')->take(6)->get()->contains('code', $this->code);
- }
-
- /**
- * Returns a clone of this semester with the provided code and alpha.
- * This functions also copies every section and professor_section
- *
- * @param string $newSemCode
- * @param string $newSemAlpha
- *
- * @return Semester
- */
- public function clone(string $newSemCode, string $newSemAlpha) {
- if (Semester::find($newSemCode)) {
- throw new Exception('A semester with code ' . $newSemCode . ' already exists');
- }
- $newSem = Semester::create([
- 'code' => $newSemCode,
- 'alpha' => $newSemAlpha,
- ]);
-
- // Copy all sections to new semester
- foreach($this->sections as $section) {
- $sectionClone = $section->replicate();
- $sectionClone->semester_code = $newSem->code;
- $sectionClone->save();
-
- foreach($section->professors as $professor) {
- $sectionClone->professors()->attach($professor, ['percent' => $professor->pivot->percent]);
- }
-
- foreach($section->schedules as $schedule) {
- $sectionClone->schedules()->save($schedule);
- }
- }
-
- return $newSem;
- }
-
- }
|