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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow automatic shift exchanges #57

Open
wants to merge 4 commits 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
9 changes: 6 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_DATABASE=swap
DB_USERNAME=root
DB_PASSWORD=swap

BROADCAST_DRIVER=log
CACHE_DRIVER=file
Expand All @@ -28,6 +28,9 @@ MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

SOLVER_HOST=10.0.2.2
SOLVER_PORT=4567

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
72 changes: 72 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/focal64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network "private_network", ip: "192.168.56.21"
config.vm.synced_folder "./", "/vagrant", type: "rsync", rsync__auto: true, rsync__exclude: ['./node_modules*']
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
110 changes: 110 additions & 0 deletions app/Http/Controllers/EnrollmentAutomaticExchangeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App\Http\Controllers;

use App\Judite\Models\Course;
use App\Judite\Models\Exchange;
use App\Judite\Models\Enrollment;
use App\Judite\Models\Shift;
use App\Judite\Models\solver;
use Illuminate\Support\Facades\DB;
use App\Events\ExchangeWasConfirmed;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\AutoExchange\CreateRequest;
use App\Exceptions\EnrollmentCannotBeExchangedException;
use App\Exceptions\ExchangeEnrollmentsOnDifferentCoursesException;

class EnrollmentAutomaticExchangeController extends Controller
{
/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('can.student');
$this->middleware('student.verified');
$this->middleware('can.exchange');
}

/**
* Show the form for creating a new resource.
*
* @param int $id
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
*/
public function create($id)
{
try {
$data = DB::transaction(function () use ($id) {
$enrollment = Auth::student()->enrollments()->findOrFail($id);
$course = $enrollment->course()->first();
$to_shift_tag = $enrollment->shift()->value('tag');

if (! $enrollment->availableForExchange()) {
throw new \LogicException('The enrollment is not available for exchange.');
}

$matchingShifts = $course->shifts()->where('tag','!=',$to_shift_tag)->orderBy('tag','asc')->get();

return compact('enrollment','to_shift_tag', 'matchingShifts');
});

$data['matchingShifts'] = $data['matchingShifts']->map(function ($item) {
return [
'tag' => $item->tag,
'_toString' => $item->present()->inlineToString(),
];
});
return view('autoExchanges.create', $data);

} catch (\LogicException $e) {
flash($e->getMessage())->error();

return redirect()->route('dashboard');
}
}

/**
* Store a newly created resource in storage.
*
* @param int $id
* @param \App\Http\Requests\AutoExchange\CreateRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store($id, CreateRequest $request)
{
try {

$exchange = DB::transaction(function () use ($id, $request) {


$fromEnrollment = Auth::student()->enrollments()->findOrFail($id);
$toShift = $fromEnrollment->course()->first()->getShiftByTag($request->input('to_shift_tag'));
$toEnrollment = Enrollment::make();
$toEnrollment->student()->associate(null);
$toEnrollment->course()->associate($fromEnrollment->course()->first());
$toEnrollment->shift()->associate($toShift);
$toEnrollment->save();

$exchange = Exchange::make();
$exchange->setExchangeEnrollments($fromEnrollment, $toEnrollment);
$exchange->save();

return $exchange;
});
DB::beginTransaction();
$message = 'The exchange was successfully saved';
flash($message)->success();
Solver::SolveAutomicExchangesOfCourse($exchange->course());
DB::commit();

} catch (EnrollmentCannotBeExchangedException | ExchangeEnrollmentsOnDifferentCoursesException $e) {
flash($e->getMessage())->error();
}

return redirect()->route('dashboard');
}
}
8 changes: 7 additions & 1 deletion app/Http/Controllers/ExchangeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ public function decline($id)
public function destroy($id)
{
DB::transaction(function () use ($id) {
Auth::student()->requestedExchanges()->findOrFail($id)->delete();
$exchange=Auth::student()->requestedExchanges()->findOrFail($id);
$enrollment=$exchange->toEnrollment;
$exchange->delete();
if ($enrollment->student==null){
$enrollment->delete();
}
});
flash('The shift exchange request was successfully deleted.')->success();

return redirect()->back();
}

}
27 changes: 27 additions & 0 deletions app/Http/Middleware/AuthorizeAutoExchangeActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Middleware;

use Closure;

class AuthorizeExchangeActions
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (! app('settings')->withinExchangePeriod()) {
flash('The exchanges period is closed. You are not allowed to perform this action.')->error();

return redirect()->route('dashboard');
}

return $next($request);
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/AutoExchange/CreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\AutoExchange;

use Illuminate\Foundation\Http\FormRequest;

class CreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'to_shift_tag' => 'required',
];
}
}
Loading