add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. // Course category.
  72. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), array());
  73. $form->applyFilter('category_code', 'html_filter');
  74. CourseManager::select_and_sort_categories($categories_select);
  75. $form->addElement('static', null, null, get_lang('TargetFac'));
  76. // Course code.
  77. $form->add_textfield('wanted_code', get_lang('Code'), false, array('size' => '$maxlength', 'maxlength' => $maxlength));
  78. $form->applyFilter('wanted_code', 'html_filter');
  79. $form->addRule('wanted_code', get_lang('Max'), 'maxlength', $maxlength);
  80. // The teacher.
  81. $titular = & $form->add_textfield('tutor_name', get_lang('Professor'), null, array('size' => '60', 'disabled' => 'disabled'));
  82. $form->addElement('static', null, null, get_lang('ExplicationTrainers'));
  83. //$form->applyFilter('tutor_name', 'html_filter');
  84. if ($course_validation_feature) {
  85. // Description of the requested course.
  86. $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'));
  87. $form->addRule('description', get_lang('ThisFieldIsRequired'), 'required', '', '');
  88. // Objectives of the requested course.
  89. $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'));
  90. $form->addRule('objetives', get_lang('ThisFieldIsRequired'), 'required', '', '');
  91. // Target audience of the requested course.
  92. $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'));
  93. $form->addRule('target_audience', get_lang('ThisFieldIsRequired'), 'required', '', '');
  94. }
  95. // Course language.
  96. $form->addElement('select_language', 'course_language', get_lang('Ln'));
  97. $form->applyFilter('select_language', 'html_filter');
  98. if ($course_validation_feature) {
  99. // A special URL to terms and conditions that is set in the platform settings page.
  100. $terms_and_conditions_url = trim(api_get_setting('course_validation_terms_and_conditions_url'));
  101. // If the special setting is empty, then we may get the URL from Chamilo's module "Terms and conditions", if it is activated.
  102. if (empty($terms_and_conditions_url)) {
  103. if (api_get_setting('allow_terms_conditions') == 'true') {
  104. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  105. }
  106. }
  107. if (!empty($terms_and_conditions_url)) {
  108. // Terms and conditions to be accepted before sending a course request.
  109. $form->addElement('checkbox', 'legal', get_lang('IAcceptTermsAndConditions'), '', 1);
  110. $form->addRule('legal', get_lang('YouHaveToAcceptTermsAndConditions'), 'required', '', '');
  111. // Link to terms and conditions.
  112. $link_terms_and_conditions = '<script type="text/JavaScript">
  113. <!--
  114. function MM_openBrWindow(theURL,winName,features) { //v2.0
  115. window.open(theURL,winName,features);
  116. }
  117. //-->
  118. </script>
  119. <div class="row">
  120. <div class="formw">
  121. <a href="#" onclick="javascript: MM_openBrWindow(\''.$terms_and_conditions_url.'\',\'Conditions\',\'scrollbars=yes, width=800\')">';
  122. $link_terms_and_conditions .= get_lang('ReadTermsAndConditions').'</a></div></div>';
  123. $form->addElement('html', $link_terms_and_conditions);
  124. }
  125. }
  126. // Submit button.
  127. $form->addElement('style_submit_button', null, $course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'), 'class="add"');
  128. // The progress bar of this form.
  129. $form->add_progress_bar();
  130. // Set default values.
  131. if (isset($_user['language']) && $_user['language'] != '') {
  132. $values['course_language'] = $_user['language'];
  133. } else {
  134. $values['course_language'] = api_get_setting('platformLanguage');
  135. }
  136. $values['tutor_name'] = api_get_person_name($_user['firstName'], $_user['lastName'], null, null, $values['course_language']);
  137. $form->setDefaults($values);
  138. // Validate the form.
  139. if ($form->validate()) {
  140. $course_values = $form->exportValues();
  141. $wanted_code = Security::remove_XSS($course_values['wanted_code']);
  142. $tutor_name = $course_values['tutor_name'];
  143. $category_code = $course_values['category_code'];
  144. $title = Security::remove_XSS($course_values['title']);
  145. $course_language = $course_values['course_language'];
  146. if ($course_validation_feature) {
  147. $description = Security::remove_XSS($course_values['description']);
  148. $objetives = Security::remove_XSS($course_values['objetives']);
  149. $target_audience = Security::remove_XSS($course_values['target_audience']);
  150. $status = '0';
  151. // TODO: Why escaping quotes is needed here?
  152. $description = str_replace('"', '', $description);
  153. $objetives = str_replace('"', '', $objetives);
  154. $target_audience = str_replace('"', '', $target_audience);
  155. }
  156. $wanted_code = Database::escape_string($wanted_code);
  157. $title = Database::escape_string($title);
  158. if ($course_validation_feature) {
  159. $description = Database::escape_string($description);
  160. $objetives = Database::escape_string($objetives);
  161. $target_audience = Database::escape_string($target_audience);
  162. }
  163. if (trim($wanted_code) == '') {
  164. $wanted_code = generate_course_code(api_substr($title, 0, $maxlength));
  165. $wanted_code = Database::escape_string($wanted_code);
  166. }
  167. // Check whether the requested course code has already been occupied.
  168. if (!$course_validation_feature) {
  169. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  170. } else {
  171. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  172. }
  173. if ($course_code_ok) {
  174. if (!$course_validation_feature) {
  175. // Create the course immediately.
  176. $keys = define_course_keys($wanted_code, '', $_configuration['db_prefix']);
  177. if (count($keys)) {
  178. $visual_code = $keys['currentCourseCode'];
  179. $code = $keys['currentCourseId'];
  180. $db_name = $keys['currentCourseDbName'];
  181. $directory = $keys['currentCourseRepository'];
  182. $expiration_date = time() + $firstExpirationDelay;
  183. prepare_course_repository($directory, $code);
  184. update_Db_course($db_name);
  185. $pictures_array = fill_course_repository($directory);
  186. fill_Db_course($db_name, $directory, $course_language, $pictures_array);
  187. register_course($code, $visual_code, $directory, $db_name, $tutor_name, $category_code, $title, $course_language, api_get_user_id(), $expiration_date);
  188. // Preparing a confirmation message.
  189. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  190. $message = get_lang('JustCreated');
  191. $message .= ' <a href="'.$link.'">'.$title.'</a>';
  192. Display :: display_confirmation_message($message, false);
  193. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  194. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  195. '</div>';
  196. } else {
  197. Display :: display_error_message(get_lang('CourseCreationFailed'), false);
  198. // Display the form.
  199. $form->display();
  200. }
  201. } else {
  202. // Create a request for a new course.
  203. $request_id = CourseRequestManager::create_course_request($wanted_code, $title, $description, $category_code, $course_language, $objetives, $target_audience);
  204. if ($request_id) {
  205. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  206. $visual_code = is_array($course_request_info) ? $course_request_info['visual_code'] : '';
  207. $message = get_lang('CourseRequestCreated');
  208. $message .= ' <strong>'.$visual_code.'</strong>';
  209. Display :: display_confirmation_message($message, false);
  210. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  211. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  212. '</div>';
  213. } else {
  214. Display :: display_error_message(get_lang('CourseRequestCreationFailed'), false);
  215. // Display the form.
  216. $form->display();
  217. }
  218. }
  219. } else {
  220. Display :: display_error_message(get_lang('CourseCodeAlreadyExists'), false);
  221. // Display the form.
  222. $form->display();
  223. //echo '<p>'.get_lang('CourseCodeAlreadyExistExplained').'</p>';
  224. }
  225. } else {
  226. // Display the form.
  227. $form->display();
  228. if (!$course_validation_feature) {
  229. Display::display_normal_message(get_lang('Explanation'));
  230. }
  231. }
  232. // Footer
  233. Display :: display_footer();