course_add.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once '../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. $form->addElement('select_language', 'course_language', get_lang('CourseLanguage'));
  88. $form->applyFilter('select_language', 'html_filter');
  89. $form->addElement('checkbox', 'exemplary_content', '', get_lang('FillWithExemplaryContent'));
  90. $group = array();
  91. $group[]= $form->createElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  92. $group[]= $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  93. $group[]= $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  94. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  95. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  96. $form->addGroup($group,'', get_lang('CourseAccess'), '<br />');
  97. $group = array();
  98. $group[]= $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  99. $group[]= $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  100. $form->addGroup($group,'', get_lang('Subscription'), '<br />');
  101. $group = array();
  102. $group[]= $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  103. $group[]= $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  104. $form->addGroup($group, '', get_lang('Unsubscription'), '<br />');
  105. $form->addElement('text','disk_quota',array(get_lang('CourseQuota'), null, get_lang('MB')));
  106. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  107. $obj = new GradeModel();
  108. $obj->fill_grade_model_select_in_form($form);
  109. //Extra fields
  110. $extra_field = new ExtraField('course');
  111. $extra = $extra_field->addElements($form);
  112. $htmlHeadXtra[] ='
  113. <script>
  114. $(function() {
  115. '.$extra['jquery_ready_content'].'
  116. });
  117. </script>';
  118. $form->add_progress_bar();
  119. $form->addButtonCreate(get_lang('CreateCourse'));
  120. // Set some default values.
  121. $values['course_language'] = api_get_setting('platformLanguage');
  122. $values['disk_quota'] = round(api_get_setting('default_document_quotum')/1024/1024, 1);
  123. $default_course_visibility = api_get_setting('courses_default_creation_visibility');
  124. if (isset($default_course_visibility)) {
  125. $values['visibility'] = api_get_setting('courses_default_creation_visibility');
  126. } else {
  127. $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
  128. }
  129. $values['subscribe'] = 1;
  130. $values['unsubscribe'] = 0;
  131. $values['course_teachers'] = array(api_get_user_id());
  132. $form->setDefaults($values);
  133. // Validate the form
  134. if ($form->validate()) {
  135. $course = $form->exportValues();
  136. $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
  137. $course['disk_quota'] = $course['disk_quota']*1024*1024;
  138. $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
  139. $course['teachers'] = $course_teachers;
  140. $course['wanted_code'] = $course['visual_code'];
  141. $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
  142. // Fixing category code
  143. $course['course_category'] = isset($course['category_code']) ? $course['category_code'] : '';
  144. $course_info = CourseManager::create_course($course);
  145. header('Location: course_list.php'.($course_info===false?'?action=show_msg&warn='.api_get_last_failure():''));
  146. exit;
  147. }
  148. // Display the form.
  149. $content = $form->returnForm();
  150. $tpl = new Template($tool_name);
  151. $tpl->assign('content', $content);
  152. $tpl->display_one_col_template();