lp_final_item.php 9.3 KB

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