add_course.php 12 KB

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