add_course.php 13 KB

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