From 50d80b4268c2be064ecce34c2598ff255b236fc0 Mon Sep 17 00:00:00 2001 From: Cristian Fasanando Date: Fri, 18 Dec 2009 11:09:53 -0500 Subject: [PATCH] Fixed imports for users and courses and import xls with filter in exercise tool - partial DT#5666 --- main/admin/course_import.php | 24 +++-- main/admin/course_user_import.php | 120 ++++++++++++++++-------- main/admin/user_import.php | 81 ++++++++-------- main/exercice/exercice.php | 4 +- main/exercice/exercice_submit.php | 4 +- main/exercice/exercise_result.class.php | 65 +++++++++++-- main/inc/lib/events.lib.inc.php | 53 +++++------ 7 files changed, 226 insertions(+), 125 deletions(-) diff --git a/main/admin/course_import.php b/main/admin/course_import.php index 0f6c89346f..8e6f72a0af 100644 --- a/main/admin/course_import.php +++ b/main/admin/course_import.php @@ -89,7 +89,7 @@ function validate_data($courses) { $sql = "SELECT * FROM $category_table WHERE code = '".Database::escape_string($course['CourseCategory'])."'"; $res = Database::query($sql, __FILE__, __LINE__); if (Database::num_rows($res) == 0) { - $course['error'] = get_lang('UnkownCategory').' ('.$course['CourseCategory'].')'; + $course['error'] = get_lang('UnkownCategoryCourseCode').' ('.$course['CourseCategory'].')'; $errors[] = $course; } } @@ -197,15 +197,19 @@ if ($_POST['formSent']) { if (empty($_FILES['import_file']['tmp_name'])) { $error_message = get_lang('UplUploadFailed'); Display :: display_error_message($error_message, false); - } else { - $file_type = $_POST['file_type']; - $courses = parse_csv_data($_FILES['import_file']['tmp_name']); - $errors = validate_data($courses); - if (count($errors) == 0) { - //$users = complete_missing_data($courses); - save_data($courses); - //header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported'))); - //exit (); + } else { + $allowed_file_mimetype = array('csv'); + + $ext_import_file = substr($_FILES['import_file']['name'],(strrpos($_FILES['import_file']['name'],'.')+1)); + + if (!in_array($ext_import_file,$allowed_file_mimetype)) { + Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption')); + } else { + $courses = parse_csv_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($courses); + if (count($errors) == 0) { + save_data($courses); + } } } } diff --git a/main/admin/course_user_import.php b/main/admin/course_user_import.php index ddfe9848fd..85c171331a 100644 --- a/main/admin/course_user_import.php +++ b/main/admin/course_user_import.php @@ -66,24 +66,40 @@ function save_data($users_courses) { $user_table= Database::get_main_table(TABLE_MAIN_USER); $course_user_table= Database::get_main_table(TABLE_MAIN_COURSE_USER); $csv_data = array(); + foreach ($users_courses as $index => $user_course) { $csv_data[$user_course['UserName']][$user_course['CourseCode']] = $user_course['Status']; } + foreach($csv_data as $username => $csv_subscriptions) { + + $user_id = 0; $sql = "SELECT * FROM $user_table u WHERE u.username = '".Database::escape_string($username)."'"; $res = Database::query($sql, __FILE__, __LINE__); $obj = Database::fetch_object($res); $user_id = $obj->user_id; - $sql = "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id"; - $res = Database::query($sql, __FILE__, __LINE__); - $db_subscriptions = array(); - while($obj = Database::fetch_object($res)) { - $db_subscriptions[$obj->course_code] = $obj->status; + $to_subscribe = $to_unsubscribe = array(); + + if ($_POST['subscribe']) { + $sql = "SELECT * FROM $course_user_table WHERE user_id = $user_id "; + $res_suscribe = Database::query($sql, __FILE__, __LINE__); + $db_subscriptions = array(); + while($obj_suscribe = Database::fetch_object($res_suscribe)) { + $db_subscriptions[$obj_suscribe->course_code] = $obj_suscribe->user_id; + } + $to_subscribe = array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions)); } - - $to_subscribe = array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions)); - $to_unsubscribe = array_diff(array_keys($db_subscriptions),array_keys($csv_subscriptions)); + + if ($_POST['unsubscribe']) { + $sql = "SELECT * FROM $course_user_table WHERE user_id NOT IN ($user_id) "; + $res_unsubscribe = Database::query($sql, __FILE__, __LINE__); + $db_unsubscriptions = array(); + while($obj_unsubscribe = Database::fetch_object($res_unsubscribe)) { + $db_unsubscriptions[$obj_unsubscribe->course_code] = $obj_unsubscribe->user_id; + } + $to_unsubscribe = $db_unsubscriptions; + } global $inserted_in_course; if (!isset($inserted_in_course)) { @@ -92,8 +108,9 @@ function save_data($users_courses) { if($_POST['subscribe']) { foreach($to_subscribe as $index => $course_code) { if(CourseManager :: course_exists($course_code)) { + CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]); - $course_info = CourseManager::get_course_information($course_code); + $course_info = CourseManager::get_course_information($course_code); $inserted_in_course[$course_code] = $course_info['title']; } if (CourseManager :: course_exists($course_code,true)) { @@ -108,10 +125,12 @@ function save_data($users_courses) { } } } - } + } } + if($_POST['unsubscribe']) { - foreach($to_unsubscribe as $index => $course_code) { + + foreach($to_unsubscribe as $course_code => $user_id) { if(CourseManager :: course_exists($course_code)) { CourseManager::unsubscribe_user($user_id,$course_code); $course_info = CourseManager::get_course_information($course_code); @@ -174,39 +193,66 @@ set_time_limit(0); // Creating the form. $form = new FormValidator('course_user_import'); $form->addElement('header', '', $tool_name); -$form->addElement('file', 'import_file', get_lang('ImportFileLocation')); +$form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation')); $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed')); $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile')); $form->addElement('style_submit_button', 'submit',get_lang('Import'),'class="save"'); -if ($form->validate()) { - $users_courses = parse_csv_data($_FILES['import_file']['tmp_name']); - $errors = validate_data($users_courses); - if (count($errors) == 0) { - $inserted_in_course = array(); - save_data($users_courses); - // Build the alert message in case there were visual codes subscribed to. - if ($_POST['subscribe']) { - $warn = get_lang('UsersSubscribedToBecauseVisualCode').': '; - } else { - $warn = get_lang('UsersUnsubscribedFromBecauseVisualCode').': '; - } - if (count($inserted_in_course) > 1) { - // The users have been inserted in more than one course. - foreach ($inserted_in_course as $code => $info) { - $warn .= ' '.$info.' ('.$code.'),'; - } - $warn = substr($warn,0,-1); - } - Security::clear_token(); - $tok = Security::get_token(); - header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($warn).'&sec_token='.$tok); - exit (); - } + +$error_message = $message = ''; +$inserted_in_course = array(); +if ($form->validate() && $_FILES['import_file']['size'] !== 0) { + + + $allowed_file_mimetype = array('csv'); + $ext_import_file = substr($_FILES['import_file']['name'],(strrpos($_FILES['import_file']['name'],'.')+1)); + + if (!in_array($ext_import_file,$allowed_file_mimetype)) { + $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption'); + } else if (!isset($_POST['subscribe']) && !isset($_POST['unsubscribe'])) { + $error_message = get_lang('YouMustSelectAnAction'); + } else { + + $users_courses = parse_csv_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($users_courses); + + if (count($errors) == 0) { + save_data($users_courses); + // Build the alert message in case there were visual codes subscribed to. + $warn = ''; + if (count($inserted_in_course) > 1) { + if ($_POST['subscribe']) { + $warn = get_lang('UsersSubscribedToBecauseVisualCode').': '; + } else { + $warn = get_lang('UsersUnsubscribedFromBecauseVisualCode').': '; + } + // The users have been inserted in more than one course. + foreach ($inserted_in_course as $code => $info) { + $warn .= ' '.$info.' ('.$code.'),'; + } + $warn = substr($warn,0,-1); + } + + Security::clear_token(); + $tok = Security::get_token(); + $message = get_lang('FileImported'); + //header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($warn).'&sec_token='.$tok); + //exit (); + } + } } // Displaying the header. Display :: display_header($tool_name); +if (!empty($error_message)) { + Display :: display_error_message($error_message); +} else if (!empty($message)) { + if (!empty($warn)) { + Display :: display_warning_message($warn); + } + Display :: display_normal_message($message); +} + // Displaying the tool title. // api_display_tool_title($tool_name); @@ -218,7 +264,7 @@ if (count($errors) != 0) { $error_message .= ''; } $error_message .= ''; - Display :: display_error_message($error_message); + Display :: display_error_message($error_message,false); } // Displaying the form. diff --git a/main/admin/user_import.php b/main/admin/user_import.php index 3d14c77f6d..600b57bcfd 100644 --- a/main/admin/user_import.php +++ b/main/admin/user_import.php @@ -280,25 +280,34 @@ if (is_array($extAuthSource)) { } $tool_name = get_lang('ImportUserListXMLCSV'); - $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin')); set_time_limit(0); $extra_fields = Usermanager::get_extra_fields(0, 0, 5, 'ASC', false); $user_id_error = array(); +$error_message = ''; + if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { $file_type = $_POST['file_type']; Security::clear_token(); $tok = Security::get_token(); - - if (strcmp($file_type, 'csv') === 0) { //&& strcmp($_FILES['import_file']['type'],'text/'.$file_type.'')===0) { - $users = parse_csv_data($_FILES['import_file']['tmp_name']); - $errors = validate_data($users); - $error_kind_file = false; - } elseif (strcmp($file_type, 'xml') === 0) { // && strcmp($_FILES['import_file']['type'],'text/'.$file_type.'')===0) { - $users = parse_xml_data($_FILES['import_file']['tmp_name']); - $errors = validate_data($users); - $error_kind_file = false; + $allowed_file_mimetype = array('csv','xml'); + $error_kind_file = false; + + $ext_import_file = substr($_FILES['import_file']['name'],(strrpos($_FILES['import_file']['name'],'.')+1)); + + if (in_array($ext_import_file,$allowed_file_mimetype)) { + if (strcmp($file_type, 'csv') === 0 && $ext_import_file==$allowed_file_mimetype[0]) { + $users = parse_csv_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($users); + $error_kind_file = false; + } elseif (strcmp($file_type, 'xml') === 0 && $ext_import_file==$allowed_file_mimetype[1]) { + $users = parse_xml_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($users); + $error_kind_file = false; + } else { + $error_kind_file = true; + } } else { $error_kind_file = true; } @@ -326,8 +335,6 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { save_data($users_to_insert); } else { $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption'); - header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_import.php?warn='.urlencode($error_message).'&file_type='.$file_type.'&sec_token='.$tok); - exit; } if (count($errors) > 0) { @@ -335,6 +342,7 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { } else { $see_message_import = get_lang('FileImported'); } + /* $msg2 = ''; if (count($inserted_in_course) > 1) { $msg2 .= '
'.get_lang('UsersSubscribedToSeveralCoursesBecauseOfVirtualCourses').':'; @@ -344,47 +352,42 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { $msg2 = substr($msg2, 0, -1); $msg2 .= '
'; } - + */ + if (count($errors) != 0) { - $error_message = ''; + } + + // if the warning message is too long then we display the warning message trough a session + if (api_strlen($warning_message) > 150) { + $_SESSION['session_message_import_users'] = $warning_message; + $warning_message = 'session_message'; + } + + if ($error_kind_file) { + $error_message = get_lang('YouMustImportAFileAccordingToSelectedOption'); + } else { + header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=show_message&warn='.urlencode($warning_message).'&message='.urlencode($see_message_import).'&sec_token='.$tok); + exit; } - // if the warning message is too long then we display the warning message trough a session - if (api_strlen($error_message) > 150) { - $_SESSION['session_message_import_users'] = $error_message; - $error_message = 'session_message'; - } - header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=show_message&message='.urlencode($see_message_import).'&warn='.urlencode($error_message).'&sec_token='.$tok); - exit; } - Display :: display_header($tool_name); -//api_display_tool_title($tool_name); - -if ($_FILES['import_file']['size'] == 0 AND $_POST) { - Display::display_error_message(get_lang('ThisFieldIsRequired')); -} -if ($error_kind_file === true) { - Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption')); -} else if (isset($_GET['warn'])) { - $error_message = Security::remove_XSS($_GET['warn']); - Display :: display_error_message($error_message); +if (!empty($error_message)) { + Display::display_error_message($error_message); } $form = new FormValidator('user_import','post','user_import.php'); $form->addElement('header', '', $tool_name); $form->addElement('hidden', 'formSent'); $form->addElement('file', 'import_file', get_lang('ImportFileLocation')); -//$form->addRule('import_file', get_lang('ThisFieldIsRequired'), 'required'); // This rule does not work, probably due to the security mechanism here. -$allowed_file_types = array ('xml', 'csv'); -//$form->addRule('import_file', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types); // This rule does not work, probably due to the security mechanism here. $form->addElement('radio', 'file_type', get_lang('FileType'), 'XML ('.get_lang('ExampleXMLFile').')', 'xml'); $form->addElement('radio', 'file_type', null, 'CSV ('.get_lang('ExampleCSVFile').')', 'csv'); $form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1); diff --git a/main/exercice/exercice.php b/main/exercice/exercice.php index eaa9bae674..17b92d8591 100644 --- a/main/exercice/exercice.php +++ b/main/exercice/exercice.php @@ -471,7 +471,7 @@ if (!empty ($_POST['export_report']) && $_POST['export_report'] == 'export_repor switch ($_POST['export_format']) { case 'xls' : $export = new ExerciseResult(); - $export->exportCompleteReportXLS($documentPath, $user_id, $_SESSION['export_user_fields']); + $export->exportCompleteReportXLS($documentPath, $user_id, $_SESSION['export_user_fields'], $_POST['export_filter']); exit; break; case 'csv' : @@ -795,9 +795,11 @@ if (($is_allowedToEdit) and ($origin != 'learnpath')) { echo '
'; echo ''; echo ''; + echo ''; echo '
'; echo '
'; echo ''; + echo ''; echo ''; echo '
'; //echo '
'; diff --git a/main/exercice/exercice_submit.php b/main/exercice/exercice_submit.php index 216cbb1d13..b93bef162d 100644 --- a/main/exercice/exercice_submit.php +++ b/main/exercice/exercice_submit.php @@ -1220,8 +1220,8 @@ if ($_configuration['live_exercise_tracking'] == true && $exerciseFeedbackType ! } if ($exerciseType == 2) { - $sql = "INSERT INTO $stat_table($sql_fields exe_exo_id,exe_user_id,exe_cours_id,status,session_id,data_tracking,start_date,orig_lp_id,orig_lp_item_id,exe_duration) - VALUES($sql_fields_values '$exerciseId','" . api_get_user_id() . "','" . $_course['id'] . "','incomplete','" . api_get_session_id() . "','" . implode(',', $questionList) . "','" . date('Y-m-d H:i:s') . "',$safe_lp_id,$safe_lp_item_id,)"; + $sql = "INSERT INTO $stat_table($sql_fields exe_exo_id,exe_user_id,exe_cours_id,status,session_id,data_tracking,start_date,orig_lp_id,orig_lp_item_id) + VALUES($sql_fields_values '$exerciseId','" . api_get_user_id() . "','" . $_course['id'] . "','incomplete','" . api_get_session_id() . "','" . implode(',', $questionList) . "','" . date('Y-m-d H:i:s') . "',$safe_lp_id,$safe_lp_item_id)"; Database::query($sql, __FILE__, __LINE__); } else { $sql = "INSERT INTO $stat_table ($sql_fields exe_exo_id,exe_user_id,exe_cours_id,status,session_id,start_date,orig_lp_id,orig_lp_item_id) diff --git a/main/exercice/exercise_result.class.php b/main/exercice/exercise_result.class.php index 7bb3770051..1a7263f845 100644 --- a/main/exercice/exercise_result.class.php +++ b/main/exercice/exercise_result.class.php @@ -109,7 +109,7 @@ class ExerciseResult * @param string The document path (for HotPotatoes retrieval) * @param integer User ID. Optional. If no user ID is provided, we take all the results. Defauts to null */ - function _getExercisesReporting($document_path,$user_id=null) + function _getExercisesReporting($document_path,$user_id=null,$filter=0) { $return = array(); $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST); @@ -160,12 +160,42 @@ class ExerciseResult $NoTestRes = 0; $NoHPTestRes = 0; - $j=0; + $j=0; + + if ($filter) { + switch ($filter) { + case 1 : + $filter_by_not_revised = true; + break; + case 2 : + $filter_by_revised = true; + break; + default : + null; + } + } + //Print the results of tests if(is_array($results)) { for($i = 0; $i < sizeof($results); $i++) { + + $revised = false; + $sql_exe = 'SELECT exe_id FROM ' . Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING) . ' + WHERE author != ' . "''" . ' AND exe_id = ' . "'" . Database :: escape_string($results[$i][5]) . "'" . ' LIMIT 1'; + $query = Database::query($sql_exe, __FILE__, __LINE__); + + if (Database :: num_rows($query) > 0) { + $revised = true; + } + if ($filter_by_not_revised && $revised == true) { + continue; + } + if ($filter_by_revised && $revised == false) { + continue; + } + $return[$i] = array(); $id = $results[$i][5]; $mailid = $results[$i][6]; @@ -220,10 +250,10 @@ class ExerciseResult * @param boolean Whether to include user fields or not * @return boolean False on error */ - public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields) + public function exportCompleteReportCSV($document_path='',$user_id=null, $export_user_fields = array(), $export_filter = 0) { global $charset; - $this->_getExercisesReporting($document_path,$user_id); + $this->_getExercisesReporting($document_path,$user_id,$export_filter); $filename = 'exercise_results_'.date('YmdGis').'.csv'; if(!empty($user_id)) { @@ -302,11 +332,18 @@ class ExerciseResult * Exports the complete report as an XLS file * @return boolean False on error */ - public function exportCompleteReportXLS($document_path='',$user_id=null, $export_user_fields) + public function exportCompleteReportXLS($document_path='',$user_id=null, $export_user_fields=array(), $export_filter = 0) { global $charset; - $this->_getExercisesReporting($document_path,$user_id); + + + + $this->_getExercisesReporting($document_path,$user_id,$export_filter); $filename = 'exercise_results_'.date('YmdGis').'.xls'; + + + + if(!empty($user_id)) { $filename = 'exercise_results_user_'.$user_id.'_'.date('YmdGis').'.xls'; @@ -317,11 +354,21 @@ class ExerciseResult $worksheet =& $workbook->addWorksheet('Report '.date('YmdGis')); $line = 0; $column = 0; //skip the first column (row titles) - if(!empty($this->results[0]['user'])) - { + + // check if exists column 'user' + $with_column_user = false; + foreach ($this->results as $result) { + if (!empty($result['user'])) { + $with_column_user = true; + break; + } + } + + if($with_column_user) { $worksheet->write($line,$column,get_lang('User')); $column++; } + if($export_user_fields) { //show user fields section with a big th colspan that spans over all fields @@ -342,6 +389,8 @@ class ExerciseResult $worksheet->write($line,$column,get_lang('Weighting')); $line++; + + foreach($this->results as $row) { $column = 0; diff --git a/main/inc/lib/events.lib.inc.php b/main/inc/lib/events.lib.inc.php index 9f8f991763..08d74308a6 100644 --- a/main/inc/lib/events.lib.inc.php +++ b/main/inc/lib/events.lib.inc.php @@ -398,18 +398,18 @@ function update_event_exercice($exeid,$exo_id, $score, $weighting,$session_id,$l { if ($exeid!='') { - $current_time = time(); - if (isset($_SESSION['expired_time'])) { //Only for exercice of type "One page" - $expired_date = $_SESSION['expired_time']; - $expired_time = strtotime($expired_date); - } + //Validation in case of fraud + if (isset($_SESSION['expired_time'])) { //Only for exercice of type "One page" + $current_time = time(); + $expired_date = $_SESSION['expired_time']; + $expired_time = strtotime($expired_date); + + $total_time_allowed = $expired_time + 30; + if ($total_time_allowed < $current_time) { + $score = 0; + } + } - //Validation in case of fraud - $total_time_allowed = $expired_time + 30; - if ($total_time_allowed < $current_time) { - $score = 0; - } - $TABLETRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES); $reallyNow = time(); $sql = "UPDATE $TABLETRACK_EXERCICES SET @@ -494,20 +494,17 @@ function exercise_attempt($score,$answer,$quesId,$exeId,$j) global $_configuration, $_user, $_cid; $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); - $current_time = time(); - + //Validation in case of fraud if (isset($_SESSION['expired_time'])) { //Only for exercice of type "One page" + $current_time = time(); $expired_date = $_SESSION['expired_time']; $expired_time = strtotime($expired_date); - } - - //Validation in case of fraud - $total_time_allowed = $expired_time + 30; - - if ($total_time_allowed < $current_time) { - $score = 0; - $answer = 0; - $j = 0; + $total_time_allowed = $expired_time + 30; + if ($total_time_allowed < $current_time) { + $score = 0; + $answer = 0; + $j = 0; + } } // if tracking is disabled record nothing @@ -587,17 +584,17 @@ function exercise_attempt_hotspot($exe_id, $question_id, $answer_id, $correct, $ return 0; } - $current_time = time(); + //Validation in case of fraud if (isset($_SESSION['expired_time'])) { //Only for exercice of type "One page" + $current_time = time(); $expired_date = $_SESSION['expired_time']; $expired_time = strtotime($expired_date); + $total_time_allowed = $expired_time + 30; + if ($total_time_allowed < $current_time) { + $correct = 0; + } } - //Validation in case of fraud - $total_time_allowed = $expired_time + 30; - if ($total_time_allowed < $current_time) { - $correct = 0; - } $tbl_track_e_hotspot = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_HOTSPOT); $sql = "INSERT INTO $tbl_track_e_hotspot " . "(hotspot_user_id, hotspot_course_code, hotspot_exe_id, hotspot_question_id, hotspot_answer_id, hotspot_correct, hotspot_coordinate)".