/**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
     Schema::create('delegations', function (Blueprint $table) {
         $table->increments('id');
         $table->integer('delegate_to_id')->unsigned();
         $table->integer('delegate_from_id')->unsigned();
         $table->integer('department_id')->unsigned();
         $table->boolean('user_set')->default(0);
         $table->timestamps();
     });
     Schema::table('delegations', function ($table) {
         $table->unique(array('department_id', 'delegate_from_id'));
         //A user can only vote once on a motion
         $table->foreign('delegate_to_id')->references('id')->on('users');
         $table->foreign('delegate_from_id')->references('id')->on('users');
         $table->foreign('department_id')->references('id')->on('departments');
     });
     $validUsers = User::notCouncillor()->get();
     $departments = Department::all();
     $numberOfCouncilors = User::councillor()->count();
     if ($numberOfCouncilors) {
         foreach ($validUsers as $user) {
             foreach ($departments as $department) {
                 $councillors = User::councillor()->get();
                 $leastDelegatedToCouncillor = $councillors->sortBy('totalDelegationsTo')->first();
                 $newDelegation = new Delegation();
                 $newDelegation->department_id = $department->id;
                 $newDelegation->delegate_from_id = $user->id;
                 $newDelegation->delegate_to_id = $leastDelegatedToCouncillor->id;
                 $newDelegation->save();
             }
         }
     }
 }
 /**
  * Handle the event.
  *
  * @param  DepartmentCreated  $event
  * @return void
  */
 public function handle(DepartmentCreated $event)
 {
     $department = $event->department;
     $users = User::notCouncillor()->get();
     $councillors = User::councillor()->get();
     if ($councillors->isEmpty()) {
         return true;
     }
     foreach ($users as $user) {
         $newDelegation = new Delegation();
         $newDelegation->department_id = $department->id;
         $newDelegation->delegate_from_id = $user->id;
         $newDelegation->delegate_to_id = $councillors->random()->id;
         $newDelegation->save();
     }
 }
Ejemplo n.º 3
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     Delegation::where('user_set', 0)->delete();
     $users = User::with('delegatedFrom')->validVoter()->get();
     $departments = Department::all();
     $councillors = User::councillor()->get();
     foreach ($users as $user) {
         $user->createDefaultDelegations($departments, $councillors);
     }
 }
Ejemplo n.º 4
0
 public function createDefaultDelegations($departments = null, $councillors = null)
 {
     if (!$this->can('create-votes')) {
         return true;
     }
     if (!$departments) {
         $departments = Department::all();
     }
     if (!$councillors) {
         $councillors = User::councillor()->get();
     }
     if ($councillors->isEmpty()) {
         return true;
         // "there are no councillors";
     }
     if ($this->hasRole('councillor')) {
         return true;
         //A councillor cannot delegate
     }
     // Code to potentially do this more efficiently with fewer database calls
     // $userDelegations = $user->delegatedFrom;
     // $filteredDepartments = $departments->filter(function($item){
     //     return $item->id; + is not in a flattened array of this users delegations
     // });
     //  $insertsArray = [];
     //foreach($filteredDepartments as $filteredDepartment){
     // Add to the inserts array, at the end do one huge insert
     //}
     // $this->insert(Insert all these array items)
     foreach ($departments as $department) {
         $leastDelegatedToCouncillor = $councillors->sortBy('totalDelegationsTo')->first();
         $newDelegation = new Delegation();
         $newDelegation->department_id = $department->id;
         $newDelegation->delegate_from_id = $this->id;
         $newDelegation->delegate_to_id = $leastDelegatedToCouncillor->id;
         $newDelegation->save();
     }
 }