lp_final_item.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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('The file was not found'), '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->getServiceSales(
  32. $userId,
  33. BuyCoursesPlugin::SERVICE_STATUS_COMPLETED,
  34. BuyCoursesPlugin::SERVICE_TYPE_LP_FINAL_ITEM,
  35. $lpId
  36. );
  37. if (empty($userServiceSale)) {
  38. // Instance a new template : No page tittle, No header, No footer
  39. $tpl = new Template(null, false, false);
  40. $url = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php';
  41. $content = sprintf(
  42. Display::return_message(
  43. get_lang('If you want to get the certificate and/or skills associated with this course, you need to buy the certificate service. You can go to the services catalog by clicking this link: %s'),
  44. 'normal',
  45. false
  46. ),
  47. '<a href="'.$url.'">'.$url.'</a>'
  48. );
  49. $tpl->assign('content', $content);
  50. $tpl->display_blank_template();
  51. exit;
  52. }
  53. }
  54. // Initialize variables required for the template
  55. $downloadCertificateLink = '';
  56. $viewCertificateLink = '';
  57. $badgeLink = '';
  58. $finalItemTemplate = '';
  59. // Check prerequisites and total completion of the learning path
  60. $lp = new Learnpath($courseCode, $lpId, $userId);
  61. $count = $lp->getTotalItemsCountWithoutDirs();
  62. $completed = $lp->get_complete_items_count(true);
  63. $currentItemId = $lp->get_current_item_id();
  64. $currentItem = $lp->items[$currentItemId];
  65. $currentItemStatus = $currentItem->get_status();
  66. $accessGranted = false;
  67. if (($count - $completed == 0) ||
  68. ($count - $completed == 1 && ($currentItemStatus == 'incomplete') || ($currentItemStatus == 'not attempted'))
  69. ) {
  70. if ($lp->prerequisites_match($currentItemId)) {
  71. $accessGranted = true;
  72. }
  73. }
  74. // Update the progress in DB from the items completed
  75. $lp->save_last();
  76. // unset the (heavy) lp object to free memory - we don't need it anymore
  77. unset($lp);
  78. unset($currentItem);
  79. // If for some reason we consider the requirements haven't been completed yet,
  80. // show a prerequisites warning
  81. if ($accessGranted == false) {
  82. echo Display::return_message(
  83. get_lang('This learning object cannot display because the course prerequisites are not completed. This happens when a course imposes that you follow it step by step or get a minimum score in tests before you reach the next steps.'),
  84. 'warning'
  85. );
  86. $finalItemTemplate = '';
  87. } else {
  88. $catLoad = Category::load(
  89. null,
  90. null,
  91. $courseCode,
  92. null,
  93. null,
  94. $sessionId,
  95. 'ORDER By id'
  96. );
  97. // If not gradebook has been defined
  98. if (empty($catLoad)) {
  99. $finalItemTemplate = generateLPFinalItemTemplate(
  100. $id,
  101. $courseCode,
  102. $sessionId,
  103. $downloadCertificateLink,
  104. $badgeLink
  105. );
  106. } else {
  107. // A gradebook was found, proceed...
  108. /** @var Category $category */
  109. $category = $catLoad[0];
  110. $categoryId = $category->get_id();
  111. $link = LinkFactory::load(
  112. null,
  113. null,
  114. $lpId,
  115. null,
  116. $courseCode,
  117. $categoryId
  118. );
  119. if ($link) {
  120. $cat = new Category();
  121. $catCourseCode = CourseManager::get_course_by_category($categoryId);
  122. $show_message = $cat->show_message_resource_delete($catCourseCode);
  123. if (false === $show_message && !api_is_allowed_to_edit() && !api_is_excluded_user_type()) {
  124. $certificate = Category::generateUserCertificate(
  125. $categoryId,
  126. $userId
  127. );
  128. if (!empty($certificate['pdf_url']) ||
  129. !empty($certificate['badge_link'])
  130. ) {
  131. if (is_array($certificate)) {
  132. $downloadCertificateLink = Category::getDownloadCertificateBlock($certificate);
  133. }
  134. if (is_array($certificate) &&
  135. isset($certificate['badge_link'])
  136. ) {
  137. $courseId = api_get_course_int_id();
  138. $badgeLink = generateLPFinalItemTemplateBadgeLinks(
  139. $userId,
  140. $courseId,
  141. $sessionId
  142. );
  143. }
  144. }
  145. $currentScore = Category::getCurrentScore(
  146. $userId,
  147. $category,
  148. true
  149. );
  150. Category::registerCurrentScore(
  151. $currentScore,
  152. $userId,
  153. $categoryId
  154. );
  155. }
  156. }
  157. $finalItemTemplate = generateLPFinalItemTemplate(
  158. $id,
  159. $courseCode,
  160. $sessionId,
  161. $downloadCertificateLink,
  162. $badgeLink
  163. );
  164. if (!$finalItemTemplate) {
  165. echo Display::return_message(get_lang('The file was not found'), 'warning');
  166. }
  167. }
  168. }
  169. // Instance a new template : No page tittle, No header, No footer
  170. $tpl = new Template(null, false, false);
  171. $tpl->assign('content', $finalItemTemplate);
  172. $tpl->display_blank_template();
  173. // A few functions used only here...
  174. /**
  175. * Return a HTML string to show as final document in learning path.
  176. *
  177. * @param int $lpItemId
  178. * @param string $courseCode
  179. * @param int $sessionId
  180. * @param string $downloadCertificateLink
  181. * @param string $badgeLink
  182. *
  183. * @return mixed|string
  184. */
  185. function generateLPFinalItemTemplate(
  186. $lpItemId,
  187. $courseCode,
  188. $sessionId = 0,
  189. $downloadCertificateLink = '',
  190. $badgeLink = ''
  191. ) {
  192. $documentInfo = DocumentManager::get_document_data_by_id(
  193. $lpItemId,
  194. $courseCode,
  195. true,
  196. $sessionId
  197. );
  198. $finalItemTemplate = file_get_contents($documentInfo['absolute_path']);
  199. $finalItemTemplate = str_replace('((certificate))', $downloadCertificateLink, $finalItemTemplate);
  200. $finalItemTemplate = str_replace('((skill))', $badgeLink, $finalItemTemplate);
  201. return $finalItemTemplate;
  202. }
  203. /**
  204. * Return HTML string with badges list.
  205. *
  206. * @param int $userId
  207. * @param int $courseId
  208. * @param int $sessionId
  209. *
  210. * @return string HTML string for badges
  211. */
  212. function generateLPFinalItemTemplateBadgeLinks($userId, $courseId, $sessionId = 0)
  213. {
  214. $em = Database::getManager();
  215. $skillRelUser = new SkillRelUser();
  216. $userSkills = $skillRelUser->getUserSkills($userId, $courseId, $sessionId);
  217. $skillList = '';
  218. $badgeLink = '';
  219. if ($userSkills) {
  220. foreach ($userSkills as $userSkill) {
  221. $skill = $em->find('ChamiloCoreBundle:Skill', $userSkill['skill_id']);
  222. $skillList .= "
  223. <div class='row'>
  224. <div class='col-md-2 col-xs-4'>
  225. <div class='thumbnail'>
  226. <img class='skill-badge-img' src='".Skill::getWebIconPath($skill)."' >
  227. </div>
  228. </div>
  229. <div class='col-md-8 col-xs-8'>
  230. <h5><b>".$skill->getName()."</b></h5>
  231. ".$skill->getDescription()."
  232. </div>
  233. <div class='col-md-2 col-xs-12'>
  234. <h5><b>".get_lang('Share with your friends')."</b></h5>
  235. <a href='http://www.facebook.com/sharer.php?u=".api_get_path(WEB_PATH)."badge/".$skill->getId()."/user/".$userId."' target='_new'>
  236. <em class='fa fa-facebook-square fa-3x text-info' aria-hidden='true'></em>
  237. </a>
  238. <a href='https://twitter.com/home?status=".sprintf(get_lang('I have achieved skill %s on %s'), '"'.$skill->getName().'"', api_get_setting('siteName')).' - '.api_get_path(WEB_PATH).'badge/'.$skill->getId().'/user/'.$userId."' target='_new'>
  239. <em class='fa fa-twitter-square fa-3x text-light' aria-hidden='true'></em>
  240. </a>
  241. </div>
  242. </div>
  243. ";
  244. }
  245. $badgeLink .= "
  246. <div class='panel panel-default'>
  247. <div class='panel-body'>
  248. <h3 class='text-center'>".get_lang('Additionally, you have achieved the following skills')."</h3>
  249. $skillList
  250. </div>
  251. </div>
  252. ";
  253. }
  254. return $badgeLink;
  255. }