issued_all.php 10 KB

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