add_course.php 11 KB

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