add_course.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 type="text/javascript">
  38. function setFocus(){
  39. $("#title").focus();
  40. }
  41. $(window).load(function () {
  42. setFocus();
  43. });
  44. function advanced_parameters() {
  45. if(document.getElementById(\'options\').style.display == \'none\') {
  46. document.getElementById(\'options\').style.display = \'block\';
  47. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  48. } else {
  49. document.getElementById(\'options\').style.display = \'none\';
  50. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'\';
  51. }
  52. }
  53. </script>';
  54. $interbreadcrumb[] = array('url' => api_get_path(WEB_PATH).'user_portal.php', 'name' => get_lang('MyCourses'));
  55. // Displaying the header.
  56. $tool_name = $course_validation_feature ? get_lang('CreateCourseRequest') : get_lang('CreateSite');
  57. if (api_get_setting('allow_users_to_create_courses') == 'false' && !api_is_platform_admin()) {
  58. api_not_allowed(true);
  59. }
  60. Display :: display_header($tool_name);
  61. // Check access rights.
  62. if (!api_is_allowed_to_create_course()) {
  63. Display :: display_error_message(get_lang('NotAllowed'));
  64. Display::display_footer();
  65. exit;
  66. }
  67. global $_configuration;
  68. $dbnamelength = strlen($_configuration['db_prefix']);
  69. // Ensure the database prefix + database name do not get over 40 characters.
  70. $maxlength = 40 - $dbnamelength;
  71. // Build the form.
  72. $form = new FormValidator('add_course');
  73. // Form title
  74. $form->addElement('header', '', $tool_name);
  75. // Title
  76. $form->addElement('text', 'title', get_lang('CourseName'), array('size' => '60', 'id' => 'title'));
  77. $form->applyFilter('title', 'html_filter');
  78. $form->addElement('static', null, null, get_lang('Ex'));
  79. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  80. // Course category.
  81. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), array(), array('style'=>'width:350px'));
  82. $form->applyFilter('category_code', 'html_filter');
  83. CourseManager::select_and_sort_categories($categories_select);
  84. $form->addElement('static', null, null, get_lang('TargetFac'));
  85. $form -> addElement('html','<div class="row">
  86. <div class="label">&nbsp;</div>
  87. <div class="formw">
  88. <a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><div style="vertical-align:top;" ><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</div></span></a>
  89. </div>
  90. </div>');
  91. $form -> addElement('html','<div id="options" style="display:none">');
  92. // Course code.
  93. $form->add_textfield('wanted_code', get_lang('Code'), '', array('size' => $maxlength, 'maxlength' => $maxlength));
  94. $form->addElement('static', null, null, get_lang('OnlyLettersAndNumbers'));
  95. $form->applyFilter('wanted_code', 'html_filter');
  96. $form->addRule('wanted_code', get_lang('Max'), 'maxlength', $maxlength);
  97. if ($course_validation_feature) {
  98. $form->addRule('wanted_code', get_lang('ThisFieldIsRequired'), 'required');
  99. }
  100. // The teacher.
  101. $titular = & $form->add_textfield('tutor_name', get_lang('Professor'), null, array('size' => '60', 'disabled' => 'disabled'));
  102. $form->addElement('static', null, null, get_lang('ExplicationTrainers'));
  103. //$form->applyFilter('tutor_name', 'html_filter');
  104. if ($course_validation_feature) {
  105. // Description of the requested course.
  106. $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'));
  107. $form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
  108. // Objectives of the requested course.
  109. $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'));
  110. $form->addRule('objetives', get_lang('ThisFieldIsRequired'), 'required');
  111. // Target audience of the requested course.
  112. $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'));
  113. $form->addRule('target_audience', get_lang('ThisFieldIsRequired'), 'required');
  114. }
  115. // Course language.
  116. $form->addElement('select_language', 'course_language', get_lang('Ln'));
  117. $form->applyFilter('select_language', 'html_filter');
  118. // Exemplary content checkbox.
  119. $form->addElement('checkbox', 'exemplary_content', get_lang('FillWithExemplaryContent'));
  120. if ($course_validation_feature) {
  121. // A special URL to terms and conditions that is set in the platform settings page.
  122. $terms_and_conditions_url = trim(api_get_setting('course_validation_terms_and_conditions_url'));
  123. // If the special setting is empty, then we may get the URL from Chamilo's module "Terms and conditions", if it is activated.
  124. if (empty($terms_and_conditions_url)) {
  125. if (api_get_setting('allow_terms_conditions') == 'true') {
  126. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  127. }
  128. }
  129. if (!empty($terms_and_conditions_url)) {
  130. // Terms and conditions to be accepted before sending a course request.
  131. $form->addElement('checkbox', 'legal', get_lang('IAcceptTermsAndConditions'), '', 1);
  132. $form->addRule('legal', get_lang('YouHaveToAcceptTermsAndConditions'), 'required');
  133. // Link to terms and conditions.
  134. $link_terms_and_conditions = '<script type="text/JavaScript">
  135. <!--
  136. function MM_openBrWindow(theURL,winName,features) { //v2.0
  137. window.open(theURL,winName,features);
  138. }
  139. //-->
  140. </script>
  141. <div class="row">
  142. <div class="formw">
  143. <a href="#" onclick="javascript: MM_openBrWindow(\''.$terms_and_conditions_url.'\',\'Conditions\',\'scrollbars=yes, width=800\')">';
  144. $link_terms_and_conditions .= get_lang('ReadTermsAndConditions').'</a></div></div>';
  145. $form->addElement('html', $link_terms_and_conditions);
  146. }
  147. }
  148. $form -> addElement('html','</div>');
  149. // Submit button.
  150. $form->addElement('style_submit_button', null, $course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'), 'class="add"');
  151. // The progress bar of this form.
  152. $form->add_progress_bar();
  153. // Set default values.
  154. if (isset($_user['language']) && $_user['language'] != '') {
  155. $values['course_language'] = $_user['language'];
  156. } else {
  157. $values['course_language'] = api_get_setting('platformLanguage');
  158. }
  159. $values['tutor_name'] = api_get_person_name($_user['firstName'], $_user['lastName'], null, null, $values['course_language']);
  160. $form->setDefaults($values);
  161. // Validate the form.
  162. if ($form->validate()) {
  163. $course_values = $form->exportValues();
  164. $wanted_code = trim(Security::remove_XSS(stripslashes($course_values['wanted_code'])));
  165. $tutor_name = stripslashes($course_values['tutor_name']);
  166. $category_code = $course_values['category_code'];
  167. $title = Security::remove_XSS(stripslashes($course_values['title']));
  168. $course_language = $course_values['course_language'];
  169. $exemplary_content = !empty($course_values['exemplary_content']);
  170. if ($course_validation_feature) {
  171. $description = Security::remove_XSS(stripslashes($course_values['description']));
  172. $objetives = Security::remove_XSS(stripslashes($course_values['objetives']));
  173. $target_audience = Security::remove_XSS(stripslashes($course_values['target_audience']));
  174. $status = '0';
  175. }
  176. if ($wanted_code == '') {
  177. $wanted_code = generate_course_code(api_substr($title, 0, $maxlength));
  178. }
  179. // Check whether the requested course code has already been occupied.
  180. if (!$course_validation_feature) {
  181. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  182. } else {
  183. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  184. }
  185. if ($course_code_ok) {
  186. if (!$course_validation_feature) {
  187. // Create the course immediately.
  188. $keys = define_course_keys($wanted_code, '', $_configuration['db_prefix']);
  189. if (count($keys)) {
  190. $visual_code = $keys['currentCourseCode'];
  191. $code = $keys['currentCourseId'];
  192. $db_name = $keys['currentCourseDbName'];
  193. $directory = $keys['currentCourseRepository'];
  194. $expiration_date = time() + $firstExpirationDelay;
  195. prepare_course_repository($directory, $code);
  196. update_Db_course($db_name);
  197. $pictures_array = fill_course_repository($directory, $exemplary_content);
  198. fill_Db_course($db_name, $directory, $course_language, $pictures_array, $exemplary_content);
  199. register_course($code, $visual_code, $directory, $db_name, $tutor_name, $category_code, $title, $course_language, api_get_user_id(), $expiration_date);
  200. // Preparing a confirmation message.
  201. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  202. $message = get_lang('JustCreated');
  203. $message .= ' <a href="'.$link.'">'.$title.'</a>';
  204. Display :: display_confirmation_message($message, false);
  205. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  206. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  207. '</div>';
  208. } else {
  209. Display :: display_error_message(get_lang('CourseCreationFailed'), false);
  210. // Display the form.
  211. $form->display();
  212. }
  213. } else {
  214. // Create a request for a new course.
  215. $request_id = CourseRequestManager::create_course_request($wanted_code, $title, $description, $category_code, $course_language, $objetives, $target_audience, api_get_user_id(), $exemplary_content);
  216. if ($request_id) {
  217. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  218. $message = (is_array($course_request_info) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang('CourseRequestCreated');
  219. Display :: display_confirmation_message($message, false);
  220. echo '<div style="float: right; margin:0px; padding: 0px;">' .
  221. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  222. '</div>';
  223. } else {
  224. Display :: display_error_message(get_lang('CourseRequestCreationFailed'), false);
  225. // Display the form.
  226. $form->display();
  227. }
  228. }
  229. } else {
  230. Display :: display_error_message(get_lang('CourseCodeAlreadyExists'), false);
  231. // Display the form.
  232. $form->display();
  233. //echo '<p>'.get_lang('CourseCodeAlreadyExistExplained').'</p>';
  234. }
  235. } else {
  236. if (!$course_validation_feature) {
  237. Display::display_normal_message(get_lang('Explanation'));
  238. }
  239. // Display the form.
  240. $form->display();
  241. }
  242. // Footer
  243. Display :: display_footer();