dokeos-admin
[ class tree: dokeos-admin ] [ index: dokeos-admin ] [ all elements ]

Source for file course_import.php

Documentation is available at course_import.php

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

Documentation generated on Thu, 12 Jun 2008 13:15:23 -0500 by phpDocumentor 1.4.1