add_course.php 13 KB

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