add_course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. // Flag forcing the "current course" reset.
  15. $cidReset = true;
  16. // Including the global initialization file.
  17. require_once '../inc/global.inc.php';
  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 immediately, after filling this form.
  23. $course_validation_feature = false;
  24. if (api_get_setting('course_validation') == 'true' && !api_is_platform_admin()) {
  25. $course_validation_feature = true;
  26. }
  27. $htmlHeadXtra[] = '<script type="text/javascript">
  28. function setFocus(){
  29. $("#title").focus();
  30. }
  31. $(window).load(function () {
  32. setFocus();
  33. });
  34. </script>';
  35. $interbreadcrumb[] = array(
  36. 'url' => api_get_path(WEB_PATH) . 'user_portal.php',
  37. 'name' => get_lang('MyCourses')
  38. );
  39. // Displaying the header.
  40. $tool_name = $course_validation_feature ? get_lang('CreateCourseRequest') : get_lang('CreateSite');
  41. $tpl = new Template($tool_name);
  42. if (
  43. api_get_setting('allow_users_to_create_courses') == 'false' &&
  44. !api_is_platform_admin()
  45. ) {
  46. api_not_allowed(true);
  47. }
  48. // Check access rights.
  49. if (!api_is_allowed_to_create_course()) {
  50. api_not_allowed(true);
  51. exit;
  52. }
  53. // Build the form.
  54. $form = new FormValidator('add_course');
  55. // Form title
  56. $form->addElement('header', $tool_name);
  57. // Title
  58. $form->addElement(
  59. 'text',
  60. 'title',
  61. array(
  62. get_lang('CourseName'),
  63. get_lang('Ex')
  64. ),
  65. array(
  66. 'id' => 'title'
  67. )
  68. );
  69. $form->applyFilter('title', 'html_filter');
  70. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  71. $form->addButtonAdvancedSettings('advanced_params');
  72. $form->addElement(
  73. 'html',
  74. '<div id="advanced_params_options" style="display:none">'
  75. );
  76. // Category category.
  77. $url = api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_category';
  78. $form->addElement(
  79. 'select_ajax',
  80. 'category_code',
  81. get_lang('CourseFaculty'),
  82. null,
  83. array('url' => $url)
  84. );
  85. // Course code
  86. $form->addText(
  87. 'wanted_code',
  88. array(
  89. get_lang('Code'),
  90. get_lang('OnlyLettersAndNumbers')
  91. ),
  92. '',
  93. array(
  94. 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
  95. 'pattern' => '[a-zA-Z0-9]+',
  96. 'title' => get_lang('OnlyLettersAndNumbers')
  97. )
  98. );
  99. $form->applyFilter('wanted_code', 'html_filter');
  100. $form->addRule(
  101. 'wanted_code',
  102. get_lang('Max'),
  103. 'maxlength',
  104. CourseManager::MAX_COURSE_LENGTH_CODE
  105. );
  106. // The teacher
  107. //array(get_lang('Professor'), null), null, array('size' => '60', 'disabled' => 'disabled'));
  108. $titular = & $form->addElement('hidden', 'tutor_name', '');
  109. if ($course_validation_feature) {
  110. // Description of the requested course.
  111. $form->addElement(
  112. 'textarea',
  113. 'description',
  114. get_lang('Description'),
  115. array('rows' => '3')
  116. );
  117. // Objectives of the requested course.
  118. $form->addElement(
  119. 'textarea',
  120. 'objetives',
  121. get_lang('Objectives'),
  122. array('rows' => '3')
  123. );
  124. // Target audience of the requested course.
  125. $form->addElement(
  126. 'textarea',
  127. 'target_audience',
  128. get_lang('TargetAudience'),
  129. array('rows' => '3')
  130. );
  131. }
  132. // Course language.
  133. $form->addElement(
  134. 'select_language',
  135. 'course_language',
  136. get_lang('Ln'),
  137. array(),
  138. array('style' => 'width:150px')
  139. );
  140. $form->applyFilter('select_language', 'html_filter');
  141. // Exemplary content checkbox.
  142. $form->addElement(
  143. 'checkbox',
  144. 'exemplary_content',
  145. null,
  146. get_lang('FillWithExemplaryContent')
  147. );
  148. if ($course_validation_feature) {
  149. // A special URL to terms and conditions that is set
  150. // in the platform settings page.
  151. $terms_and_conditions_url = trim(
  152. api_get_setting('course_validation_terms_and_conditions_url')
  153. );
  154. // If the special setting is empty,
  155. // then we may get the URL from Chamilo's module "Terms and conditions",
  156. // if it is activated.
  157. if (empty($terms_and_conditions_url)) {
  158. if (api_get_setting('allow_terms_conditions') == 'true') {
  159. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  160. }
  161. }
  162. if (!empty($terms_and_conditions_url)) {
  163. // Terms and conditions to be accepted before sending a course request.
  164. $form->addElement(
  165. 'checkbox',
  166. 'legal',
  167. null,
  168. get_lang('IAcceptTermsAndConditions'),
  169. 1
  170. );
  171. $form->addRule(
  172. 'legal', get_lang('YouHaveToAcceptTermsAndConditions'),
  173. 'required'
  174. );
  175. // Link to terms and conditions.
  176. $link_terms_and_conditions = '
  177. <script>
  178. function MM_openBrWindow(theURL, winName, features) { //v2.0
  179. window.open(theURL,winName,features);
  180. }
  181. </script>
  182. ';
  183. $link_terms_and_conditions .= Display::url(
  184. get_lang('ReadTermsAndConditions'),
  185. '#',
  186. ['onclick' => "javascript:MM_openBrWindow('$terms_and_conditions_url', 'Conditions', 'scrollbars=yes, width=800');"]
  187. );
  188. $form->addElement('label', null, $link_terms_and_conditions);
  189. }
  190. }
  191. $obj = new GradeModel();
  192. $obj->fill_grade_model_select_in_form($form);
  193. $form->addElement('html', '</div>');
  194. // Submit button.
  195. $form->addButtonCreate($course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'));
  196. // The progress bar of this form.
  197. $form->add_progress_bar();
  198. // Set default values.
  199. if (isset($_user['language']) && $_user['language'] != '') {
  200. $values['course_language'] = $_user['language'];
  201. } else {
  202. $values['course_language'] = api_get_setting('platformLanguage');
  203. }
  204. $form->setDefaults($values);
  205. $message = null;
  206. $content = null;
  207. // Validate the form.
  208. if ($form->validate()) {
  209. $course_values = $form->exportValues();
  210. $wanted_code = $course_values['wanted_code'];
  211. $category_code = isset($course_values['category_code']) ? $course_values['category_code'] : '';
  212. $title = $course_values['title'];
  213. $course_language = $course_values['course_language'];
  214. $exemplary_content = !empty($course_values['exemplary_content']);
  215. if ($course_validation_feature) {
  216. $description = $course_values['description'];
  217. $objetives = $course_values['objetives'];
  218. $target_audience = $course_values['target_audience'];
  219. }
  220. if ($wanted_code == '') {
  221. $wanted_code = CourseManager::generate_course_code(api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
  222. }
  223. // Check whether the requested course code has already been occupied.
  224. if (!$course_validation_feature) {
  225. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  226. } else {
  227. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  228. }
  229. if ($course_code_ok) {
  230. if (!$course_validation_feature) {
  231. $params = array();
  232. $params['title'] = $title;
  233. $params['exemplary_content'] = $exemplary_content;
  234. $params['wanted_code'] = $wanted_code;
  235. $params['course_category'] = $category_code;
  236. $params['course_language'] = $course_language;
  237. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  238. $course_info = CourseManager::create_course($params);
  239. if (!empty($course_info)) {
  240. /*
  241. $directory = $course_info['directory'];
  242. $title = $course_info['title'];
  243. // Preparing a confirmation message.
  244. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  245. $tpl->assign('course_url', $link);
  246. $tpl->assign('course_title', Display::url($title, $link));
  247. $tpl->assign('course_id', $course_info['code']);
  248. $add_course_tpl = $tpl->get_template('create_course/add_course.tpl');
  249. $message = $tpl->fetch($add_course_tpl);*/
  250. $url = api_get_path(WEB_CODE_PATH);
  251. $url .= 'course_info/start.php?cidReq=';
  252. $url .= $course_info['code'];
  253. $url .= '&first=1';
  254. header('Location: ' . $url);
  255. exit;
  256. } else {
  257. $message = Display::return_message(
  258. get_lang('CourseCreationFailed'),
  259. 'error',
  260. false
  261. );
  262. // Display the form.
  263. $content = $form->returnForm();
  264. }
  265. } else {
  266. // Create a request for a new course.
  267. $request_id = CourseRequestManager::create_course_request(
  268. $wanted_code,
  269. $title,
  270. $description,
  271. $category_code,
  272. $course_language,
  273. $objetives,
  274. $target_audience,
  275. api_get_user_id(),
  276. $exemplary_content
  277. );
  278. if ($request_id) {
  279. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  280. $message = (is_array($course_request_info) ? '<strong>' . $course_request_info['code'] . '</strong> : ' : '') . get_lang('CourseRequestCreated');
  281. $message = Display::return_message(
  282. $message,
  283. 'confirmation',
  284. false
  285. );
  286. $message .= Display::tag(
  287. 'div',
  288. Display::url(
  289. get_lang('Enter'),
  290. api_get_path(WEB_PATH) . 'user_portal.php',
  291. ['class' => 'btn btn-default']
  292. ),
  293. ['style' => 'float: left; margin:0px; padding: 0px;']
  294. );
  295. } else {
  296. $message = Display::return_message(
  297. get_lang('CourseRequestCreationFailed'),
  298. 'error',
  299. false
  300. );
  301. // Display the form.
  302. $content = $form->return_form();
  303. }
  304. }
  305. } else {
  306. $message = Display::return_message(
  307. get_lang('CourseCodeAlreadyExists'),
  308. 'error',
  309. false
  310. );
  311. // Display the form.
  312. $content = $form->return_form();
  313. }
  314. } else {
  315. if (!$course_validation_feature) {
  316. $message = Display::return_message(get_lang('Explanation'));
  317. }
  318. // Display the form.
  319. $content = $form->returnForm();
  320. }
  321. $tpl->assign('message', $message);
  322. $tpl->assign('content', $content);
  323. $template = $tpl->get_template('layout/layout_1_col.tpl');
  324. $tpl->display($template);