skill_create.php 3.7 KB

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