3) { $outs[$k] = $val; } else { $outs["other"] += $val; } } return $outs; } function getSurveys($incomplete = 1) { $db = get_db(); $q = "SELECT answers from surveys WHERE projects.id = surveys.project "; if ( ! $incomplete ) { $q .= " where surveys.incomplete = false"; } $r = pg_exec($db, $q); if ( ! $r ) { return 0; } $projects = pg_fetchall($r); return $projects; } function getDomainTotals() { $db = get_db(); $q = "SELECT domain, count(domain) as count from surveys WHERE surveys.incomplete = false group by domain"; $r = pg_exec($db, $q); if ( ! $r ) { return 0; } $totals = pg_fetchall($r); return $totals; } function getSurvey($id) { // XXX: merge with validateCode one day $db = get_db(); // XXX: make view with project + survey $r = pg_exec($db, "SELECT to_char(surveys.tartime, 'YYYY-MM-DD') as tartimefixed, * from projects, surveys WHERE projects.id = $id AND projects.id = surveys.project"); if ( ! $r ) { return 0; } $projects = pg_fetchall($r); // only one item should come back $project = $projects[0]; if ( ! $project ) { return 0; } //var_dump($project); $project["id"] = $id; $project["tartime"] = $project[tartimefixed]; $project["answers"] = explodeList($project["answers"]); $project["others"] = explodeList($project["others"]); $project["comments"] = explodeList($project["comments"]); return $project; } function getSLOC($id) { $db = get_db(); $r = pg_exec($db, "SELECT * from slocs WHERE project = $id"); if ( ! $r ) { return 0; } $data = pg_fetchall($r); $r = pg_exec($db, "SELECT sum(loc) from slocs WHERE project = $id"); $sum = pg_fetchall($r); $sum = $sum[0]["sum"]; return array($data, $sum); } # 1st Generation default 1.00 320 # 2nd Generation default 3.00 107 # 3rd Generation default 4.00 80 # 4th Generation default 16.00 20 # 5th Generation default 70.00 5 $PLT = array( "ada" => 6.50, "ansic" => 2.50 , "asm" => 1.0 , "awk" => 15.00, "cpp" => 6.0, "cs" => 6.0, # C# -> Used same as java "csh" => 15.00, # Unix shell scripts "exp" => 15.00, # Expect -> Used same as awk "fortran" => 4.0, # Fortran 90 "haskell" => 8.50, "java" => 6.0, "jsp" => 15.00, # -> Used same as PHP "lex" => 7.0, # Used same as FLEX "lisp" => 5.0, "ml" => 6.0, # -> Same as C++ "objc" => 12.00, "pascal" => 3.50, "perl" => 15.00, "php" => 15.00, # -> Used same as perl "python" => 15.00, # -> Used same as perl "ruby" => 15.00, # -> Used same as perl "sed" => 15.00, # -> Used same as sh "sh" => 15.00, # Unix shell scripts "tcl" => 15.00, # -> Used same as perl "yacc" => 7.0 # Used same as flex ); function getASLOC($id) { global $PLT; $sloc = getSLOC($id); $langcounts = $sloc[0]; foreach($langcounts as $langcount) { $lang = $langcount[lang]; $total += $langcount[loc] * $PLT[$lang]; } return round($total); } function explodeList($a) { // remove { } $a = trim(substr($a,1,count($a)-2)); // remove "s $a = ereg_replace('"','',$a); // break on ,s $a = explode(",",$a); return $a; } function buildSurveyData($vars, $questions) { $values = array(); $value_comments = array(); $value_others = array(); foreach ( $questions as $q ) { $name = $q["items"]; $values[] = $vars[$name]; $name_comment = $name."_comment"; $comment = pg_quote($vars[$name_comment]); $value_comments[] = $comment; $name_other = $name."_other"; $other = pg_quote($vars[$name_other]); $value_others[] = $other; } return array( "values" => join($values,","), "value_comments" => join($value_comments,","), "value_others" => join($value_others,",") ); } function surveyExists($projid) { $db = get_db(); $q = pg_exec ($db, "SELECT id FROM surveys WHERE project = $projid"); if ( ! $q ) { print "ASSERTION FAILED! BAD THINGS HAPPEN!"; exit; } $surveys = pg_fetchall($q); if ( $surveys[0] ) { return 1; } } function projectExists($projid) { $db = get_db(); $q = pg_exec ($db, "SELECT id FROM projects WHERE id = $projid"); if ( ! $q ) { print "ASSERTION FAILED! BAD THINGS HAPPEN!"; exit; } $surveys = pg_fetchall($q); if ( $surveys[0] ) { return 1; } } function insertSurvey($vars, $questions) { $projid = $vars["projid"]; $email = pg_quote($vars["email"]); srand ((double) microtime() * 1000000); $code = rand(100000,200000); $private = $vars["private"] ? "true" : "false"; $ret = buildSurveyData($vars, $questions); $values = $ret["values"]; $value_comments = $ret["value_comments"]; $value_others = $ret["value_others"]; //var_dump($values); //var_dump($value_comments); //var_dump($value_others); $db = get_db(); $q = pg_exec($db, "BEGIN"); $q = pg_exec ($db, "SELECT id FROM surveys WHERE project = $projid"); // if project already exists if ( $q ) { $surveys = pg_fetchall($q); //var_dump($surveys); // only one item should come back $survey = $surveys[0]; if ( $survey ) { return -2; } } $qstr = "INSERT INTO surveys ( project, private, answers, comments, others, code ) VALUES ( $projid, $private, '{".$values."}', '{".$value_comments."}', '{".$value_others."}', $code )"; $q = pg_exec ($db, $qstr); if ( ! $q ) { mail("kiko@async.com.br","SURVEY ERROR",$qstr); return -1; } $qstr = "UPDATE projects SET admin = '$email' WHERE id = $projid"; $q = pg_exec ($db, $qstr); if ( ! $q ) { mail("kiko@async.com.br","SURVEY ERROR",$qstr); return -1; } $q = pg_exec("COMMIT"); return $code; } function updateSurvey($vars, $questions) { $projid = $vars["projid"]; $ret = buildSurveyData($vars, $questions); $values = $ret["values"]; $value_comments = $ret["value_comments"]; $value_others = $ret["value_others"]; //var_dump($ret); $db = get_db(); $qstr = "UPDATE surveys set answers = '{".$values."}', comments = '{".$value_comments."}', others = '{".$value_others."}', utime = now() WHERE project = $projid"; $q = pg_exec ($db, $qstr); if ( ! $q ) { mail("kiko@async.com.br","SURVEY ERROR",$qstr); return -1; } return 0; } function getProjects() { $db = get_db(); $r = pg_exec($db,"SELECT * FROM projects"); $projects = pg_fetchall($r); return $projects; } function getProjectByID($id) { $db = get_db(); $r = pg_exec($db,"SELECT * FROM projects WHERE id = $id"); $projects = pg_fetchall($r); return $projects[0]; } function getNextProject($id, $private) { $db = get_db(); $pstr = ""; if (! $private) { $pstr = "AND private = FALSE"; } $r = pg_exec($db,"SELECT id FROM fullsurveys WHERE id > $id $pstr ORDER BY id"); $projects = pg_fetchall($r); return $projects[0][id]; } function getPrevProject($id, $private) { $db = get_db(); $pstr = ""; if (! $private) { $pstr = "AND private = FALSE"; } $r = pg_exec($db,"SELECT id FROM fullsurveys WHERE id < $id $pstr ORDER BY id DESC"); $projects = pg_fetchall($r); return $projects[0][id]; } function getProjectByName($name) { $db = get_db(); $r = pg_exec($db,"SELECT * FROM projects WHERE name = '$name'"); $projects = pg_fetchall($r); return $projects[0]; } function getUnansweredProjects() { $db = get_db(); $r = pg_exec($db,"select * from projects where id not in ( select project from surveys )"); $projects = pg_fetchall($r); return $projects; } function getAnsweredProjects($only_public=0, $incomplete=0) { $db = get_db(); $q = "select projects.id as id, projects.name as name, projects.admin as admin, projects.url as url, surveys.private as private, surveys.domain as domain, surveys.others as others, surveys.answers as answers, surveys.comments as comments, surveys.vip as vip, surveys.tarsize as tarsize, surveys.goodcomments as goodcomments, surveys.distribution as distribution, surveys.multiproject as multiproject FROM projects,projectsurveys as surveys where projects.id = surveys.id "; if ( $only_public ) { $q .= " and surveys.private = false"; } if ( ! $incomplete ) { $q .= " and surveys.incomplete = false"; } //var_dump($q); $r = pg_exec($db, $q); $projects = pg_fetchall($r); return $projects; } function getAnswersGrouped($answer, $value, $multiple, $group) { $db = get_db(); if ($multiple) { $where = "WHERE answers[$answer] & $value = $value"; } else { $where = "WHERE answers[$answer] = $value"; } $q = "select answers[$group], count(answers[$answer]) FROM fullsurveys $where group by answers[$group]"; $r = pg_exec($db, $q); $counts = pg_fetchall($r); return $counts; } function createProject($name, $url) { $name = pg_quote($name); $url = pg_quote($url); $db = get_db(); $r = pg_exec($db,"BEGIN"); $r = pg_exec($db,"INSERT INTO projects ( name, url ) VALUES ( '$name', '$url' )"); $r = pg_exec($db,"COMMIT"); return getProjectByName($name); } function getAnswerTotals($questions) { $db = get_db(); $res = array(); for ( $i = 0; $i < count($questions); $i++ ) { $pgi = $i + 1; // postgres indexes start at 1 # $str = "select count(id), answers[$pgi] as answer from surveys group by answers[$pgi]"; $str = "select count(id), answers[$pgi] as answer from projectsurveys where incomplete = false group by answers[$pgi]"; $r = pg_exec($db, $str ); $res[] = pg_fetchall($r); } return $res; } function getMultipleAnswerCount($answers, $show_private=0) { $db = get_db(); # Don't ignore incomplete projects - see mine.content's uppercase # message $private = $show_private == 0 ? ' private = false and ' : ''; $str = "select id, name, vip, goodcomments from projectsurveys where $private"; for ( $i = 0; $i < count($answers); $i++ ) { $answer = $answers[$i]; if ($answer[0] != NULL) { if ($answer[1] == "makeChecks") { $t = 0; foreach ($answer[0] as $a) { $t = $t + $a; } $str .= " answers[$i+1] & $t = $t and "; } else { $str .= " answers[$i+1] = ".$answer[0]." and "; } } } $str .= " 1 = 1 order by name"; // HACK HACK HACK :-) $r = pg_exec($db, $str ); $res = pg_fetchall($r); return $res; } function getCheckTotal($i, $exp) { $db = get_db(); $pgi = $i + 1; // postgres indexes start at 1 $mask = pow(2,$exp); $str = "select count(id), answers[$pgi] & $mask as answer from projectsurveys where incomplete = false and answers[$pgi] & $mask = $mask group by answers[$pgi] & $mask"; $q = pg_exec($db, $str); $res = pg_fetchall($q,0); return $res[0]; } function getCheckUnanswered($i) { $db = get_db(); $pgi = $i + 1; // postgres indexes start at 1 $str = "select count(id), answers[$pgi] as answer from projectsurveys where incomplete = false and answers[$pgi] = 0 group by answers[$pgi]"; $q = pg_exec($db, $str); $res = pg_fetchall($q,0); return $res[0]; } function getNumSurveys() { $db = get_db(); $q = pg_exec($db,"SELECT count(id) from surveys"); $r = pg_fetch_array($q,0); return $r[0]; } function getNumSurveysMetaDist() { $db = get_db(); $q = pg_exec($db,"SELECT count(id) from surveys where distribution=true or multiproject=true"); $r = pg_fetch_array($q,0); return $r[0]; } function getNumSurveysVProj() { $db = get_db(); $q = pg_exec($db,"SELECT count(id) from projectsurveys"); $r = pg_fetch_array($q,0); return $r[0]; } function getNumSurveysWTar() { $db = get_db(); $q = pg_exec($db,"SELECT count(id) from surveys where tarsize > 0 and incomplete = false"); $r = pg_fetch_array($q,0); return $r[0]; } function getNumSurveysWLOC() { $db = get_db(); $q = pg_exec($db," select count(project) from (select distinct slocs.project from slocs, surveys where slocs.project = surveys.project and surveys.incomplete = false) as foo"); $r = pg_fetch_array($q,0); return $r[0]; } function getNumInvalidSurveys() { $db = get_db(); $q = pg_exec($db,"SELECT count(id) from surveys where incomplete = true"); $r = pg_fetch_array($q,0); return $r[0]; } function completeSurvey($project, $vip, $goodcomments, $incomplete, $multiproject, $distribution, $domain) { $db = get_db(); $qstr = "UPDATE surveys set vip = $vip, goodcomments = $goodcomments, incomplete = $incomplete, multiproject = $multiproject, distribution = $distribution, domain = '$domain' WHERE project = $project"; $q = pg_exec ($db, $qstr); } function getTarInterval($id) { $db = get_db(); $qstr = "SELECT date_part('day', now() - tartime) from surveys where project = $id;"; $q = pg_exec($db, $qstr); $d = pg_fetchall($q); return $d[0][0]; } function getAverages() { $db = get_db(); $ret = array(); $qstr = "SELECT avg(tarsize) from surveys where incomplete = false"; $q = pg_exec($db, $qstr); $d = pg_fetchall($q); $ret["avg_size"] = $d[0][0]; $qstr = "SELECT date_part('day', avg(now() - tartime)) from surveys where incomplete = false;"; $q = pg_exec($db, $qstr); $d = pg_fetchall($q); $ret["avg_time"] = $d[0][0]; $qstr = "select avg(langs) from ( select count(lang) as langs from slocs, fullsurveys where slocs.project = fullsurveys.id and fullsurveys.incomplete = false group by project ) as sel"; $q = pg_exec($db, $qstr); $d = pg_fetchall($q); $ret["avg_langcount"] = $d[0][0]; $qstr = "select avg(sloc) from ( select sum(loc) as sloc from slocs, fullsurveys where slocs.project = fullsurveys.id and fullsurveys.incomplete = false group by project ) as sel"; $q = pg_exec($db, $qstr); $d = pg_fetchall($q); $ret["avg_sloc"] = $d[0][0]; return $ret; } function pg_fetchall($q) { $res = array(); $num = pg_numrows($q); for ( $i=0; $i < $num ; $i++ ) { $res[] = pg_fetch_array($q,$i); } return $res; } function get_db() { global $conn; // hidden global cache $conn = isset($conn)? $conn : pg_connect("host=localhost dbname=survey user=nobody"); return $conn; } function mailCode($email, $projid, $code) { $project = getProjectByID($projid); mail($email, "FS Engineering survey code: $code", "This is an automated response from the Free software engineering survey engine. It's just a reminder of the project that you answered for and the survey code generated: Project: $project[name] Code: $code Note that you can go back and complete your survey at any time by using that code and visiting http://www.async.com.br/~kiko/fsp/presurvey.php I think that's it. Thanks a lot for your participation. -- Christian Reis, Software Engineer | kiko@async.com.br | +55 16 261 2688 ", "From: Christian Reis X-Mailer: FSE Survey Mail 0.1b"); } ?>