add_course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. 'class' => 'span6',
  67. 'id' => 'title'
  68. )
  69. );
  70. $form->applyFilter('title', 'html_filter');
  71. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  72. $form->addButtonAdvancedSettings('advanced_params');
  73. $form->addElement(
  74. 'html',
  75. '<div id="advanced_params_options" style="display:none">'
  76. );
  77. // Category category.
  78. $url = api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_category';
  79. $form->addElement(
  80. 'select_ajax',
  81. 'category_code',
  82. get_lang('CourseFaculty'),
  83. null,
  84. array('url' => $url)
  85. );
  86. // Course code
  87. $form->addText(
  88. 'wanted_code',
  89. array(
  90. get_lang('Code'),
  91. get_lang('OnlyLettersAndNumbers')
  92. ),
  93. '',
  94. array(
  95. 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE,
  96. 'pattern' => '[a-zA-Z0-9]+',
  97. 'title' => get_lang('OnlyLettersAndNumbers')
  98. )
  99. );
  100. $form->applyFilter('wanted_code', 'html_filter');
  101. $form->addRule(
  102. 'wanted_code',
  103. get_lang('Max'),
  104. 'maxlength',
  105. CourseManager::MAX_COURSE_LENGTH_CODE
  106. );
  107. // The teacher
  108. //array(get_lang('Professor'), null), null, array('size' => '60', 'disabled' => 'disabled'));
  109. $titular = & $form->addElement('hidden', 'tutor_name', '');
  110. if ($course_validation_feature) {
  111. // Description of the requested course.
  112. $form->addElement(
  113. 'textarea',
  114. 'description',
  115. get_lang('Description'),
  116. array('rows' => '3')
  117. );
  118. // Objectives of the requested course.
  119. $form->addElement(
  120. 'textarea',
  121. 'objetives',
  122. get_lang('Objectives'),
  123. array('rows' => '3')
  124. );
  125. // Target audience of the requested course.
  126. $form->addElement(
  127. 'textarea',
  128. 'target_audience',
  129. get_lang('TargetAudience'),
  130. array('rows' => '3')
  131. );
  132. }
  133. // Course language.
  134. $form->addElement(
  135. 'select_language',
  136. 'course_language',
  137. get_lang('Ln'),
  138. array(),
  139. array('style' => 'width:150px')
  140. );
  141. $form->applyFilter('select_language', 'html_filter');
  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);
  161. $terms_and_conditions_url .= 'auth/inscription.php?legal';
  162. }
  163. }
  164. if (!empty($terms_and_conditions_url)) {
  165. // Terms and conditions to be accepted before sending a course request.
  166. $form->addElement(
  167. 'checkbox',
  168. 'legal',
  169. null,
  170. get_lang('IAcceptTermsAndConditions'),
  171. 1
  172. );
  173. $form->addRule(
  174. 'legal', 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. $form->addElement('html', '</div>');
  196. // Submit button.
  197. $form->addButtonCreate($course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'));
  198. // The progress bar of this form.
  199. $form->add_progress_bar();
  200. // Set default values.
  201. if (isset($_user['language']) && $_user['language'] != '') {
  202. $values['course_language'] = $_user['language'];
  203. } else {
  204. $values['course_language'] = api_get_setting('platformLanguage');
  205. }
  206. $form->setDefaults($values);
  207. $message = null;
  208. $content = null;
  209. // Validate the form.
  210. if ($form->validate()) {
  211. $course_values = $form->exportValues();
  212. $wanted_code = $course_values['wanted_code'];
  213. $category_code = $course_values['category_code'];
  214. $title = $course_values['title'];
  215. $course_language = $course_values['course_language'];
  216. $exemplary_content = !empty($course_values['exemplary_content']);
  217. if ($course_validation_feature) {
  218. $description = $course_values['description'];
  219. $objetives = $course_values['objetives'];
  220. $target_audience = $course_values['target_audience'];
  221. }
  222. if ($wanted_code == '') {
  223. $wanted_code = CourseManager::generate_course_code(api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE));
  224. }
  225. // Check whether the requested course code has already been occupied.
  226. if (!$course_validation_feature) {
  227. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  228. } else {
  229. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  230. }
  231. if ($course_code_ok) {
  232. if (!$course_validation_feature) {
  233. $params = array();
  234. $params['title'] = $title;
  235. $params['exemplary_content'] = $exemplary_content;
  236. $params['wanted_code'] = $wanted_code;
  237. $params['course_category'] = $category_code;
  238. $params['course_language'] = $course_language;
  239. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  240. $course_info = CourseManager::create_course($params);
  241. if (!empty($course_info)) {
  242. /*
  243. $directory = $course_info['directory'];
  244. $title = $course_info['title'];
  245. // Preparing a confirmation message.
  246. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  247. $tpl->assign('course_url', $link);
  248. $tpl->assign('course_title', Display::url($title, $link));
  249. $tpl->assign('course_id', $course_info['code']);
  250. $add_course_tpl = $tpl->get_template('create_course/add_course.tpl');
  251. $message = $tpl->fetch($add_course_tpl);*/
  252. $url = api_get_path(WEB_CODE_PATH);
  253. $url .= 'course_info/start.php?cidReq=';
  254. $url .= $course_info['code'];
  255. $url .= '&first=1';
  256. header('Location: ' . $url);
  257. exit;
  258. } else {
  259. $message = Display::return_message(
  260. get_lang('CourseCreationFailed'),
  261. 'error',
  262. false
  263. );
  264. // Display the form.
  265. $content = $form->returnForm();
  266. }
  267. } else {
  268. // Create a request for a new course.
  269. $request_id = CourseRequestManager::create_course_request(
  270. $wanted_code,
  271. $title,
  272. $description,
  273. $category_code,
  274. $course_language,
  275. $objetives,
  276. $target_audience,
  277. api_get_user_id(),
  278. $exemplary_content
  279. );
  280. if ($request_id) {
  281. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  282. $message = (is_array($course_request_info) ? '<strong>' . $course_request_info['code'] . '</strong> : ' : '') . get_lang('CourseRequestCreated');
  283. $message = Display::return_message(
  284. $message,
  285. 'confirmation',
  286. false
  287. );
  288. $message .= Display::tag(
  289. 'div',
  290. Display::url(
  291. get_lang('Enter'),
  292. api_get_path(WEB_PATH) . 'user_portal.php',
  293. ['class' => 'btn btn-default']
  294. ),
  295. ['style' => 'float: left; margin:0px; padding: 0px;']
  296. );
  297. } else {
  298. $message = Display::return_message(
  299. get_lang('CourseRequestCreationFailed'),
  300. 'error',
  301. false
  302. );
  303. // Display the form.
  304. $content = $form->return_form();
  305. }
  306. }
  307. } else {
  308. $message = Display::return_message(
  309. get_lang('CourseCodeAlreadyExists'),
  310. 'error',
  311. false
  312. );
  313. // Display the form.
  314. $content = $form->return_form();
  315. }
  316. } else {
  317. if (!$course_validation_feature) {
  318. $message = Display::return_message(get_lang('Explanation'));
  319. }
  320. // Display the form.
  321. $content = $form->returnForm();
  322. }
  323. $tpl->assign('message', $message);
  324. $tpl->assign('content', $content);
  325. $template = $tpl->get_template('layout/layout_1_col.tpl');
  326. $tpl->display($template);