skill_badge_create.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Show information about Mozilla OpenBadges.
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. *
  8. * @package chamilo.admin.openbadges
  9. */
  10. $cidReset = true;
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. api_protect_admin_script();
  13. Skill::isAllowed();
  14. $this_section = SECTION_PLATFORM_ADMIN;
  15. $skillId = intval($_GET['id']);
  16. $objSkill = new Skill();
  17. $skill = $objSkill->get($skillId);
  18. $htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/media/css/core.css" rel="stylesheet">';
  19. // Add badge studio paths
  20. $badgeStudio = [
  21. 'core' => api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/',
  22. 'media' => api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/media/',
  23. 'templates' => api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/media/images/templates/',
  24. 'masks' => api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/media/images/masks/',
  25. 'script_js' => '<script src="'.api_get_path(WEB_LIBRARY_JS_PATH).'badge-studio/media/js/studio.js?"></script>',
  26. ];
  27. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  28. $params = [
  29. 'id' => $skillId,
  30. ];
  31. if ((isset($_FILES['image']) && $_FILES['image']['error'] == 0) ||
  32. !empty($_POST['badge_studio_image'])
  33. ) {
  34. $dirPermissions = api_get_permissions_for_new_directories();
  35. $fileName = sha1($skill['name']);
  36. $badgePath = api_get_path(SYS_UPLOAD_PATH).'badges/';
  37. $existsBadgesDirectory = is_dir($badgePath);
  38. if (!$existsBadgesDirectory) {
  39. $existsBadgesDirectory = api_create_protected_dir('badges', api_get_path(SYS_UPLOAD_PATH));
  40. }
  41. if ($existsBadgesDirectory) {
  42. if (!empty($skill['icon'])) {
  43. $iconFileAbsolutePath = $badgePath.$skill['icon'];
  44. if (Security::check_abs_path($iconFileAbsolutePath, $badgePath)) {
  45. unlink($badgePath.$skill['icon']);
  46. }
  47. }
  48. $skillImagePath = sprintf("%s%s.png", $badgePath, $fileName);
  49. if (!empty($_POST['badge_studio_image'])) {
  50. $badgeImage = base64_decode(
  51. preg_replace('#^data:image/\w+;base64,#i', '', $_POST['badge_studio_image'])
  52. );
  53. file_put_contents($skillImagePath, $badgeImage);
  54. $skillImage = new Image($skillImagePath);
  55. } else {
  56. $skillImage = new Image($_FILES['image']['tmp_name']);
  57. }
  58. $skillImage->send_image($skillImagePath, -1, 'png');
  59. $skillThumbPath = sprintf("%s%s-small.png", $badgePath, $fileName);
  60. $skillImageThumb = new Image($skillImagePath);
  61. $skillImageThumb->resize(ICON_SIZE_BIG);
  62. $skillImageThumb->send_image($skillThumbPath);
  63. $params['icon'] = sprintf("%s.png", $fileName);
  64. } else {
  65. Display::addFlash(Display::return_message(get_lang('The uploaded file could not be saved (perhaps a permission problem?)')), 'warning');
  66. }
  67. }
  68. Display::addFlash(Display::return_message(get_lang('Update successful')));
  69. $objSkill->update($params);
  70. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/skill_list.php');
  71. exit;
  72. }
  73. $interbreadcrumb[] = [
  74. 'url' => api_get_path(WEB_CODE_PATH).'admin/index.php',
  75. 'name' => get_lang('Administration'),
  76. ];
  77. $interbreadcrumb[] = ['url' => 'skill_list.php', 'name' => get_lang('Manage skills')];
  78. $toolbar = $objSkill->getToolBar();
  79. $tpl = new Template(get_lang('Create badge'));
  80. $tpl->assign('platformAdminEmail', api_get_setting('emailAdministrator'));
  81. $tpl->assign('skill', $skill);
  82. $tpl->assign('badge_studio', $badgeStudio);
  83. $templateName = $tpl->get_template('skill/badge_create.tpl');
  84. $contentTemplate = $tpl->fetch($templateName);
  85. $tpl->assign('content', $toolbar.$contentTemplate);
  86. $tpl->display_one_col_template();