Commit 0b7d983e authored by User expired's avatar User expired
Browse files

added functionality for deletion of old tests

parent beb84cd2
curl -L -b $2/headers --data "mode=4&sel=1&test_id=$1&display_mode=0&order_field=total_score%2C+user_lastname%2C+user_firstname&orderdir=0" -G -v $3/admin/code/tce_pdf_results.php > $2/backup_$1.pdf
curl -L -b $2/headers --data "sel=1&test_id=$1&display_mode=0&order_field=total_score%2C+user_lastname%2C+user_firstname&orderdir=0" $3/admin/code/tce_tsv_result_allusers.php > $2/backup_$1.tsv
curl -d "xuser_name=backoffice&xuser_password=123backoffice45&logaction=login" --dump-header $2/headers $3/admin/code/index.php
<?php
//============================================================+
// File name : delete_old_tests.php
// Begin : 2016-08-05
// Last Update : 201y-mm-dd
//
// Description : deletion routine for older tests
//
// Author: Ewald Strohmar-Mauler
//
//============================================================+
require_once('../config/delete_old_tests_ini.php');
require_once('../config/tce_config.php');
//all the other necessary require_once's are called in tcexam's login routine when we call index.php in background
$pagelevel = K_AUTH_ADMIN_TESTS;
$server = 'http://'.K_UIBK_SERVER_NAME.':'.K_UIBK_SERVER_PORT;
$arr_log = array();
$logfile= K_UIBK_BACKUP_TESTS_FOLDER. '/backup_'. date(DATE_W3C).'.log';
$mailtext="";
//command line argument *--override_pdf_errors*: ignore pdf errors and delete test results nevertheless
if (!isset($argv[1])) $override = FALSE;
strtolower($argv[1]) == "--override_pdf_errors" ? $override = TRUE : $override = FALSE;
if ($argv[1] == "--help" ) {echo "Usage: php ". __FILE__ ." [--override_pdf_errors|--help]\n\n"; exit;}
$sqlq = 'SELECT test_id, test_name FROM '. K_TABLE_TESTS;
$sqlq.= ' WHERE test_end_time < current_date - ' .intval(K_UIBK_DELETION_INTERVAL_1);
$sqlq.= ' AND test_id IN (SELECT testuser_test_id FROM '. K_TABLE_TEST_USER. ')'; //only tests with results
$sqlq.= ' ORDER BY test_id;';
if($r = F_db_query($sqlq, $db)) {
//first, login to tcexam (just once, b/c there's a bruteforce prevention for multiple logins within a short time period)
$detailed ? error_log('exec '.dirname(__FILE__) .'/backup_tests_login 0 '. K_UIBK_BACKUP_TESTS_FOLDER. ' ' .$server."\n",3,$logfile):"";
exec(dirname(__FILE__) .'/backup_tests_login 0 '. K_UIBK_BACKUP_TESTS_FOLDER. ' ' .$server." > /dev/null", $arr_log, $ret);
if ($ret > 0) {
$arrlog = print_r($arr_log, true);
error_log("\nGot error ".$ret.'/'.$arrlog."\n",3,$logfile);
error_notification_email("error in login procedure".$ret.'/'.$arrlog."<br />");
exit;
}
while($m = F_db_fetch_array($r)){
unset($ret);
$test_id = $m['test_id'];
$test_name = $m['test_name'];
//only process tests without unanswered questions
$num_unanswered = text_questions_corrected($test_id);
error_log("\nprocessing test ". $test_id." / ". $test_name. " - " . date(DATE_RFC2822)."\n", 3, $logfile);
if ($num_unanswered == 0){
//backup as tsv and pdf
$detailed ? error_log('exec '.dirname(__FILE__) .'/backup_tests_backup '.$test_id.' '.K_UIBK_BACKUP_TESTS_FOLDER. ' ' .$server."\n", 3, $logfile):"";
exec(dirname(__FILE__) .'/backup_tests_backup '.$test_id.' '.K_UIBK_BACKUP_TESTS_FOLDER. ' ' .$server." > /dev/null", $arr_log, $ret);
$ret = pdf_error($test_id,$override); //check for erroneaous output
if ($ret > 0) {
switch($ret) {
case 18: //timeout
$errtext = "Got timeout condition for test ".$test_id." / ". $test_name."\n";
error_log($errtext,3,$logfile);
$detailed ? error_log("Sending information email",3,$logfile):"";
$mailtext .= "\n".$errtext;
break;
case 2: //pictures missing
$errtext = "Some pictures are missing for test ".$test_id." / ". $test_name."\n";
error_log($errtext,3,$logfile);
$detailed ? error_log("Sending information email",3,$logfile):"";
$mailtext .= "\n".$errtext;
break;
default: //other error
$arrlog = print_r($arr_log, true);
$errtext = "Got a yet uncaught PDFlib error ".$ret." for test ".$test_id." / ". $test_name.'/'.$arrlog."\n";
error_log($errtext,3,$logfile);
$detailed ? error_log("Sending information email",3,$logfile):"";
$mailtext .= "\n".$errtext;
break;
}
}
else { //delete test results only if no error occurred
$sqldel = 'DELETE FROM '. K_TABLE_TEST_USER;
$sqldel.= ' WHERE testuser_test_id = '.$test_id;
if (F_db_query($sqldel, $db)) {
error_log("Now deleting test ". $test_id." / ". $test_name. ' - with '.$sqldel."\n",3,$logfile);
} else {
$dberr = F_display_db_error();
$errtext = "Got database error ".$dberr."\n";
error_log($errtext,3,$logfile);
$mailtext .= "\n".$errtext;
}
}
}
else {
$errtext = "Test ".$test_id." / ". $test_name." has unanswered text questions\n";
error_log($errtext,3,$logfile);
$detailed ? error_log("Sending information email",3,$logfile):"";
$mailtext .= "\n".$errtext;
}
}//end while
//delete all according to interval_2
//deletes complete test, not only results
$sqldel = 'DELETE FROM '. K_TABLE_TESTS;
$sqldel.= ' WHERE test_end_time < current_date - ' .intval(K_UIBK_DELETION_INTERVAL_2);
$sqldel.= ' RETURNING *;';
if (F_db_query($sqldel, $db)) {
error_log("\nAnd finally... ".$sqldel."\n",3,$logfile);
} else {
$dberr = F_display_db_error();
$errtext = "Got database error ".$dberr."\n";
error_log($errtext,3,$logfile);
$mailtext .= "\n".$errtext;
}
} else {
$dberr = F_display_db_error();
$errtext = "Got database error ".$dberr."\n";
error_log($errtext,3,$logfile);
$mailtext .= "\n".$errtext;
}
$arrlog = print_r($arr_log, true);
$detailed?error_log("\nIn the end, we got return code ".$ret."\nand arr_log is ".$arrlog."\n",3,$logfile):"";
error_notification_email($mailtext); //if empty, we know everything was o.k.
//delete header file
unlink(K_UIBK_BACKUP_TESTS_FOLDER."/headers");
exit; // end of main
/*
* Returns number of unanswered text questions
*/
function text_questions_corrected($test_id) {
global $db;
require_once('../config/tce_config.php');
$sql = 'SELECT count(*) FROM (SELECT testlog_id FROM '.K_TABLE_TESTS.', '.K_TABLE_TEST_USER.', '.K_TABLE_TESTS_LOGS.', '.K_TABLE_QUESTIONS.' ';
$sql .= ' WHERE testuser_test_id=test_id AND testlog_testuser_id=testuser_id AND testlog_question_id=question_id ';
$sql .= ' AND testuser_test_id='.$test_id.' ';
$sql .= ' AND question_type=3 AND testlog_score IS NULL) as number_of_unanswered_text_questions_for_this_test;';
$sql .= '';
if($r = F_db_query($sql, $db)) {
if($m = F_db_fetch_array($r)) {
return $m[0];
}
} else {
$dberr = F_display_db_error();
error_log("\nGot database error ".$dberr."\n",3,$logfile);
}
}//end function
/*
*simulates grep "Some data has already been output, can't send PDF file" *.pdf
*/
function pdf_error($test_id,$override){
if ($override) return 0;
$file = K_UIBK_BACKUP_TESTS_FOLDER."/backup_".$test_id.".pdf";
$contents = file_get_contents($file);
//Array for possible errors and their respective return values
$searchfor = [
["504 Gateway Time-out" , 18],
["FEHLER: [2] imagecreatefrom", 2]
];
foreach($searchfor as list ($errtext,$retval)) {
$pattern = preg_quote($errtext, '/'); // escape special characters in the query
$pattern = "/^.*$pattern.*\$/m"; // finalise the regular expression, matching the whole line
if(preg_match($pattern, $contents, $matches)==1){
return $retval;
}
}
return 0;
}//end function
/*
* error notification
*/
function error_notification_email($mailtext){
$mailreceiver = K_UIBK_EMAIL_RECEIVER;
$mailsubject = "eExams: error report from test deletion routine";
$mailheader = K_UIBK_EMAIL_HEADER; // . something else?
mail($mailreceiver, $mailsubject, $mailtext, $mailheader);
}
?>
......@@ -232,7 +232,6 @@ else {
F_display_db_error();
}
echo '</select>'.K_NEWLINE;
// link for test selection popup
$jsaction = 'selectWindow=window.open(\'tce_select_tests_popup.php?cid=test_id\', \'selectWindow\', \'dependent, height=600, width=800, menubar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no\');return false;';
echo '<a href="#" onclick="'.$jsaction.'" class="xmlbutton" title="'.$l['w_select'].'">...</a>';
......@@ -253,7 +252,7 @@ echo '<span class="formw">'.K_NEWLINE;
echo '<select name="group_id" id="group_id" size="0">'.K_NEWLINE;
//show only groups the user belongs to
$sql = 'SELECT g.group_name, g.group_id FROM '. K_TABLE_GROUPS . ' g, '. K_TABLE_USERGROUP .' u WHERE u.usrgrp_group_id = g.group_id ';
if ($_SESSION['session_user_level'] < 10) { //not admin
if ($_SESSION['session_user_level'] < K_AUTH_BACKOFFICE) { //not admin
$sql .= ' AND u.usrgrp_user_id = '. $_SESSION['session_user_id'];
} else {
$sql .= ' GROUP BY g.group_name, g.group_id ';
......@@ -296,7 +295,7 @@ if ($test_id > 0) {
$sql .= ', '.K_TABLE_TEST_USER.' WHERE testuser_user_id=user_id AND testuser_test_id='.$test_id.'';
} elseif ($group_id > 0) {
$sql .= ', '.K_TABLE_USERGROUP.' WHERE usrgrp_user_id=user_id AND usrgrp_group_id='.$group_id.' AND user_id>1';
} elseif ($_SESSION['session_user_level'] < 10) { //show only users from groups the user belongs to if not admin
} elseif ($_SESSION['session_user_level'] < K_AUTH_BACKOFFICE) { //show only users from groups the user belongs to if not admin
$sqli = 'SELECT usrgrp_group_id FROM ' . K_TABLE_USERGROUP . ' WHERE usrgrp_user_id = ' . $_SESSION['session_user_id'];
$sql .= ', '.K_TABLE_USERGROUP. ' WHERE usrgrp_user_id = user_id AND usrgrp_group_id in('. $sqli . ') ';
}
......@@ -451,4 +450,4 @@ require_once('../code/tce_page_footer.php');
//============================================================+
// END OF FILE
//============================================================+
\ No newline at end of file
//============================================================+
<?php
//============================================================+
// File name : delete_old_tests_ini.php
// Begin : 2016-08-05
// Last Update :
//
// Description : configuration file for deletion routine for older tests
//
// Author: Ewald Strohmar-Mauler
//
//============================================================+
/**
* server's ip or name and port
*/
define ('K_UIBK_SERVER_NAME', 'localhost');
#define ('K_UIBK_SERVER_PORT', '8888');
//workaround for shell call - would normally be provided by web call
$_SERVER['SERVER_NAME'] = K_UIBK_SERVER_NAME;
#$_SERVER['SERVER_PORT'] = K_UIBK_SERVER_PORT;
/**
* intervals in days after which older tests are backed up (1) and deleted (2)
* interval_2 must be greater than interval_1
*/
define ('K_UIBK_DELETION_INTERVAL_1', 60);
define ('K_UIBK_DELETION_INTERVAL_2', 390);
/**
* folder location for backups of older tests
*/
define ('K_UIBK_BACKUP_TESTS_FOLDER', '/somedir/testresults');
/**
* settings for email notification
*/
define ('K_UIBK_EMAIL_RECEIVER', 'john.doe@mydomain.xyz');
define ('K_UIBK_EMAIL_HEADER', "From: sender@mydomain.xyz\r\nReply-To: noreply@mydomain.xyz");
/*
* detailed log entries, include used exec statements, etc.
*/
$detailed=FALSE;
......@@ -38,7 +38,7 @@ function F_select_modules_sql($andwhere='') {
global $l;
require_once('../config/tce_config.php');
$sql = 'SELECT * FROM '.K_TABLE_MODULES.'';
if ($_SESSION['session_user_level'] >= K_AUTH_ADMINISTRATOR) {
if ($_SESSION['session_user_level'] >= K_AUTH_BACKOFFICE) {
if (!empty($andwhere)) {
$sql .= ' WHERE '.$andwhere;
}
......@@ -75,7 +75,7 @@ function F_select_module_subjects_sql($andwhere='') {
require_once('../config/tce_config.php');
$sql = 'SELECT * FROM '.K_TABLE_MODULES.','.K_TABLE_SUBJECTS.'';
$sql .= ' WHERE module_id=subject_module_id';
if ($_SESSION['session_user_level'] < K_AUTH_ADMINISTRATOR) {
if ($_SESSION['session_user_level'] < K_AUTH_BACKOFFICE) {
$authorized_users = F_getAuthorizedUsers($_SESSION['session_user_id']);
$sql .= ' AND (module_user_id IN ('.$authorized_users.') OR subject_user_id IN ('.$authorized_users.'))';
}
......@@ -96,7 +96,7 @@ function F_select_tests_sql() {
global $l;
require_once('../config/tce_config.php');
$sql = 'SELECT * FROM '.K_TABLE_TESTS.'';
if ($_SESSION['session_user_level'] < K_AUTH_ADMINISTRATOR) {
if ($_SESSION['session_user_level'] < K_AUTH_BACKOFFICE) {
$sql .= ' WHERE test_user_id IN ('.F_getAuthorizedUsers($_SESSION['session_user_id']).')';
}
$sql .= ' ORDER BY test_begin_time DESC, test_name';
......@@ -119,7 +119,7 @@ function F_select_executed_tests_sql() {
FROM '.K_TABLE_TEST_USER.'
WHERE testuser_status>0
)';
if ($_SESSION['session_user_level'] < K_AUTH_ADMINISTRATOR) {
if ($_SESSION['session_user_level'] < K_AUTH_BACKOFFICE) {
$sql .= ' AND test_user_id IN ('.F_getAuthorizedUsers($_SESSION['session_user_id']).')';
}
$sql .= ' ORDER BY test_begin_time DESC, test_name';
......
......@@ -189,7 +189,7 @@ function F_isAuthorizedUser($table, $field_id_name, $value_id, $field_user_id) {
$field_user_id = F_escape_sql($db, $field_user_id);
$user_id = intval($_SESSION['session_user_id']);
// check for administrator
if (defined('K_AUTH_ADMINISTRATOR') AND ($_SESSION['session_user_level'] >= K_AUTH_ADMINISTRATOR)) {
if (defined('K_AUTH_BACKOFFICE') AND ($_SESSION['session_user_level'] >= K_AUTH_BACKOFFICE)) {
return true;
}
// check for original author
......
......@@ -1084,7 +1084,7 @@ function F_getAllUsersTestStat($test_id, $group_id=0, $user_id=0, $startdate=0,
$sqlr .= ' AND user_id='.$user_id.'';
}
// use only data from groups session_user is a member of
if ($_SESSION ['session_user_level'] < K_AUTH_ADMINISTRATOR) {
if ($_SESSION ['session_user_level'] < K_AUTH_BACKOFFICE) {
$sqlin = 'SELECT tstgrp_test_id FROM ' . K_TABLE_TEST_GROUPS . ' WHERE tstgrp_group_id IN (SELECT usrgrp_group_id FROM ' . K_TABLE_USERGROUP . ' LEFT JOIN '. K_TABLE_GROUPS.' ON usrgrp_group_id = group_id WHERE usrgrp_user_id = ' . $_SESSION ['session_user_id'] . ' AND group_type = \'B\')';
$sqlr .= ' AND testuser_test_id IN (' . $sqlin . ') ';
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment