add_course.php 12 KB

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