add_course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. $form->addElement('html', '</div>');
  195. // Submit button.
  196. $form->addButtonCreate($course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'));
  197. // The progress bar of this form.
  198. $form->add_progress_bar();
  199. // Set default values.
  200. if (isset($_user['language']) && $_user['language'] != '') {
  201. $values['course_language'] = $_user['language'];
  202. } else {
  203. $values['course_language'] = api_get_setting('platformLanguage');
  204. }
  205. $form->setDefaults($values);
  206. $message = null;
  207. $content = null;
  208. // Validate the form.
  209. if ($form->validate()) {
  210. $course_values = $form->exportValues();
  211. $wanted_code = $course_values['wanted_code'];
  212. $category_code = $course_values['category_code'];
  213. $title = $course_values['title'];
  214. $course_language = $course_values['course_language'];
  215. $exemplary_content = !empty($course_values['exemplary_content']);
  216. if ($course_validation_feature) {
  217. $description = $course_values['description'];
  218. $objetives = $course_values['objetives'];
  219. $target_audience = $course_values['target_audience'];
  220. }
  221. if ($wanted_code == '') {
  222. $wanted_code = CourseManager::generate_course_code(api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
  223. }
  224. // Check whether the requested course code has already been occupied.
  225. if (!$course_validation_feature) {
  226. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  227. } else {
  228. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  229. }
  230. if ($course_code_ok) {
  231. if (!$course_validation_feature) {
  232. $params = array();
  233. $params['title'] = $title;
  234. $params['exemplary_content'] = $exemplary_content;
  235. $params['wanted_code'] = $wanted_code;
  236. $params['course_category'] = $category_code;
  237. $params['course_language'] = $course_language;
  238. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  239. $course_info = CourseManager::create_course($params);
  240. if (!empty($course_info)) {
  241. /*
  242. $directory = $course_info['directory'];
  243. $title = $course_info['title'];
  244. // Preparing a confirmation message.
  245. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  246. $tpl->assign('course_url', $link);
  247. $tpl->assign('course_title', Display::url($title, $link));
  248. $tpl->assign('course_id', $course_info['code']);
  249. $add_course_tpl = $tpl->get_template('create_course/add_course.tpl');
  250. $message = $tpl->fetch($add_course_tpl);*/
  251. $url = api_get_path(WEB_CODE_PATH);
  252. $url .= 'course_info/start.php?cidReq=';
  253. $url .= $course_info['code'];
  254. $url .= '&first=1';
  255. header('Location: ' . $url);
  256. exit;
  257. } else {
  258. $message = Display::return_message(
  259. get_lang('CourseCreationFailed'),
  260. 'error',
  261. false
  262. );
  263. // Display the form.
  264. $content = $form->returnForm();
  265. }
  266. } else {
  267. // Create a request for a new course.
  268. $request_id = CourseRequestManager::create_course_request(
  269. $wanted_code,
  270. $title,
  271. $description,
  272. $category_code,
  273. $course_language,
  274. $objetives,
  275. $target_audience,
  276. api_get_user_id(),
  277. $exemplary_content
  278. );
  279. if ($request_id) {
  280. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  281. $message = (is_array($course_request_info) ? '<strong>' . $course_request_info['code'] . '</strong> : ' : '') . get_lang('CourseRequestCreated');
  282. $message = Display::return_message(
  283. $message,
  284. 'confirmation',
  285. false
  286. );
  287. $message .= Display::tag(
  288. 'div',
  289. Display::url(
  290. get_lang('Enter'),
  291. api_get_path(WEB_PATH) . 'user_portal.php',
  292. ['class' => 'btn btn-default']
  293. ),
  294. ['style' => 'float: left; margin:0px; padding: 0px;']
  295. );
  296. } else {
  297. $message = Display::return_message(
  298. get_lang('CourseRequestCreationFailed'),
  299. 'error',
  300. false
  301. );
  302. // Display the form.
  303. $content = $form->return_form();
  304. }
  305. }
  306. } else {
  307. $message = Display::return_message(
  308. get_lang('CourseCodeAlreadyExists'),
  309. 'error',
  310. false
  311. );
  312. // Display the form.
  313. $content = $form->return_form();
  314. }
  315. } else {
  316. if (!$course_validation_feature) {
  317. $message = Display::return_message(get_lang('Explanation'));
  318. }
  319. // Display the form.
  320. $content = $form->returnForm();
  321. }
  322. $tpl->assign('message', $message);
  323. $tpl->assign('content', $content);
  324. $template = $tpl->get_template('layout/layout_1_col.tpl');
  325. $tpl->display($template);