lp_final_item.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 ($show_message == '') {
  125. if (!api_is_allowed_to_edit() && !api_is_excluded_user_type()) {
  126. $certificate = Category::generateUserCertificate(
  127. $categoryId,
  128. $userId
  129. );
  130. if (!empty($certificate['pdf_url']) ||
  131. !empty($certificate['badge_link'])
  132. ) {
  133. if (is_array($certificate) &&
  134. isset($certificate['pdf_url'])
  135. ) {
  136. $downloadCertificateLink = generateLPFinalItemTemplateCertificateLinks(
  137. $certificate
  138. );
  139. }
  140. if (is_array($certificate) &&
  141. isset($certificate['badge_link'])
  142. ) {
  143. $courseId = api_get_course_int_id();
  144. $badgeLink = generateLPFinalItemTemplateBadgeLinks(
  145. $userId,
  146. $courseId,
  147. $sessionId
  148. );
  149. }
  150. }
  151. $currentScore = Category::getCurrentScore(
  152. $userId,
  153. $category,
  154. true
  155. );
  156. Category::registerCurrentScore(
  157. $currentScore,
  158. $userId,
  159. $categoryId
  160. );
  161. }
  162. }
  163. }
  164. $finalItemTemplate = generateLPFinalItemTemplate(
  165. $id,
  166. $courseCode,
  167. $sessionId,
  168. $downloadCertificateLink,
  169. $badgeLink
  170. );
  171. if (!$finalItemTemplate) {
  172. echo Display::return_message(get_lang('FileNotFound'), 'warning');
  173. }
  174. }
  175. }
  176. // Instance a new template : No page tittle, No header, No footer
  177. $tpl = new Template(null, false, false);
  178. $tpl->assign('content', $finalItemTemplate);
  179. $tpl->display_blank_template();
  180. // A few functions used only here...
  181. /**
  182. * Return a HTML string to show as final document in learning path.
  183. *
  184. * @param int $lpItemId
  185. * @param string $courseCode
  186. * @param int $sessionId
  187. * @param string $downloadCertificateLink
  188. * @param string $badgeLink
  189. *
  190. * @return mixed|string
  191. */
  192. function generateLPFinalItemTemplate(
  193. $lpItemId,
  194. $courseCode,
  195. $sessionId = 0,
  196. $downloadCertificateLink = '',
  197. $badgeLink = ''
  198. ) {
  199. $documentInfo = DocumentManager::get_document_data_by_id(
  200. $lpItemId,
  201. $courseCode,
  202. true,
  203. $sessionId
  204. );
  205. $finalItemTemplate = file_get_contents($documentInfo['absolute_path']);
  206. $finalItemTemplate = str_replace('((certificate))', $downloadCertificateLink, $finalItemTemplate);
  207. $finalItemTemplate = str_replace('((skill))', $badgeLink, $finalItemTemplate);
  208. return $finalItemTemplate;
  209. }
  210. /**
  211. * Return HTML string with badges list.
  212. *
  213. * @param int $userId
  214. * @param int $courseId
  215. * @param int $sessionId
  216. *
  217. * @return string HTML string for badges
  218. */
  219. function generateLPFinalItemTemplateBadgeLinks($userId, $courseId, $sessionId = 0)
  220. {
  221. $em = Database::getManager();
  222. $skillRelUser = new SkillRelUser();
  223. $userSkills = $skillRelUser->getUserSkills($userId, $courseId, $sessionId);
  224. $skillList = '';
  225. $badgeLink = '';
  226. if ($userSkills) {
  227. foreach ($userSkills as $userSkill) {
  228. $skill = $em->find('ChamiloCoreBundle:Skill', $userSkill['skill_id']);
  229. $skillList .= "
  230. <div class='row'>
  231. <div class='col-md-2 col-xs-4'>
  232. <div class='thumbnail'>
  233. <img class='skill-badge-img' src='".Skill::getWebIconPath($skill)."' >
  234. </div>
  235. </div>
  236. <div class='col-md-8 col-xs-8'>
  237. <h5><b>".$skill->getName()."</b></h5>
  238. ".$skill->getDescription()."
  239. </div>
  240. <div class='col-md-2 col-xs-12'>
  241. <h5><b>".get_lang('ShareWithYourFriends')."</b></h5>
  242. <a href='http://www.facebook.com/sharer.php?u=".api_get_path(WEB_PATH)."badge/".$skill->getId()."/user/".$userId."' target='_new'>
  243. <em class='fa fa-facebook-square fa-3x text-info' aria-hidden='true'></em>
  244. </a>
  245. <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'>
  246. <em class='fa fa-twitter-square fa-3x text-light' aria-hidden='true'></em>
  247. </a>
  248. </div>
  249. </div>
  250. ";
  251. }
  252. $badgeLink .= "
  253. <div class='panel panel-default'>
  254. <div class='panel-body'>
  255. <h3 class='text-center'>".get_lang('AdditionallyYouHaveObtainedTheFollowingSkills')."</h3>
  256. $skillList
  257. </div>
  258. </div>
  259. ";
  260. }
  261. return $badgeLink;
  262. }
  263. /**
  264. * Return HTML string with certificate links.
  265. *
  266. * @param array $certificate
  267. *
  268. * @return string HTML string for certificates
  269. */
  270. function generateLPFinalItemTemplateCertificateLinks($certificate)
  271. {
  272. $downloadCertificateLink = Display::url(
  273. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadCertificatePdf'),
  274. $certificate['pdf_url'],
  275. ['class' => 'btn btn-default']
  276. );
  277. $viewCertificateLink = $certificate['certificate_link'];
  278. $downloadCertificateLink = "
  279. <div class='panel panel-default'>
  280. <div class='panel-body'>
  281. <h3 class='text-center'>".get_lang('NowDownloadYourCertificateClickHere')."</h3>
  282. <div class='text-center'>$downloadCertificateLink $viewCertificateLink</div>
  283. </div>
  284. </div>
  285. ";
  286. return $downloadCertificateLink;
  287. }