issued_all.php 10 KB

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