lp_final_item.php 9.0 KB

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