issued_all.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 all issued badges with same skill by user.
  8. *
  9. * @author José Loguercio Silva <jose.loguercio@beeznest.com>
  10. *
  11. * @package chamilo.badge
  12. */
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. $userId = isset($_GET['user']) ? (int) $_GET['user'] : 0;
  15. $skillId = isset($_GET['skill']) ? (int) $_GET['skill'] : 0;
  16. if (!$userId || !$skillId) {
  17. api_not_allowed(true);
  18. }
  19. Skill::isAllowed($userId);
  20. $em = Database::getManager();
  21. $user = api_get_user_entity($userId);
  22. $skill = $em->find('ChamiloCoreBundle:Skill', $skillId);
  23. $currentUserId = api_get_user_id();
  24. if (!$user || !$skill) {
  25. Display::addFlash(
  26. Display::return_message(get_lang('NoResults'), 'error')
  27. );
  28. header('Location: '.api_get_path(WEB_PATH));
  29. exit;
  30. }
  31. $skillRepo = $em->getRepository('ChamiloCoreBundle:Skill');
  32. $skillUserRepo = $em->getRepository('ChamiloCoreBundle:SkillRelUser');
  33. $skillLevelRepo = $em->getRepository('ChamiloSkillBundle:Level');
  34. $userSkills = $skillUserRepo->findBy([
  35. 'user' => $user,
  36. 'skill' => $skill,
  37. ]);
  38. $userInfo = [
  39. 'id' => $user->getId(),
  40. 'complete_name' => UserManager::formatUserFullName($user),
  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($skill),
  49. 'courses' => [],
  50. ];
  51. $allUserBadges = [];
  52. /** @var SkillRelUser $skillIssue */
  53. foreach ($userSkills as $index => $skillIssue) {
  54. $currentUser = api_get_user_entity($currentUserId);
  55. $allowDownloadExport = $currentUser ? $currentUser->getId() === $user->getId() : false;
  56. $allowComment = $currentUser ? Skill::userCanAddFeedbackToUser($currentUser, $user) : false;
  57. $skillIssueDate = api_get_local_time($skillIssue->getAcquiredSkillAt());
  58. $currentSkillLevel = get_lang('NoLevelAcquiredYet');
  59. if ($skillIssue->getAcquiredLevel()) {
  60. $currentSkillLevel = $skillLevelRepo->find(['id' => $skillIssue->getAcquiredLevel()])->getName();
  61. }
  62. $argumentationAuthor = api_get_user_info($skillIssue->getArgumentationAuthorId());
  63. $skillIssueInfo = [
  64. 'id' => $skillIssue->getId(),
  65. 'datetime' => api_format_date($skillIssueDate, DATE_TIME_FORMAT_SHORT),
  66. 'acquired_level' => $currentSkillLevel,
  67. 'argumentation_author_id' => $skillIssue->getArgumentationAuthorId(),
  68. 'argumentation_author_name' => api_get_person_name(
  69. $argumentationAuthor['firstname'],
  70. $argumentationAuthor['lastname']
  71. ),
  72. 'argumentation' => $skillIssue->getArgumentation(),
  73. 'source_name' => $skillIssue->getSourceName(),
  74. 'user_id' => $skillIssue->getUser()->getId(),
  75. 'user_complete_name' => UserManager::formatUserFullName($skillIssue->getUser()),
  76. 'skill_id' => $skillIssue->getSkill()->getId(),
  77. 'skill_badge_image' => Skill::getWebIconPath($skillIssue->getSkill()),
  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' => SkillRelUserManager::getAssertionUrl($skillIssue),
  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' => UserManager::formatUserFullName($comment->getFeedbackGiver()),
  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->getSkillParents($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 = !empty($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(
  131. 'acquired_level'.$skillIssue->getId(),
  132. 'post',
  133. SkillRelUserManager::getIssueUrlAll($skillIssue)
  134. );
  135. $formAcquiredLevel->addSelect('acquired_level', get_lang('AcquiredLevel'), $acquiredLevel);
  136. $formAcquiredLevel->addHidden('user', $skillIssue->getUser()->getId());
  137. $formAcquiredLevel->addHidden('issue', $skillIssue->getId());
  138. $formAcquiredLevel->addButtonSend(get_lang('Save'));
  139. if ($formAcquiredLevel->validate() && $allowComment) {
  140. $values = $formAcquiredLevel->exportValues();
  141. $level = $skillLevelRepo->find($values['acquired_level']);
  142. $skillIssue->setAcquiredLevel($level);
  143. $em->persist($skillIssue);
  144. $em->flush();
  145. header('Location: '.SkillRelUserManager::getIssueUrlAll($skillIssue));
  146. exit;
  147. }
  148. $form = new FormValidator(
  149. 'comment'.$skillIssue->getId(),
  150. 'post',
  151. SkillRelUserManager::getIssueUrlAll($skillIssue)
  152. );
  153. $form->addTextarea('comment', get_lang('NewComment'), ['rows' => 4]);
  154. $form->applyFilter('comment', 'trim');
  155. $form->addRule('comment', get_lang('ThisFieldIsRequired'), 'required');
  156. $form->addSelect(
  157. 'value',
  158. [get_lang('Value'), get_lang('RateTheSkillInPractice')],
  159. ['-', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  160. );
  161. $form->addHidden('user', $skillIssue->getUser()->getId());
  162. $form->addHidden('issue', $skillIssue->getId());
  163. $form->addButtonSend(get_lang('Send'));
  164. if ($form->validate() && $allowComment) {
  165. $values = $form->exportValues();
  166. $skillUserComment = new SkillRelUserComment();
  167. $skillUserComment
  168. ->setFeedbackDateTime(new DateTime())
  169. ->setFeedbackGiver($currentUser)
  170. ->setFeedbackText($values['comment'])
  171. ->setFeedbackValue($values['value'] ? $values['value'] : null)
  172. ->setSkillRelUser($skillIssue);
  173. $em->persist($skillUserComment);
  174. $em->flush();
  175. header('Location: '.SkillRelUserManager::getIssueUrlAll($skillIssue));
  176. exit;
  177. }
  178. $badgeInfoError = '';
  179. $personalBadge = '';
  180. if ($allowDownloadExport) {
  181. $backpack = 'https://backpack.openbadges.org/';
  182. $configBackpack = api_get_setting('openbadges_backpack');
  183. if (strcmp($backpack, $configBackpack) !== 0) {
  184. $backpack = $configBackpack;
  185. }
  186. $htmlHeadXtra[] = '<script src="'.$backpack.'issuer.js"></script>';
  187. $objSkill = new Skill();
  188. $assertionUrl = $skillIssueInfo['badge_assertion'];
  189. $skills = $objSkill->get($skillId);
  190. $unbakedBadge = api_get_path(SYS_UPLOAD_PATH)."badges/".$skills['icon'];
  191. if (!is_file($unbakedBadge)) {
  192. $unbakedBadge = api_get_path(WEB_CODE_PATH).'img/icons/128/badges-default.png';
  193. }
  194. $unbakedBadge = file_get_contents($unbakedBadge);
  195. $badgeInfoError = false;
  196. $personalBadge = "";
  197. $png = new PNGImageBaker($unbakedBadge);
  198. if ($png->checkChunks("tEXt", "openbadges")) {
  199. $bakedInfo = $png->addChunk("tEXt", "openbadges", $assertionUrl);
  200. $bakedBadge = UserManager::getUserPathById($userId, "system");
  201. $bakedBadge = $bakedBadge.'badges';
  202. if (!file_exists($bakedBadge)) {
  203. mkdir($bakedBadge, api_get_permissions_for_new_directories(), true);
  204. }
  205. $skillRelUserId = $skillIssueInfo['id'];
  206. if (!file_exists($bakedBadge."/badge_".$skillRelUserId)) {
  207. file_put_contents($bakedBadge."/badge_".$skillRelUserId.".png", $bakedInfo);
  208. }
  209. //Process to validate a baked badge
  210. $badgeContent = file_get_contents($bakedBadge."/badge_".$skillRelUserId.".png");
  211. $verifyBakedBadge = $png->extractBadgeInfo($badgeContent);
  212. if (!is_array($verifyBakedBadge)) {
  213. $badgeInfoError = true;
  214. }
  215. if (!$badgeInfoError) {
  216. $personalBadge = UserManager::getUserPathById($userId, "web");
  217. $personalBadge = $personalBadge."badges/badge_".$skillRelUserId.".png";
  218. }
  219. }
  220. }
  221. $allUserBadges[$index]['issue_info'] = $skillIssueInfo;
  222. $allUserBadges[$index]['allow_comment'] = $allowComment;
  223. $allUserBadges[$index]['allow_download_export'] = $allowDownloadExport;
  224. $allUserBadges[$index]['comment_form'] = $form->returnForm();
  225. $allUserBadges[$index]['acquired_level_form'] = $formAcquiredLevel->returnForm();
  226. $allUserBadges[$index]['badge_error'] = $badgeInfoError;
  227. $allUserBadges[$index]['personal_badge'] = $personalBadge;
  228. }
  229. $template = new Template(get_lang('IssuedBadgeInformation'));
  230. $template->assign('user_badges', $allUserBadges);
  231. $template->assign('show_level', api_get_configuration_value('hide_skill_levels') == false);
  232. $content = $template->fetch(
  233. $template->get_template('skill/issued_all.tpl')
  234. );
  235. $template->assign('header', get_lang('IssuedBadgeInformation'));
  236. $template->assign('content', $content);
  237. $template->display_one_col_template();