lp_final_item.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Print a learning path finish page with details
  5. * @author Jose Loguercio <jose.loguercio@beeznest.com>
  6. * @package chamilo.learnpath
  7. */
  8. $_in_course = true;
  9. require_once __DIR__ . '/../inc/global.inc.php';
  10. $current_course_tool = TOOL_GRADEBOOK;
  11. // Make sure no anonymous user gets here without permission
  12. api_protect_course_script(true);
  13. // Get environment variables
  14. $courseCode = api_get_course_id();
  15. $userId = api_get_user_id();
  16. $sessionId = api_get_session_id();
  17. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  18. $lpId = isset($_GET['lp_id']) ? intval($_GET['lp_id']) : 0;
  19. // This page can only be shown from inside a learning path
  20. if (!$id && !$lpId) {
  21. Display::display_warning_message(get_lang('FileNotFound'));
  22. exit;
  23. }
  24. // Initialize variables required for the template
  25. $downloadCertificateLink = '';
  26. $viewCertificateLink = '';
  27. $badgeLink = '';
  28. $finalItemTemplate = '';
  29. // Check prerequisites and total completion of the learning path
  30. $lp = new Learnpath($courseCode, $lpId, $userId);
  31. $count = $lp->getTotalItemsCountWithoutDirs();
  32. $completed = $lp->get_complete_items_count(true);
  33. $currentItemId = $lp->get_current_item_id();
  34. $currentItem = $lp->items[$currentItemId];
  35. $currentItemStatus = $currentItem->get_status();
  36. $accessGranted = false;
  37. if (
  38. ($count - $completed == 0) ||
  39. ($count - $completed == 1 && ($currentItemStatus == 'incomplete') || ($currentItemStatus == 'not attempted'))
  40. ) {
  41. if ($lp->prerequisites_match($currentItemId)) {
  42. $accessGranted = true;
  43. }
  44. }
  45. // Update the progress in DB from the items completed
  46. $lp->save_last();
  47. // unset the (heavy) lp object to free memory - we don't need it anymore
  48. unset($lp);
  49. unset($currentItem);
  50. // If for some reason we consider the requirements haven't been completed yet,
  51. // show a prerequisites warning
  52. if ($accessGranted == false) {
  53. Display::display_warning_message(get_lang('LearnpathPrereqNotCompleted'));
  54. $finalItemTemplate = '';
  55. } else {
  56. $catLoad = Category::load(null, null, $courseCode, null, null, $sessionId, 'ORDER By id');
  57. // If not gradebook has been defined
  58. if (empty($catLoad)) {
  59. $finalItemTemplate = generateLPFinalItemTemplate(
  60. $id,
  61. $courseCode,
  62. $sessionId,
  63. $downloadCertificateLink,
  64. $badgeLink
  65. );
  66. // TODO: Missing validation of learning path completion
  67. } else {
  68. // A gradebook was found, proceed...
  69. $categoryId = $catLoad[0]->get_id();
  70. $link = LinkFactory::load(null, null, $lpId, null, $courseCode, $categoryId);
  71. if ($link) {
  72. $cat = new Category();
  73. $catCourseCode = CourseManager::get_course_by_category($categoryId);
  74. $show_message = $cat->show_message_resource_delete($catCourseCode);
  75. if ($show_message == '') {
  76. if (!api_is_allowed_to_edit() && !api_is_excluded_user_type()) {
  77. $certificate = Category::register_user_certificate($categoryId, $userId);
  78. if (!empty($certificate['pdf_url']) || !empty($certificate['badge_link'])) {
  79. if (is_array($certificate) && isset($certificate['pdf_url'])) {
  80. $downloadCertificateLink = generateLPFinalItemTemplateCertificateLinks($certificate);
  81. }
  82. if (is_array($certificate) && isset($certificate['badge_link'])) {
  83. $courseId = api_get_course_int_id();
  84. $badgeLink = generateLPFinalItemTemplateBadgeLinks($userId, $courseId, $sessionId);
  85. }
  86. }
  87. $currentScore = Category::getCurrentScore($userId, $categoryId, $courseCode, $sessionId, true);
  88. Category::registerCurrentScore($currentScore, $userId, $categoryId);
  89. }
  90. }
  91. }
  92. $finalItemTemplate = generateLPFinalItemTemplate($id, $courseCode, $sessionId, $downloadCertificateLink, $badgeLink);
  93. if (!$finalItemTemplate) {
  94. Display::display_warning_message(get_lang('FileNotFound'));
  95. }
  96. }
  97. }
  98. // Instance a new template : No page tittle, No header, No footer
  99. $tpl = new Template(null, false, false);
  100. $tpl->assign('content', $finalItemTemplate);
  101. $tpl->display_blank_template();
  102. // A few functions used only here...
  103. /**
  104. * Return a HTML string to show as final document in learning path
  105. * @param int $lpItemId
  106. * @param string $courseCode
  107. * @param int $sessionId
  108. * @param string $downloadCertificateLink
  109. * @param string $badgeLink
  110. * @return mixed|string
  111. */
  112. function generateLPFinalItemTemplate($lpItemId, $courseCode, $sessionId = 0, $downloadCertificateLink = '', $badgeLink = '')
  113. {
  114. $documentInfo = DocumentManager::get_document_data_by_id(
  115. $lpItemId,
  116. $courseCode,
  117. true,
  118. $sessionId
  119. );
  120. $finalItemTemplate = file_get_contents($documentInfo['absolute_path']);
  121. $finalItemTemplate = str_replace('((certificate))', $downloadCertificateLink, $finalItemTemplate);
  122. $finalItemTemplate = str_replace('((skill))', $badgeLink, $finalItemTemplate);
  123. return $finalItemTemplate;
  124. }
  125. /**
  126. * Return HTML string with badges list
  127. * @param int $userId
  128. * @param int $courseId
  129. * @param int $sessionId
  130. * @return string HTML string for badges
  131. */
  132. function generateLPFinalItemTemplateBadgeLinks($userId, $courseId, $sessionId = 0)
  133. {
  134. $em = Database::getManager();
  135. $skillRelUser = new SkillRelUser();
  136. $userSkills = $skillRelUser->get_user_skills($userId, $courseId, $sessionId);
  137. $skillList = '';
  138. $badgeLink = '';
  139. if ($userSkills) {
  140. foreach ($userSkills as $userSkill) {
  141. $skill = $em->find('ChamiloCoreBundle:Skill', $userSkill['skill_id']);
  142. $skillList .= "
  143. <div class='row'>
  144. <div class='col-md-2 col-xs-4'>
  145. <div class='thumbnail'>
  146. <img class='skill-badge-img' src='" . $skill->getWebIconPath() . "' >
  147. </div>
  148. </div>
  149. <div class='col-md-8 col-xs-8'>
  150. <h5><b>" . $skill->getName() . "</b></h5>
  151. " . $skill->getDescription() . "
  152. </div>
  153. <div class='col-md-2 col-xs-12'>
  154. <h5><b>" . get_lang('ShareWithYourFriends') . "</b></h5>
  155. <a href='http://www.facebook.com/sharer.php?u=" . api_get_path(WEB_PATH) . "badge/" . $skill->getId() . "/user/" . $userId . "' target='_new'>
  156. <em class='fa fa-facebook-square fa-3x text-info' aria-hidden='true'></em>
  157. </a>
  158. <a href='https://twitter.com/home?status=" . sprintf(get_lang('IHaveObtainedSkillXOnY'), '"' . $skill->getName() . '"', api_get_setting('siteName')) . ' - ' . api_get_path(WEB_PATH) . 'badge/' . $skill->getId() . '/user/' . $userId . "' target='_new'>
  159. <em class='fa fa-twitter-square fa-3x text-light' aria-hidden='true'></em>
  160. </a>
  161. </div>
  162. </div>
  163. ";
  164. }
  165. $badgeLink .= "
  166. <div class='panel panel-default'>
  167. <div class='panel-body'>
  168. <h3 class='text-center'>" . get_lang('AdditionallyYouHaveObtainedTheFollowingSkills') . "</h3>
  169. $skillList
  170. </div>
  171. </div>
  172. ";
  173. }
  174. return $badgeLink;
  175. }
  176. /**
  177. * Return HTML string with certificate links
  178. * @param array $certificate
  179. * @return string HTML string for certificates
  180. */
  181. function generateLPFinalItemTemplateCertificateLinks($certificate)
  182. {
  183. $downloadCertificateLink = Display::url(
  184. Display::returnFontAwesomeIcon('file-pdf-o') . get_lang('DownloadCertificatePdf'),
  185. $certificate['pdf_url'],
  186. ['class' => 'btn btn-default']
  187. );
  188. $viewCertificateLink = $certificate['certificate_link'];
  189. $downloadCertificateLink = "
  190. <div class='panel panel-default'>
  191. <div class='panel-body'>
  192. <h3 class='text-center'>" . get_lang('NowDownloadYourCertificateClickHere') . "</h3>
  193. <div class='text-center'>$downloadCertificateLink $viewCertificateLink</div>
  194. </div>
  195. </div>
  196. ";
  197. return $downloadCertificateLink;
  198. }