add_course.php 12 KB

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