skill_create.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Create skill form
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. * @package chamilo.admin
  8. */
  9. $cidReset = true;
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. api_protect_admin_script();
  13. if (api_get_setting('allow_skills_tool') != 'true') {
  14. api_not_allowed();
  15. }
  16. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  17. $interbreadcrumb[] = array('url' => 'skill_list.php', 'name' => get_lang('ManageSkills'));
  18. /* Process data */
  19. $skillParentId = isset($_GET['parent']) ? intval($_GET['parent']) : 0;
  20. $formDefaultValues = [];
  21. $objSkill = new Skill();
  22. $objGradebook = new Gradebook();
  23. if ($skillParentId > 0) {
  24. $skillParentInfo = $objSkill->get_skill_info($skillParentId);
  25. $formDefaultValues = [
  26. 'parent_id' => $skillParentInfo['id'],
  27. 'gradebook_id' => []
  28. ];
  29. foreach ($skillParentInfo['gradebooks'] as $gradebook) {
  30. $formDefaultValues['gradebook_id'][] = intval($gradebook['id']);
  31. }
  32. }
  33. $allSkills = $objSkill->get_all();
  34. $allGradebooks = $objGradebook->find('all');
  35. // This procedure is for check if there is already a Skill with no Parent (Root by default)
  36. $isAlreadyRootSkill = false;
  37. foreach ($allSkills as $checkedSkill) {
  38. if (intval($checkedSkill['parent_id']) > 0) {
  39. $isAlreadyRootSkill = true;
  40. break;
  41. }
  42. }
  43. $skillList = $isAlreadyRootSkill ? [] : [0 => get_lang('None')];
  44. $gradebookList = [];
  45. foreach ($allSkills as $skill) {
  46. $skillList[$skill['id']] = $skill['name'];
  47. }
  48. foreach ($allGradebooks as $gradebook) {
  49. $gradebookList[$gradebook['id']] = $gradebook['name'];
  50. }
  51. /* Form */
  52. $createForm = new FormValidator('skill_create');
  53. $createForm->addHeader(get_lang('CreateSkill'));
  54. $createForm->addText('name', get_lang('Name'), true, ['id' => 'name']);
  55. $createForm->addText('short_code', get_lang('ShortCode'), false, ['id' => 'short_code']);
  56. $createForm->addSelect('parent_id', get_lang('Parent'), $skillList, ['id' => 'parent_id']);
  57. $createForm->addSelect(
  58. 'gradebook_id',
  59. [get_lang('Gradebook'), get_lang('WithCertificate')],
  60. $gradebookList,
  61. ['id' => 'gradebook_id', 'multiple' => 'multiple', 'size' => 10]
  62. );
  63. $createForm->addTextarea('description', get_lang('Description'), ['id' => 'description', 'rows' => 7]);
  64. // EXTRA FIELDS
  65. $extraField = new ExtraField('skill');
  66. $returnParams = $extraField->addElements($createForm);
  67. $jquery_ready_content = $returnParams['jquery_ready_content'];
  68. // the $jquery_ready_content variable collects all functions that will be load in the $(document).ready javascript function
  69. if (!empty($jquery_ready_content)) {
  70. $htmlHeadXtra[] = '<script>
  71. $(document).ready(function(){
  72. ' . $jquery_ready_content.'
  73. });
  74. </script>';
  75. }
  76. $createForm->addButtonSave(get_lang('Save'));
  77. $createForm->addHidden('id', null);
  78. $createForm->setDefaults($formDefaultValues);
  79. if ($createForm->validate()) {
  80. $skillValues = $createForm->getSubmitValues();
  81. $created = $objSkill->add($skillValues);
  82. $skillValues['item_id'] = $created;
  83. $extraFieldValue = new ExtraFieldValue('skill');
  84. $extraFieldValue->saveFieldValues($skillValues);
  85. if ($created) {
  86. Session::write(
  87. 'message',
  88. Display::return_message(get_lang('TheSkillHasBeenCreated'), 'success')
  89. );
  90. } else {
  91. Session::write(
  92. 'message',
  93. Display::return_message(get_lang('CannotCreateSkill'), 'error')
  94. );
  95. }
  96. header('Location: '.api_get_path(WEB_CODE_PATH).'admin/skill_list.php');
  97. exit;
  98. }
  99. /* view */
  100. $tpl = new Template(get_lang('CreateSkill'));
  101. $tpl->assign('content', $createForm->returnForm());
  102. $tpl->display_one_col_template();