course_import.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // $Id: course_import.php 8216 2006-03-15 16:33:13Z turboke $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This tool allows platform admins to create courses by uploading a CSV file
  20. * @todo Add some langvars to DLTT
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. /**
  25. * validate the imported data
  26. */
  27. function validate_data($courses)
  28. {
  29. $errors = array ();
  30. $coursecodes = array ();
  31. foreach ($courses as $index => $course)
  32. {
  33. $course['line'] = $index +1;
  34. //1. check if mandatory fields are set
  35. $mandatory_fields = array ('Code', 'Title', 'CourseCategory', 'Teacher');
  36. foreach ($mandatory_fields as $key => $field)
  37. {
  38. if (!isset ($course[$field]) || strlen($course[$field]) == 0)
  39. {
  40. $course['error'] = get_lang($field.'Mandatory');
  41. $errors[] = $course;
  42. }
  43. }
  44. //2. check if code isn't in use
  45. if (isset ($course['Code']) && strlen($course['Code']) != 0)
  46. {
  47. //2.1 check if code allready used in this CVS-file
  48. if (isset ($coursecodes[$course['Code']]))
  49. {
  50. $course['error'] = get_lang('CodeTwiceInFile');
  51. $errors[] = $course;
  52. }
  53. elseif(strlen($course['Code']) > 20)
  54. {
  55. $course['error'] = get_lang('Max');
  56. $errors[] = $course;
  57. }
  58. //2.3 check if code allready used in DB
  59. else
  60. {
  61. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  62. $sql = "SELECT * FROM $course_table WHERE code = '".mysql_real_escape_string($course['Code'])."'";
  63. $res = api_sql_query($sql, __FILE__, __LINE__);
  64. if (mysql_num_rows($res) > 0)
  65. {
  66. $course['error'] = get_lang('CodeExists');
  67. $errors[] = $course;
  68. }
  69. }
  70. $coursecodes[$course['Code']] = 1;
  71. }
  72. //3. check if teacher exists
  73. if (isset ($course['Teacher']) && strlen($course['Teacher']) != 0)
  74. {
  75. if (UserManager :: is_username_available($course['Teacher']))
  76. {
  77. $course['error'] = get_lang('UnknownTeacher');
  78. $errors[] = $course;
  79. }
  80. }
  81. //4. check if category exists
  82. if (isset ($course['CourseCategory']) && strlen($course['CourseCategory']) != 0)
  83. {
  84. $category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  85. $sql = "SELECT * FROM $category_table WHERE code = '".mysql_real_escape_string($course['CourseCategory'])."'";
  86. $res = api_sql_query($sql, __FILE__, __LINE__);
  87. if (mysql_num_rows($res) == 0)
  88. {
  89. $course['error'] = get_lang('UnkownCategory');
  90. $errors[] = $course;
  91. }
  92. }
  93. }
  94. return $errors;
  95. }
  96. /**
  97. * Save the imported data
  98. */
  99. function save_data($courses)
  100. {
  101. global $_configuration, $firstExpirationDelay;
  102. foreach($courses as $index => $course)
  103. {
  104. $keys = define_course_keys($course['Code'], "", $_configuration['db_prefix']);
  105. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  106. $sql = "SELECT user_id, CONCAT(lastname,' ',firstname) AS name FROM $user_table WHERE username = '".mysql_real_escape_string($course['Teacher'])."'";
  107. $res = api_sql_query($sql,__FILE__,__LINE__);
  108. $teacher = mysql_fetch_object($res);
  109. $visual_code = $keys["currentCourseCode"];
  110. $code = $keys["currentCourseId"];
  111. $db_name = $keys["currentCourseDbName"];
  112. $directory = $keys["currentCourseRepository"];
  113. $expiration_date = time() + $firstExpirationDelay;
  114. prepare_course_repository($directory, $code);
  115. update_Db_course($db_name);
  116. fill_course_repository($directory);
  117. fill_Db_course($db_name, $directory, api_get_setting('platformLanguage'));
  118. register_course($code, $visual_code, $directory, $db_name, $teacher->name, $course['CourseCategory'], $course['Title'], api_get_setting('platformLanguage'), $teacher->user_id, $expiration_date);
  119. echo $code.' CREATED<br />';
  120. }
  121. }
  122. /**
  123. * Read the CSV-file
  124. * @param string $file Path to the CSV-file
  125. * @return array All course-information read from the file
  126. */
  127. function parse_csv_data($file)
  128. {
  129. $courses = Import :: csv_to_array($file);
  130. return $courses;
  131. }
  132. $language_file = array ('admin', 'registration','create_course', 'document');
  133. $cidReset = true;
  134. include ('../inc/global.inc.php');
  135. api_protect_admin_script();
  136. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  137. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  138. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  139. require_once (api_get_path(CONFIGURATION_PATH).'add_course.conf.php');
  140. require_once (api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  141. $formSent = 0;
  142. $errorMsg = '';
  143. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  144. if (is_array($extAuthSource))
  145. {
  146. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  147. }
  148. $tool_name = get_lang('AddCourse').' CSV';
  149. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  150. set_time_limit(0);
  151. Display :: display_header($tool_name);
  152. api_display_tool_title($tool_name);
  153. if ($_POST['formSent'])
  154. {
  155. if(empty($_POST['import_file']['tmp_name']))
  156. {
  157. $error_message = get_lang('UplUploadFailed');
  158. Display :: display_error_message($error_message, false);
  159. }
  160. else
  161. {
  162. $file_type = $_POST['file_type'];
  163. $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  164. $errors = validate_data($courses);
  165. if (count($errors) == 0)
  166. {
  167. //$users = complete_missing_data($courses);
  168. save_data($courses);
  169. //header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  170. //exit ();
  171. }
  172. }
  173. }
  174. if (count($errors) != 0)
  175. {
  176. $error_message = '<ul>';
  177. foreach ($errors as $index => $error_course)
  178. {
  179. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <b>'.$error_course['error'].'</b>: ';
  180. $error_message .= $error_course['Code'].' '.$error_course['Title'];
  181. $error_message .= '</li>';
  182. }
  183. $error_message .= '</ul>';
  184. Display :: display_error_message($error_message, false);
  185. }
  186. ?>
  187. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin:0px;">
  188. <input type="file" name="import_file"/>
  189. <input type="hidden" name="formSent" value="1"/>
  190. <input type="submit" value="<?php echo get_lang('Ok'); ?>"/>
  191. </form>
  192. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  193. <blockquote>
  194. <pre>
  195. <b>Code</b>;<b>Title</b>;<b>CourseCategory</b>;<b>Teacher</b>
  196. BIO0015;Biology;BIO;username
  197. </pre>
  198. </blockquote>
  199. <?php
  200. /*
  201. ==============================================================================
  202. FOOTER
  203. ==============================================================================
  204. */
  205. Display :: display_footer();
  206. ?>