add_course.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // $Id: add_course.php 20588 2009-05-13 12:34:18Z pcool $
  3. /* For licensing terms, see /dokeos_license.txt */
  4. /**
  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 dokeos.create_course
  10. ==============================================================================
  11. */
  12. // name of the language file that needs to be included
  13. $language_file = "create_course";
  14. //delete the globals["_cid"] we don't need it here
  15. $cidReset = true; // Flag forcing the 'current course' reset
  16. // including the global file
  17. include ('../inc/global.inc.php');
  18. // section for the tabs
  19. $this_section=SECTION_COURSES;
  20. // include configuration file
  21. include (api_get_path(CONFIGURATION_PATH).'add_course.conf.php');
  22. // include additional libraries
  23. include_once (api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  24. include_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  25. include_once (api_get_path(LIBRARY_PATH).'debug.lib.inc.php');
  26. include_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  27. include_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  28. include_once (api_get_path(CONFIGURATION_PATH).'course_info.conf.php');
  29. $interbreadcrumb[] = array('url'=>api_get_path(WEB_PATH).'user_portal.php', 'name'=> get_lang('MyCourses'));
  30. // Displaying the header
  31. $tool_name = get_lang('CreateSite');
  32. if (api_get_setting('allow_users_to_create_courses')=='false' && !api_is_platform_admin()) {
  33. api_not_allowed(true);
  34. }
  35. Display :: display_header($tool_name);
  36. // Check access rights
  37. if (!api_is_allowed_to_create_course()) {
  38. Display :: display_error_message(get_lang("NotAllowed"));
  39. Display::display_footer();
  40. exit;
  41. }
  42. // Get all course categories
  43. $table_course_category = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  44. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  45. global $_configuration;
  46. $dbnamelength = strlen($_configuration['db_prefix']);
  47. //Ensure the database prefix + database name do not get over 40 characters
  48. $maxlength = 40 - $dbnamelength;
  49. // Build the form
  50. $categories = array();
  51. $form = new FormValidator('add_course');
  52. // form title
  53. $form->addElement('header', '', $tool_name);
  54. //title
  55. $form->add_textfield('title',get_lang('CourseName'),true,array('size'=>'60'));
  56. $form->applyFilter('title', 'html_filter');
  57. $form->addElement('static',null,null,get_lang('Ex'));
  58. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), $categories);
  59. $form->applyFilter('category_code', 'html_filter');
  60. CourseManager::select_and_sort_categories($categories_select);
  61. $form->addElement('static',null,null, get_lang('TargetFac'));
  62. $form->add_textfield('wanted_code', get_lang('Code'),false,array('size'=>'$maxlength','maxlength'=>$maxlength));
  63. $form->applyFilter('wanted_code', 'html_filter');
  64. $form->addRule('wanted_code',get_lang('Max'),'maxlength',$maxlength);
  65. $titular= &$form->add_textfield('tutor_name', get_lang('Professors'),null,array('size'=>'60','disabled'=>'disabled'));
  66. $form->addElement('static',null,null,get_lang('ExplicationTrainers'));
  67. //$form->applyFilter('tutor_name', 'html_filter');
  68. $form->addElement('select_language', 'course_language', get_lang('Ln'));
  69. $form->applyFilter('select_language', 'html_filter');
  70. $form->addElement('style_submit_button', null, get_lang('CreateCourseArea'), 'class="add"');
  71. $form->add_progress_bar();
  72. // Set default values
  73. if (isset($_user["language"]) && $_user["language"]!="") {
  74. $values['course_language'] = $_user["language"];
  75. } else {
  76. $values['course_language'] = api_get_setting('platformLanguage');
  77. }
  78. $values['tutor_name'] = api_get_person_name($_user['firstName'], $_user['lastName'], null, null, $values['course_language']);
  79. $form->setDefaults($values);
  80. // Validate the form
  81. if ($form->validate()) {
  82. $course_values = $form->exportValues();
  83. $wanted_code = $course_values['wanted_code'];
  84. $tutor_name = $course_values['tutor_name'];
  85. $category_code = $course_values['category_code'];
  86. $title = $course_values['title'];
  87. $course_language = $course_values['course_language'];
  88. if (trim($wanted_code) == '') {
  89. $wanted_code = generate_course_code(api_substr($title,0,$maxlength));
  90. }
  91. $keys = define_course_keys($wanted_code, "", $_configuration['db_prefix']);
  92. $sql_check = sprintf('SELECT * FROM '.$table_course.' WHERE visual_code = "%s"',Database :: escape_string($wanted_code));
  93. $result_check = Database::query($sql_check,__FILE__,__LINE__); //I don't know why this api function doesn't work...
  94. if ( Database::num_rows($result_check)<1 ) {
  95. if (sizeof($keys)) {
  96. $visual_code = $keys["currentCourseCode"];
  97. $code = $keys["currentCourseId"];
  98. $db_name = $keys["currentCourseDbName"];
  99. $directory = $keys["currentCourseRepository"];
  100. $expiration_date = time() + $firstExpirationDelay;
  101. prepare_course_repository($directory, $code);
  102. update_Db_course($db_name);
  103. $pictures_array=fill_course_repository($directory);
  104. fill_Db_course($db_name, $directory, $course_language,$pictures_array);
  105. register_course($code, $visual_code, $directory, $db_name, $tutor_name, $category_code, $title, $course_language, api_get_user_id(), $expiration_date);
  106. }
  107. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  108. $message = get_lang('JustCreated');
  109. $message .= ' <a href="'.$link.'">'.$title."</a>";
  110. //$message .= "<br /><br /><br />";
  111. //$message .= '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>';
  112. Display :: display_confirmation_message($message,false);
  113. echo '<div style="float:right; margin:0px; padding:0px;">' .
  114. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  115. '</div>';
  116. } else {
  117. Display :: display_error_message(get_lang('CourseCodeAlreadyExists'),false);
  118. $form->display();
  119. //echo '<p>'.get_lang('CourseCodeAlreadyExistExplained').'</p>';
  120. }
  121. } else {
  122. // Display the form
  123. $form->display();
  124. Display::display_normal_message(get_lang('Explanation'));
  125. }
  126. /*
  127. ==============================================================================
  128. FOOTER
  129. ==============================================================================
  130. */
  131. Display :: display_footer();