add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /**
  14. * Code
  15. */
  16. // Name of the language file that needs to be included.
  17. $language_file = array('create_course', 'registration', 'admin', 'exercice', 'course_description', 'course_info');
  18. // Flag forcing the "current course" reset.
  19. $cidReset = true;
  20. // Including the global initialization file.
  21. require_once '../inc/global.inc.php';
  22. // Section for the tabs.
  23. $this_section = SECTION_COURSES;
  24. // "Course validation" feature. This value affects the way of a new course creation:
  25. // true - the new course is requested only and it is created after approval;
  26. // false - the new course is created immedialely, after filling this form.
  27. $course_validation_feature = api_get_setting('course_validation') == 'true';
  28. $htmlHeadXtra[] = '<script>
  29. function setFocus(){
  30. $("#title").focus();
  31. }
  32. $(window).load(function () {
  33. setFocus();
  34. });
  35. function advanced_parameters() {
  36. if(document.getElementById(\'options\').style.display == \'none\') {
  37. document.getElementById(\'options\').style.display = \'block\';
  38. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_hide.gif" alt="" />&nbsp;'.get_lang(
  39. 'AdvancedParameters'
  40. ).'\';
  41. } else {
  42. document.getElementById(\'options\').style.display = \'none\';
  43. document.getElementById(\'img_plus_and_minus\').innerHTML=\'&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang(
  44. 'AdvancedParameters'
  45. ).'\';
  46. }
  47. }
  48. </script>';
  49. $interbreadcrumb[] = array('url' => api_get_path(WEB_PATH).'user_portal.php', 'name' => get_lang('MyCourses'));
  50. // Displaying the header.
  51. $tool_name = $course_validation_feature ? get_lang('CreateCourseRequest') : get_lang('CreateSite');
  52. $tpl = $app['template'];
  53. if (api_get_setting('allow_users_to_create_courses') == 'false' && !api_is_platform_admin()) {
  54. api_not_allowed(true);
  55. }
  56. // Check access rights.
  57. if (!api_is_allowed_to_create_course()) {
  58. api_not_allowed(true);
  59. }
  60. // Build the form.
  61. $form = new FormValidator('add_course');
  62. // Form title
  63. $form->addElement('header', $tool_name);
  64. // Title
  65. $form->addElement(
  66. 'text',
  67. 'title',
  68. array(get_lang('CourseName'), get_lang('Ex')),
  69. array('class' => 'span6', 'id' => 'title')
  70. );
  71. $form->applyFilter('title', 'html_filter');
  72. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  73. $advanced = '<a href="javascript://" onclick=" return advanced_parameters()"><span id="img_plus_and_minus"><div style="vertical-align:top;" ><img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang(
  74. 'AdvancedParameters'
  75. ).'</div></span></a>';
  76. $form->addElement('advanced_settings', $advanced);
  77. $form->addElement('html', '<div id="options" style="display:none">');
  78. // Course category.
  79. $categories_select = $form->addElement(
  80. 'select',
  81. 'category_code',
  82. array(get_lang('Fac'), get_lang('TargetFac')),
  83. array(),
  84. array('id' => 'category_code', 'class' => 'chzn-select', 'style' => 'width:350px')
  85. );
  86. $form->applyFilter('category_code', 'html_filter');
  87. $categories_select->addOption('-', '');
  88. CourseManager::select_and_sort_categories($categories_select);
  89. // Course code
  90. $form->add_textfield(
  91. 'wanted_code',
  92. array(get_lang('Code'), get_lang('OnlyLettersAndNumbers')),
  93. '',
  94. array('class' => 'span3', 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE)
  95. );
  96. $form->applyFilter('wanted_code', 'html_filter');
  97. $form->addRule('wanted_code', get_lang('Max'), 'maxlength', CourseManager::MAX_COURSE_LENGTH_CODE);
  98. /*if ($course_validation_feature) {
  99. $form->addRule('wanted_code', get_lang('ThisFieldIsRequired'), 'required');
  100. }*/
  101. // The teacher
  102. //get_lang('ExplicationTrainers')
  103. $titular = & $form->addElement(
  104. 'hidden',
  105. 'tutor_name',
  106. ''
  107. ); //array(get_lang('Professor'), null), null, array('size' => '60', 'disabled' => 'disabled'));
  108. //$form->applyFilter('tutor_name', 'html_filter');
  109. if ($course_validation_feature) {
  110. // Description of the requested course.
  111. $form->addElement('textarea', 'description', get_lang('Description'), array('class' => 'span6', 'rows' => '3'));
  112. //$form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
  113. // Objectives of the requested course.
  114. $form->addElement('textarea', 'objetives', get_lang('Objectives'), array('class' => 'span6', 'rows' => '3'));
  115. //$form->addRule('objetives', get_lang('ThisFieldIsRequired'), 'required');
  116. // Target audience of the requested course.
  117. $form->addElement(
  118. 'textarea',
  119. 'target_audience',
  120. get_lang('TargetAudience'),
  121. array('class' => 'span6', 'rows' => '3')
  122. );
  123. //$form->addRule('target_audience', get_lang('ThisFieldIsRequired'), 'required');
  124. }
  125. // Course language.
  126. $form->addElement('select_language', 'course_language', get_lang('Ln'), array(), array('style' => 'width:150px'));
  127. $form->applyFilter('select_language', 'html_filter');
  128. // Exemplary content checkbox.
  129. $form->addElement('checkbox', 'exemplary_content', null, get_lang('FillWithExemplaryContent'));
  130. if ($course_validation_feature) {
  131. // A special URL to terms and conditions that is set in the platform settings page.
  132. $terms_and_conditions_url = trim(api_get_setting('course_validation_terms_and_conditions_url'));
  133. // If the special setting is empty, then we may get the URL from Chamilo's module "Terms and conditions", if it is activated.
  134. if (empty($terms_and_conditions_url)) {
  135. if (api_get_setting('allow_terms_conditions') == 'true') {
  136. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  137. }
  138. }
  139. if (!empty($terms_and_conditions_url)) {
  140. // Terms and conditions to be accepted before sending a course request.
  141. $form->addElement('checkbox', 'legal', null, get_lang('IAcceptTermsAndConditions'), 1);
  142. $form->addRule('legal', get_lang('YouHaveToAcceptTermsAndConditions'), 'required');
  143. // Link to terms and conditions.
  144. $link_terms_and_conditions = '<script>
  145. function MM_openBrWindow(theURL,winName,features) { //v2.0
  146. window.open(theURL,winName,features);
  147. }
  148. </script><a href="#" onclick="javascript: MM_openBrWindow(\''.$terms_and_conditions_url.'\',\'Conditions\',\'scrollbars=yes, width=800\')">';
  149. $link_terms_and_conditions .= get_lang('ReadTermsAndConditions').'</a>';
  150. $form->addElement('label', null, $link_terms_and_conditions);
  151. }
  152. }
  153. $obj = new GradeModel();
  154. $obj->fill_grade_model_select_in_form($form);
  155. $form->addElement('html', '</div>');
  156. // Submit button.
  157. $form->addElement(
  158. 'button',
  159. 'submit',
  160. $course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'),
  161. 'class="add"'
  162. );
  163. // The progress bar of this form.
  164. $form->add_progress_bar();
  165. // Set default values.
  166. if (isset($_user['language']) && $_user['language'] != '') {
  167. $values['course_language'] = $_user['language'];
  168. } else {
  169. $values['course_language'] = api_get_setting('platformLanguage');
  170. }
  171. $form->setDefaults($values);
  172. $message = null;
  173. // Validate the form
  174. if ($form->validate()) {
  175. $course_values = $form->exportValues();
  176. $wanted_code = $course_values['wanted_code'];
  177. //$tutor_name = $course_values['tutor_name'];
  178. $category_code = $course_values['category_code'];
  179. $title = $course_values['title'];
  180. $course_language = $course_values['course_language'];
  181. $exemplary_content = !empty($course_values['exemplary_content']);
  182. if ($course_validation_feature) {
  183. $description = $course_values['description'];
  184. $objetives = $course_values['objetives'];
  185. $target_audience = $course_values['target_audience'];
  186. }
  187. if ($wanted_code == '') {
  188. $wanted_code = CourseManager::generate_course_code(
  189. api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE)
  190. );
  191. }
  192. // Check whether the requested course code has already been occupied.
  193. if (!$course_validation_feature) {
  194. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  195. } else {
  196. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  197. }
  198. if ($course_code_ok) {
  199. if (!$course_validation_feature) {
  200. $params = array();
  201. $params['title'] = $title;
  202. $params['exemplary_content'] = $exemplary_content;
  203. $params['wanted_code'] = $wanted_code;
  204. $params['category_code'] = $category_code;
  205. $params['course_language'] = $course_language;
  206. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  207. $course_info = CourseManager::create_course($params);
  208. if (!empty($course_info)) {
  209. $directory = $course_info['directory'];
  210. $title = $course_info['title'];
  211. // Preparing a confirmation message.
  212. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  213. $tpl->assign('course_url', $link);
  214. $tpl->assign('course_title', Display::url($title, $link));
  215. $tpl->assign('course_id', $course_info['code']);
  216. $template = $tpl->get_template('create_course/add_course.tpl');
  217. $tpl->display($template);
  218. exit;
  219. } else {
  220. $message = Display :: return_message(get_lang('CourseCreationFailed'), 'error', false);
  221. // Display the form.
  222. $content = $form->return_form();
  223. }
  224. } else {
  225. // Create a request for a new course.
  226. $request_id = CourseRequestManager::create_course_request(
  227. $wanted_code,
  228. $title,
  229. $description,
  230. $category_code,
  231. $course_language,
  232. $objetives,
  233. $target_audience,
  234. api_get_user_id(),
  235. $exemplary_content
  236. );
  237. if ($request_id) {
  238. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  239. $message = (is_array(
  240. $course_request_info
  241. ) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang('CourseRequestCreated');
  242. $message = Display :: return_message($message, 'confirmation', false);
  243. $message .= '<div style="float: left; margin:0px; padding: 0px;">'.
  244. '<a class="btn" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>'.
  245. '</div>';
  246. } else {
  247. $message = Display :: return_message(get_lang('CourseRequestCreationFailed'), 'error', false);
  248. // Display the form.
  249. $content = $form->return_form();
  250. }
  251. }
  252. } else {
  253. $message = Display :: return_message(get_lang('CourseCodeAlreadyExists'), 'error', false);
  254. // Display the form.
  255. $content = $form->return_form();
  256. }
  257. } else {
  258. if (!$course_validation_feature) {
  259. $message = Display :: return_message(get_lang('Explanation'));
  260. }
  261. // Display the form.
  262. $content = $form->return_form();
  263. }
  264. $tpl->assign('message', $message);
  265. $tpl->assign('content', $content);
  266. $tpl->display();