|
| 1 | +<?php |
| 2 | +/* For licensing terms, see /license.txt */ |
| 3 | + |
| 4 | +use Chamilo\PluginBundle\CourseHomeNotify\Entity\Notification; |
| 5 | +use Chamilo\PluginBundle\CourseHomeNotify\Entity\NotificationRelUser; |
| 6 | + |
| 7 | +require_once __DIR__.'/../../main/inc/global.inc.php'; |
| 8 | + |
| 9 | +api_block_anonymous_users(true); |
| 10 | +api_protect_course_script(true); |
| 11 | + |
| 12 | +header('Content-Type: application/json'); |
| 13 | + |
| 14 | +$plugin = CourseHomeNotifyPlugin::create(); |
| 15 | +$courseId = api_get_course_int_id(); |
| 16 | +$userId = api_get_user_id(); |
| 17 | + |
| 18 | +if ( |
| 19 | + empty($courseId) || |
| 20 | + empty($userId) || |
| 21 | + !$plugin->isEnabled() |
| 22 | +) { |
| 23 | + echo json_encode(['show' => false]); |
| 24 | + exit; |
| 25 | +} |
| 26 | + |
| 27 | +$course = api_get_course_entity($courseId); |
| 28 | +$user = api_get_user_entity($userId); |
| 29 | +$em = Database::getManager(); |
| 30 | +$schemaManager = $em->getConnection()->createSchemaManager(); |
| 31 | + |
| 32 | +if ( |
| 33 | + !$schemaManager->tablesExist([ |
| 34 | + 'course_home_notify_notification', |
| 35 | + 'course_home_notify_notification_rel_user', |
| 36 | + ]) |
| 37 | +) { |
| 38 | + echo json_encode(['show' => false]); |
| 39 | + exit; |
| 40 | +} |
| 41 | + |
| 42 | +/** @var Notification|null $notification */ |
| 43 | +$notification = $em |
| 44 | + ->getRepository(Notification::class) |
| 45 | + ->findOneBy(['course' => $course]); |
| 46 | + |
| 47 | +if (!$notification) { |
| 48 | + echo json_encode(['show' => false]); |
| 49 | + exit; |
| 50 | +} |
| 51 | + |
| 52 | +$expirationLink = $notification->getExpirationLink(); |
| 53 | + |
| 54 | +if ($expirationLink) { |
| 55 | + /** @var NotificationRelUser|null $notificationUser */ |
| 56 | + $notificationUser = $em |
| 57 | + ->getRepository(NotificationRelUser::class) |
| 58 | + ->findOneBy(['notification' => $notification, 'user' => $user]); |
| 59 | + |
| 60 | + if ($notificationUser) { |
| 61 | + echo json_encode(['show' => false]); |
| 62 | + exit; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +$contentUrl = ''; |
| 67 | + |
| 68 | +if ($expirationLink) { |
| 69 | + $contentUrl = api_get_path(WEB_PLUGIN_PATH) |
| 70 | + .$plugin->get_name() |
| 71 | + .'/content.php?hash=' |
| 72 | + .urlencode($notification->getHash()) |
| 73 | + .'&' |
| 74 | + .api_get_cidreq(); |
| 75 | +} |
| 76 | + |
| 77 | +echo json_encode( |
| 78 | + [ |
| 79 | + 'show' => true, |
| 80 | + 'title' => $plugin->get_lang('CourseNotice'), |
| 81 | + 'content' => $notification->getContent(), |
| 82 | + 'requiresLink' => !empty($expirationLink), |
| 83 | + 'linkLabel' => $plugin->get_lang('PleaseFollowThisLink'), |
| 84 | + 'contentUrl' => $contentUrl, |
| 85 | + ] |
| 86 | +); |
0 commit comments