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