issued.php 10 KB

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