lp_final_item.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 (($count - $completed == 0) ||
  66. ($count - $completed == 1 && ($currentItemStatus == 'incomplete') || ($currentItemStatus == 'not attempted'))
  67. ) {
  68. if ($lp->prerequisites_match($currentItemId)) {
  69. $accessGranted = true;
  70. }
  71. }
  72. // Update the progress in DB from the items completed
  73. $lp->save_last();
  74. // unset the (heavy) lp object to free memory - we don't need it anymore
  75. unset($lp);
  76. unset($currentItem);
  77. // If for some reason we consider the requirements haven't been completed yet,
  78. // show a prerequisites warning
  79. if ($accessGranted == false) {
  80. echo Display::return_message(
  81. get_lang('LearnpathPrereqNotCompleted'),
  82. 'warning'
  83. );
  84. $finalItemTemplate = '';
  85. } else {
  86. $catLoad = Category::load(
  87. null,
  88. null,
  89. $courseCode,
  90. null,
  91. null,
  92. $sessionId,
  93. 'ORDER By id'
  94. );
  95. // If not gradebook has been defined
  96. if (empty($catLoad)) {
  97. $finalItemTemplate = generateLPFinalItemTemplate(
  98. $id,
  99. $courseCode,
  100. $sessionId,
  101. $downloadCertificateLink,
  102. $badgeLink
  103. );
  104. // TODO: Missing validation of learning path completion
  105. } else {
  106. // A gradebook was found, proceed...
  107. /** @var Category $category */
  108. $category = $catLoad[0];
  109. $categoryId = $category->get_id();
  110. $link = LinkFactory::load(
  111. null,
  112. null,
  113. $lpId,
  114. null,
  115. $courseCode,
  116. $categoryId
  117. );
  118. if ($link) {
  119. $cat = new Category();
  120. $courseId = CourseManager::get_course_by_category($categoryId);
  121. $show_message = $cat->show_message_resource_delete($courseId);
  122. if ($show_message == '') {
  123. if (!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. isset($certificate['pdf_url'])
  133. ) {
  134. $downloadCertificateLink = generateLPFinalItemTemplateCertificateLinks(
  135. $certificate
  136. );
  137. }
  138. if (is_array($certificate) &&
  139. isset($certificate['badge_link'])
  140. ) {
  141. $courseId = api_get_course_int_id();
  142. $badgeLink = generateLPFinalItemTemplateBadgeLinks(
  143. $userId,
  144. $courseId,
  145. $sessionId
  146. );
  147. }
  148. }
  149. $currentScore = Category::getCurrentScore(
  150. $userId,
  151. $category,
  152. true
  153. );
  154. Category::registerCurrentScore(
  155. $currentScore,
  156. $userId,
  157. $categoryId
  158. );
  159. }
  160. }
  161. }
  162. $finalItemTemplate = generateLPFinalItemTemplate(
  163. $id,
  164. $courseCode,
  165. $sessionId,
  166. $downloadCertificateLink,
  167. $badgeLink
  168. );
  169. if (!$finalItemTemplate) {
  170. echo Display::return_message(get_lang('FileNotFound'), 'warning');
  171. }
  172. }
  173. }
  174. // Instance a new template : No page tittle, No header, No footer
  175. $tpl = new Template(null, false, false);
  176. $tpl->assign('content', $finalItemTemplate);
  177. $tpl->display_blank_template();
  178. // A few functions used only here...
  179. /**
  180. * Return a HTML string to show as final document in learning path
  181. * @param int $lpItemId
  182. * @param string $courseCode
  183. * @param int $sessionId
  184. * @param string $downloadCertificateLink
  185. * @param string $badgeLink
  186. * @return mixed|string
  187. */
  188. function generateLPFinalItemTemplate(
  189. $lpItemId,
  190. $courseCode,
  191. $sessionId = 0,
  192. $downloadCertificateLink = '',
  193. $badgeLink = ''
  194. ) {
  195. $documentInfo = DocumentManager::get_document_data_by_id(
  196. $lpItemId,
  197. $courseCode,
  198. true,
  199. $sessionId
  200. );
  201. $finalItemTemplate = file_get_contents($documentInfo['absolute_path']);
  202. $finalItemTemplate = str_replace('((certificate))', $downloadCertificateLink, $finalItemTemplate);
  203. $finalItemTemplate = str_replace('((skill))', $badgeLink, $finalItemTemplate);
  204. return $finalItemTemplate;
  205. }
  206. /**
  207. * Return HTML string with badges list
  208. * @param int $userId
  209. * @param int $courseId
  210. * @param int $sessionId
  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()."' >
  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. }
  257. /**
  258. * Return HTML string with certificate links
  259. * @param array $certificate
  260. * @return string HTML string for certificates
  261. */
  262. function generateLPFinalItemTemplateCertificateLinks($certificate)
  263. {
  264. $downloadCertificateLink = Display::url(
  265. Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadCertificatePdf'),
  266. $certificate['pdf_url'],
  267. ['class' => 'btn btn-default']
  268. );
  269. $viewCertificateLink = $certificate['certificate_link'];
  270. $downloadCertificateLink = "
  271. <div class='panel panel-default'>
  272. <div class='panel-body'>
  273. <h3 class='text-center'>" . get_lang('NowDownloadYourCertificateClickHere')."</h3>
  274. <div class='text-center'>$downloadCertificateLink $viewCertificateLink</div>
  275. </div>
  276. </div>
  277. ";
  278. return $downloadCertificateLink;
  279. }