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

Source for file class_user_import.php

Documentation is available at class_user_import.php

  1. <?php
  2.  
  3. // $Id: class_user_import.php 9018 2006-06-28 15:11:16Z evie_em $
  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 update class-user relations by uploading a
  26. * CSVfile
  27. @package dokeos.admin
  28. ==============================================================================
  29. */
  30. /**
  31.  * validate the imported data
  32.  */
  33. function validate_data($user_classes)
  34. {
  35.     $errors array ();
  36.     $classcodes array ();
  37.     foreach ($user_classes as $index => $user_class)
  38.     {
  39.         $user_class['line'$index +1;
  40.         //1. check if mandatory fields are set
  41.         $mandatory_fields array ('UserName''ClassName');
  42.         foreach ($mandatory_fields as $key => $field)
  43.         {
  44.             if (!isset ($user_class[$field]|| strlen($user_class[$field]== 0)
  45.             {
  46.                 $user_class['error'get_lang($field.'Mandatory');
  47.                 $errors[$user_class;
  48.             }
  49.         }
  50.         //2. check if classcode exists
  51.         if (isset ($user_class['ClassName']&& strlen($user_class['ClassName']!= 0)
  52.         {
  53.             //2.1 check if code allready used in this CVS-file
  54.             if (!isset ($classcodes[$user_class['ClassName']]))
  55.             {
  56.                 //2.1.1 check if code exists in DB
  57.                 $class_table Database :: get_main_table(TABLE_MAIN_CLASS);
  58.                 $sql "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'";
  59.                 $res api_sql_query($sql__FILE____LINE__);
  60.                 if (mysql_num_rows($res== 0)
  61.                 {
  62.                     $user_class['error'get_lang('CodeDoesNotExists');
  63.                     $errors[$user_class;
  64.                 }
  65.                 else
  66.                 {
  67.                     $classcodes[$user_class['CourseCode']] 1;
  68.                 }
  69.             }
  70.         }
  71.         //3. check if username exists
  72.         if (isset ($user_class['UserName']&& strlen($user_class['UserName']!= 0)
  73.         {
  74.             if (UserManager :: is_username_available($user_class['UserName']))
  75.             {
  76.                 $user_class['error'get_lang('UnknownUser');
  77.                 $errors[$user_class;
  78.             }
  79.         }
  80.     }
  81.     return $errors;
  82. }
  83.  
  84. /**
  85.  * Save the imported data
  86.  */
  87. function save_data($users_classes)
  88. {
  89.     // table definitions
  90.     $user_table         Database :: get_main_table(TABLE_MAIN_USER);
  91.     $class_user_table     Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  92.     $class_table         Database :: get_main_table(TABLE_MAIN_CLASS);
  93.     
  94.     $csv_data array ();
  95.     foreach ($users_classes as $index => $user_class)
  96.     {
  97.         $sql "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'";
  98.         $res api_sql_query($sql__FILE____LINE__);
  99.         $obj mysql_fetch_object($res);
  100.         $csv_data[$user_class['UserName']][$obj->id1;
  101.     }
  102.     foreach ($csv_data as $username => $csv_subscriptions)
  103.     {
  104.         $user_id 0;
  105.         $sql "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'";
  106.         $res api_sql_query($sql__FILE____LINE__);
  107.         $obj mysql_fetch_object($res);
  108.         $user_id $obj->user_id;
  109.         $sql "SELECT * FROM $class_user_table cu WHERE cu.user_id = $user_id";
  110.         $res api_sql_query($sql__FILE____LINE__);
  111.         $db_subscriptions array ();
  112.         while ($obj mysql_fetch_object($res))
  113.         {
  114.             $db_subscriptions[$obj->class_id1;
  115.         }
  116.         $to_subscribe array_diff(array_keys($csv_subscriptions)array_keys($db_subscriptions));
  117.         $to_unsubscribe array_diff(array_keys($db_subscriptions)array_keys($csv_subscriptions));
  118.         if ($_POST['subscribe'])
  119.         {
  120.             foreach ($to_subscribe as $index => $class_id)
  121.             {
  122.                 ClassManager :: add_user($user_id$class_id);
  123.                 //echo get_lang('Subscription').' : '.$course_code.'<br />';
  124.             }
  125.         }
  126.         if ($_POST['unsubscribe'])
  127.         {
  128.             foreach ($to_unsubscribe as $index => $class_id)
  129.             {
  130.                 ClassManager :: unsubscribe_user($user_id$class_id);
  131.                 //echo get_lang('Unsubscription').' : '.$course_code.'<br />';
  132.             }
  133.         }
  134.     }
  135. }
  136. /**
  137.  * Read the CSV-file
  138.  * @param string $file Path to the CSV-file
  139.  * @return array All course-information read from the file
  140.  */
  141. function parse_csv_data($file)
  142. {
  143.     $courses Import :: csv_to_array($file);
  144.     return $courses;
  145. }
  146.  
  147. // name of the language file that needs to be included 
  148. $language_file array ('admin''registration');
  149.  
  150. $cidReset true;
  151.  
  152. include ('../inc/global.inc.php');
  153.  
  154.  
  155. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  156. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  157. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  158. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  159. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  160. $formSent 0;
  161. $errorMsg '';
  162.  
  163. $tool_name get_lang('AddUsersToAClass').' CSV';
  164.  
  165. $interbreadcrumb[array ('url' => 'index.php''name' => get_lang('PlatformAdmin'));
  166.  
  167. $form new FormValidator('class_user_import');
  168. $form->addElement('file''import_file'get_lang('ImportFileLocation'));
  169. $form->addElement('checkbox''subscribe'get_lang('Action')get_lang('SubscribeUserIfNotAllreadySubscribed'));
  170. $form->addElement('checkbox''unsubscribe'''get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  171. $form->addElement('submit''submit'get_lang('Ok'));
  172. if ($form->validate())
  173. {
  174.     $users_classes parse_csv_data($_FILES['import_file']['tmp_name']);
  175.     $errors validate_data($users_classes);
  176.     if (count($errors== 0)
  177.     {
  178.         save_data($users_classes);
  179.         header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  180.         exit ();
  181.     }
  182. }
  183. Display :: display_header($tool_name);
  184. if (count($errors!= 0)
  185. {
  186.     $error_message '<ul>';
  187.     foreach ($errors as $index => $error_class_user)
  188.     {
  189.         $error_message .= '<li>'.get_lang('Line').' '.$error_class_user['line'].': <b>'.$error_class_user['error'].'</b>: ';
  190.         $error_message .= '</li>';
  191.     }
  192.     $error_message .= '</ul>';
  193.     Display :: display_error_message($error_message);
  194. }
  195.  
  196. $form->display();
  197. ?>
  198. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'?> :</p>
  199. <blockquote>
  200. <pre>
  201. <b>UserName</b>;<b>ClassName</b>
  202. jdoe;class01
  203. a.dam;class01
  204. </pre>
  205. </blockquote>
  206. <?php
  207.  
  208. /*
  209. ==============================================================================
  210.         FOOTER
  211. ==============================================================================
  212. */
  213. ?>

Documentation generated on Thu, 12 Jun 2008 13:07:58 -0500 by phpDocumentor 1.4.1