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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send new status email #87

Merged
merged 4 commits into from
Aug 28, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function finalizeStatements()
$next_semester = Semester::next();
foreach ($users as $user) {
if (! $user->isInSemester($next_semester)) {
$user->setStatusFor($next_semester, Semester::INACTIVE, 'Failed to make a statement');
$user->setStatusFor($next_semester, SemesterStatus::INACTIVE, 'Failed to make a statement');
}
}
}
Expand Down
47 changes: 47 additions & 0 deletions app/Mail/StatusUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Mail;

use App\Models\Semester;
use App\Models\SemesterStatus;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class StatusUpdated extends Mailable
{
use Queueable;
use SerializesModels;

public User $recipient;
public string $semester;
public string $status;
public ?string $comment;
public User $modifier;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(SemesterStatus $semesterStatus)
{
$this->recipient = $semesterStatus->user;
$this->semester = $semesterStatus->semester->tag;
$this->status = __('user.'.$semesterStatus->status);
$this->comment = $semesterStatus->comment;
$this->modifier = auth()->user();
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.status_updated')
->subject(__('mail.status_updated', ['semester' => $this->semester, 'status' => $this->status]));
}
}
32 changes: 0 additions & 32 deletions app/Models/Semester.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ class Semester extends Model
private const SEPARATOR = '-';

public const PARTS = [1, 2];
public const ACTIVE = 'ACTIVE';
public const INACTIVE = 'INACTIVE';
public const DEACTIVATED = 'DEACTIVATED';
public const PASSIVE = 'PASSIVE';
public const PENDING = 'PENDING';
public const STATUSES = [
self::ACTIVE,
self::INACTIVE,
self::DEACTIVATED,
self::PASSIVE,
self::PENDING,
];

// Values are in month
// TODO: change to dates?
Expand Down Expand Up @@ -299,26 +287,6 @@ public static function getOrCreate($year, $part): Semester
return $semester;
}

/**
* Returns the color belonging to the status.
*/
public static function colorForStatus($status): string
{
switch ($status) {
case self::ACTIVE:
return 'green';
case self::INACTIVE:
return 'grey';
case self::DEACTIVATED:
return 'brown';
case self::PASSIVE:
return 'orange';
case self::PENDING:
return 'lime';
default:
return 'black';
}
}

/* Helpers */

Expand Down
75 changes: 75 additions & 0 deletions app/Models/SemesterStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

/**
* @property User $user
* @property Semester $semester
* @property string $status
* @property string $comment
*/
class SemesterStatus extends Pivot
{
protected $table = 'semester_status';

public $timestamps = false;

public $incrementing = false;

protected $primaryKey = null;

protected $fillable = [
'user_id',
'semester_id',
'status',
'comment',
'verified'
];

public const ACTIVE = 'ACTIVE';
public const INACTIVE = 'INACTIVE';
public const DEACTIVATED = 'DEACTIVATED';
public const PASSIVE = 'PASSIVE';
public const PENDING = 'PENDING';
public const STATUSES = [
self::ACTIVE,
self::INACTIVE,
self::DEACTIVATED,
self::PASSIVE,
self::PENDING,
];

public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class);
}

public function semester(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Semester::class);
}


/**
* Returns the color belonging to the status.
*/
public static function color($status): string
{
switch ($status) {
case self::ACTIVE:
return 'green';
case self::INACTIVE:
return 'grey';
case self::DEACTIVATED:
return 'brown';
case self::PASSIVE:
return 'orange';
case self::PENDING:
return 'lime';
default:
return 'black';
}
}
}
9 changes: 5 additions & 4 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public static function printers()
*/
public function allSemesters()
{
return $this->belongsToMany(Semester::class, 'semester_status')->withPivot(['status', 'verified', 'comment']);
return $this->belongsToMany(Semester::class, 'semester_status')->withPivot(['status', 'verified', 'comment'])->using(SemesterStatus::class);
}

/**
Expand All @@ -453,15 +453,16 @@ public function semestersWhere($status)
{
return $this->belongsToMany(Semester::class, 'semester_status')
->wherePivot('status', '=', $status)
->withPivot('verified', 'comment');
->withPivot('verified', 'comment', 'status')
->using(SemesterStatus::class);
}

/**
* Returns the semesters where the user has the given status.
*/
public function activeSemesters()
{
return $this->semestersWhere(Semester::ACTIVE);
return $this->semestersWhere(SemesterStatus::ACTIVE);
}

/**
Expand Down Expand Up @@ -571,7 +572,7 @@ public function getStatusIn($semester): string
{
$semesters = $this->allSemesters;
if (! $semesters->contains($semester)) {
return Semester::INACTIVE;
return SemesterStatus::INACTIVE;
}

return $semesters->find($semester)->pivot->status;
Expand Down
65 changes: 65 additions & 0 deletions app/Observers/StatusObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Observers;

use App\Models\SemesterStatus;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class StatusObserver
{
/**
* Handle the SemesterStatus "created" event.
*
* @param \App\Models\SemesterStatus $semesterStatus
* @return void
*/
public function created(SemesterStatus $semesterStatus)
{
Mail::to($semesterStatus->user)->queue(new \App\Mail\StatusUpdated($semesterStatus));
}

/**
* Handle the SemesterStatus "updated" event.
*
* @param \App\Models\SemesterStatus $semesterStatus
* @return void
*/
public function updated(SemesterStatus $semesterStatus)
{
Mail::to($semesterStatus->user)->queue(new \App\Mail\StatusUpdated($semesterStatus));
}

/**
* Handle the SemesterStatus "deleted" event.
*
* @param \App\Models\SemesterStatus $semesterStatus
* @return void
*/
public function deleted(SemesterStatus $semesterStatus)
{
//
}

/**
* Handle the SemesterStatus "restored" event.
*
* @param \App\Models\SemesterStatus $semesterStatus
* @return void
*/
public function restored(SemesterStatus $semesterStatus)
{
//
}

/**
* Handle the SemesterStatus "force deleted" event.
*
* @param \App\Models\SemesterStatus $semesterStatus
* @return void
*/
public function forceDeleted(SemesterStatus $semesterStatus)
{
//
}
}
4 changes: 3 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Listeners\AutoApproveMacAddresses;
use App\Listeners\MailGate;
use App\Listeners\UpdatePhysicalIP;
use App\Models\SemesterStatus;
use App\Observers\StatusObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand Down Expand Up @@ -44,6 +46,6 @@ public function boot()
{
parent::boot();

//
SemesterStatus::observe(StatusObserver::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function up()
Schema::create('semester_status', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->unsignedSmallInteger('semester_id');
$table->set('status', \App\Models\Semester::STATUSES)->default(\App\Models\Semester::ACTIVE);
$table->set('status', \App\Models\SemesterStatus::STATUSES)->default(\App\Models\SemesterStatus::ACTIVE);
$table->text('comment')->nullable();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
$extern_id = Role::getObjectIdByName(Role::COLLEGIST, 'extern');
$collegist->roles()->detach(Role::getId(Role::COLLEGIST));
$collegist->roles()->attach(Role::getId(Role::COLLEGIST), ['object_id' => $extern_id]);
$collegist->setStatusFor(Semester::current(), Semester::ACTIVE);
$collegist->setStatusFor(Semester::current(), SemesterStatus::ACTIVE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions database/seeders/SemesterSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function run()
$semesters = Semester::all()->unique();
foreach ($semesters as $semester) {
foreach ($users as $user) {
$status = array_rand(Semester::STATUSES);
$user->setStatusFor($semester, Semester::STATUSES[$status]);
$status = array_rand(SemesterStatus::STATUSES);
$user->setStatusFor($semester, SemesterStatus::STATUSES[$status]);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'show' => 'Show',
'status_cert_request' => ':user requested a certificate to be signed:',
'status_certificate_request_subject' => 'Status certificate request',
'status_updated' => 'Status updated',
'thank_you_registering' => 'Thank you for registering to the :app system.',
'wait_for_approving_registration' => 'Please wait for an administrator to approve your registration.',
];
1 change: 1 addition & 0 deletions resources/lang/hu/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'show' => 'Megtekint',
'status_cert_request' => ':user új tagság igazolást igényelt:',
'status_certificate_request_subject' => 'Tagságigazolás igénylés',
'status_updated' => 'Új státusz a :semester félévre: :status',
'thank_you_registering' => 'Köszönjük, hogy regisztráltál az :app rendszerbe.',
'wait_for_approving_registration' => 'Regisztrációdat egy rendszergazda hamarosan kezelni fogja, addig is kérjük türelmed.',
];
13 changes: 13 additions & 0 deletions resources/views/emails/status_updated.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@component('mail::message')
<h1>Kedves {{ $recipient->name }}!</h1>
<p>
A {{ $semester }} félévre a státuszod <i>{{ $status }}</i> lett.<br>
Módosító: {{$modifier->name}}.
@if($comment)
<br>
Megjegyzés: {{$comment}}.
@endif
</p>

@lang('mail.administrators')
@endcomponent
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class="red tooltipped waves-effect btn-floating"
<i class="material-icons">directions_run</i>
</button>

<span class="new badge {{ \App\Models\Semester::colorForStatus($status) }}" data-badge-caption="" style="margin-left:10px">
<span class="new badge {{ \App\Models\SemesterStatus::color($status) }}" data-badge-caption="" style="margin-left:10px">
@lang("user." . $status)
</span>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
@endforeach
<hr>
<h6>@lang('admin.statuses')</h6>
@foreach (\App\Models\Semester::STATUSES as $s)
@foreach (\App\Models\SemesterStatus::STATUSES as $s)
@if(in_array($s, $this->statuses))
<span class="new badge {{ \App\Models\Semester::colorForStatus($s) }}" data-badge-caption=""
<span class="new badge {{ \App\Models\SemesterStatus::color($s) }}" data-badge-caption=""
style="float:none;padding:2px 2px 2px 5px;margin:2px;cursor:pointer;"
wire:click="deleteStatus('{{$s}}')">
<nobr><i>@lang('user.'.$s)</i> &cross;</nobr>
</span>
@else
<span class="new badge {{ \App\Models\Semester::colorForStatus($s) }}" data-badge-caption=""
<span class="new badge {{ \App\Models\SemesterStatus::color($s) }}" data-badge-caption=""
style="float:none;padding:2px 2px 2px 5px;margin:2px;cursor:pointer"
wire:click="addStatus('{{$s}}')">
<nobr>@lang('user.'.$s)</nobr>
Expand Down Expand Up @@ -102,7 +102,7 @@
<div class="col s12 xl1">
@if($user->hasEducationalInformation())
@can('viewEducationalInformation', $user)
<span class="new badge tag {{ \App\Models\Semester::colorForStatus($user->getStatus()) }}" data-badge-caption="">
<span class="new badge tag {{ \App\Models\SemesterStatus::color($user->getStatus()) }}" data-badge-caption="">
@lang("user." . $user->getStatus())
</span>
@endcan
Expand Down
Loading