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

Source for file course_user_import.php

Documentation is available at course_user_import.php

  1. <?php
  2. // $Id: course_user_import.php 8216 2006-03-15 16:33:13Z turboke $
  3. /*
  4. ==============================================================================
  5.     Dokeos - elearning and course management software
  6.  
  7.     Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
  8.  
  9.     For a full list of contributors, see "credits.txt".
  10.     The full license can be read in "license.txt".
  11.  
  12.     This program is free software; you can redistribute it and/or
  13.     modify it under the terms of the GNU General Public License
  14.     as published by the Free Software Foundation; either version 2
  15.     of the License, or (at your option) any later version.
  16.  
  17.     See the GNU General Public License for more details.
  18.  
  19.     Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  20. ==============================================================================
  21. */
  22. /**
  23. ==============================================================================
  24. * This tool allows platform admins to update course-user relations by uploading
  25. * a CSVfile
  26. @package dokeos.admin
  27. ==============================================================================
  28. */
  29. /**
  30.  * validate the imported data
  31.  */
  32. function validate_data($users_courses)
  33. {
  34.     $errors array ();
  35.     $coursecodes array ();
  36.     foreach ($users_courses as $index => $user_course)
  37.     {
  38.         $user_course['line'$index +1;
  39.         //1. check if mandatory fields are set
  40.         $mandatory_fields array ('UserName''CourseCode''Status');
  41.         foreach ($mandatory_fields as $key => $field)
  42.         {
  43.             if (!isset ($user_course[$field]|| strlen($user_course[$field]== 0)
  44.             {
  45.                 $user_course['error'get_lang($field.'Mandatory');
  46.                 $errors[$user_course;
  47.             }
  48.         }
  49.         //2. check if coursecode exists
  50.         if (isset ($user_course['CourseCode']&& strlen($user_course['CourseCode']!= 0)
  51.         {
  52.             //2.1 check if code allready used in this CVS-file
  53.             if (!isset ($coursecodes[$user_course['CourseCode']]))
  54.             {
  55.                 //2.1.1 check if code exists in DB
  56.                 $course_table Database :: get_main_table(TABLE_MAIN_COURSE);
  57.                 $sql "SELECT * FROM $course_table WHERE code = '".mysql_real_escape_string($user_course['CourseCode'])."'";
  58.                 $res api_sql_query($sql__FILE____LINE__);
  59.                 if (mysql_num_rows($res== 0)
  60.                 {
  61.                     $user_course['error'get_lang('CodeDoesNotExists');
  62.                     $errors[$user_course;
  63.                 }
  64.                 else
  65.                 {
  66.                     $coursecodes[$user_course['CourseCode']] 1;
  67.                 }
  68.             }
  69.         }
  70.         //3. check if username exists
  71.         if (isset ($user_course['UserName']&& strlen($user_course['UserName']!= 0)
  72.         {
  73.             if (UserManager :: is_username_available($user_course['UserName']))
  74.             {
  75.                 $user_course['error'get_lang('UnknownUser');
  76.                 $errors[$user_course;
  77.             }
  78.         }
  79.         //4. check if status is valid
  80.         if (isset ($user_course['Status']&& strlen($user_course['Status']!= 0)
  81.         {
  82.             if ($user_course['Status'!= COURSEMANAGER && $user_course['Status'!= STUDENT)
  83.             {
  84.                 $user_course['error'get_lang('UnknownStatus');
  85.                 $errors[$user_course;
  86.             }
  87.         }
  88.     }
  89.     return $errors;
  90. }
  91.  
  92. /**
  93.  * Save the imported data
  94.  */
  95. function save_data($users_courses)
  96. {
  97.     $user_tableDatabase::get_main_table(TABLE_MAIN_USER);
  98.     $course_user_tableDatabase::get_main_table(TABLE_MAIN_COURSE_USER);
  99.     $csv_data array();
  100.     foreach ($users_courses as $index => $user_course)
  101.     {
  102.         $csv_data[$user_course['UserName']][$user_course['CourseCode']] $user_course['Status'];
  103.     }
  104.     foreach($csv_data as $username => $csv_subscriptions)
  105.     {
  106.         $user_id 0;
  107.         $sql "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'";
  108.         $res api_sql_query($sql,__FILE__,__LINE__);
  109.         $obj mysql_fetch_object($res);
  110.         $user_id $obj->user_id;
  111.         $sql "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id";
  112.         $res api_sql_query($sql,__FILE__,__LINE__);
  113.         $db_subscriptions array();
  114.         while($obj mysql_fetch_object($res))
  115.         {
  116.             $db_subscriptions[$obj->course_code$obj->status;
  117.         }
  118.         //echo '<b>User '.$username.'</b><br />';
  119.         $to_subscribe array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions));
  120.         $to_unsubscribe array_diff(array_keys($db_subscriptions),array_keys($csv_subscriptions));
  121. //        echo '<pre>';
  122. //        echo "CSV SUBSCRIPTION\n";
  123. //        print_r($csv_subscriptions);
  124. //        echo "DB SUBSCRIPTION\n";
  125. //        print_r($db_subscriptions);
  126. //        echo "TO SUBSCRIBE\n";
  127. //        print_r($to_subscribe);
  128. //        echo "TO UNSUBSCRIBE\n";
  129. //        print_r($to_unsubscribe);
  130. //        echo '</pre>';
  131.         if($_POST['subscribe'])
  132.         {
  133.             foreach($to_subscribe as $index => $course_code)
  134.             {
  135.                 CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]);
  136.                 echo get_lang('Subscription').' : '.$course_code.'<br />';
  137.             }
  138.         }
  139.         if($_POST['unsubscribe'])
  140.         {
  141.             foreach($to_unsubscribe as $index => $course_code)
  142.             {
  143.                 CourseManager::unsubscribe_user($user_id,$course_code);
  144.                 echo get_lang('Unsubscription').' : '.$course_code.'<br />';
  145.             }
  146.         }
  147.     }
  148. }
  149. /**
  150.  * Read the CSV-file
  151.  * @param string $file Path to the CSV-file
  152.  * @return array All course-information read from the file
  153.  */
  154. function parse_csv_data($file)
  155. {
  156.     $courses Import :: csv_to_array($file);
  157.     return $courses;
  158. }
  159. // name of the language file that needs to be included 
  160. $language_file array ('admin''registration');
  161.  
  162. $cidReset true;
  163.  
  164. include ('../inc/global.inc.php');
  165. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  166. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  167. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  168. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  169. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  170. $formSent 0;
  171. $errorMsg '';
  172.  
  173. $tool_name get_lang('AddUsersToACourse').' CSV';
  174.  
  175. $interbreadcrumb[array ("url" => 'index.php'"name" => get_lang('PlatformAdmin'));
  176.  
  177. $form new FormValidator('course_user_import');
  178. $form->addElement('file','import_file'get_lang('ImportFileLocation'));
  179. $form->addElement('checkbox','subscribe',get_lang('Action'),get_lang('SubscribeUserIfNotAllreadySubscribed'));
  180. $form->addElement('checkbox','unsubscribe','',get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  181. $form->addElement('submit','submit',get_lang('Ok'));
  182. if ($form->validate())
  183. {
  184.     $users_courses parse_csv_data($_FILES['import_file']['tmp_name']);
  185.     $errors validate_data($users_courses);
  186.     if (count($errors== 0)
  187.     {
  188.         save_data($users_courses);
  189.         header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  190.         exit ();
  191.     }
  192. }
  193. Display :: display_header($tool_name);
  194. if (count($errors!= 0)
  195. {
  196.     $error_message '<ul>';
  197.     foreach ($errors as $index => $error_course)
  198.     {
  199.         $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <b>'.$error_course['error'].'</b>: ';
  200.         $error_message .= $error_course['Code'].' '.$error_course['Title'];
  201.         $error_message .= '</li>';
  202.     }
  203.     $error_message .= '</ul>';
  204.     Display :: display_error_message($error_message);
  205. }
  206.  
  207. $form->display();
  208. ?>
  209. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'?> :</p>
  210. <blockquote>
  211. <pre>
  212. <b>UserName</b>;<b>CourseCode</b>;<b>Status</b>
  213. jdoe;course01;<?php echo COURSEMANAGER?>
  214.  
  215. a.dam;course01;<?php echo STUDENT?>
  216. </pre>
  217. <?php
  218. echo COURSEMANAGER.': '.get_lang('Teacher').'<br />';
  219. echo STUDENT.': '.get_lang('Student').'<br />';
  220. ?>
  221. </blockquote>
  222. <?php
  223. /*
  224. ==============================================================================
  225.         FOOTER
  226. ==============================================================================
  227. */
  228. ?>

Documentation generated on Thu, 12 Jun 2008 13:16:26 -0500 by phpDocumentor 1.4.1