add_course.php 13 KB

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