lp_final_item.php 8.7 KB

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