add_course.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. exit;
  60. }
  61. // Build the form.
  62. $form = new FormValidator('add_course');
  63. // Form title
  64. $form->addElement('header', $tool_name);
  65. // Title
  66. $form->addElement(
  67. 'text',
  68. 'title',
  69. array(get_lang('CourseName'), get_lang('Ex')),
  70. array('class' => 'span6', 'id' => 'title')
  71. );
  72. $form->applyFilter('title', 'html_filter');
  73. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  74. $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(
  75. 'AdvancedParameters'
  76. ).'</div></span></a>';
  77. $form->addElement('advanced_settings', $advanced);
  78. $form->addElement('html', '<div id="options" style="display:none">');
  79. // Course category.
  80. $categories_select = $form->addElement(
  81. 'select',
  82. 'category_code',
  83. array(get_lang('Fac'), get_lang('TargetFac')),
  84. array(),
  85. array('id' => 'category_code', 'class' => 'chzn-select', 'style' => 'width:350px')
  86. );
  87. $form->applyFilter('category_code', 'html_filter');
  88. $categories_select->addOption('-', '');
  89. CourseManager::select_and_sort_categories($categories_select);
  90. // Course code
  91. $form->add_textfield(
  92. 'wanted_code',
  93. array(get_lang('Code'), get_lang('OnlyLettersAndNumbers')),
  94. '',
  95. array('class' => 'span3', 'maxlength' => CourseManager::MAX_COURSE_LENGTH_CODE)
  96. );
  97. $form->applyFilter('wanted_code', 'html_filter');
  98. $form->addRule('wanted_code', get_lang('Max'), 'maxlength', CourseManager::MAX_COURSE_LENGTH_CODE);
  99. /*if ($course_validation_feature) {
  100. $form->addRule('wanted_code', get_lang('ThisFieldIsRequired'), 'required');
  101. }*/
  102. // The teacher
  103. //get_lang('ExplicationTrainers')
  104. $titular = & $form->addElement(
  105. 'hidden',
  106. 'tutor_name',
  107. ''
  108. ); //array(get_lang('Professor'), null), null, array('size' => '60', 'disabled' => 'disabled'));
  109. //$form->applyFilter('tutor_name', 'html_filter');
  110. if ($course_validation_feature) {
  111. // Description of the requested course.
  112. $form->addElement('textarea', 'description', get_lang('Description'), array('class' => 'span6', 'rows' => '3'));
  113. //$form->addRule('description', get_lang('ThisFieldIsRequired'), 'required');
  114. // Objectives of the requested course.
  115. $form->addElement('textarea', 'objetives', get_lang('Objectives'), array('class' => 'span6', 'rows' => '3'));
  116. //$form->addRule('objetives', get_lang('ThisFieldIsRequired'), 'required');
  117. // Target audience of the requested course.
  118. $form->addElement(
  119. 'textarea',
  120. 'target_audience',
  121. get_lang('TargetAudience'),
  122. array('class' => 'span6', 'rows' => '3')
  123. );
  124. //$form->addRule('target_audience', get_lang('ThisFieldIsRequired'), 'required');
  125. }
  126. // Course language.
  127. $form->addElement('select_language', 'course_language', get_lang('Ln'), array(), array('style' => 'width:150px'));
  128. $form->applyFilter('select_language', 'html_filter');
  129. // Exemplary content checkbox.
  130. $form->addElement('checkbox', 'exemplary_content', null, get_lang('FillWithExemplaryContent'));
  131. if ($course_validation_feature) {
  132. // A special URL to terms and conditions that is set in the platform settings page.
  133. $terms_and_conditions_url = trim(api_get_setting('course_validation_terms_and_conditions_url'));
  134. // If the special setting is empty, then we may get the URL from Chamilo's module "Terms and conditions", if it is activated.
  135. if (empty($terms_and_conditions_url)) {
  136. if (api_get_setting('allow_terms_conditions') == 'true') {
  137. $terms_and_conditions_url = api_get_path(WEB_CODE_PATH).'auth/inscription.php?legal';
  138. }
  139. }
  140. if (!empty($terms_and_conditions_url)) {
  141. // Terms and conditions to be accepted before sending a course request.
  142. $form->addElement('checkbox', 'legal', null, get_lang('IAcceptTermsAndConditions'), 1);
  143. $form->addRule('legal', get_lang('YouHaveToAcceptTermsAndConditions'), 'required');
  144. // Link to terms and conditions.
  145. $link_terms_and_conditions = '<script type="text/JavaScript">
  146. <!--
  147. function MM_openBrWindow(theURL,winName,features) { //v2.0
  148. window.open(theURL,winName,features);
  149. }
  150. //-->
  151. </script><a href="#" onclick="javascript: MM_openBrWindow(\''.$terms_and_conditions_url.'\',\'Conditions\',\'scrollbars=yes, width=800\')">';
  152. $link_terms_and_conditions .= get_lang('ReadTermsAndConditions').'</a>';
  153. $form->addElement('label', null, $link_terms_and_conditions);
  154. }
  155. }
  156. $obj = new GradeModel();
  157. $obj->fill_grade_model_select_in_form($form);
  158. $form->addElement('html', '</div>');
  159. // Submit button.
  160. $form->addElement(
  161. 'button',
  162. 'submit',
  163. $course_validation_feature ? get_lang('CreateThisCourseRequest') : get_lang('CreateCourseArea'),
  164. 'class="add"'
  165. );
  166. // The progress bar of this form.
  167. $form->add_progress_bar();
  168. // Set default values.
  169. if (isset($_user['language']) && $_user['language'] != '') {
  170. $values['course_language'] = $_user['language'];
  171. } else {
  172. $values['course_language'] = api_get_setting('platformLanguage');
  173. }
  174. $form->setDefaults($values);
  175. $message = null;
  176. // Validate the form
  177. if ($form->validate()) {
  178. $course_values = $form->exportValues();
  179. $wanted_code = $course_values['wanted_code'];
  180. //$tutor_name = $course_values['tutor_name'];
  181. $category_code = $course_values['category_code'];
  182. $title = $course_values['title'];
  183. $course_language = $course_values['course_language'];
  184. $exemplary_content = !empty($course_values['exemplary_content']);
  185. if ($course_validation_feature) {
  186. $description = $course_values['description'];
  187. $objetives = $course_values['objetives'];
  188. $target_audience = $course_values['target_audience'];
  189. }
  190. if ($wanted_code == '') {
  191. $wanted_code = CourseManager::generate_course_code(
  192. api_substr($title, 0, CourseManager::MAX_COURSE_LENGTH_CODE)
  193. );
  194. }
  195. // Check whether the requested course code has already been occupied.
  196. if (!$course_validation_feature) {
  197. $course_code_ok = !CourseManager::course_code_exists($wanted_code);
  198. } else {
  199. $course_code_ok = !CourseRequestManager::course_code_exists($wanted_code);
  200. }
  201. if ($course_code_ok) {
  202. if (!$course_validation_feature) {
  203. $params = array();
  204. $params['title'] = $title;
  205. $params['exemplary_content'] = $exemplary_content;
  206. $params['wanted_code'] = $wanted_code;
  207. $params['category_code'] = $category_code;
  208. $params['course_language'] = $course_language;
  209. $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  210. $course_info = CourseManager::create_course($params);
  211. if (!empty($course_info)) {
  212. $directory = $course_info['directory'];
  213. $title = $course_info['title'];
  214. // Preparing a confirmation message.
  215. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  216. $tpl->assign('course_url', $link);
  217. $tpl->assign('course_title', Display::url($title, $link));
  218. $tpl->assign('course_id', $course_info['code']);
  219. $template = $tpl->get_template('create_course/add_course.tpl');
  220. $tpl->display($template);
  221. exit;
  222. } else {
  223. $message = Display :: return_message(get_lang('CourseCreationFailed'), 'error', false);
  224. // Display the form.
  225. $content = $form->return_form();
  226. }
  227. } else {
  228. // Create a request for a new course.
  229. $request_id = CourseRequestManager::create_course_request(
  230. $wanted_code,
  231. $title,
  232. $description,
  233. $category_code,
  234. $course_language,
  235. $objetives,
  236. $target_audience,
  237. api_get_user_id(),
  238. $exemplary_content
  239. );
  240. if ($request_id) {
  241. $course_request_info = CourseRequestManager::get_course_request_info($request_id);
  242. $message = (is_array(
  243. $course_request_info
  244. ) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang('CourseRequestCreated');
  245. $message = Display :: return_message($message, 'confirmation', false);
  246. $message .= '<div style="float: left; margin:0px; padding: 0px;">'.
  247. '<a class="btn" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>'.
  248. '</div>';
  249. } else {
  250. $message = Display :: return_message(get_lang('CourseRequestCreationFailed'), 'error', false);
  251. // Display the form.
  252. $content = $form->return_form();
  253. }
  254. }
  255. } else {
  256. $message = Display :: return_message(get_lang('CourseCodeAlreadyExists'), 'error', false);
  257. // Display the form.
  258. $content = $form->return_form();
  259. }
  260. } else {
  261. if (!$course_validation_feature) {
  262. $message = Display :: return_message(get_lang('Explanation'));
  263. }
  264. // Display the form.
  265. $content = $form->return_form();
  266. }
  267. $tpl->assign('message', $message);
  268. $tpl->assign('content', $content);
  269. $tpl->display();