course_add.php 8.8 KB

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