issued.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * @author José Loguercio Silva <jose.loguercio@beeznest.com>
  7. * @package chamilo.badge
  8. */
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $issue = isset($_REQUEST['issue']) ? intval($_REQUEST['issue']) : 0;
  11. if (!$issue) {
  12. header('Location: '.api_get_path(WEB_PATH));
  13. exit;
  14. }
  15. $entityManager = Database::getManager();
  16. $skillIssue = $entityManager->find('ChamiloCoreBundle:SkillRelUser', $issue);
  17. $skillRepo = $entityManager->getRepository('ChamiloCoreBundle:Skill');
  18. $skillLevelRepo = $entityManager->getRepository('ChamiloSkillBundle:Level');
  19. if (!$skillIssue) {
  20. Display::addFlash(
  21. Display::return_message(get_lang('TheUserXNotYetAchievedTheSkillX'), 'error')
  22. );
  23. header('Location: '.api_get_path(WEB_PATH));
  24. exit;
  25. }
  26. $user = $skillIssue->getUser();
  27. $skill = $skillIssue->getSkill();
  28. if (!$user || !$skill) {
  29. Display::addFlash(
  30. Display::return_message(get_lang('NoResults'), 'error')
  31. );
  32. header('Location: '.api_get_path(WEB_PATH));
  33. exit;
  34. }
  35. $userInfo = [
  36. 'id' => $user->getId(),
  37. 'complete_name' => $user->getCompleteName()
  38. ];
  39. $skillInfo = [
  40. 'id' => $skill->getId(),
  41. 'name' => $skill->getName(),
  42. 'short_code' => $skill->getShortCode(),
  43. 'description' => $skill->getDescription(),
  44. 'criteria' => $skill->getCriteria(),
  45. 'badge_image' => $skill->getWebIconPath(),
  46. 'courses' => []
  47. ];
  48. // Open Graph Markup
  49. $htmlHeadXtra[] = "
  50. <meta property='og:type' content='article' />
  51. <meta property='og:title' content='".sprintf(get_lang('IHaveObtainedSkillXOnY'), $skillInfo['name'], api_get_setting('siteName'))."' />
  52. <meta property='og:url' content='".api_get_path(WEB_PATH)."badge/".$issue."' />
  53. <meta property='og:description' content='".$skillInfo['description']."' />
  54. <meta property='og:image' content='".$skillInfo['badge_image']."' />
  55. ";
  56. $currentUserId = api_get_user_id();
  57. $currentUser = $entityManager->find('ChamiloUserBundle:User', $currentUserId);
  58. $allowDownloadExport = $currentUser ? $currentUser->getId() === $user->getId() : false;
  59. $allowComment = $currentUser ? Skill::userCanAddFeedbackToUser($currentUser, $user) : false;
  60. $skillIssueDate = api_get_local_time($skillIssue->getAcquiredSkillAt());
  61. $currentSkillLevel = get_lang('NoLevelAcquiredYet');
  62. if ($skillIssue->getAcquiredLevel()) {
  63. $currentSkillLevel = $skillLevelRepo->find(['id' => $skillIssue->getAcquiredLevel()])->getName();
  64. }
  65. $argumentationAuthor = api_get_user_info($skillIssue->getArgumentationAuthorId());
  66. $skillIssueInfo = [
  67. 'id' => $skillIssue->getId(),
  68. 'datetime' => api_format_date($skillIssueDate, DATE_TIME_FORMAT_SHORT),
  69. 'acquired_level' => $currentSkillLevel,
  70. 'argumentation_author_id' => $skillIssue->getArgumentationAuthorId(),
  71. 'argumentation_author_name' => api_get_person_name($argumentationAuthor['firstname'], $argumentationAuthor['lastname']),
  72. 'argumentation' => $skillIssue->getArgumentation(),
  73. 'source_name' => $skillIssue->getSourceName(),
  74. 'user_id' => $skillIssue->getUser()->getId(),
  75. 'user_complete_name' => $skillIssue->getUser()->getCompleteName(),
  76. 'skill_id' => $skillIssue->getSkill()->getId(),
  77. 'skill_badge_image' => $skillIssue->getSkill()->getWebIconPath(),
  78. 'skill_name' => $skillIssue->getSkill()->getName(),
  79. 'skill_short_code' => $skillIssue->getSkill()->getShortCode(),
  80. 'skill_description' => $skillIssue->getSkill()->getDescription(),
  81. 'skill_criteria' => $skillIssue->getSkill()->getCriteria(),
  82. 'badge_assertion' => $skillIssue->getAssertionUrl(),
  83. 'comments' => [],
  84. 'feedback_average' => $skillIssue->getAverage()
  85. ];
  86. $skillIssueComments = $skillIssue->getComments(true);
  87. $userId = $skillIssueInfo['user_id'];
  88. $skillId = $skillIssueInfo['skill_id'];
  89. foreach ($skillIssueComments as $comment) {
  90. $commentDate = api_get_local_time($comment->getFeedbackDateTime());
  91. $skillIssueInfo['comments'][] = [
  92. 'text' => $comment->getFeedbackText(),
  93. 'value' => $comment->getFeedbackValue(),
  94. 'giver_complete_name' => $comment->getFeedbackGiver()->getCompleteName(),
  95. 'datetime' => api_format_date($commentDate, DATE_TIME_FORMAT_SHORT)
  96. ];
  97. }
  98. $acquiredLevel = [];
  99. $profile = $skillRepo->find($skillId)->getProfile();
  100. if (!$profile) {
  101. $skillRelSkill = new SkillRelSkill();
  102. $parents = $skillRelSkill->get_skill_parents($skillId);
  103. krsort($parents);
  104. foreach ($parents as $parent) {
  105. $skillParentId = $parent['skill_id'];
  106. $profile = $skillRepo->find($skillParentId)->getProfile();
  107. if ($profile) {
  108. break;
  109. }
  110. if (!$profile && $parent['parent_id'] == 0) {
  111. $profile = $skillLevelRepo->findAll();
  112. $profile = $profile[0];
  113. }
  114. }
  115. }
  116. if ($profile) {
  117. $profileId = $profile->getId();
  118. $levels = $skillLevelRepo->findBy([
  119. 'profile' => $profileId
  120. ]);
  121. foreach ($levels as $level) {
  122. $profileLevels[$level->getPosition()][$level->getId()] = $level->getName();
  123. }
  124. ksort($profileLevels); // Sort the array by Position.
  125. foreach ($profileLevels as $profileLevel) {
  126. $profileId = key($profileLevel);
  127. $acquiredLevel[$profileId] = $profileLevel[$profileId];
  128. }
  129. }
  130. $formAcquiredLevel = new FormValidator('acquired_level');
  131. $formAcquiredLevel->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
  132. $formAcquiredLevel->addHidden('user', $skillIssue->getUser()->getId());
  133. $formAcquiredLevel->addHidden('issue', $skillIssue->getId());
  134. $formAcquiredLevel->addButtonSend(get_lang('Save'));
  135. if ($formAcquiredLevel->validate() && $allowComment) {
  136. $values = $formAcquiredLevel->exportValues();
  137. $level = $skillLevelRepo->find(intval($values['acquired_level']));
  138. $skillIssue->setAcquiredLevel($level);
  139. $entityManager->persist($skillIssue);
  140. $entityManager->flush();
  141. header("Location: ".$skillIssue->getIssueUrl());
  142. exit;
  143. }
  144. $form = new FormValidator('comment');
  145. $form->addTextarea('comment', get_lang('NewComment'), ['rows' => 4]);
  146. $form->applyFilter('comment', 'trim');
  147. $form->addRule('comment', get_lang('ThisFieldIsRequired'), 'required');
  148. $form->addSelect(
  149. 'value',
  150. [get_lang('Value'), get_lang('RateTheSkillInPractice')],
  151. ['-', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  152. );
  153. $form->addHidden('user', $skillIssue->getUser()->getId());
  154. $form->addHidden('issue', $skillIssue->getId());
  155. $form->addButtonSend(get_lang('Send'));
  156. if ($form->validate() && $allowComment) {
  157. $values = $form->exportValues();
  158. $skillUserComment = new Chamilo\CoreBundle\Entity\SkillRelUserComment();
  159. $skillUserComment
  160. ->setFeedbackDateTime(new DateTime)
  161. ->setFeedbackGiver($currentUser)
  162. ->setFeedbackText($values['comment'])
  163. ->setFeedbackValue($values['value'] ? $values['value'] : null)
  164. ->setSkillRelUser($skillIssue);
  165. $entityManager->persist($skillUserComment);
  166. $entityManager->flush();
  167. header("Location: ".$skillIssue->getIssueUrl());
  168. exit;
  169. }
  170. $badgeInfoError = "";
  171. $personalBadge = "";
  172. if ($allowDownloadExport) {
  173. $backpack = 'https://backpack.openbadges.org/';
  174. $configBackpack = api_get_setting('openbadges_backpack');
  175. if (strcmp($backpack, $configBackpack) !== 0) {
  176. $backpack = $configBackpack;
  177. }
  178. $htmlHeadXtra[] = '<script src="'.$backpack.'issuer.js"></script>';
  179. $objSkill = new Skill();
  180. $assertionUrl = $skillIssueInfo['badge_assertion'];
  181. $skills = $objSkill->get($skillId);
  182. $unbakedBadge = api_get_path(SYS_UPLOAD_PATH)."badges/".$skills['icon'];
  183. if (!is_file($unbakedBadge)) {
  184. $unbakedBadge = api_get_path(WEB_CODE_PATH).'img/icons/128/badges-default.png';
  185. }
  186. $unbakedBadge = file_get_contents($unbakedBadge);
  187. $badgeInfoError = false;
  188. $personalBadge = "";
  189. $png = new PNGImageBaker($unbakedBadge);
  190. if ($png->checkChunks("tEXt", "openbadges")) {
  191. $bakedInfo = $png->addChunk("tEXt", "openbadges", $assertionUrl);
  192. $bakedBadge = UserManager::getUserPathById($userId, "system");
  193. $bakedBadge = $bakedBadge.'badges';
  194. if (!file_exists($bakedBadge)) {
  195. mkdir($bakedBadge, api_get_permissions_for_new_directories(), true);
  196. }
  197. $skillRelUserId = $skillIssueInfo['id'];
  198. if (!file_exists($bakedBadge."/badge_".$skillRelUserId)) {
  199. file_put_contents($bakedBadge."/badge_".$skillRelUserId.".png", $bakedInfo);
  200. }
  201. //Process to validate a baked badge
  202. $badgeContent = file_get_contents($bakedBadge."/badge_".$skillRelUserId.".png");
  203. $verifyBakedBadge = $png->extractBadgeInfo($badgeContent);
  204. if (!is_array($verifyBakedBadge)) {
  205. $badgeInfoError = true;
  206. }
  207. if (!$badgeInfoError) {
  208. $personalBadge = UserManager::getUserPathById($userId, "web");
  209. $personalBadge = $personalBadge."badges/badge_".$skillRelUserId.".png";
  210. }
  211. }
  212. }
  213. $template = new Template(get_lang('IssuedBadgeInformation'));
  214. $template->assign('issue_info', $skillIssueInfo);
  215. $template->assign('allow_comment', $allowComment);
  216. $template->assign('allow_download_export', $allowDownloadExport);
  217. $template->assign('comment_form', $form->returnForm());
  218. $template->assign('acquired_level_form', $formAcquiredLevel->returnForm());
  219. $template->assign('badge_error', $badgeInfoError);
  220. $template->assign('personal_badge', $personalBadge);
  221. $content = $template->fetch(
  222. $template->get_template('skill/issued.tpl')
  223. );
  224. $template->assign('header', get_lang('IssuedBadgeInformation'));
  225. $template->assign('content', $content);
  226. $template->display_one_col_template();