course_add.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. $this_section = SECTION_PLATFORM_ADMIN;
  9. api_protect_admin_script();
  10. $tool_name = get_lang('AddCourse');
  11. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), '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. $languages = api_get_languages();
  88. if (count($languages['name']) === 1) {
  89. // If there's only one language available, there's no point in asking
  90. $form->addElement('hidden', 'course_language', $languages['folder'][0]);
  91. } else {
  92. $form->addElement(
  93. 'select_language',
  94. 'course_language',
  95. get_lang('Ln'),
  96. array(),
  97. array('style' => 'width:150px')
  98. );
  99. $form->applyFilter('select_language', 'html_filter');
  100. }
  101. if (api_get_setting('course.teacher_can_select_course_template') === 'true') {
  102. $form->addElement(
  103. 'select_ajax',
  104. 'course_template',
  105. [
  106. get_lang('CourseTemplate'),
  107. get_lang('PickACourseAsATemplateForThisNewCourse'),
  108. ],
  109. null,
  110. ['url' => api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_course']
  111. );
  112. }
  113. $form->addElement('checkbox', 'exemplary_content', '', get_lang('FillWithExemplaryContent'));
  114. $group = array();
  115. $group[]= $form->createElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  116. $group[]= $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  117. $group[]= $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  118. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  119. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  120. $form->addGroup($group,'', get_lang('CourseAccess'));
  121. $group = array();
  122. $group[]= $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  123. $group[]= $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  124. $form->addGroup($group,'', get_lang('Subscription'));
  125. $group = array();
  126. $group[]= $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  127. $group[]= $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  128. $form->addGroup($group, '', get_lang('Unsubscription'));
  129. $form->addElement('text','disk_quota',array(get_lang('CourseQuota'), null, get_lang('MB')));
  130. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  131. $obj = new GradeModel();
  132. $obj->fill_grade_model_select_in_form($form);
  133. //Extra fields
  134. $extra_field = new ExtraField('course');
  135. $extra = $extra_field->addElements($form);
  136. $htmlHeadXtra[] ='
  137. <script>
  138. $(function() {
  139. '.$extra['jquery_ready_content'].'
  140. });
  141. </script>';
  142. $form->addProgress();
  143. $form->addButtonCreate(get_lang('CreateCourse'));
  144. // Set some default values.
  145. $values['course_language'] = api_get_setting('language.platform_language');
  146. $values['disk_quota'] = round(
  147. api_get_setting('document.default_document_quotum') / 1024 / 1024,
  148. 1
  149. );
  150. $default_course_visibility = api_get_setting('course.courses_default_creation_visibility');
  151. if ($default_course_visibility != '') {
  152. $values['visibility'] = $default_course_visibility;
  153. } else {
  154. $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
  155. }
  156. $values['subscribe'] = 1;
  157. $values['unsubscribe'] = 0;
  158. $values['course_teachers'] = array(api_get_user_id());
  159. $form->setDefaults($values);
  160. // Validate the form
  161. if ($form->validate()) {
  162. $course = $form->exportValues();
  163. $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
  164. $course['disk_quota'] = $course['disk_quota']*1024*1024;
  165. $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
  166. $course['teachers'] = $course_teachers;
  167. $course['wanted_code'] = $course['visual_code'];
  168. $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
  169. // Fixing category code
  170. $course['course_category'] = isset($course['category_code']) ? $course['category_code'] : '';
  171. $course_info = CourseManager::create_course($course);
  172. if ($courseInfo && isset($courseInfo['course_public_url'])) {
  173. Display::addFlash(
  174. Display::return_message(
  175. sprintf(
  176. get_lang('CourseXAdded'),
  177. Display::url($courseInfo['title'], $courseInfo['course_public_url'])
  178. ),
  179. 'confirmation',
  180. false
  181. )
  182. );
  183. }
  184. header('Location: course_list.php');
  185. exit;
  186. }
  187. // Display the form.
  188. $content = $form->returnForm();
  189. $repo = Database::getManager()->getRepository('ChamiloCoreBundle:Course');
  190. $url = Database::getManager()->getRepository('ChamiloCoreBundle:AccessUrlRelCourse')->find(1);
  191. if ($url) {
  192. $limit = $repo->getCountActiveCoursesByUrl($url->getUrl());
  193. }
  194. echo $content;