course_add.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /* INITIALIZATION SECTION */
  7. // Language files that need to be included.
  8. $language_file = array('admin', 'create_course');
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. api_protect_admin_script();
  13. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  14. require_once api_get_path(CONFIGURATION_PATH).'add_course.conf.php';
  15. require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
  16. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  17. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  18. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  19. $tool_name = get_lang('AddCourse');
  20. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  21. $interbreadcrumb[] = array('url' => 'course_list.php', 'name' => get_lang('CourseList'));
  22. /* MAIN CODE */
  23. global $_configuration;
  24. // Get all possible teachers.
  25. $order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname' : ' ORDER BY lastname, firstname';
  26. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  27. $sql = "SELECT user_id,lastname,firstname FROM $table_user WHERE status=1".$order_clause;
  28. // Filtering teachers when creating a course.
  29. if ($_configuration['multiple_access_urls']) {
  30. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  31. $sql = "SELECT u.user_id,lastname,firstname FROM $table_user as u
  32. INNER JOIN $access_url_rel_user_table url_rel_user
  33. 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;
  34. }
  35. $res = Database::query($sql);
  36. $teachers = array();
  37. $teachers[0] = '-- '.get_lang('NoManager').' --';
  38. while($obj = Database::fetch_object($res)) {
  39. $teachers[$obj->user_id] = api_get_person_name($obj->firstname, $obj->lastname);
  40. }
  41. $dbnamelength = strlen($_configuration['db_prefix']);
  42. // Ensure the database prefix + database name do not get over 40 characters.
  43. $maxlength = 40 - $dbnamelength;
  44. // Build the form.
  45. $form = new FormValidator('update_course');
  46. $form->addElement('header', '', $tool_name);
  47. // Title
  48. $form->add_textfield('title', get_lang('Title'), true, array ('size' => '60'));
  49. $form->applyFilter('title', 'html_filter');
  50. $form->applyFilter('title', 'trim');
  51. // Code
  52. $form->add_textfield('visual_code', get_lang('CourseCode'), false, array('size' => '20', 'maxlength' => 20));
  53. $form->applyFilter('visual_code', 'api_strtoupper');
  54. $form->applyFilter('visual_code', 'html_filter');
  55. $form->addRule('visual_code', get_lang('Max'), 'maxlength', $maxlength);
  56. $form->addElement('select', 'tutor_id', get_lang('CourseTitular'), $teachers, array('style' => 'width:350px'));
  57. $form->applyFilter('tutor_id', 'html_filter');
  58. $form->addElement('select', 'course_teachers', get_lang('CourseTeachers'), $teachers, 'style="width:350px" multiple=multiple size=5');
  59. $form->applyFilter('course_teachers', 'html_filter');
  60. $categories_select = $form->addElement('select', 'category_code', get_lang('CourseFaculty'), $categories, array('style' => 'width:350px'));
  61. $form->applyFilter('category_code', 'html_filter');
  62. //This function fills the category_code select ...
  63. CourseManager::select_and_sort_categories($categories_select);
  64. // Course department
  65. $form->add_textfield('department_name', get_lang('CourseDepartment'), false, array ('size' => '60'));
  66. $form->applyFilter('department_name', 'html_filter');
  67. $form->applyFilter('department_name', 'trim');
  68. // Department URL
  69. $form->add_textfield('department_url', get_lang('CourseDepartmentURL'), false, array ('size' => '60'));
  70. $form->applyFilter('department_url', 'html_filter');
  71. $form->addElement('select_language', 'course_language', get_lang('CourseLanguage'));
  72. $form->applyFilter('select_language', 'html_filter');
  73. $form->addElement('checkbox', 'exemplary_content', get_lang('FillWithExemplaryContent'));
  74. $form->addElement('radio', 'visibility', get_lang('CourseAccess'), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  75. $form->addElement('radio', 'visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  76. $form->addElement('radio', 'visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  77. $form->addElement('radio', 'visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  78. $form->addElement('radio', 'subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  79. $form->addElement('radio', 'subscribe', null, get_lang('Denied'), 0);
  80. $form->addElement('radio', 'unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  81. $form->addElement('radio', 'unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  82. $form->add_textfield('disk_quota', get_lang('CourseQuota'));
  83. $form->addRule('disk_quota', get_lang('ThisFieldShouldBeNumeric'), 'numeric');
  84. $form->add_progress_bar();
  85. $form->addElement('style_submit_button', 'submit', get_lang('CreateCourse'), 'class="add"');
  86. // Set some default values.
  87. $values['course_language'] = api_get_setting('platformLanguage');
  88. $values['disk_quota'] = api_get_setting('default_document_quotum');
  89. $values['visibility'] = COURSE_VISIBILITY_OPEN_PLATFORM;
  90. $values['subscribe'] = 1;
  91. $values['unsubscribe'] = 0;
  92. reset($teachers);
  93. $values['course_teachers'] = key($teachers);
  94. $form->setDefaults($values);
  95. // Validate the form.
  96. if ($form->validate()) {
  97. $course = $form->exportValues();
  98. $code = $course['visual_code'];
  99. $tutor_name = $teachers[$course['tutor_id']];
  100. $teacher_id = $course['tutor_id'];
  101. $course_teachers = $course['course_teachers'];
  102. $test = false;
  103. // The course tutor has been selected in the teachers list so we must remove him to avoid double records in the database.
  104. foreach ($course_teachers as $key => $value){
  105. if ($value == $teacher_id) {
  106. unset($course_teachers[$key]);
  107. break;
  108. }
  109. }
  110. $title = $course['title'];
  111. $category = $course['category_code'];
  112. $department_name = $course['department_name'];
  113. $department_url = $course['department_url'];
  114. $course_language = $course['course_language'];
  115. $exemplary_content = empty($course['exemplary_content']) ? false : true;
  116. $disk_quota = $course['disk_quota'];
  117. if (stripos($department_url, 'http://') === false && stripos($department_url, 'https://') === false) {
  118. $department_url = 'http://'.$department_url;
  119. }
  120. if (trim($code) == '') {
  121. $code = generate_course_code(api_substr($title, 0, $maxlength));
  122. }
  123. $keys = define_course_keys($code, '', $_configuration['db_prefix']);
  124. if (count($keys)) {
  125. $current_course_code = $keys['currentCourseCode'];
  126. $current_course_id = $keys['currentCourseId'];
  127. $current_course_db_name = $keys['currentCourseDbName'];
  128. $current_course_repository = $keys['currentCourseRepository'];
  129. $expiration_date = time() + $firstExpirationDelay;
  130. prepare_course_repository($current_course_repository, $current_course_id);
  131. update_Db_course($current_course_db_name);
  132. $pictures_array = fill_course_repository($current_course_repository, $exemplary_content);
  133. fill_Db_course($current_course_db_name, $current_course_repository, $course_language, $pictures_array, $exemplary_content);
  134. register_course($current_course_id, $current_course_code, $current_course_repository, $current_course_db_name, $tutor_name, $category, $title, $course_language, $teacher_id, $expiration_date, $course_teachers);
  135. $sql = "UPDATE $table_course SET disk_quota = '".$disk_quota."', visibility = '".Database::escape_string($course['visibility'])."', subscribe = '".Database::escape_string($course['subscribe'])."', unsubscribe='".Database::escape_string($course['unsubscribe'])."' WHERE code = '".$current_course_id."'";
  136. Database::query($sql);
  137. header('Location: course_list.php');
  138. exit ();
  139. }
  140. }
  141. // The header.
  142. Display::display_header($tool_name);
  143. // Display the form.
  144. $form->display();
  145. /* FOOTER */
  146. Display :: display_footer();