add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. require_once __DIR__.'/../inc/global.inc.php';
  16. // Check access rights.
  17. if (!api_is_allowed_to_create_course()) {
  18. api_not_allowed(true);
  19. exit;
  20. }
  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. $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. $titular = & $form->addElement('hidden', 'tutor_name', '');
  100. if ($course_validation_feature) {
  101. // Description of the requested course.
  102. $form->addElement(
  103. 'textarea',
  104. 'description',
  105. get_lang('Description'),
  106. array('rows' => '3')
  107. );
  108. // Objectives of the requested course.
  109. $form->addElement(
  110. 'textarea',
  111. 'objetives',
  112. get_lang('Objectives'),
  113. array('rows' => '3')
  114. );
  115. // Target audience of the requested course.
  116. $form->addElement(
  117. 'textarea',
  118. 'target_audience',
  119. get_lang('TargetAudience'),
  120. array('rows' => '3')
  121. );
  122. }
  123. // Course language.
  124. $languages = api_get_languages();
  125. if (count($languages['name']) === 1) {
  126. // If there's only one language available, there's no point in asking
  127. $form->addElement('hidden', 'course_language', $languages['folder'][0]);
  128. } else {
  129. $form->addSelectLanguage(
  130. 'course_language',
  131. get_lang('Ln'),
  132. array(),
  133. array('style' => 'width:150px')
  134. );
  135. }
  136. // Exemplary content checkbox.
  137. $form->addElement(
  138. 'checkbox',
  139. 'exemplary_content',
  140. null,
  141. get_lang('FillWithExemplaryContent')
  142. );
  143. if ($course_validation_feature) {
  144. // A special URL to terms and conditions that is set
  145. // in the platform settings page.
  146. $terms_and_conditions_url = trim(
  147. api_get_setting('course_validation_terms_and_conditions_url')
  148. );
  149. // If the special setting is empty,
  150. // then we may get the URL from Chamilo's module "Terms and conditions",
  151. // if it is activated.
  152. if (empty($terms_and_conditions_url)) {
  153. if (api_get_setting('allow_terms_conditions') === 'true') {
  154. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  155. }
  156. }
  157. if (!empty($terms_and_conditions_url)) {
  158. // Terms and conditions to be accepted before sending a course request.
  159. $form->addElement(
  160. 'checkbox',
  161. 'legal',
  162. null,
  163. get_lang('IAcceptTermsAndConditions'),
  164. 1
  165. );
  166. $form->addRule(
  167. 'legal',
  168. get_lang('YouHaveToAcceptTermsAndConditions'),
  169. 'required'
  170. );
  171. // Link to terms and conditions.
  172. $link_terms_and_conditions = '
  173. <script>
  174. function MM_openBrWindow(theURL, winName, features) { //v2.0
  175. window.open(theURL,winName,features);
  176. }
  177. </script>
  178. ';
  179. $link_terms_and_conditions .= Display::url(
  180. get_lang('ReadTermsAndConditions'),
  181. '#',
  182. ['onclick' => "javascript:MM_openBrWindow('$terms_and_conditions_url', 'Conditions', 'scrollbars=yes, width=800');"]
  183. );
  184. $form->addElement('label', null, $link_terms_and_conditions);
  185. }
  186. }
  187. $obj = new GradeModel();
  188. $obj->fill_grade_model_select_in_form($form);
  189. if (api_get_setting('teacher_can_select_course_template') === 'true') {
  190. $form->addElement(
  191. 'select_ajax',
  192. 'course_template',
  193. [
  194. get_lang('CourseTemplate'),
  195. get_lang('PickACourseAsATemplateForThisNewCourse'),
  196. ],
  197. null,
  198. ['url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course']
  199. );
  200. }
  201. $form->addElement('html', '</div>');
  202. // Submit button.
  203. $form->addButtonCreate($course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'));
  204. // The progress bar of this form.
  205. $form->addProgress();
  206. // Set default values.
  207. if (isset($_user['language']) && $_user['language'] != '') {
  208. $values['course_language'] = $_user['language'];
  209. } else {
  210. $values['course_language'] = api_get_setting('platformLanguage');
  211. }
  212. $form->setDefaults($values);
  213. $message = null;
  214. $content = null;
  215. // Validate the form.
  216. if ($form->validate()) {
  217. $course_values = $form->exportValues();
  218. $wanted_code = $course_values['wanted_code'];
  219. $category_code = isset($course_values['category_code']) ? $course_values['category_code'] : '';
  220. $title = $course_values['title'];
  221. $course_language = $course_values['course_language'];
  222. $exemplary_content = !empty($course_values['exemplary_content']);
  223. if ($course_validation_feature) {
  224. $description = $course_values['description'];
  225. $objetives = $course_values['objetives'];
  226. $target_audience = $course_values['target_audience'];
  227. }
  228. if ($wanted_code == '') {
  229. $wanted_code = CourseManager::generate_course_code(api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
  230. }
  231. // Check whether the requested course code has already been occupied.
  232. if (!$course_validation_feature) {
  233. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  234. } else {
  235. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  236. }
  237. if ($course_code_ok) {
  238. if (!$course_validation_feature) {
  239. $params = array();
  240. $params['title'] = $title;
  241. $params['exemplary_content'] = $exemplary_content;
  242. $params['wanted_code'] = $wanted_code;
  243. $params['course_category'] = $category_code;
  244. $params['course_language'] = $course_language;
  245. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  246. $params['course_template'] = $course_values['course_template'];
  247. include_once api_get_path(SYS_CODE_PATH).'lang/english/trad4all.inc.php';
  248. $file_to_include = api_get_path(SYS_CODE_PATH).'lang/'.$course_language.'/trad4all.inc.php';
  249. if (file_exists($file_to_include)) {
  250. include $file_to_include;
  251. }
  252. $course_info = CourseManager::create_course($params);
  253. if (!empty($course_info)) {
  254. /*
  255. $directory = $course_info['directory'];
  256. $title = $course_info['title'];
  257. // Preparing a confirmation message.
  258. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  259. $tpl->assign('course_url', $link);
  260. $tpl->assign('course_title', Display::url($title, $link));
  261. $tpl->assign('course_id', $course_info['code']);
  262. $add_course_tpl = $tpl->get_template('create_course/add_course.tpl');
  263. $message = $tpl->fetch($add_course_tpl);*/
  264. $splash = api_get_setting('course_creation_splash_screen');
  265. if ($splash === 'true') {
  266. $url = api_get_path(WEB_CODE_PATH);
  267. $url .= 'course_info/start.php?'.api_get_cidreq_params($course_info['code']);
  268. $url .= '&first=1';
  269. header('Location: '.$url);
  270. exit;
  271. } else {
  272. $url = api_get_path(WEB_COURSE_PATH).$course_info['directory'].'/';
  273. header('Location: '.$url);
  274. exit;
  275. }
  276. } else {
  277. $message = Display::return_message(
  278. get_lang('CourseCreationFailed'),
  279. 'error',
  280. false
  281. );
  282. // Display the form.
  283. $content = $form->returnForm();
  284. }
  285. } else {
  286. // Create a request for a new course.
  287. $request_id = CourseRequestManager::create_course_request(
  288. $wanted_code,
  289. $title,
  290. $description,
  291. $category_code,
  292. $course_language,
  293. $objetives,
  294. $target_audience,
  295. api_get_user_id(),
  296. $exemplary_content
  297. );
  298. if ($request_id) {
  299. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  300. $message = (is_array($course_request_info) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang('CourseRequestCreated');
  301. $message = Display::return_message(
  302. $message,
  303. 'confirmation',
  304. false
  305. );
  306. $message .= Display::tag(
  307. 'div',
  308. Display::url(
  309. get_lang('Enter'),
  310. api_get_path(WEB_PATH).'user_portal.php',
  311. ['class' => 'btn btn-default']
  312. ),
  313. ['style' => 'float: left; margin:0px; padding: 0px;']
  314. );
  315. } else {
  316. $message = Display::return_message(
  317. get_lang('CourseRequestCreationFailed'),
  318. 'error',
  319. false
  320. );
  321. // Display the form.
  322. $content = $form->returnForm();
  323. }
  324. }
  325. } else {
  326. $message = Display::return_message(
  327. get_lang('CourseCodeAlreadyExists'),
  328. 'error',
  329. false
  330. );
  331. // Display the form.
  332. $content = $form->returnForm();
  333. }
  334. } else {
  335. if (!$course_validation_feature) {
  336. $message = Display::return_message(get_lang('Explanation'));
  337. }
  338. // Display the form.
  339. $content = $form->returnForm();
  340. }
  341. $tpl->assign('message', $message);
  342. $tpl->assign('content', $content);
  343. $template = $tpl->get_template('layout/layout_1_col.tpl');
  344. $tpl->display($template);