add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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);
  160. $terms_and_conditions_url .= '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', get_lang('YouHaveToAcceptTermsAndConditions'),
  174. 'required'
  175. );
  176. // Link to terms and conditions.
  177. $link_terms_and_conditions = '
  178. <script>
  179. function MM_openBrWindow(theURL, winName, features) { //v2.0
  180. window.open(theURL,winName,features);
  181. }
  182. </script>
  183. ';
  184. $link_terms_and_conditions .= Display::url(
  185. get_lang('ReadTermsAndConditions'),
  186. '#',
  187. ['onclick' => "javascript:MM_openBrWindow('$terms_and_conditions_url', 'Conditions', 'scrollbars=yes, width=800');"]
  188. );
  189. $form->addElement('label', null, $link_terms_and_conditions);
  190. }
  191. }
  192. $obj = new GradeModel();
  193. $obj->fill_grade_model_select_in_form($form);
  194. if (api_get_setting('teacher_can_select_course_template') === 'true') {
  195. $form->addElement(
  196. 'select_ajax',
  197. 'course_template',
  198. [
  199. get_lang('CourseTemplate'),
  200. get_lang('PickACourseAsATemplateForThisNewCourse'),
  201. ],
  202. null,
  203. ['url' => api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_course']
  204. );
  205. }
  206. $form->addElement('html', '</div>');
  207. // Submit button.
  208. $form->addButtonCreate($course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'));
  209. // The progress bar of this form.
  210. $form->add_progress_bar();
  211. // Set default values.
  212. if (isset($_user['language']) && $_user['language'] != '') {
  213. $values['course_language'] = $_user['language'];
  214. } else {
  215. $values['course_language'] = api_get_setting('platformLanguage');
  216. }
  217. $form->setDefaults($values);
  218. $message = null;
  219. $content = null;
  220. // Validate the form.
  221. if ($form->validate()) {
  222. $course_values = $form->exportValues();
  223. $wanted_code = $course_values['wanted_code'];
  224. $category_code = isset($course_values['category_code']) ? $course_values['category_code'] : '';
  225. $title = $course_values['title'];
  226. $course_language = $course_values['course_language'];
  227. $exemplary_content = !empty($course_values['exemplary_content']);
  228. if ($course_validation_feature) {
  229. $description = $course_values['description'];
  230. $objetives = $course_values['objetives'];
  231. $target_audience = $course_values['target_audience'];
  232. }
  233. if ($wanted_code == '') {
  234. $wanted_code = CourseManager::generate_course_code(api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
  235. }
  236. // Check whether the requested course code has already been occupied.
  237. if (!$course_validation_feature) {
  238. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  239. } else {
  240. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  241. }
  242. if ($course_code_ok) {
  243. if (!$course_validation_feature) {
  244. $params = array();
  245. $params['title'] = $title;
  246. $params['exemplary_content'] = $exemplary_content;
  247. $params['wanted_code'] = $wanted_code;
  248. $params['course_category'] = $category_code;
  249. $params['course_language'] = $course_language;
  250. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  251. $course_info = CourseManager::create_course($params);
  252. if (!empty($course_info)) {
  253. /*
  254. $directory = $course_info['directory'];
  255. $title = $course_info['title'];
  256. // Preparing a confirmation message.
  257. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  258. $tpl->assign('course_url', $link);
  259. $tpl->assign('course_title', Display::url($title, $link));
  260. $tpl->assign('course_id', $course_info['code']);
  261. $add_course_tpl = $tpl->get_template('create_course/add_course.tpl');
  262. $message = $tpl->fetch($add_course_tpl);*/
  263. $url = api_get_path(WEB_CODE_PATH);
  264. $url .= 'course_info/start.php?cidReq=';
  265. $url .= $course_info['code'];
  266. $url .= '&first=1';
  267. header('Location: ' . $url);
  268. exit;
  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->return_form();
  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->return_form();
  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);