Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Exams Stats generator
- //Started on: 4 feb 2015 21:15
- //First version (0.5 beta) finisher on: 5 feb 2015 20:41
- //Enrico Ronconi <enrico.ronconi@yahoo.it>
- function connect(&$MYSQL) {
- global $verbose;
- //Default connection settings
- $MYSQL_host = 'localhost';
- $MYSQL_username = 'php';
- $MYSQL_password = 'cqc4psB7eLBeDdHH';
- $MYSQL_database = 'polimi';
- if($verbose) echo("Trying to connect to $MYSQL_host MySQl database using user $MYSQL_username (db: $MYSQL_database)...\n");
- $MYSQL = @new mysqli($MYSQL_host, $MYSQL_username, $MYSQL_password, $MYSQL_database); //Start connection
- if($MYSQL->connect_error) die('Error while connection (Errno: '.$MYSQL->connect_errno.' - '.$MYSQL->connect_error.")!\nAborted\n"); echo("Succesfully connected!\n");
- $MYSQL->set_charset("utf8");
- }
- function close_connection(&$MYSQL) {
- $MYSQL->close();
- }
- function exec_query($query,&$obj) {
- global $db,$verbose;
- if($verbose) echo("Exec $query\n");
- $obj = $db->query($query);
- if($obj){
- if($verbose) echo("Success!\n");
- return False;
- } else {
- echo("Error ".$db->errno." - ".$db->error."\n");
- return True;
- }
- }
- function get_students_number($exam) {
- global $db, $result_table, $info_table, $matr_field_name, $sex_field, $examid_field, $retired_field;
- //General
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $general = array($total, $male, $female, $unknown);
- //Assenti/Ritirati
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$retired_field` = 1",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$retired_field` = 1" ,$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$retired_field` = 1",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$retired_field` = 1",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $retired = array($total, $male, $female, $unknown);
- return array($general,$retired);
- }
- function get_students_result($exam) {
- global $db, $verbose, $result_table, $info_table, $matr_field_name, $sex_field, $examid_field, $retired_field, $passed_vote, $vote_field, $exam_info_table, $examinfo_id_field, $debit_field, $all_votes;
- //Passed
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` >= $passed_vote",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` >= $passed_vote",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` >= $passed_vote",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` >= $passed_vote",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $passed = array($total, $male, $female, $unknown);
- //Debit
- if(exec_query("SELECT `$debit_field` FROM `$exam_info_table` WHERE `$examinfo_id_field` = $exam",$x)) return False;
- $allow_debit = $x->fetch_row(); $x->close(); $allow_debit = $allow_debit[0];
- if(is_null($allow_debit)) {
- if($verbose) echo("No debits allowed!\n");
- $debit = NULL;
- } else {
- $debit_vote = intval($allow_debit);
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` < $passed_vote AND `$vote_field` >= $debit_vote",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` < $passed_vote AND `$vote_field` >= $debit_vote",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` < $passed_vote AND `$vote_field` >= $debit_vote",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` < $passed_vote AND `$vote_field` >= $debit_vote",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $debit = array($total, $male, $female, $unknown, $debit_vote);
- }
- if(exec_query("SELECT `$all_votes` FROM `$exam_info_table` WHERE `$examinfo_id_field` = $exam",$x)) return False;
- $i_have_all_votes = $x->fetch_row(); $x->close(); $i_have_all_votes = intval($i_have_all_votes[0]);
- if($i_have_all_votes) {
- //Rejected
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` < $passed_vote",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` < $passed_vote",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` < $passed_vote",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` < $passed_vote",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $rejected = array($total, $male, $female, $unknown);
- } else {
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` IS NULL AND `$retired_field` = 0",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = intval($total[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` IS NULL AND `$retired_field` = 0",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = intval($male[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` IS NULL AND `$retired_field` = 0",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = intval($female[0]);
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` IS NULL AND `$retired_field` = 0",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = intval($unknown[0]);
- $rejected = array($total, $male, $female, $unknown);
- }
- return array($passed,$debit,$rejected);
- }
- function get_exam_info($exam) {
- global $db, $all_votes, $exam_info_table, $examinfo_id_field, $debit_field, $exam_subject_field, $exam_date_field, $exam_course_field, $exam_type_field, $exam_oral_field, $date_format;
- if(exec_query("SELECT `$exam_subject_field`,DATE_FORMAT(`$exam_date_field`,\"$date_format\"),`$exam_course_field`,`$exam_type_field`,`$exam_oral_field`,`$debit_field`,DATE_FORMAT(`$exam_date_field`,\"%d%m%y\") FROM `$exam_info_table` WHERE `$examinfo_id_field` = $exam",$x)) return False;
- $total = $x->fetch_row(); $x->close(); return $total;
- }
- function get_average($exam) {
- global $db, $result_table, $info_table, $matr_field_name, $sex_field, $examid_field, $retired_field, $passed_vote, $vote_field, $all_votes, $exam_info_table, $examinfo_id_field;
- //Passed
- if(exec_query("SELECT AVG(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` >= $passed_vote",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = floatval($total[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` >= $passed_vote",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = floatval($male[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` >= $passed_vote",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = floatval($female[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` >= $passed_vote",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = floatval($unknown[0]);
- $passed = array($total, $male, $female, $unknown);
- //Global average
- if(exec_query("SELECT `$all_votes` FROM `$exam_info_table` WHERE `$examinfo_id_field` = $exam",$x)) return False;
- $i_have_all_votes = $x->fetch_row(); $x->close(); $i_have_all_votes = intval($i_have_all_votes[0]);
- if($i_have_all_votes) {
- if(exec_query("SELECT AVG(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam AND `$vote_field` IS NOT NULL",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = floatval($total[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0 AND `$vote_field` IS NOT NULL",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = floatval($male[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1 AND `$vote_field` IS NOT NULL",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = floatval($female[0]);
- if(exec_query("SELECT AVG(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL AND `$vote_field` IS NOT NULL",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = floatval($unknown[0]);
- $general = array($total, $male, $female, $unknown);
- } else {
- $general = NULL;
- }
- if(exec_query("SELECT MIN(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = floatval($total[0]);
- if(exec_query("SELECT MIN(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = floatval($male[0]);
- if(exec_query("SELECT MIN(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = floatval($female[0]);
- if(exec_query("SELECT MIN(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = floatval($unknown[0]);
- $min = array($total, $male, $female, $unknown);
- if(exec_query("SELECT MAX(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam",$x)) return False;
- $total = $x->fetch_row(); $x->close(); $total = floatval($total[0]);
- if(exec_query("SELECT MAX(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 0",$x)) return False;
- $male = $x->fetch_row(); $x->close(); $male = floatval($male[0]);
- if(exec_query("SELECT MAX(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` = 1",$x)) return False;
- $female = $x->fetch_row(); $x->close(); $female = floatval($female[0]);
- if(exec_query("SELECT MAX(`$vote_field`) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name`) WHERE `$examid_field` = $exam AND `$info_table`.`$sex_field` IS NULL",$x)) return False;
- $unknown = $x->fetch_row(); $x->close(); $unknown = floatval($unknown[0]);
- $max = array($total, $male, $female, $unknown);
- return array($general,$passed,$min,$max);
- }
- function halp_pls() {
- echo('Exam stats generator V 0.5 Beta
- Enrico Ronconi <ronconi.enrico@yahoo.it> student @ Politecnico di Milano (IT)
- OPTIONS:
- REQUIRED:
- -i --info-table="name" Table wich contents the info about the students.
- -r --result-table="table name" Table wich contents the exams results.
- -e --exams-info-table="table name" Table wich contents the info about the exams.
- -t --target-exam Specify the exam to analyze. You can compare
- exams result by inserting more than one exam ID
- separated using ","
- Examples:
- --target-exam="2"
- --target-exam="2,4,5".
- OPTIONAL:
- -o --filename="name" Specify an output filename (Omit .pdf)
- (Default: "ddmmyy_subject")
- --date-format="format" Date format, refer to mysql DATE_FORMAT() manpage
- (Default: "%W %d %M %Y").
- --course-name="name" Specify a different course name instead of using
- the one indicated in the exam-info-table.
- --skip-graph Don\'t create the graph.
- --make-only-latex-source Create only latex source without compiling it.
- --keepfiles Keep intermediate files (pdflatex aux file,
- gnuplot files, latex source, etc.).
- -h --help Show this help.
- -v --verbose Verbose mode.
- ');
- die();
- }
- function main($argv) {
- $options = getopt("i:r:e:t:hvo:",array("info-table:","result-table:","exams-info-table:","target-exam:","date-format:","course-name:","help","verbose","skip-graph","keepfiles","make-only-latex-source","filename"));
- if(array_key_exists('h',$options) OR array_key_exists('help',$options)) halp_pls();
- if(@is_null($options['i']) AND @is_null($options['info-table'])) die("Pleas specify the table that contents the information about the students!\nTry the option -h for more informations\nAborted\n");
- if(@is_null($options['r']) AND @is_null($options['result-table'])) die("Pleas specify the table that contents the results of the exam(s)!\nTry the option -h for more informations\nAborted\n");
- if(@is_null($options['e']) AND @is_null($options['exams-info-table'])) die("Pleas specify the table that contents the infos about the exams!\nTry the option -h for more informations\nAborted\n");
- if(@is_null($options['t']) AND @is_null($options['target-exam'])) die("Pleas specify the target exam(s)!\nTry the option -h for more informations\nAborted\n");
- global $matr_field_name; $matr_field_name = 'Matricola';
- global $sex_field; $sex_field = 'HaveFiga';
- global $name_field; $name_field = 'Nome';
- global $examid_field; $examid_field = 'ID_Esame';
- global $retired_field; $retired_field = 'Ritirato';
- global $vote_field; $vote_field = 'Voto';
- global $debit_field; $debit_field = 'Soglia_debito';
- global $all_votes; $all_votes = 'Voti_bocciati';
- global $examinfo_id_field; $examinfo_id_field = 'ID';
- global $exam_subject_field; $exam_subject_field = 'Materia';
- global $exam_date_field; $exam_date_field = 'Data';
- global $exam_course_field; $exam_course_field = 'Corso';
- global $exam_type_field; $exam_type_field = 'Tipo';
- global $exam_oral_field; $exam_oral_field = 'Orale';
- global $date_format; $date_format = '%W %d %M %Y';
- if(array_key_exists('date-format',$options)) $date_format = $options['date-format'];
- global $db;
- global $verbose; $verbose = 0;
- if(array_key_exists('v',$options) OR array_key_exists('verbose',$options)) $verbose = 1;
- global $passed_vote; $passed_vote = 18;
- global $info_table;
- global $result_table;
- global $exam_info_table;
- global $columns;
- global $delete_files; if(array_key_exists('keepfiles',$options)) $delete_files = 0; else $delete_files = 1;
- global $compile_latex; if(array_key_exists('make-only-latex-source',$options)) $compile_latex = 0; else $compile_latex = 1;
- $info_table = @$options['i'].@$options['info-table'];
- $result_table = @$options['r'].@$options['result-table'];
- $exam_info_table = @$options['e'].@$options['exams-info-table'];
- $columns = explode(",",@$options['t'].@$options['target-exams']);
- if(count($columns) == 0) die("Pleas specify at least one column that contents the results of the exam!\nAborted\n");
- if(count($columns) > 1) die("Sorry, the multiple column examination has not been implemented yet!\nAborted\n");
- echo("I will examinate the result table ".$result_table." using the info from the table ".$info_table."!\nThe following exams will be examinated: ".implode($columns,", ")."\n\n");
- connect($db);
- $n = get_students_number($columns[0]);
- $i = get_exam_info($columns[0]);
- $r = get_students_result($columns[0]);
- $a = get_average($columns[0]);
- if(array_key_exists('course-name',$options)) $i[2] = $options['course-name'];
- $filename = compose_name($i);
- if((array_key_exists('filename',$options) AND !@is_null($options['filename'])) OR (array_key_exists('o',$options) AND !@is_null($options['o']))) {
- $filename = @$options['filename'].@$options['o'];
- echo("Warning: you changed the output filename to $filename
- The program will delete all files that match the regexp \"$filename.*\" and \"graph_$filename*\"!
- Are you sure? (Y/N)
- ");
- $in = readline();
- if($in != "Y" AND $in != 'y') die("Interruped\n");
- }
- $name = compose_name($i);
- if(array_key_exists('skip-graph',$options)) $grap_filename = NULL; else {
- $grap_filename = create_graph(
- array(partitionate_votes($columns[0],0),partitionate_votes($columns[0],1),partitionate_votes($columns[0],NULL)),
- $filename
- );
- }
- if($n && $i && $r && $a) generate_latex($filename,$n,$r,$a,$i,$grap_filename);
- close_connection($db);
- }
- function compose_name($i) {
- return str_replace(array(" ","'",'"'),"_",$i[0]." ".$i[6]);
- }
- function generate_latex($filename,$numbers,$results,$averages,$infos,$graph_tex_file = NULL) {
- $p = array($numbers[0][0]-$numbers[1][0],$numbers[0][1]-$numbers[1][1],$numbers[0][2]-$numbers[1][2],$numbers[0][3]-$numbers[1][3]);
- global $verbose, $delete_files, $compile_latex;
- $debit_rows = '';
- if (!is_null($results[1])){
- $debit_rows = '
- \ii Di cui: &&&&&&\\\\
- \iii Promossi senza debito: & \q{'.$results[0][0].'} & \z{'.perc($results[0][0],$p[0]).'} & \q{'.$results[0][1].'} & \z{'.perc($results[0][1],$p[1]).'} & \q{'.$results[0][2].'} & \z{'.perc($results[0][2],$p[2]).'} \\\\
- \iii Promossi con debito: & \q{'.$results[1][0].'} & \z{'.perc($results[1][0],$p[0]).'} & \q{'.$results[1][1].'} & \z{'.perc($results[1][1],$p[1]).'} & \q{'.$results[1][2].'} & \z{'.perc($results[1][2],$p[2]).'} \\\\
- ';
- }
- $graph_code = '';
- if($graph_tex_file){
- $graph_code = '
- \begin{figure}[h]
- \include{'.$graph_tex_file.'}
- \end{figure}
- ';
- }
- $global_avg = '';
- if (!is_null($averages[0])) {
- $global_avg = 'Voto complessivo medio & \q{'.r($averages[0][0]).'} & --- & \q{'.r($averages[0][1]).'} & --- & \q{'.r($averages[0][2]).'} & --- \\\\';
- }
- $keeplog = 0;
- $orale = 'prova scritta'; if($infos[4]) $orale = 'esame orale';
- $pdflatex_options = '-interaction=nonstopmode';
- $source_name = $filename.".tex";
- $latexsource =
- '
- \documentclass{article}
- \usepackage[italian]{babel}
- \usepackage[T1]{fontenc}
- \usepackage[utf8]{inputenc}
- \usepackage{booktabs}
- \usepackage[margin=2cm]{geometry}
- \usepackage{multirow}
- \usepackage{graphicx}
- \usepackage{epstopdf}
- \usepackage{xcolor,colortbl}
- \newcommand{\q}[1]{\texttt{#1}}
- \newcommand{\z}[1]{\texttt{#1\%}}
- \newcommand{\ii}[0]{\hspace{3mm}}
- \newcommand{\iii}{\hspace{6mm}}
- \begin{document}
- %Title
- \begin{center}
- {\Large Statistiche sull\'esame di '.$infos[0].' di '.$infos[1].' }
- \vspace{2mm}
- {\large Tipo: '.$infos[3].' ('.$orale.') - Corso di ingegneria '.$infos[2].'}
- \vspace{5mm}
- \begin{tabular}{l|rr|rr|rr}
- \toprule
- & \multicolumn{2}{c|}{Totale} & \multicolumn{2}{c|}{Uomini} & \multicolumn{2}{c}{Donne} \\\\
- & Assoluto & Rapporto & Assoluto & Rapporto & Assoluto & Rapporto \\\\
- \midrule
- Iscritti all\'esame & \q{'.$numbers[0][0].'} & --- & \q{'.$numbers[0][1].'} & --- & \q{'.$numbers[0][2].'} & --- \\\\
- Assenti o ritirati & \q{'.$numbers[1][0].'} & --- & \q{'.$numbers[1][1].'} & --- & \q{'.$numbers[1][2].'} & --- \\\\
- Presenti & \q{'.($numbers[0][0]-$numbers[1][0]).'} & \z{'.perc($numbers[0][0]-$numbers[1][0],$p[0]).'} & \q{'.($numbers[0][1]-$numbers[1][1]).'} & \z{'.perc($numbers[0][1]-$numbers[1][1],$p[1]).'} & \q{'.($numbers[0][2]-$numbers[1][2]).'} & \z{'.perc($numbers[0][2]-$numbers[1][2],$p[2]).'} \\\\
- Di cui: &&&&&&\\\\
- \ii \color{red} Promossi & \color{red} \q{'.($results[0][0] + $results[1][0]).'} & \color{red} \z{'.perc(($results[0][0] + $results[1][0]),$p[0]).'} & \color{red} \q{'.($results[0][1] + $results[1][1]).'} & \color{red} \z{'.perc($results[0][1] + $results[1][1],$p[1]).'} & \color{red} \q{'.($results[0][2] + $results[1][2]).'} & \color{red} \z{'.perc($results[0][2] + $results[1][2],$p[2]).'} \\\\
- '.$debit_rows.'
- \ii Bocciati & \q{'.($results[2][0] - $results[1][0]).'} & \z{'.perc(($results[2][0] - $results[1][0]),$p[0]).'} & \q{'.($results[2][1] - $results[1][1]).'} & \z{'.perc($results[2][1] - $results[1][1],$p[1]).'} & \q{'.($results[2][2] - $results[1][2]).'} & \z{'.perc($results[2][2] - $results[1][2],$p[2]).'} \\\\
- \midrule
- Voto più alto & \q{'.r($averages[3][0]).'} & --- & \q{'.r($averages[3][1]).'} & --- & \q{'.r($averages[3][2]).'} & --- \\\\
- Voto sufficiente medio & \q{'.r($averages[1][0]).'} & --- & \q{'.r($averages[1][1]).'} & --- & \q{'.r($averages[1][2]).'} & --- \\\\
- '.$global_avg.'
- Voto più basso & \q{'.r($averages[2][0]).'} & --- & \q{'.r($averages[2][1]).'} & --- & \q{'.r($averages[2][2]).'} & --- \\\\
- \bottomrule
- \end{tabular}
- '.$graph_code.'
- \end{center}
- \end{document}
- ';
- if($verbose) echo("Writing latex source on $source_name...\n");
- if (file_put_contents($source_name,$latexsource) == False) {echo ("Error while writing on file $filename!\npdf export aborted\n"); eliminate_garbage_files(NULL,$filename); return 1;}
- if($verbose) echo("Latex source has been wrote succesfully on $source_name\n");
- if ($compile_latex) {
- if($verbose) echo("Trying to compile latex source using pdflatex...\n");
- system("pdflatex -interaction=nonstopmode $source_name >>/dev/null 2>>/dev/null",$code);
- if ($code) {echo("There was an error during the execution of pdflatex (exit code: $code)\nTry to compile $source_name manually or check the log file!\npdf export aborted\n"); return 1;}
- echo("Succesfully wrote $filename.pdf!\n");
- }
- if ($delete_files AND $compile_latex) eliminate_garbage_files($filename.".pdf",$filename);
- if ($delete_files) exec("rm --interactive=never graph_$filename* >> /dev/null 2>>/dev/null");
- return 0;
- }
- function partitionate_votes($exam, $sex, $intervals = 9, $maxvote = 30, $minvote = NULL, $upper_bound = NULL){
- global $db, $result_table, $info_table, $matr_field_name, $sex_field, $examid_field, $retired_field, $passed_vote, $vote_field, $all_votes, $exam_info_table, $examinfo_id_field;
- $incr = $maxvote/$intervals;
- $votes = array();
- if($sex === 1) $sex = "`$sex_field` = 1";
- if($sex === 0) $sex = "`$sex_field` = 0";
- if($sex === NULL) $sex = "`$sex_field` IS NULL";
- if(is_null($minvote)){
- if(exec_query("SELECT MIN(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam",$x)) return False;
- $minvote = $x->fetch_row(); $x->close(); $minvote = floatval($minvote[0]);
- }
- if(is_null($upper_bound)){
- if(exec_query("SELECT MAX(`$vote_field`) FROM `$result_table` WHERE `$examid_field` = $exam",$x)) return False;
- $upper_bound = $x->fetch_row(); $x->close(); $upper_bound = floatval($upper_bound[0]);
- }
- if(exec_query("SELECT COUNT(*) FROM `$result_table` WHERE `$examid_field` = $exam AND `$retired_field` = 0",$x)) return False;
- $tot = $x->fetch_row(); $x->close(); $tot = floatval($tot[0]);
- for($i=0;($i+1)*$incr<=$maxvote;$i++){
- $left_bound = round($i*$incr);
- $right_bound = round($i*$incr+$incr)-1;
- if($right_bound+1 == $maxvote) $right_bound = $right_bound+1;
- $name = $left_bound." -- ".$right_bound;
- if($left_bound == 0 AND $minvote < 0) {$left_bound = $minvote; $name = $left_bound." -- ".$right_bound;}
- if($right_bound == $maxvote AND $maxvote < $upper_bound) {$right_bound = $upper_bound; $name = $left_bound." -- 30L";};
- array_push($votes,array($name,$left_bound,$right_bound,0));
- }
- foreach($votes as &$interval) {
- if(exec_query("SELECT COUNT(*) FROM (`$result_table` JOIN `$info_table` ON `$result_table`.`$matr_field_name` = `$info_table`.`$matr_field_name` ) WHERE `$examid_field` = $exam AND `$vote_field` >= $interval[1] AND `$vote_field` <= $interval[2] AND $sex",$x)) return False;
- $temp = $x->fetch_row(); $x->close(); $interval[3] = round(floatval($temp[0])/floatval($tot)*100,2);
- }
- return $votes;
- }
- function eliminate_garbage_files($keepthis,$basename){
- global $delete_files;
- if(is_null($keepthis) == False) {
- exec("mv $keepthis keepthis >> /dev/null 2>>/dev/null");
- }
- if ($delete_files) exec("rm --interactive=never $basename* >> /dev/null 2>>/dev/null");
- if(is_null($keepthis) == False) {
- exec("mv keepthis $keepthis >> /dev/null 2>>/dev/null");
- }
- }
- function r($x){ return number_format(round($x,1),1); }
- function perc($x,$y){
- return number_format(round(floatval($x)/floatval($y)*100.0,1),1);
- }
- function get_string_data($x) {
- $ret_val = '';
- foreach($x as $y){
- $ret_val = $ret_val."$y[0];$y[3]\n";
- }
- return $ret_val;
- }
- function create_graph($votes,$filename){
- global $verbose, $delete_files;
- if($verbose) echo("Writing male data on file '$filename.male'...\n");
- if (file_put_contents("$filename.male",get_string_data($votes[0])) == False) {echo ("Error while writing on file '$filename.male'!\nGraph creation aborted\n"); eliminate_garbage_files(NULL,$filename); return 1;}
- if($verbose) echo("Male data has been succesfully wrote on file '$filename.male'\n");
- if($verbose) echo("Writing female data on file '$filename.female'...\n");
- if (file_put_contents("$filename.female",get_string_data($votes[1])) == False) {echo ("Error while writing on file '$filename.female'!\nGraph creation aborted\n"); eliminate_garbage_files(NULL,$filename); return 1;}
- if($verbose) echo("Female data has been succesfully wrote on file '$filename.female'\n");
- if($verbose) echo("Writing NULL sex data on file '$filename.unkw'...\n");
- if (file_put_contents("$filename.unkw",get_string_data($votes[2])) == False) {echo ("Error while writing on file '$filename.unkw'!\nGraph creation aborted\n"); eliminate_garbage_files(NULL,$filename); return 1;}
- if($verbose) echo("NULL sex data has been succesfully wrote on file '$filename.unkw'\n");
- $graph_size_x = "17.00cm";
- $graph_size_y = "14.00cm";
- $gnuplot_file_name = $filename.".plt";
- $graph_file_name = "graph_".$filename.".tex";
- $gnuplot_settings = '
- set terminal epslatex leveldefault color blacktext \
- dashed dashlength 1.0 linewidth 1.0 butt noclip \
- nobackground \
- palfuncparam 2000,0.003 \
- input size '.$graph_size_x.' , '.$graph_size_y.' "" 11 fontscale 1.0
- set output "'.$graph_file_name.'"
- unset clip points
- set clip one
- unset clip two
- set bar 1.000000 front
- set border 31 front linetype -1 linewidth 1.000
- set timefmt z "%d/%m/%y,%H:%M"
- set zdata
- set timefmt y "%d/%m/%y,%H:%M"
- set ydata
- set timefmt x "%d/%m/%y,%H:%M"
- set xdata
- set timefmt cb "%d/%m/%y,%H:%M"
- set timefmt y2 "%d/%m/%y,%H:%M"
- set y2data
- set timefmt x2 "%d/%m/%y,%H:%M"
- set x2data
- set boxwidth 0.75 absolute
- set style fill solid 0.50 border lt 1
- set style rectangle back fc lt -3 fillstyle solid 1.00 border lt -1
- set style circle radius graph 0.02, first 0, 0
- set style ellipse size graph 0.05, 0.03, first 0 angle 0 units xy
- set dummy x,y
- set format x "% g"
- set format y "% g"
- set format x2 "% g"
- set format y2 "% g"
- set format z "% g"
- set format cb "% g"
- set format r "% g"
- set angles radians
- set grid nopolar
- set grid xtics nomxtics ytics nomytics noztics nomztics \
- nox2tics nomx2tics noy2tics nomy2tics nocbtics nomcbtics
- set grid layerdefault linetype 0 linewidth 1.000, linetype 0 linewidth 1.000
- set raxis
- set key title ""
- set key inside left top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
- set key noinvert samplen 4 spacing 1 width 0 height 0
- set key maxcolumns 0 maxrows 0
- set key noopaque
- unset label
- unset arrow
- set style increment default
- unset style line
- unset style arrow
- set style histogram rowstacked title offset character 0, 0, 0
- unset logscale
- set offsets 0, 0, 0, 0
- set pointsize 1
- set pointintervalbox 1
- set encoding default
- unset polar
- unset parametric
- unset decimalsign
- set view 60, 30, 1, 1
- set samples 100, 100
- set isosamples 10, 10
- set surface
- unset contour
- set clabel \'%8.3g\'
- set mapping cartesian
- set datafile separator ";"
- unset hidden3d
- set cntrparam order 4
- set cntrparam linear
- set cntrparam levels auto 5
- set cntrparam points 5
- set size ratio 0 1,1
- set origin 0,0
- set style data histograms
- set style function lines
- set xzeroaxis linetype -2 linewidth 1.000
- set yzeroaxis linetype -2 linewidth 1.000
- set zzeroaxis linetype -2 linewidth 1.000
- set x2zeroaxis linetype -2 linewidth 1.000
- set y2zeroaxis linetype -2 linewidth 1.000
- set ticslevel 0.5
- set mxtics default
- set mytics default
- set mztics default
- set mx2tics default
- set my2tics default
- set mcbtics default
- set xtics border in scale 1,0.5 mirror rotate by -45 offset character 0, 0, 0 autojustify
- set xtics norangelimit
- set xtics ()
- set ytics border in scale 1,0.5 mirror norotate offset character 0, 0, 0 autojustify
- set ytics autofreq norangelimit
- set ztics border in scale 1,0.5 nomirror norotate offset character 0, 0, 0 autojustify
- set ztics autofreq norangelimit
- set nox2tics
- set noy2tics
- set cbtics border in scale 1,0.5 mirror norotate offset character 0, 0, 0 autojustify
- set cbtics autofreq norangelimit
- set rtics axis in scale 1,0.5 nomirror norotate offset character 0, 0, 0 autojustify
- set rtics autofreq norangelimit
- set title "Distribuzione dei voti"
- set title offset character 0, 0, 0 font "" norotate
- set timestamp bottom
- set timestamp ""
- set timestamp offset character 0, 0, 0 font "" norotate
- set rrange [ * : * ] noreverse nowriteback
- set trange [ * : * ] noreverse nowriteback
- set urange [ * : * ] noreverse nowriteback
- set vrange [ * : * ] noreverse nowriteback
- set xlabel ""
- set xlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
- set x2label ""
- set x2label offset character 0, 0, 0 font "" textcolor lt -1 norotate
- set xrange [ * : * ] noreverse nowriteback
- set x2range [ -3.29471 : 12.2948 ] noreverse nowriteback
- set ylabel ""
- set ylabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
- set y2label ""
- set y2label offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
- set yrange [ * : * ] noreverse nowriteback
- set y2range [ -4.47918 : 25.9092 ] noreverse nowriteback
- set zlabel ""
- set zlabel offset character 0, 0, 0 font "" textcolor lt -1 norotate
- set zrange [ * : * ] noreverse nowriteback
- set cblabel ""
- set cblabel offset character 0, 0, 0 font "" textcolor lt -1 rotate by -270
- set cbrange [ * : * ] noreverse nowriteback
- set zero 1e-08
- set lmargin -1
- set bmargin -1
- set rmargin -1
- set tmargin -1
- set locale "it_IT.UTF-8"
- set pm3d explicit at s
- set pm3d scansautomatic
- set pm3d interpolate 1,1 flush begin noftriangles nohidden3d corners2color mean
- set palette positive nops_allcF maxcolors 0 gamma 1.5 color model RGB
- set palette rgbformulae 7, 5, 15
- set colorbox default
- set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front bdefault
- set style boxplot candles range 1.50 outliers pt 7 separation 1 labels auto unsorted
- set loadpath
- set fontpath
- set psdir
- #set fit noerrorvariables noprescale
- set xlabel "Fascia di voti"
- set ylabel "Distribuzione relativa percentuale"
- plot \
- "'.$filename.'.female" using 2:xtic(1) title "Donne" fs noborder solid 0.4 lc rgbcolor "red" , \
- "'.$filename.'.male" using 2:xtic(1) title "Uomini" fs noborder solid 0.4 lc rgbcolor "blue", \
- "'.$filename.'.unkw" using 2:xtic(1) title "NULL" fs noborder solid 0.4 lc rgbcolor "black"
- set output
- ';
- if($verbose) echo("Writing gnuplot file on $gnuplot_file_name...\n");
- if (file_put_contents($gnuplot_file_name,$gnuplot_settings) == False) {echo ("Error while writing on file $gnuplot_file_name!\nGraph creation aborted\n"); eliminate_garbage_files(NULL,$filename); return 1;}
- if($verbose) echo("Gnuplot file has been succesfully wrote on $gnuplot_file_name\n");
- if($verbose) echo("Trying to exec gnuplot...\n");
- system("gnuplot $gnuplot_file_name >>/dev/null 2>>/dev/null",$code);
- if ($code) {echo("There was an error during the execution of gnuplot (exit code: $code)\nTry to analyze $gnuplot_file_name manually\nGraph creation aborted\n"); return 1;}
- echo("Succesfully created $graph_file_name!\n");
- if ($delete_files) system("rm --interactive=never $filename* >>/dev/null 2>>/dev/null");
- return "graph_".$filename;
- }
- main($argv);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement