course_add.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* MAIN CODE */
  14. global $_configuration;
  15. // Get all possible teachers.
  16. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  17. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  18. $sql = "SELECT user_id,lastname,firstname FROM $table_user 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 FROM $table_user as u
  23. INNER JOIN $access_url_rel_user_table url_rel_user
  24. ON (u.user_id=url_rel_user.user_id) WHERE url_rel_user.access_url_id=".api_get_current_access_url_id()." AND status=1".$order_clause;
  25. }
  26. $res = Database::query($sql);
  27. $teachers = array();
  28. while ($obj = Database::fetch_object($res)) {
  29. $teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  30. }
  31. // Build the form.
  32. $form = new FormValidator('update_course');
  33. $form->addElement('header', $tool_name);
  34. // Title
  35. $form->addText('title', get_lang('Title'), true);
  36. $form->applyFilter('title', 'html_filter');
  37. $form->applyFilter('title', 'trim');
  38. // Code
  39. $form->addText(
  40. 'visual_code',
  41. array(
  42. get_lang('CourseCode'),
  43. get_lang('OnlyLettersAndNumbers')
  44. ),
  45. false,
  46. [
  47. 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
  48. 'pattern' => '[a-zA-Z0-9]+',
  49. 'title' => get_lang('OnlyLettersAndNumbers')
  50. ]
  51. );
  52. $form->applyFilter('visual_code', 'api_strtoupper');
  53. $form->applyFilter('visual_code', 'html_filter');
  54. $form->addRule('visual_code', get_lang('Max'), 'maxlength', CourseManager::MAX_COURSE_LENGTH_CODE);
  55. $form->addElement(
  56. 'select',
  57. 'course_teachers',
  58. get_lang('CourseTeachers'),
  59. $teachers,
  60. ' id="course_teachers" class="chzn-select" style="width:350px" multiple="multiple" '
  61. );
  62. $form->applyFilter('course_teachers', 'html_filter');
  63. // Category code
  64. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  65. $form->addElement(
  66. 'select_ajax',
  67. 'category_code',
  68. get_lang('CourseFaculty'),
  69. null,
  70. array(
  71. 'url' => $url
  72. // 'formatResult' => 'function(item) { return item.name + "'" +item.code; }'
  73. )
  74. );
  75. // Course department
  76. $form->addText('department_name', get_lang('CourseDepartment'), false, array ('size' => '60'));
  77. $form->applyFilter('department_name', 'html_filter');
  78. $form->applyFilter('department_name', 'trim');
  79. // Department URL
  80. $form->addText('department_url', get_lang('CourseDepartmentURL'), false, array ('size' => '60'));
  81. $form->applyFilter('department_url', 'html_filter');
  82. $form->addElement('select_language', 'course_language', get_lang('CourseLanguage'));
  83. $form->applyFilter('select_language', 'html_filter');
  84. $form->addElement('checkbox', 'exemplary_content', '', get_lang('FillWithExemplaryContent'));
  85. $group = array();
  86. $group[]= $form->createElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  87. $group[]= $form->createElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  88. $group[]= $form->createElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  89. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  90. $group[]= $form->createElement('radio', 'visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  91. $form->addGroup($group,'', get_lang('CourseAccess'), '<br />');
  92. $group = array();
  93. $group[]= $form->createElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  94. $group[]= $form->createElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  95. $form->addGroup($group,'', get_lang('Subscription'), '<br />');
  96. $group = array();
  97. $group[]= $form->createElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  98. $group[]= $form->createElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  99. $form->addGroup($group,'', get_lang('Unsubscription'), '<br />');
  100. $form->addElement('text','disk_quota',array(get_lang('CourseQuota'), null, get_lang('MB')));
  101. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  102. $obj = new GradeModel();
  103. $obj->fill_grade_model_select_in_form($form);
  104. //Extra fields
  105. $extra_field = new ExtraField('course');
  106. $extra = $extra_field->addElements($form);
  107. $htmlHeadXtra[] ='
  108. <script>
  109. $(function() {
  110. '.$extra['jquery_ready_content'].'
  111. });
  112. </script>';
  113. $form->add_progress_bar();
  114. $form->addButtonCreate(get_lang('CreateCourse'));
  115. // Set some default values.
  116. $values['course_language'] = api_get_setting('platformLanguage');
  117. $values['disk_quota'] = round(api_get_setting('default_document_quotum')/1024/1024, 1);
  118. $default_course_visibility = api_get_setting('courses_default_creation_visibility');
  119. if (isset($default_course_visibility)) {
  120. $values['visibility'] = api_get_setting('courses_default_creation_visibility');
  121. } else {
  122. $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
  123. }
  124. $values['subscribe'] = 1;
  125. $values['unsubscribe'] = 0;
  126. $values['course_teachers'] = array(api_get_user_id());
  127. $form->setDefaults($values);
  128. // Validate the form
  129. if ($form->validate()) {
  130. $course = $form->exportValues();
  131. $course_teachers = isset($course['course_teachers']) ? $course['course_teachers'] : null;
  132. $course['disk_quota'] = $course['disk_quota']*1024*1024;
  133. $course['exemplary_content'] = empty($course['exemplary_content']) ? false : true;
  134. $course['teachers'] = $course_teachers;
  135. $course['wanted_code'] = $course['visual_code'];
  136. $course['gradebook_model_id'] = isset($course['gradebook_model_id']) ? $course['gradebook_model_id'] : null;
  137. // Fixing category code
  138. $course['course_category'] = $course['category_code'];
  139. $course_info = CourseManager::create_course($course);
  140. header('Location: course_list.php'.($course_info===false?'?action=show_msg&warn='.api_get_last_failure():''));
  141. exit;
  142. }
  143. // Display the form.
  144. $content = $form->return_form();
  145. $tpl = new Template($tool_name);
  146. $tpl->assign('content', $content);
  147. $tpl->display_one_col_template();