course_add.php 7.8 KB

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