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