issued.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Show information about the issued badge
  5. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  6. * @package chamilo.badge
  7. */
  8. require_once '../inc/global.inc.php';
  9. require_once '../inc/lib/baker.lib.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('openbadges_backpack');
  87. if (strcmp($backpack, $configBackpack) !== 0) {
  88. $backpack = $configBackpack;
  89. }
  90. $htmlHeadXtra[] = '<script src="' . $backpack . 'issuer.js"></script>';
  91. }
  92. $objSkill = new Skill();
  93. $skills = $objSkill->get($skillId);
  94. $unbakedBadge = api_get_path(SYS_UPLOAD_PATH) . "badges/".$skills['icon'];
  95. $unbakedBadge = file_get_contents($unbakedBadge);
  96. $badgeInfoError = false;
  97. $personalBadge = "";
  98. $png = new PNGImageBaker($unbakedBadge);
  99. if ($png->checkChunks("tEXt", "openbadges")) {
  100. $bakedInfo = $png->addChunk("tEXt", "openbadges", $assertionUrl);
  101. $bakedBadge = UserManager::getUserPathById($userId, "system");
  102. $bakedBadge = $bakedBadge.'badges';
  103. if (!file_exists($bakedBadge)) {
  104. mkdir($bakedBadge, api_get_permissions_for_new_directories(), true);
  105. }
  106. $skillRelUserId = $userSkills[0]->getId();
  107. if (!file_exists($bakedBadge . "/badge_" . $skillRelUserId)) {
  108. file_put_contents($bakedBadge . "/badge_" . $skillRelUserId . ".png", $bakedInfo);
  109. }
  110. //Process to validate a baked badge
  111. $badgeContent = file_get_contents($bakedBadge . "/badge_" . $skillRelUserId . ".png");
  112. $verifyBakedBadge = $png->extractBadgeInfo($badgeContent);
  113. if (!is_array($verifyBakedBadge)) {
  114. $badgeInfoError = true;
  115. }
  116. if (!$badgeInfoError) {
  117. $personalBadge = UserManager::getUserPathById($userId, "web");
  118. $personalBadge = $personalBadge."badges/badge_" . $skillRelUserId . ".png";
  119. }
  120. }
  121. $template = new Template('');
  122. $template->assign('skill_info', $skillInfo);
  123. $template->assign('user_info', $userInfo);
  124. $template->assign('allow_export', $allowExport);
  125. $template->assign('badge_error', $badgeInfoError);
  126. $template->assign('personal_badge', $personalBadge);
  127. if ($allowExport) {
  128. $template->assign('assertions', $badgeAssertions);
  129. }
  130. $content = $template->fetch(
  131. $template->get_template('skill/issued.tpl')
  132. );
  133. $template->assign('header', get_lang('IssuedBadgeInformation'));
  134. $template->assign('content', $content);
  135. $template->display_one_col_template();