add_course.php 13 KB

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