course_add.php 8.1 KB

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