skill_badge_create.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Chamilo\CoreBundle\Framework\Container;
  5. /**
  6. * Show information about Mozilla OpenBadges
  7. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  8. * @package chamilo.admin.openbadges
  9. */
  10. $cidReset = true;
  11. //require_once '../inc/global.inc.php';
  12. if (!api_is_platform_admin() || api_get_setting(
  13. 'skill.allow_skills_tool'
  14. ) !== 'true'
  15. ) {
  16. api_not_allowed(true);
  17. }
  18. $this_section = SECTION_PLATFORM_ADMIN;
  19. $skillId = intval($_GET['id']);
  20. $objSkill = new Skill();
  21. $skill = $objSkill->get($skillId);
  22. $htmlHeadXtra[] = '<link href="'. api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/media/css/core.css" rel="stylesheet">';
  23. // Add badge studio paths
  24. $badgeStudio = [
  25. 'core' => api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/',
  26. 'media' => api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/media/',
  27. 'templates' => api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/media/images/templates/',
  28. 'masks' => api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/media/images/masks/',
  29. 'script_js' => '<script src="'. api_get_path(WEB_LIBRARY_JS_PATH) .'badge-studio/media/js/studio.js?"></script>'
  30. ];
  31. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  32. $params = array(
  33. 'name' => $_POST['name'],
  34. 'description' => $_POST['description'],
  35. 'criteria' => $_POST['criteria'],
  36. 'id' => $skillId
  37. );
  38. if ((isset($_FILES['image']) && $_FILES['image']['error'] == 0) || !empty($_POST['badge_studio_image'])) {
  39. $dirPermissions = api_get_permissions_for_new_directories();
  40. $fileName = sha1($_POST['name']);
  41. $badgePath = api_get_path(SYS_UPLOAD_PATH).'badges/';
  42. $existsBadgesDirectory = is_dir($badgePath);
  43. if (!$existsBadgesDirectory) {
  44. $existsBadgesDirectory = api_create_protected_dir('badges', api_get_path(SYS_UPLOAD_PATH));
  45. }
  46. if ($existsBadgesDirectory) {
  47. if (!empty($skill['icon'])) {
  48. $iconFileAbsolutePath = $badgePath . $skill['icon'];
  49. if (Security::check_abs_path($iconFileAbsolutePath, $badgePath)) {
  50. unlink($badgePath . $skill['icon']);
  51. }
  52. }
  53. $skillImagePath = sprintf("%s%s.png", $badgePath, $fileName);
  54. if (!empty($_POST['badge_studio_image'])) {
  55. $badgeImage = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $_POST['badge_studio_image']));
  56. file_put_contents($skillImagePath, $badgeImage);
  57. $skillImage = new Image($skillImagePath);
  58. } else {
  59. $skillImage = new Image($_FILES['image']['tmp_name']);
  60. }
  61. $skillImage->send_image($skillImagePath, -1, 'png');
  62. $skillThumbPath = sprintf("%s%s-small.png", $badgePath, $fileName);
  63. $skillImageThumb = new Image($skillImagePath);
  64. $skillImageThumb->resize(ICON_SIZE_BIG);
  65. $skillImageThumb->send_image($skillThumbPath);
  66. $params['icon'] = sprintf("%s.png", $fileName);
  67. } else {
  68. Session::write('errorMessage', get_lang('UplUnableToSaveFile'));
  69. }
  70. }
  71. $objSkill->update($params);
  72. header('Location: ' . api_get_path(WEB_CODE_PATH) . 'admin/skill_badge_list.php');
  73. exit;
  74. }
  75. $interbreadcrumb = array(
  76. array(
  77. 'url' => api_get_path(WEB_CODE_PATH) . 'admin/index.php',
  78. 'name' => get_lang('Administration')
  79. ),
  80. array(
  81. 'url' => api_get_path(WEB_CODE_PATH) . 'admin/skill_badge.php',
  82. 'name' => get_lang('Badges')
  83. )
  84. );
  85. $toolbar = Display::toolbarButton(
  86. get_lang('ManageSkills'),
  87. api_get_path(WEB_CODE_PATH) . 'admin/skill_list.php',
  88. 'list',
  89. 'primary',
  90. ['title' => get_lang('ManageSkills')]
  91. );
  92. echo $toolbar;
  93. echo Container::getTemplating()->render(
  94. '@template_style/skill/badge_create.html.twig',
  95. [
  96. 'skill' => $skill
  97. ]
  98. );