course_add.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\CourseCategory;
  4. use Chamilo\CoreBundle\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('Create a course');
  13. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('Administration')];
  14. $interbreadcrumb[] = ['url' => 'course_list.php', 'name' => get_lang('Course list')];
  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('Code'),
  39. get_lang('Only letters (a-z) and numbers (0-9)'),
  40. ],
  41. false,
  42. [
  43. 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
  44. 'pattern' => '[a-zA-Z0-9]+',
  45. 'title' => get_lang('Only letters (a-z) and numbers (0-9)'),
  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('Category'),
  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('Category'),
  78. $categoriesOptions
  79. );
  80. }
  81. $form->addRule(
  82. 'visual_code',
  83. get_lang('max. 20 characters, e.g. <i>INNOV21</i>'),
  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('Teachers'),
  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('Department'),
  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('DepartmentURL'),
  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) === 1) {
  119. // If there's only one language available, there's no point in asking
  120. $form->addElement('hidden', 'course_language', $languages[0]);
  121. } else {
  122. $form->addSelectLanguage(
  123. 'course_language',
  124. get_lang('Language'),
  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('Course template'),
  135. get_lang('Pick a course as template for this new course'),
  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('Fill with demo content'));
  142. $group = [];
  143. $group[] = $form->createElement('radio', 'visibility', get_lang('Course access'), get_lang('Public - access allowed for the whole world'), COURSE_VISIBILITY_OPEN_WORLD);
  144. $group[] = $form->createElement('radio', 'visibility', null, get_lang(' Open - access allowed for users registered on the platform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  145. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Private access (access authorized to group members only)'), COURSE_VISIBILITY_REGISTERED);
  146. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Closed - the course is only accessible to the teachers'), COURSE_VISIBILITY_CLOSED);
  147. $group[] = $form->createElement('radio', 'visibility', null, get_lang('Hidden - Completely hidden to all users except the administrators'), COURSE_VISIBILITY_HIDDEN);
  148. $form->addGroup($group, '', get_lang('Course access'));
  149. $group = [];
  150. $group[] = $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  151. $group[] = $form->createElement('radio', 'subscribe', null, get_lang('This function is only available to trainers'), 0);
  152. $form->addGroup($group, '', get_lang('Subscription'));
  153. $group = [];
  154. $group[] = $form->createElement('radio', 'unsubscribe', get_lang('Unsubscribe'), get_lang('Users are allowed to unsubscribe from this course'), 1);
  155. $group[] = $form->createElement('radio', 'unsubscribe', null, get_lang('NotUsers are allowed to unsubscribe from this course'), 0);
  156. $form->addGroup($group, '', get_lang('Unsubscribe'));
  157. $form->addElement('text', 'disk_quota', [get_lang('Disk Space'), null, get_lang('MB')], [
  158. 'id' => 'disk_quota',
  159. ]);
  160. $form->addRule('disk_quota', get_lang('This field should be numeric'), '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('Create a course'));
  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('Course %s added'),
  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();