add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script allows professors and administrative staff to create course sites.
  5. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  6. * @author Roan Embrechts, refactoring
  7. * @package chamilo.create_course
  8. * "Course validation" feature:
  9. * @author Jose Manuel Abuin Mosquera <chema@cesga.es>, Centro de Supercomputacion de Galicia
  10. * "Course validation" feature, technical adaptation for Chamilo 1.8.8:
  11. * @author Ivan Tcholakov <ivantcholakov@gmail.com>
  12. */
  13. // Name of the language file that needs to be included.
  14. $language_file = 'create_course';
  15. // Flag forcing the "current course" reset.
  16. $cidReset = true;
  17. // Including the global initialization file.
  18. require_once '../inc/global.inc.php';
  19. // Section for the tabs.
  20. $this_section = SECTION_COURSES;
  21. // Include configuration file.
  22. require_once api_get_path(CONFIGURATION_PATH).'add_course.conf.php';
  23. // "Course validation" feature. This value affects the way of a new course creation:
  24. // true - the new course is requested only and it is created after approval;
  25. // false - the new course is created immedialely, after filling this form.
  26. $course_validation_feature = api_get_setting('course_validation') == 'true';
  27. // Require additional libraries.
  28. require_once api_get_path(LIBRARY_PATH).'add_course.lib.inc.php';
  29. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  30. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  31. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  32. require_once api_get_path(CONFIGURATION_PATH).'course_info.conf.php';
  33. if ($course_validation_feature) {
  34. require_once api_get_path(LIBRARY_PATH).'course_request.lib.php';
  35. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  36. }
  37. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  38. $htmlHeadXtra[] = '<script type="text/javascript">
  39. function setFocus(){
  40. $("#title").focus();
  41. }
  42. $(window).load(function () {
  43. setFocus();
  44. });
  45. </script>';
  46. $interbreadcrumb[] = array('url' => api_get_path(WEB_PATH).'user_portal.php', 'name' => get_lang('MyCourses'));
  47. // Displaying the header.
  48. $tool_name = $course_validation_feature ? get_lang('CreateCourseRequest') : get_lang('CreateSite');
  49. if (api_get_setting('allow_users_to_create_courses') == 'false' && !api_is_platform_admin()) {
  50. api_not_allowed(true);
  51. }
  52. Display :: display_header($tool_name);
  53. // Check access rights.
  54. if (!api_is_allowed_to_create_course()) {
  55. Display :: display_error_message(get_lang('NotAllowed'));
  56. Display::display_footer();
  57. exit;
  58. }
  59. global $_configuration;
  60. $dbnamelength = strlen($_configuration['db_prefix']);
  61. // Ensure the database prefix + database name do not get over 40 characters.
  62. $maxlength = 40 - $dbnamelength;
  63. // Build the form.
  64. $form = new FormValidator('add_course');
  65. // Form title
  66. $form->addElement('header', '', $tool_name);
  67. // Title
  68. $form->addElement('text', 'title', get_lang('CourseName'), array('size' => '60', 'id' => 'title'));
  69. $form->applyFilter('title', 'html_filter');
  70. $form->addElement('static', null, null, get_lang('Ex'));
  71. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  72. // Course category.
  73. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), array());
  74. $form->applyFilter('category_code', 'html_filter');
  75. CourseManager::select_and_sort_categories($categories_select);
  76. $form->addElement('static', null, null, get_lang('TargetFac'));
  77. // Course code.
  78. $form->add_textfield('wanted_code', get_lang('Code'), false, array('size' => '$maxlength', 'maxlength' => $maxlength));
  79. $form->applyFilter('wanted_code', 'html_filter');
  80. $form->addRule('wanted_code', get_lang('Max'), 'maxlength', $maxlength);
  81. if ($course_validation_feature) {
  82. $form->addRule('wanted_code', get_lang('ThisFieldIsRequired'), 'required');
  83. }
  84. // The teacher.
  85. $titular = & $form->add_textfield('tutor_name', get_lang('Professor'), null, array('size' => '60', 'disabled' => 'disabled'));
  86. $form->addElement('static', null, null, get_lang('ExplicationTrainers'));
  87. //$form->applyFilter('tutor_name', 'html_filter');
  88. if ($course_validation_feature) {
  89. // Description of the requested course.
  90. $form->addElement('textarea', 'description', get_lang('Description'), array('style' => 'border:#A5ACB2 solid 1px; font-family:arial,verdana,helvetica,sans-serif; font-size:12px', 'rows' => '3', 'cols' => '116'));
  91. $form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
  92. // Objectives of the requested course.
  93. $form->addElement('textarea', 'objetives', get_lang('Objectives'), array('style' => 'border:#A5ACB2 solid 1px; font-family:arial,verdana,helvetica,sans-serif; font-size:12px', 'rows' => '3', 'cols' => '116'));
  94. $form->addRule('objetives', get_lang('ThisFieldIsRequired'), 'required');
  95. // Target audience of the requested course.
  96. $form->addElement('textarea', 'target_audience', get_lang('TargetAudience'), array('style' => 'border:#A5ACB2 solid 1px; font-family:arial,verdana,helvetica,sans-serif; font-size:12px', 'rows' => '3', 'cols' => '116'));
  97. $form->addRule('target_audience', get_lang('ThisFieldIsRequired'), 'required');
  98. }
  99. // Course language.
  100. $form->addElement('select_language', 'course_language', get_lang('Ln'));
  101. $form->applyFilter('select_language', 'html_filter');
  102. // Exemplary content checkbox.
  103. $form->addElement('checkbox', 'exemplary_content', get_lang('FillWithExemplaryContent'));
  104. if ($course_validation_feature) {
  105. // A special URL to terms and conditions that is set in the platform settings page.
  106. $terms_and_conditions_url = trim(api_get_setting('course_validation_terms_and_conditions_url'));
  107. // If the special setting is empty, then we may get the URL from Chamilo's module "Terms and conditions", if it is activated.
  108. if (empty($terms_and_conditions_url)) {
  109. if (api_get_setting('allow_terms_conditions') == 'true') {
  110. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  111. }
  112. }
  113. if (!empty($terms_and_conditions_url)) {
  114. // Terms and conditions to be accepted before sending a course request.
  115. $form->addElement('checkbox', 'legal', get_lang('IAcceptTermsAndConditions'), '', 1);
  116. $form->addRule('legal', get_lang('YouHaveToAcceptTermsAndConditions'), 'required');
  117. // Link to terms and conditions.
  118. $link_terms_and_conditions = '<script type="text/JavaScript">
  119. <!--
  120. function MM_openBrWindow(theURL,winName,features) { //v2.0
  121. window.open(theURL,winName,features);
  122. }
  123. //-->
  124. </script>
  125. <div class="row">
  126. <div class="formw">
  127. <a href="#" onclick="javascript: MM_openBrWindow(\''.$terms_and_conditions_url.'\',\'Conditions\',\'scrollbars=yes, width=800\')">';
  128. $link_terms_and_conditions .= get_lang('ReadTermsAndConditions').'</a></div></div>';
  129. $form->addElement('html', $link_terms_and_conditions);
  130. }
  131. }
  132. // Submit button.
  133. $form->addElement('style_submit_button', null, $course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'), 'class="add"');
  134. // The progress bar of this form.
  135. $form->add_progress_bar();
  136. // Set default values.
  137. if (isset($_user['language']) && $_user['language'] != '') {
  138. $values['course_language'] = $_user['language'];
  139. } else {
  140. $values['course_language'] = api_get_setting('platformLanguage');
  141. }
  142. $values['tutor_name'] = api_get_person_name($_user['firstName'], $_user['lastName'], null, null, $values['course_language']);
  143. $form->setDefaults($values);
  144. // Validate the form.
  145. if ($form->validate()) {
  146. $course_values = $form->exportValues();
  147. $wanted_code = trim(Security::remove_XSS(stripslashes($course_values['wanted_code'])));
  148. $tutor_name = stripslashes($course_values['tutor_name']);
  149. $category_code = $course_values['category_code'];
  150. $title = Security::remove_XSS(stripslashes($course_values['title']));
  151. $course_language = $course_values['course_language'];
  152. $exemplary_content = !empty($course_values['exemplary_content']);
  153. if ($course_validation_feature) {
  154. $description = Security::remove_XSS(stripslashes($course_values['description']));
  155. $objetives = Security::remove_XSS(stripslashes($course_values['objetives']));
  156. $target_audience = Security::remove_XSS(stripslashes($course_values['target_audience']));
  157. $status = '0';
  158. }
  159. if ($wanted_code == '') {
  160. $wanted_code = generate_course_code(api_substr($title, 0, $maxlength));
  161. }
  162. // Check whether the requested course code has already been occupied.
  163. if (!$course_validation_feature) {
  164. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  165. } else {
  166. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  167. }
  168. if ($course_code_ok) {
  169. if (!$course_validation_feature) {
  170. // Create the course immediately.
  171. $keys = define_course_keys($wanted_code, '', $_configuration['db_prefix']);
  172. if (count($keys)) {
  173. $visual_code = $keys['currentCourseCode'];
  174. $code = $keys['currentCourseId'];
  175. $db_name = $keys['currentCourseDbName'];
  176. $directory = $keys['currentCourseRepository'];
  177. $expiration_date = time() + $firstExpirationDelay;
  178. prepare_course_repository($directory, $code);
  179. update_Db_course($db_name);
  180. $pictures_array = fill_course_repository($directory, $exemplary_content);
  181. fill_Db_course($db_name, $directory, $course_language, $pictures_array, $exemplary_content);
  182. register_course($code, $visual_code, $directory, $db_name, $tutor_name, $category_code, $title, $course_language, api_get_user_id(), $expiration_date);
  183. // Preparing a confirmation message.
  184. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  185. $message = get_lang('JustCreated');
  186. $message .= ' <a href="'.$link.'">'.$title.'</a>';
  187. Display :: display_confirmation_message($message, false);
  188. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  189. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  190. '</div>';
  191. } else {
  192. Display :: display_error_message(get_lang('CourseCreationFailed'), false);
  193. // Display the form.
  194. $form->display();
  195. }
  196. } else {
  197. // Create a request for a new course.
  198. $request_id = CourseRequestManager::create_course_request($wanted_code, $title, $description, $category_code, $course_language, $objetives, $target_audience, api_get_user_id(), $exemplary_content);
  199. if ($request_id) {
  200. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  201. $message = (is_array($course_request_info) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang('CourseRequestCreated');
  202. Display :: display_confirmation_message($message, false);
  203. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  204. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  205. '</div>';
  206. } else {
  207. Display :: display_error_message(get_lang('CourseRequestCreationFailed'), false);
  208. // Display the form.
  209. $form->display();
  210. }
  211. }
  212. } else {
  213. Display :: display_error_message(get_lang('CourseCodeAlreadyExists'), false);
  214. // Display the form.
  215. $form->display();
  216. //echo '<p>'.get_lang('CourseCodeAlreadyExistExplained').'</p>';
  217. }
  218. } else {
  219. // Display the form.
  220. $form->display();
  221. if (!$course_validation_feature) {
  222. Display::display_normal_message(get_lang('Explanation'));
  223. }
  224. }
  225. // Footer
  226. Display :: display_footer();