add_course.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
  30. $htmlHeadXtra[] = '<script type="text/javascript">
  31. function setFocus(){
  32. $("#title").focus();
  33. }
  34. $(window).load(function () {
  35. setFocus();
  36. });
  37. </script>';
  38. $interbreadcrumb[] = array('url'=>api_get_path(WEB_PATH).'user_portal.php', 'name'=> get_lang('MyCourses'));
  39. // Displaying the header
  40. $tool_name = get_lang('CreateSite');
  41. if (api_get_setting('allow_users_to_create_courses')=='false' && !api_is_platform_admin()) {
  42. api_not_allowed(true);
  43. }
  44. Display :: display_header($tool_name);
  45. // Check access rights
  46. if (!api_is_allowed_to_create_course()) {
  47. Display :: display_error_message(get_lang("NotAllowed"));
  48. Display::display_footer();
  49. exit;
  50. }
  51. // Get all course categories
  52. $table_course_category = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  53. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  54. global $_configuration;
  55. $dbnamelength = strlen($_configuration['db_prefix']);
  56. //Ensure the database prefix + database name do not get over 40 characters
  57. $maxlength = 40 - $dbnamelength;
  58. // Build the form
  59. $categories = array();
  60. $form = new FormValidator('add_course');
  61. // form title
  62. $form->addElement('header', '', $tool_name);
  63. //title
  64. $form->addElement('text', 'title', get_lang('CourseName'), array('size'=>'60','id' => 'title'));
  65. $form->applyFilter('title', 'html_filter');
  66. $form->addElement('static',null,null,get_lang('Ex'));
  67. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), $categories);
  68. $form->applyFilter('category_code', 'html_filter');
  69. CourseManager::select_and_sort_categories($categories_select);
  70. $form->addElement('static',null,null, get_lang('TargetFac'));
  71. $form->add_textfield('wanted_code', get_lang('Code'),false,array('size'=>'$maxlength','maxlength'=>$maxlength));
  72. $form->applyFilter('wanted_code', 'html_filter');
  73. $form->addRule('wanted_code',get_lang('Max'),'maxlength',$maxlength);
  74. $titular= &$form->add_textfield('tutor_name', get_lang('Professors'),null,array('size'=>'60','disabled'=>'disabled'));
  75. $form->addElement('static',null,null,get_lang('ExplicationTrainers'));
  76. //$form->applyFilter('tutor_name', 'html_filter');
  77. $form->addElement('select_language', 'course_language', get_lang('Ln'));
  78. $form->applyFilter('select_language', 'html_filter');
  79. $form->addElement('style_submit_button', null, get_lang('CreateCourseArea'), 'class="add"');
  80. $form->add_progress_bar();
  81. // Set default values
  82. if (isset($_user["language"]) && $_user["language"]!="") {
  83. $values['course_language'] = $_user["language"];
  84. } else {
  85. $values['course_language'] = api_get_setting('platformLanguage');
  86. }
  87. $values['tutor_name'] = api_get_person_name($_user['firstName'], $_user['lastName'], null, null, $values['course_language']);
  88. $form->setDefaults($values);
  89. // Validate the form
  90. if ($form->validate()) {
  91. $course_values = $form->exportValues();
  92. $wanted_code = $course_values['wanted_code'];
  93. $tutor_name = $course_values['tutor_name'];
  94. $category_code = $course_values['category_code'];
  95. $title = $course_values['title'];
  96. $course_language = $course_values['course_language'];
  97. if (trim($wanted_code) == '') {
  98. $wanted_code = generate_course_code(api_substr($title,0,$maxlength));
  99. }
  100. $keys = define_course_keys($wanted_code, "", $_configuration['db_prefix']);
  101. $sql_check = sprintf('SELECT * FROM '.$table_course.' WHERE visual_code = "%s"',Database :: escape_string($wanted_code));
  102. $result_check = Database::query($sql_check,__FILE__,__LINE__); //I don't know why this api function doesn't work...
  103. if ( Database::num_rows($result_check)<1 ) {
  104. if (sizeof($keys)) {
  105. $visual_code = $keys["currentCourseCode"];
  106. $code = $keys["currentCourseId"];
  107. $db_name = $keys["currentCourseDbName"];
  108. $directory = $keys["currentCourseRepository"];
  109. $expiration_date = time() + $firstExpirationDelay;
  110. prepare_course_repository($directory, $code);
  111. update_Db_course($db_name);
  112. $pictures_array=fill_course_repository($directory);
  113. fill_Db_course($db_name, $directory, $course_language,$pictures_array);
  114. register_course($code, $visual_code, $directory, $db_name, $tutor_name, $category_code, $title, $course_language, api_get_user_id(), $expiration_date);
  115. }
  116. $link = api_get_path(WEB_COURSE_PATH).$directory.'/';
  117. $message = get_lang('JustCreated');
  118. $message .= ' <a href="'.$link.'">'.$title."</a>";
  119. //$message .= "<br /><br /><br />";
  120. //$message .= '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>';
  121. Display :: display_confirmation_message($message,false);
  122. echo '<div style="float:right; margin:0px; padding:0px;">' .
  123. '<a class="bottom-link" href="'.api_get_path(WEB_PATH).'user_portal.php">'.get_lang('Enter').'</a>' .
  124. '</div>';
  125. } else {
  126. Display :: display_error_message(get_lang('CourseCodeAlreadyExists'),false);
  127. $form->display();
  128. //echo '<p>'.get_lang('CourseCodeAlreadyExistExplained').'</p>';
  129. }
  130. } else {
  131. // Display the form
  132. $form->display();
  133. Display::display_normal_message(get_lang('Explanation'));
  134. }
  135. /*
  136. ==============================================================================
  137. FOOTER
  138. ==============================================================================
  139. */
  140. Display :: display_footer();