add_course.php 13 KB

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