skill_badge_create.php 4.1 KB

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