source: trunk/scripts/task-difficulty @ 1184

Revision 1179, 3.0 KB checked in by adrian.budau, 2 months ago (diff)

Added task security(private, protected, public) and also fixed some
problems like the fact that users could add hidden tasks to user-rounds
during contests or cache crashing during task creation.
Reviewboard URL: http://reviewboard.infoarena.ro/r/203/

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3
4
5require_once(dirname($argv[0]) . "/utilities.php");
6
7require_once(IA_ROOT_DIR."www/config.php");
8require_once(IA_ROOT_DIR."common/db/round.php");
9require_once(IA_ROOT_DIR."common/db/score.php");
10db_connect();
11
12
13// Groups $rows by column $pivot, creating an array (or a dictionary indexed by $subpivot) of
14// `value elements` for each unique group.
15//
16// Each `value element` is either:
17//   - value of column $valueCol,       when $valueCol is string
18//   - whole row                        anything else (e.g. null)
19//
20// Additionally, each cluster array/dictionary may be reduced to a single column value by
21// specifying $reduceClusterToCol
22function cluster_rows($rows, $pivot, $valueCol = null, $subpivot = null, $reduceClusterToCol = null) {
23    $clusters = array();
24
25    foreach ($rows as $row) {
26        // create cluster
27        if (!isset($clusters[$row[$pivot]])) {
28            $clusters[$row[$pivot]] = array();
29        }
30        // element value
31        if (is_string($valueCol)) {
32            $value = $row[$valueCol];
33        }
34        else {
35            $value = $row;
36        }
37        // append element
38        if (is_null($subpivot)) {
39            $clusters[$row[$pivot]][] = $value;
40        }
41        else {
42            $clusters[$row[$pivot]][$row[$subpivot]] = $value;
43        }
44    }
45
46    // reduce cluster elements to single values
47    if (!is_null($reduceClusterToCol)) {
48        foreach ($clusters as $k => $cluster) {
49            $clusters[$k] = getattr($cluster, $reduceClusterToCol);
50        }
51    }
52
53    return $clusters;
54}
55
56
57
58$query = "SELECT id FROM ia_task WHERE security != 'private'";
59$tasks = array_values(cluster_rows(db_fetch_all($query), 'id', 'id', null, 0));
60
61$diff = array();
62foreach ($tasks as $task) {
63    $query = <<<SQL
64        SELECT COUNT(*)
65        FROM ia_score_user_round_task
66        LEFT JOIN ia_user ON ia_user.id = user_id
67        WHERE rating_cache > 0 AND round_id='arhiva'
68                AND task_id='%s'
69SQL;
70    $sample_space = (int)db_query_value(sprintf($query, db_escape($task)));
71    if ($sample_space < 50) {
72        $diff[] = array('task_id' => $task,
73                        'diff' => -1);
74        continue;
75    }
76
77    $sample_space = round($sample_space / 3.5);
78
79    $query = <<<SQL
80        SELECT AVG(rating_cache) FROM (
81            SELECT rating_cache
82            FROM ia_score_user_round_task
83            LEFT JOIN ia_user ON ia_user.id = user_id
84            WHERE rating_cache > 0 AND round_id='arhiva'
85                    AND task_id='%s'
86            ORDER BY rating_cache
87            LIMIT %d
88        ) samples
89SQL;
90    $difficulty = db_query_value(sprintf($query, db_escape($task), $sample_space));
91    if ($difficulty) {
92        $diff[] = array('task_id' => $task,
93                        'diff' => $difficulty);
94    }
95}
96
97function diffcmp($a, $b) {
98    if ($a['diff'] < $b['diff'])
99        return -1;
100    else
101        return 1;
102}
103
104usort($diff, 'diffcmp');
105
106foreach ($diff as $row) {
107    if (-1 == $row['diff']) {
108        continue;
109    }
110    echo sprintf("%-15s%f", $row['task_id'], $row['diff']), "\n";
111}
112
113?>
Note: See TracBrowser for help on using the repository browser.