|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Exceptions\Handler; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Exception; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | + |
| 11 | +class notifyBetaCourseApprovals extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'notify:BetaCourseApprovals'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Tell instructors when they need to approve assignments'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new command instance. |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + parent::__construct(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Execute the console command. |
| 39 | + * |
| 40 | + * @return int |
| 41 | + * @throws Exception |
| 42 | + */ |
| 43 | + public function handle() |
| 44 | + { |
| 45 | + try { |
| 46 | + $yesterday = Carbon::now()->subDay()->format('Y-m-d H:i:s'); |
| 47 | + $pending_approvals = DB::table('beta_course_approvals') |
| 48 | + ->join('assignments', 'beta_course_approvals.beta_assignment_id', '=', 'assignments.id') |
| 49 | + ->join('courses', 'assignments.course_id', '=', 'courses.id') |
| 50 | + ->join('users', 'courses.user_id', '=', 'users.id') |
| 51 | + ->select('courses.id', 'courses.name AS course_name', |
| 52 | + 'assignments.id as assignment_id', |
| 53 | + 'assignments.name AS assignment_name', |
| 54 | + DB::raw('count(*) AS total_pending'), |
| 55 | + 'users.id AS user_id', |
| 56 | + 'users.email', |
| 57 | + 'users.first_name' |
| 58 | + ) |
| 59 | + ->where('courses.beta_approval_notifications', 1) |
| 60 | + ->where('beta_course_approvals.created_at', '>', $yesterday) |
| 61 | + ->groupBy('assignments.id') |
| 62 | + ->get(); |
| 63 | + $pending_approvals_by_user_id = []; |
| 64 | + foreach ($pending_approvals as $pending_approval) { |
| 65 | + if (!isset($pending_approval_by_user_id[$pending_approval->user_id])) { |
| 66 | + $pending_approvals_by_user_id[$pending_approval->user_id] = []; |
| 67 | + $pending_approvals_by_user_id[$pending_approval->user_id]['first_name'] = $pending_approval->first_name; |
| 68 | + $pending_approvals_by_user_id[$pending_approval->user_id]['email'] = $pending_approval->email; |
| 69 | + $pending_approvals_by_user_id[$pending_approval->user_id]['pending_approvals'] = ''; |
| 70 | + } |
| 71 | + $app_url = config('app.url'); |
| 72 | + $pending_approvals_by_user_id[$pending_approval->user_id]['pending_approvals'] .= "<li>$pending_approval->course_name: <a href='$app_url/instructors/assignments/$pending_approval->assignment_id/information/questions'>$pending_approval->assignment_name</a> has $pending_approval->total_pending pending approvals</li>"; |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($pending_approvals_by_user_id as $pending_approval) { |
| 76 | + $beauty_mail = app()->make(\Snowfire\Beautymail\Beautymail::class); |
| 77 | + $to_email = $pending_approval['email']; |
| 78 | + $email_info = ['pending_approvals' => $pending_approval['pending_approvals'], |
| 79 | + 'first_name' => $pending_approval['first_name'] |
| 80 | + ]; |
| 81 | + $beauty_mail->send('emails.pending_beta_course_approvals', $email_info, function ($message) |
| 82 | + use ($to_email) { |
| 83 | + $message |
| 84 | + ->from('adapt@noreply.libretexts.org', 'Adapt') |
| 85 | + ->to($to_email) |
| 86 | + ->subject("Pending Beta Course Approvals"); |
| 87 | + }); |
| 88 | + } |
| 89 | + } catch (Exception $e) { |
| 90 | + $h = new Handler(app()); |
| 91 | + $h->report($e); |
| 92 | + exit; |
| 93 | + } |
| 94 | + return 0; |
| 95 | + } |
| 96 | +} |
0 commit comments