course_add.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\CourseCategory;
  4. use Chamilo\CoreBundle\Entity\Repository\CourseCategoryRepository;
  5. /**
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. $tool_name = get_lang('AddCourse');
  13. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
  14. $interbreadcrumb[] = ['url' => 'course_list.php', 'name' => get_lang('CourseList')];
  15. $em = Database::getManager();
  16. /** @var CourseCategoryRepository $courseCategoriesRepo */
  17. $courseCategoriesRepo = $em->getRepository('ChamiloCoreBundle:CourseCategory');
  18. // Get all possible teachers.
  19. $accessUrlId = api_get_current_access_url_id();
  20. // Build the form.
  21. $form = new FormValidator('update_course');
  22. $form->addElement('header', $tool_name);
  23. // Title
  24. $form->addText(
  25. 'title',
  26. get_lang('Title'),
  27. true,
  28. [
  29. 'aria-label' => get_lang('Title'),
  30. ]
  31. );
  32. $form->applyFilter('title', 'html_filter');
  33. $form->applyFilter('title', 'trim');
  34. // Code
  35. $form->addText(
  36. 'visual_code',
  37. [
  38. get_lang('CourseCode'),
  39. get_lang('OnlyLettersAndNumbers'),
  40. ],
  41. false,
  42. [
  43. 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
  44. 'pattern' => '[a-zA-Z0-9]+',
  45. 'title' => get_lang('OnlyLettersAndNumbers'),
  46. 'id' => 'visual_code',
  47. ]
  48. );
  49. $form->applyFilter('visual_code', 'api_strtoupper');
  50. $form->applyFilter('visual_code', 'html_filter');
  51. $countCategories = $courseCategoriesRepo->countAllInAccessUrl(
  52. $accessUrlId,
  53. api_get_configuration_value('allow_base_course_category')
  54. );
  55. if ($countCategories >= 100) {
  56. // Category code
  57. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  58. $form->addElement(
  59. 'select_ajax',
  60. 'category_code',
  61. get_lang('CourseFaculty'),
  62. null,
  63. ['url' => $url]
  64. );
  65. } else {
  66. $categories = $courseCategoriesRepo->findAllInAccessUrl(
  67. $accessUrlId,
  68. api_get_configuration_value('allow_base_course_category')
  69. );
  70. $categoriesOptions = [null => get_lang('None')];
  71. /** @var CourseCategory $category */
  72. foreach ($categories as $category) {
  73. $categoriesOptions[$category->getCode()] = (string) $category;
  74. }
  75. $form->addSelect(
  76. 'category_code',
  77. get_lang('CourseFaculty'),
  78. $categoriesOptions
  79. );
  80. }
  81. $form->addRule(
  82. 'visual_code',
  83. get_lang('Max'),
  84. 'maxlength',
  85. CourseManager::MAX_COURSE_LENGTH_CODE
  86. );
  87. $currentTeacher = api_get_user_entity(api_get_user_id());
  88. $form->addSelectAjax(
  89. 'course_teachers',
  90. get_lang('CourseTeachers'),
  91. [$currentTeacher->getId() => UserManager::formatUserFullName($currentTeacher, true)],
  92. [
  93. 'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=teacher_to_basis_course',
  94. 'id' => 'course_teachers',
  95. 'multiple' => 'multiple',
  96. ]
  97. );
  98. $form->applyFilter('course_teachers', 'html_filter');
  99. // Course department
  100. $form->addText(
  101. 'department_name',
  102. get_lang('CourseDepartment'),
  103. false,
  104. ['size' => '60', 'id' => 'department_name']
  105. );
  106. $form->applyFilter('department_name', 'html_filter');
  107. $form->applyFilter('department_name', 'trim');
  108. // Department URL
  109. $form->addText(
  110. 'department_url',
  111. get_lang('CourseDepartmentURL'),
  112. false,
  113. ['size' => '60', 'id' => 'department_url']
  114. );
  115. $form->applyFilter('department_url', 'html_filter');
  116. // Course language.
  117. $languages = api_get_languages();
  118. if (count($languages['name']) === 1) {
  119. // If there's only one language available, there's no point in asking
  120. $form->addElement('hidden', 'course_language', $languages['folder'][0]);
  121. } else {
  122. $form->addSelectLanguage(
  123. 'course_language',
  124. get_lang('Ln'),
  125. [],
  126. ['style' => 'width:150px']
  127. );
  128. }
  129. if (api_get_setting('teacher_can_select_course_template') === 'true') {
  130. $form->addElement(
  131. 'select_ajax',
  132. 'course_template',
  133. [
  134. get_lang('CourseTemplate'),
  135. get_lang('PickACourseAsATemplateForThisNewCourse'),
  136. ],
  137. null,
  138. ['url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course']
  139. );
  140. }
  141. $form->addElement('checkbox', 'exemplary_content', '', get_lang('FillWithExemplaryContent'));
  142. $group = [];
  143. $group[] = $form->createElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  144. $group[] = $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  145. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  146. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  147. $group[] = $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  148. $form->addGroup($group, '', get_lang('CourseAccess'));
  149. $group = [];
  150. $group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  151. $group[] = $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  152. $form->addGroup($group, '', get_lang('Subscription'));
  153. $group = [];
  154. $group[] = $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  155. $group[] = $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  156. $form->addGroup($group, '', get_lang('Unsubscription'));
  157. $form->addElement('text', 'disk_quota', [get_lang('CourseQuota'), null, get_lang('MB')], [
  158. 'id' => 'disk_quota',
  159. ]);
  160. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  161. $obj = new GradeModel();
  162. $obj->fill_grade_model_select_in_form($form);
  163. //Extra fields
  164. $extra_field = new ExtraField('course');
  165. $extra = $extra_field->addElements($form);
  166. $htmlHeadXtra[] = '
  167. <script>
  168. $(function() {
  169. '.$extra['jquery_ready_content'].'
  170. });
  171. </script>';
  172. $form->addProgress();
  173. $form->addButtonCreate(get_lang('CreateCourse'));
  174. // Set some default values.
  175. $values['course_language'] = api_get_setting('platformLanguage');
  176. $values['disk_quota'] = round(api_get_setting('default_document_quotum') / 1024 / 1024, 1);
  177. $default_course_visibility = api_get_setting('courses_default_creation_visibility');
  178. if (isset($default_course_visibility)) {
  179. $values['visibility'] = api_get_setting('courses_default_creation_visibility');
  180. } else {
  181. $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
  182. }
  183. $values['subscribe'] = 1;
  184. $values['unsubscribe'] = 0;
  185. $values['course_teachers'] = [$currentTeacher->getId()];
  186. $form->setDefaults($values);
  187. // Validate the form
  188. if ($form->validate()) {
  189. $course = $form->exportValues();
  190. $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
  191. $course['disk_quota'] = $course['disk_quota'] * 1024 * 1024;
  192. $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
  193. $course['teachers'] = $course_teachers;
  194. $course['wanted_code'] = $course['visual_code'];
  195. $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
  196. // Fixing category code
  197. $course['course_category'] = isset($course['category_code']) ? $course['category_code'] : '';
  198. include_once api_get_path(SYS_CODE_PATH).'lang/english/trad4all.inc.php';
  199. $file_to_include = api_get_path(SYS_CODE_PATH).'lang/'.$course['course_language'].'/trad4all.inc.php';
  200. if (file_exists($file_to_include)) {
  201. include $file_to_include;
  202. }
  203. $courseInfo = CourseManager::create_course($course);
  204. if ($courseInfo && isset($courseInfo['course_public_url'])) {
  205. Display::addFlash(
  206. Display::return_message(
  207. sprintf(
  208. get_lang('CourseXAdded'),
  209. Display::url($courseInfo['title'], $courseInfo['course_public_url'])
  210. ),
  211. 'confirmation',
  212. false
  213. )
  214. );
  215. }
  216. header('Location: course_list.php');
  217. exit;
  218. }
  219. // Display the form.
  220. $content = $form->returnForm();
  221. $tpl = new Template($tool_name);
  222. $tpl->assign('content', $content);
  223. $tpl->display_one_col_template();