course_import.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php // $Id: course_import.php 8216 2006-03-15 16:33:13Z turboke $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  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, rue du Corbeau, 108, B-1030 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. global $_configuration;
  29. $dbnamelength = strlen($_configuration['db_prefix']);
  30. //Ensure the prefix + database name do not get over 40 characters
  31. $maxlength = 40 - $dbnamelength;
  32. $errors = array ();
  33. $coursecodes = array ();
  34. foreach ($courses as $index => $course) {
  35. $course['line'] = $index +1;
  36. //1. check if mandatory fields are set
  37. $mandatory_fields = array ('Code', 'Title', 'CourseCategory', 'Teacher');
  38. foreach ($mandatory_fields as $key => $field) {
  39. if (!isset ($course[$field]) || strlen($course[$field]) == 0)
  40. {
  41. $course['error'] = get_lang($field.'Mandatory');
  42. $errors[] = $course;
  43. }
  44. }
  45. //2. check if code isn't in use
  46. if (isset ($course['Code']) && strlen($course['Code']) != 0) {
  47. //2.1 check if code allready used in this CVS-file
  48. if (isset ($coursecodes[$course['Code']])) {
  49. $course['error'] = get_lang('CodeTwiceInFile');
  50. $errors[] = $course;
  51. } elseif (api_strlen(($course['Code']) > $maxlength)) {
  52. $course['error'] = get_lang('Max');
  53. $errors[] = $course;
  54. }
  55. //2.3 check if code allready used in DB
  56. else {
  57. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  58. $sql = "SELECT * FROM $course_table WHERE code = '".Database::escape_string($course['Code'])."'";
  59. $res = api_sql_query($sql, __FILE__, __LINE__);
  60. if (Database::num_rows($res) > 0) {
  61. $course['error'] = get_lang('CodeExists');
  62. $errors[] = $course;
  63. }
  64. }
  65. $coursecodes[$course['Code']] = 1;
  66. }
  67. //3. check if teacher exists
  68. if (isset ($course['Teacher']) && strlen($course['Teacher']) != 0)
  69. {
  70. if (UserManager :: is_username_available($course['Teacher']))
  71. {
  72. $course['error'] = get_lang('UnknownTeacher');
  73. $errors[] = $course;
  74. }
  75. }
  76. //4. check if category exists
  77. if (isset ($course['CourseCategory']) && strlen($course['CourseCategory']) != 0)
  78. {
  79. $category_table = Database :: get_main_table(TABLE_MAIN_CATEGORY);
  80. $sql = "SELECT * FROM $category_table WHERE code = '".mysql_real_escape_string($course['CourseCategory'])."'";
  81. $res = api_sql_query($sql, __FILE__, __LINE__);
  82. if (mysql_num_rows($res) == 0)
  83. {
  84. $course['error'] = get_lang('UnkownCategory');
  85. $errors[] = $course;
  86. }
  87. }
  88. }
  89. return $errors;
  90. }
  91. /**
  92. * Save the imported data
  93. * @param array List of courses info
  94. */
  95. function save_data($courses)
  96. {
  97. global $_configuration, $firstExpirationDelay;
  98. $msg = '';
  99. $enabled_languages = api_get_languages();
  100. $enabled_languages = $enabled_languages["folder"];
  101. foreach($courses as $index => $course)
  102. {
  103. $course_language = $course['Language'];
  104. if (empty($course_language) || !in_array($course_language, $enabled_languages))
  105. {
  106. $course_language = api_get_setting('platformLanguage');
  107. }
  108. $keys = define_course_keys($course['Code'], "", $_configuration['db_prefix']);
  109. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  110. $sql = "SELECT user_id, CONCAT(lastname,' ',firstname) AS name FROM $user_table WHERE username = '".Database::escape_string($course['Teacher'])."'";
  111. $res = api_sql_query($sql,__FILE__,__LINE__);
  112. $teacher = mysql_fetch_object($res);
  113. $visual_code = $keys["currentCourseCode"];
  114. $code = $keys["currentCourseId"];
  115. $db_name = $keys["currentCourseDbName"];
  116. $directory = $keys["currentCourseRepository"];
  117. $expiration_date = time() + $firstExpirationDelay;
  118. prepare_course_repository($directory, $code);
  119. update_Db_course($db_name);
  120. fill_course_repository($directory);
  121. fill_Db_course($db_name, $directory, $course_language, array());
  122. register_course($code, $visual_code, $directory, $db_name, $teacher->name, $course['CourseCategory'], $course['Title'], $course_language, $teacher->user_id, $expiration_date);
  123. $msg .= '<a href="'.api_get_path(WEB_COURSE_PATH).$directory.'/">'.$code.'</a> '.get_lang('Created').'<br />';
  124. }
  125. if (!empty($msg)) {
  126. Display::display_normal_message($msg,false);
  127. }
  128. }
  129. /**
  130. * Read the CSV-file
  131. * @param string $file Path to the CSV-file
  132. * @return array All course-information read from the file
  133. */
  134. function parse_csv_data($file)
  135. {
  136. $courses = Import :: csv_to_array($file);
  137. return $courses;
  138. }
  139. $language_file = array ('admin', 'registration','create_course', 'document');
  140. $cidReset = true;
  141. include ('../inc/global.inc.php');
  142. api_protect_admin_script();
  143. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  144. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  145. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  146. require_once (api_get_path(CONFIGURATION_PATH).'add_course.conf.php');
  147. require_once (api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  148. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  149. $formSent = 0;
  150. $errorMsg = '';
  151. $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
  152. if (is_array($extAuthSource))
  153. {
  154. $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
  155. }
  156. $tool_name = get_lang('ImportCourses').' CSV';
  157. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  158. set_time_limit(0);
  159. Display :: display_header($tool_name);
  160. if ($_POST['formSent'])
  161. {
  162. if(empty($_FILES['import_file']['tmp_name']))
  163. {
  164. $error_message = get_lang('UplUploadFailed');
  165. Display :: display_error_message($error_message, false);
  166. }
  167. else
  168. {
  169. $file_type = $_POST['file_type'];
  170. $courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  171. $errors = validate_data($courses);
  172. if (count($errors) == 0)
  173. {
  174. //$users = complete_missing_data($courses);
  175. save_data($courses);
  176. //header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  177. //exit ();
  178. }
  179. }
  180. }
  181. if (count($errors) != 0)
  182. {
  183. $error_message = '<ul>';
  184. foreach ($errors as $index => $error_course)
  185. {
  186. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <b>'.$error_course['error'].'</b>: ';
  187. $error_message .= $error_course['Code'].' '.$error_course['Title'];
  188. $error_message .= '</li>';
  189. }
  190. $error_message .= '</ul>';
  191. Display :: display_error_message($error_message, false);
  192. }
  193. ?>
  194. <form method="post" action="<?php echo api_get_self(); ?>" enctype="multipart/form-data" style="margin:0px;">
  195. <div class="row"><div class="form_header"><?php echo $tool_name; ?></div></div>
  196. <div class="row">
  197. <div class="label"><?php echo get_lang('ImportCSVFileLocation');?></div>
  198. <div class="formw">
  199. <input type="file" name="import_file"/>
  200. </div>
  201. </div>
  202. <div class="row">
  203. <div class="label"></div>
  204. <div class="formw">
  205. <button type="submit" class="save" value="<?php echo get_lang('Import'); ?>"><?php echo get_lang('Import'); ?></button>
  206. </div>
  207. </div>
  208. <input type="hidden" name="formSent" value="1"/>
  209. </form>
  210. <div style="clear: both;"></div>
  211. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  212. <blockquote>
  213. <pre>
  214. <b>Code</b>;<b>Title</b>;<b>CourseCategory</b>;<b>Teacher</b>;Language
  215. BIO0015;Biology;BIO;username;english
  216. </pre>
  217. </blockquote>
  218. <?php
  219. /*
  220. ==============================================================================
  221. FOOTER
  222. ==============================================================================
  223. */
  224. Display :: display_footer();
  225. ?>