add_course.php 10 KB

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