issued_all.php 10 KB

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