Skip to content

Commit

Permalink
General Courses Feature Now Added
Browse files Browse the repository at this point in the history
  • Loading branch information
therealsmat committed Feb 12, 2018
1 parent 8ec0747 commit 2c90c67
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 10 deletions.
13 changes: 12 additions & 1 deletion app/Http/Controllers/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ public function index(Department $department, Course $course, Request $request)
'semester' => $sem,
'session' => $sess
])->get();

$generalCourses = $course->where([
'level' => $level,
'semester' => $sem,
'session' => $sess
])->get();

$courses = collect($courses)->merge($generalCourses)->unique()->values()->all();

$departments = $department->all();
return view('courses', compact('departments', 'courses'));
}

public function store(Request $request, Course $course)
{
try{
$course->create($request->all());
$data = $request->all();
$data['dept'] = $data['dept'] ?? 0;
$course->create($data);
return redirect()->back()->with('success', 'Course created successfully');
} catch (\Exception $e)
{
Expand Down
27 changes: 27 additions & 0 deletions app/Http/Controllers/LevelsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers;

use App\Course;
use App\Department;
use Illuminate\Http\Request;

class LevelsController extends Controller
{
public function index(Department $department, Course $course, Request $request)
{
$level = $request->level ?? 0;
$sem = $request->sem ?? 0;
$sess = $request->sess ?? 0;
$dept = $request->dept ?? 0;

$courses = $course->where([
'level' => $level,
'semester' => $sem,
'session' => $sess,
'dept' => $dept
])->get();
$title = 'Level';
return view('level', compact('title', 'courses'));
}
}
9 changes: 5 additions & 4 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public function store(Request $request, Setting $setting)
$record = $setting->where('key', $key);
if ($record->exists()) {
$record->update(['value' => $val]);
} else {
$record->create([
'key' => $key,
'value' => $val
]);
}
$record->create([
'key' => $key,
'value' => $val
]);
}
return redirect()->back()->with('success', 'Setting Saved Successfully');
}
Expand Down
13 changes: 11 additions & 2 deletions app/Http/Controllers/TimeTableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ class TimeTableController extends Controller
public function generate(Course $course, TimeTable $table)
{
$condition = request()->all();
if (!isset($condition['dept'])) $condition['dept'] = 0;

$courses = $course->where($condition)->get()->toArray();

$levelWideCourses = null;
$newCondition = $condition;
$newCondition['dept'] = 0;
if ($table->alreadyHas($newCondition)) {
$levelWideCourses = $table->where($newCondition)->first()->schedule;
}

try{
$timeTable = new TimeTableGenerator($courses);
$timeTable->levelWideCourse(json_decode($levelWideCourses, true));
$timeTable = $timeTable->generate();

if ($table->alreadyHas($condition)) {
$table->where($condition)->update(['schedule' => json_encode($timeTable) ]);
} else {
$table->create([
'dept' => $condition['dept'],
'dept' => $condition['dept'] ?? 0,
'level' => $condition['level'],
'semester' => $condition['semester'],
'session' => $condition['session'],
Expand All @@ -33,7 +42,7 @@ public function generate(Course $course, TimeTable $table)
return redirect()->route('timetable.index', $condition);
} catch (\Exception $e)
{
return redirect()->back()->with('error', $e->getMessage());
return redirect()->back()->with('error', $e->getTraceAsString());
}
}

Expand Down
32 changes: 31 additions & 1 deletion app/TimeTableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class TimeTableGenerator {
*/
protected $gene;

protected $levelWideCourses = null;

/**
* Individual courses represented as a whole
* in multiples of their units...
Expand Down Expand Up @@ -88,6 +90,17 @@ public function initializeTimeTable()
return $this->timeTable = array(array(), array());
}

/**
* Add level wide courses if they exists
* @param $courses
* @return $this
*/
public function levelWideCourse($courses)
{
$this->levelWideCourses = $courses;
return $this;
}

/**
* This method compares the available slots
* with the total number of units.
Expand Down Expand Up @@ -132,12 +145,24 @@ private function createInitialGeneration()
for ($z = 0; $z < $this->sizeOfDay; $z++) {
$indicator = $this->emptyIndicator;
if ($z == $this->indexOfBreak) $indicator = $this->break[$y];
if ($this->hasLevelWideCourses()) {
if ($this->levelWideCourses[$y][$z] != '-') $indicator = $this->levelWideCourses[$y][$z];
}
$this->timeTable[$y][$z] = $indicator;
}
}
return $this;
}

/**
* Checks if the level wide courses is not null
* @return bool
*/
public function hasLevelWideCourses()
{
return !is_null($this->levelWideCourses);
}

/**
* Perform the actual schedule
* @return TimeTableGenerator
Expand All @@ -156,7 +181,12 @@ private function startSelection()
if ($this->isBreakTimeZone($seed)) continue;
$row = (int) ($seed / $this->sizeOfDay);
$col = $seed % $this->sizeOfDay;
$this->timeTable[$row][$col] = $this->chromosomes[$x];

if ($this->hasLevelWideCourses() && $this->levelWideCourses[$row][$col] != '-') {
$this->timeTable[$row][$col] = $this->levelWideCourses[$row][$col];
} else {
$this->timeTable[$row][$col] = $this->chromosomes[$x];
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
$table->integer('semester')->unsigned();
$table->integer('level')->unsigned();
$table->string('session');
$table->string('schedule');
$table->text('schedule');
$table->timestamps();
});
}
Expand Down
1 change: 1 addition & 0 deletions resources/views/courses.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<button class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">
<i class="glyphicon glyphicon-plus"></i> New
</button>&nbsp;

@if(count($courses))
<a href="/generate?dept={{ request('dept') }}&level={{ request('level') }}&semester={{ request('sem') }}&session={{ request('sess') }}"
class="btn btn-success pull-right" style="margin-right:5px;">
Expand Down
1 change: 1 addition & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
@else
<li><a href="{{ route('courses') }}">Course</a></li>
<li><a href="{{ route('departments') }}">Departments</a></li>
<li><a href="{{ route('level') }}">Level</a></li>
<li><a href="{{ route('settings') }}">Settings</a></li>
{{--<li><a href="{{ route('timetable.index') }}">Time table</a></li>--}}
<li class="dropdown">
Expand Down
174 changes: 174 additions & 0 deletions resources/views/level.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading" style="overflow: hidden;">Levels
@if(count($courses))
<a href="/generate?dept=0&level={{ request('level') }}&semester={{ request('sem') }}&session={{ request('sess') }}"
class="btn btn-success pull-right" style="margin-right:5px;">
<i class="glyphicon glyphicon-tasks"></i> Generate Time Schedule
</a>
<a href="/time-table?level={{ request('level') }}&semester={{ request('sem') }}&session={{ request('sess') }}"
class="btn btn-info pull-right" style="margin-right:5px;">
<i class="glyphicon glyphicon-eye-open"></i> View Schedule
</a>
@endif
<button class="btn btn-default pull-right" data-toggle="modal" data-target="#myModal2" style="margin-right: 5px;">
<i class="glyphicon glyphicon-plus"></i> New
</button>&nbsp;
</div>

<div class="panel-body">
<form action="">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="level">Level</label>
<select name="level" id="level" class="form-control">
@for($x = 100; $x <= 700; $x += 100)
<option value="{{ $x }}" {{ request('level') == $x ? 'SELECTED' : '' }}>{{ $x }}</option>
@endfor
</select>
</div>
</div>

<div class="col-md-2">
<div class="form-group">
<label for="semester">Semester</label>
<select name="sem" id="semester" class="form-control">
<option value="1" {{ request('sem') == 1 ? 'SELECTED' : '' }}>1st</option>
<option value="2" {{ request('sem') == 2 ? 'SELECTED' : '' }}>2nd</option>
</select>
</div>
</div>

<div class="col-md-2">
<div class="form-group">
<label for="semester">Session</label>
<input type="text" name="sess" class="form-control" value="2017/2018">
<input type="hidden" name="dept" class="form-control" value="0">
</div>
</div>

<div class="col-md-3">
<div class="form-group">
<br>
<button class="btn btn-success">Apply!</button>
</div>
</div>

</div>
</form>

<hr>

@if(count($courses))
<table class="table table-responsive table-striped">
<tr>
<th>#</th>
<th>Course Title</th>
<th class="text-center">Course Code</th>
<th class="text-center">Units</th>
<th>Lecture Time</th>
<th>Lecturer</th>
</tr>
@foreach($courses as $course)
<tr>
<td>{{ $loop->iteration }}</td>
<td><a href="#">{{ strtoupper($course->title) }}</a></td>
<td class="text-center"><code>{{ $course->code }}</code></td>
<td class="text-center">{{ $course->units }}</td>
<td class="text-center"><code>{{ $course->units }}hrs</code></td>
<td>{{ 'NIL' }}</td>
</tr>
@endforeach
</table>

@else
<p class="text-center text-danger">No courses available</p>
@endif

</div>
</div>
</div>
</div>

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form action="{{ route('course.store') }}">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">New Course</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title">Course Title</label>
<input type="text" class="form-control" name="title">
</div>
</div>

<div class="col-md-3">
<div class="form-group">
<label for="title">Course Code</label>
<input type="text" class="form-control" name="code">
</div>
</div>

<div class="col-md-2">
<div class="form-group">
<label for="title">Units</label>
<input type="number" class="form-control" min="0" name="units">
</div>
</div>
</div>

<div class="row">
<div class="col-md-3">
<div class="form-group">
<label for="title">Level</label>
<select name="level" id="level" class="form-control">
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
<option value="500">500</option>
<option value="600">600</option>
</select>
</div>
</div>

<div class="col-md-3">
<div class="form-group">
<label for="title">Semester</label>
<select name="semester" id="semester" class="form-control">
<option value="1">1st</option>
<option value="2">2nd</option>
</select>
</div>
</div>

<div class="col-md-3">
<div class="form-group">
<label for="title">Session</label>
<input type="text" class="form-control" value="2017/2018" name="session">
<input type="hidden" name="general" value="1">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
2 changes: 1 addition & 1 deletion resources/views/timetable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading" style="overflow: hidden;">
<h4 class="text-center" style="text-transform: uppercase">
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
Route::delete('departments/remove/{id}', 'DepartmentsController@remove')->name('department.remove');
Route::get('time-table', 'TimeTableController@index')->name('timetable.index');

Route::get('level', 'LevelsController@index')->name('level');

Route::get('settings', 'SettingsController@index')->name('settings');
Route::post('settings/store', 'SettingsController@store')->name('settings.store');

Expand Down

0 comments on commit 2c90c67

Please sign in to comment.