issued.php 9.6 KB

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