add_course.php 12 KB

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