Created
January 16, 2019 14:54
-
-
Save joel-extremo/85c250114ab7ecc83f3450eb2ba9d688 to your computer and use it in GitHub Desktop.
NewHomeworkNotification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Repository\PushNotification; | |
class NewHomeworkNotification extends OkusPushNotification | |
{ | |
protected $notificationTitle = 'Nueva Tarea'; | |
protected $notificationType = 'new_homework'; | |
protected $messages = [ | |
'student' => 'Tienes una nueva tarea de {{homeworkName}}.', | |
'tutor' => 'A {{studentName}} le han asignado una nueva tarea de {{homeworkName}}.' | |
]; | |
public function send() | |
{ | |
try { | |
$this->sendToStudents(); | |
$this->sendToTutors(); | |
return true; | |
} catch (Exception $e) { | |
return false; | |
} | |
} | |
public function sendToStudents() | |
{ | |
$studentsIds = $this->homework->studentsIds(); | |
$tokens = $this->getStudentsTokens($studentsIds); | |
$this->sendToDevices( | |
$this->getStudentMessage(), | |
$tokens | |
); | |
} | |
public function sendToTutors() | |
{ | |
$students = $this->homework->section->students; | |
foreach ($students as $student) | |
{ | |
$tutorsIds = $student->tutors()->get()->pluck('id')->toArray(); | |
$tokens = $this->getTutorTokens($tutorsIds); | |
$this->sendToDevices( | |
$this->getTutorMessage($student->firstname), | |
$tokens | |
); | |
} | |
} | |
public function getTutorMessage($studentName) | |
{ | |
$homeworkName = $this->homework->name; | |
return stringReplace( | |
$this->messages['tutor'], | |
compact('studentName', 'homeworkName') | |
); | |
} | |
public function getStudentMessage() | |
{ | |
$homeworkName = $this->homework->name; | |
return stringReplace( | |
$this->messages['student'], | |
compact('homeworkName') | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment