issued.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Show information about the issued badge
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. * @package chamilo.badge
  8. */
  9. //require_once '../inc/global.inc.php';
  10. $userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
  11. $skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
  12. if (!isset($_GET['user'], $_GET['skill'])) {
  13. header('Location: ' . api_get_path(WEB_PATH));
  14. exit;
  15. }
  16. $entityManager = Database::getManager();
  17. $user = $entityManager->find('ChamiloUserBundle:User', $_GET['user']);
  18. $skill = $entityManager->find('ChamiloCoreBundle:Skill', $_GET['skill']);
  19. if (!$user || !$skill) {
  20. Display::addFlash(
  21. Display::return_message(get_lang('NoResults'), 'error')
  22. );
  23. header('Location: ' . api_get_path(WEB_PATH));
  24. exit;
  25. }
  26. $skillUserRepo = $entityManager->getRepository('ChamiloCoreBundle:SkillRelUser');
  27. $userSkills = $skillUserRepo->findBy([
  28. 'userId' => $user->getId(),
  29. 'skillId' => $skill->getId()
  30. ]);
  31. if (!$userSkills) {
  32. Display::addFlash(
  33. Display::return_message(get_lang('TheUserXNotYetAchievedTheSkillX'), 'error')
  34. );
  35. header('Location: ' . api_get_path(WEB_PATH));
  36. exit;
  37. }
  38. $userInfo = [
  39. 'id' => $user->getId(),
  40. 'complete_name' => $user->getCompleteName()
  41. ];
  42. $skillInfo = [
  43. 'id' => $skill->getId(),
  44. 'name' => $skill->getName(),
  45. 'short_code' => $skill->getShortCode(),
  46. 'description' => $skill->getDescription(),
  47. 'criteria' => $skill->getCriteria(),
  48. 'badge_image' => $skill->getWebIconPath(),
  49. 'courses' => []
  50. ];
  51. // Open Graph Markup
  52. $htmlHeadXtra[] = "
  53. <meta property='og:type' content='article' />
  54. <meta property='og:title' content='".sprintf(get_lang('IHaveObtainedSkillXOnY'), $skillInfo['name'], api_get_setting('siteName'))."' />
  55. <meta property='og:url' content='".api_get_path(WEB_PATH)."badge/".$skillId."/user/".$userId."' />
  56. <meta property='og:description' content='".$skillInfo['description']."' />
  57. <meta property='og:image' content='".$skillInfo['badge_image']."' />
  58. ";
  59. $badgeAssertions = [];
  60. foreach ($userSkills as $userSkill) {
  61. $sessionId = 0;
  62. $course = $entityManager->find('ChamiloCoreBundle:Course', $userSkill->getCourseId());
  63. $courseName = $course ? $course->getTitle() : '';
  64. if ($userSkill->getSessionId()) {
  65. $session = $entityManager->find('ChamiloCoreBundle:Session', $userSkill->getSessionId());
  66. $sessionId = $session->getId();
  67. $courseName = "[{$session->getName()}] {$course->getTitle()}";
  68. }
  69. $userSkillDate = api_get_local_time($userSkill->getAcquiredSkillAt());
  70. $skillInfo['courses'][] = [
  71. 'name' => $courseName,
  72. 'date_issued' => api_format_date($userSkillDate, DATE_TIME_FORMAT_LONG)
  73. ];
  74. $assertionUrl = api_get_path(WEB_CODE_PATH) . "badge/assertion.php?";
  75. $assertionUrl .= http_build_query(array(
  76. 'user' => $user->getId(),
  77. 'skill' => $skill->getId(),
  78. 'course' => $userSkill->getCourseId(),
  79. 'session' => $userSkill->getSessionId()
  80. ));
  81. $badgeAssertions[] = $assertionUrl;
  82. }
  83. $allowExport = api_get_user_id() == $user->getId();
  84. if ($allowExport) {
  85. $backpack = 'https://backpack.openbadges.org/';
  86. $configBackpack = api_get_setting('gradebook.openbadges_backpack');
  87. if (strcmp($backpack, $configBackpack) !== 0) {
  88. $backpack = $configBackpack;
  89. }
  90. $htmlHeadXtra[] = '<script src="' . $backpack . 'issuer.js"></script>';
  91. }
  92. $badgeInfoError = false;
  93. $personalBadge = "";
  94. if ($skill->getIcon()) {
  95. $unbakedBadge = file_get_contents(
  96. api_get_path(SYS_UPLOAD_PATH) . "badges/" . $skill->getIcon()
  97. );
  98. $png = new PNGImageBaker($unbakedBadge);
  99. if ($png->checkChunks("tEXt", "openbadges")) {
  100. $bakedInfo = $png->addChunk("tEXt", "openbadges", $assertionUrl);
  101. $bakedBadge = UserManager::getUserPathById($userId, "system") . 'badges';
  102. if (!file_exists($bakedBadge)) {
  103. mkdir($bakedBadge, api_get_permissions_for_new_directories(), true);
  104. }
  105. $skillRelUserId = $userSkills[0]->getId();
  106. if (!file_exists($bakedBadge . "/badge_" . $skillRelUserId)) {
  107. file_put_contents($bakedBadge . "/badge_" . $skillRelUserId . ".png", $bakedInfo);
  108. }
  109. //Process to validate a baked badge
  110. $badgeContent = file_get_contents($bakedBadge . "/badge_" . $skillRelUserId . ".png");
  111. $verifyBakedBadge = $png->extractBadgeInfo($badgeContent);
  112. if (!is_array($verifyBakedBadge)) {
  113. $badgeInfoError = true;
  114. }
  115. if (!$badgeInfoError) {
  116. $personalBadge = UserManager::getUserPathById($userId, "web");
  117. $personalBadge = $personalBadge."badges/badge_" . $skillRelUserId . ".png";
  118. }
  119. }
  120. }
  121. echo Container::getTemplating()->render(
  122. '@template_style/skill/issued.html.twig',
  123. [
  124. 'assertions' => $badgeAssertions,
  125. 'skill_info' => $skillInfo,
  126. 'user_info' => $userInfo,
  127. 'allow_export' => $allowExport,
  128. 'badge_error' => $badgeInfoError,
  129. 'personal_badge' => $personalBadge
  130. ]
  131. );
  132. //$template->assign('header', get_lang('IssuedBadgeInformation'));