Changeset 1006


Ignore:
Timestamp:
02/28/09 18:14:02 (3 years ago)
Author:
bogdan2412
Message:

Implemented virtual contests (mircea and bogdan)

http://reviewboard.infoarena.ro/r/65/
http://reviewboard.infoarena.ro/r/66/

There is still a security hole, don't put on live.

Location:
trunk
Files:
4 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/common/db/round.php

    r997 r1006  
    179179            $values[] = "('".db_escape($round_id)."', '".db_escape($task_id)."')"; 
    180180        } 
    181         $query = "INSERT INTO ia_round_task (round_id, task_id)  
     181        $query = "INSERT INTO ia_round_task (round_id, task_id) 
    182182                  VALUES ". implode(', ', $values); 
    183183        db_query($query); 
     
    230230 
    231231// Returs list of registred user to round $round_id, order by rating 
    232 // round can be  
     232// round can be 
    233233function round_get_registered_users_range($round_id, $start, $range) 
    234234{ 
     
    312312    $query = <<<SQL 
    313313SELECT * FROM `ia_round` 
    314     WHERE `state` != 'running' AND  
    315     `start_time` <= '%s' AND  
     314    WHERE `state` != 'running' AND 
     315    `start_time` <= '%s' AND 
    316316    DATE_ADD(`start_time`, INTERVAL ($duration_subquery) * 60 * 60 SECOND) > '%s' 
    317317    LIMIT 1 
     
    370370} 
    371371 
     372function round_get_many($options) { 
     373    $field_list = "`ia_round`.*"; 
     374    $where = Array(); 
     375    if (getattr($options, "name_regexp")) { 
     376        $where[] = "`ia_round`.`id` REGEXP " . db_quote($options["name_regexp"]); 
     377    } 
     378    if (getattr($options, "type")) { 
     379        $where[] = "`ia_round`.`type` = " . db_quote($options["type"]); 
     380    } 
     381 
     382    // Add a join for username. 
     383    $join = ""; 
     384    if (getattr($options, 'username', false) == true) { 
     385        $field_list .= ", `ia_user`.`username` AS `user_name`" . 
     386                       ", `ia_user`.`full_name` AS `user_fullname`" . 
     387                       ", `ia_user`.`rating_cache` AS `user_rating`"; 
     388        $join = "LEFT JOIN ia_user ON `ia_round`.`user_id` = `ia_user`.`id`"; 
     389    } 
     390 
     391    if (strtolower(getattr($options, "order", "desc") == "desc")) { 
     392        $order = "DESC"; 
     393    } else { 
     394        $order = "ASC"; 
     395    } 
     396 
     397    $limit = db_quote(getattr($options, "limit", 50)); 
     398    $offset = db_quote(getattr($options, "offset", 0)); 
     399 
     400    if (!empty($where)) { 
     401        $where = " WHERE (" . implode(") AND (", $where) . ")"; 
     402    } else { 
     403        $where = ""; 
     404    } 
     405 
     406    $query = "SELECT $field_list FROM `ia_round` $join $where"; 
     407    $query .= " ORDER BY `ia_round`.`start_time` $order, `ia_round`.`id` $order"; 
     408    $query .= " LIMIT $offset, $limit"; 
     409 
     410    $rounds = db_fetch_all($query); 
     411 
     412    if (getattr($options, "get_count")) { 
     413        $query = "SELECT COUNT(*) FROM `ia_round` $where"; 
     414        $rounds["count"] = db_fetch($query); 
     415        $rounds["count"] = array_pop($rounds["count"]); 
     416    } 
     417 
     418    return $rounds; 
     419} 
     420 
    372421?> 
  • trunk/common/round.php

    r911 r1006  
    77            'classic' => 'Concurs clasic', 
    88            'archive' => 'Arhiva de pregatire', 
     9            'user-defined' => 'Concurs virtual', 
    910    ); 
    1011} 
     
    2728                            'type' => 'bool', 
    2829                    ), 
    29             ), 
     30                ), 
    3031            'archive' => array( 
    3132                    'duration' => array( 
     
    3536                            'type' => 'float', 
    3637                    ), 
    37             ), 
    38     ); 
     38                ), 
     39            'user-defined' => array( 
     40                'duration' => array( 
     41                            'name' => 'Durata', 
     42                            'description' => "Durata concursului, in ore", 
     43                            'default' => '4.5', 
     44                            'type' => 'float', 
     45                    ), 
     46                ), 
     47            ); 
    3948} 
    4049 
     
    4251function round_validate_parameters($round_type, $parameters) { 
    4352    $errors = array(); 
    44     if ($round_type == 'classic') { 
     53    if ($round_type == 'classic' or $round_type == 'user-defined') { 
    4554        // Check duration 
    4655        $duration = getattr($parameters, 'duration'); 
     
    6978            'state' => 'waiting', 
    7079            'start_time' => NULL, 
    71             'public_eval' => (($round_type == 'archive') ? 1 : 0) 
     80            'public_eval' => (($round_type == 'archive') ? 1 : 0), 
     81            'user_id' => $user['id'] 
    7282    ); 
    7383 
     
    111121    } 
    112122 
     123    if (!is_user_id(getattr($round, 'user_id', ''))) { 
     124        $errors['user_id'] = "ID-ul userului este invalid"; 
     125    } 
     126 
    113127    return $errors; 
    114128} 
  • trunk/common/security.php

    r1002 r1006  
    129129        case 'task-create': 
    130130        case 'task-delete': 
    131         case 'round-edit': 
    132         case 'round-create': 
    133131        case 'round-delete': 
    134132        case 'textblock-delete': 
     
    154152        case 'grader-download': 
    155153        case 'task-submit': 
     154        case 'round-edit': 
     155        case 'round-create': 
    156156        case 'round-submit': 
    157157        case 'round-view-tasks': 
     
    449449    switch ($action) { 
    450450        case 'simple-view': 
    451             return true; 
     451          return true; 
     452 
     453        case 'round-create': 
     454          if ($round['type'] == 'user-defined') { 
     455              return $usersec != 'anonymous'; 
     456          } else { 
     457              return $is_admin; 
     458          } 
     459 
     460        case 'round-edit': 
     461        case 'simple-rev-edit': 
     462          if ($usersec == 'anonymous') { 
     463              return false; 
     464          } 
     465          if ($round['type'] == 'user-defined') { 
     466              return $user['id'] == $round['user_id'] || $is_admin; 
     467          } else { 
     468              return $is_admin; 
     469          } 
    452470 
    453471        case 'round-view-tasks': 
     
    456474            return $round['public_eval'] == true || $is_admin; 
    457475 
    458         case 'simple-rev-edit': 
    459476        case 'simple-edit': 
    460477        case 'simple-critical': 
  • trunk/tests/round.php

    r852 r1006  
    1111log_print("WARNING: This test requires the evaluator."); 
    1212 
    13 log_print("Helper1 looks at new round page, fails"); 
     13log_print("Helper1 looks at new round page, works"); 
    1414$res = curl_test(array( 
    1515        'url' => url_round_create(), 
    1616        'user' => 'test_helper1', 
    1717)); 
    18 log_assert($res['url'] != url_absolute(url_round_create())); 
     18log_assert_equal($res['url'], url_absolute(url_round_create())); 
     19 
     20 
     21log_print("Helper1 tries to create a new round, works"); 
     22$res = curl_test(array( 
     23        'url' => url_round_create(), 
     24        'user' => 'test_helper1', 
     25        'post' => array( 
     26          'id' => 'tEst_Round', 
     27          'type' => 'user-defined', 
     28))); 
     29log_assert_equal($res['url'], url_absolute(url_round_edit('tEst_Round'))); 
    1930 
    2031 
     
    2435        'user' => 'test_helper1', 
    2536        'post' => array( 
    26                 'id' => 'tEst_Round', 
    27 ))); 
    28 log_assert_equal($res['url'], url_absolute(url_home())); 
    29  
    30  
    31 log_print("Helper2 looks at round page, but it's not there"); 
     37          'id' => 'tEst_Round', 
     38          'type' => 'classic', 
     39))); 
     40log_assert_equal($res['url'], url_absolute(url_round_create())); 
     41 
     42 
     43log_print("Helper2 looks at round page, and it's there"); 
    3244$res = curl_test(array( 
    3345        'url' => url_textblock('runda/tEst_Round'), 
    3446        'user' => 'test_helper2', 
    3547)); 
    36 log_assert_equal($res['url'], url_absolute(url_textblock_edit('runda/test_round'))); 
     48log_assert_equal($res['url'], url_absolute(url_textblock('runda/tEst_Round'))); 
    3749 
    3850 
     
    4557 
    4658 
    47 log_print("Admin creates round."); 
     59log_print("Admin creates round, already exists."); 
    4860$res = curl_test(array( 
    4961        'url' => url_round_create(), 
     
    5264                'id' => 'tEst_Round', 
    5365))); 
    54 log_assert_equal($res['url'], url_absolute(url_round_edit('tEst_Round'))); 
     66log_assert_equal($res['url'], url_absolute(url_round_create())); 
    5567log_assert(strstr($res['content'], 'tEst_Round')); 
    5668 
     
    117129log_assert_equal($res['url'], url_absolute(url_textblock('runda/tEst_Round'))); 
    118130log_assert(strstr($res['content'], 'xzx-round-title-xzx')); 
    119 log_assert(strstr($res['content'], '<span class="round status waiting">')); 
     131log_assert(strstr($res['content'], 'Nu esti inscris la')); 
    120132log_assert(!strstr($res['content'], '<a href="'. 
    121133            url_textblock('problema/cmmdc'))); 
     
    150162log_assert_equal($res['url'], url_absolute(url_textblock('runda/tEst_Round'))); 
    151163log_assert(strstr($res['content'], 'xzx-round-title-xzx')); 
    152 log_assert(strstr($res['content'], '<span class="round status waiting">')); 
    153 log_assert(!strstr($res['content'], '<span class="round status running">')); 
    154 log_assert(!strstr($res['content'], '<span class="round status complete">')); 
     164log_assert(strstr($res['content'], 'Nu esti inscris la')); 
     165log_assert(!strstr($res['content'], 'Nu se mai pot face inscrieri')); 
     166log_assert(!strstr($res['content'], 'Runda s-a incheiat')); 
    155167log_assert(strstr($res['content'], '<a href="'. 
    156168            url_textblock('problema/cmmdc'))); 
     
    162174 
    163175// Yuck 
    164 log_print("Waiting for 4 seconds..."); 
    165 usleep(4 * 1000000); 
     176log_print("Waiting for 5 seconds..."); 
     177usleep(5 * 1000000); 
    166178 
    167179 
     
    172184log_assert_equal($res['url'], url_absolute(url_textblock('runda/tEst_Round'))); 
    173185log_assert(strstr($res['content'], 'xzx-round-title-xzx')); 
    174 log_assert(!strstr($res['content'], '<span class="round status waiting">'), "Round still waiting"); 
    175 log_assert(strstr($res['content'], '<span class="round status running">')); 
    176 log_assert(!strstr($res['content'], '<span class="round status complete">')); 
     186log_assert(!strstr($res['content'], 'Nu esti inscris la'),  
     187    "Round still waiting, is the evaluator ON?"); 
     188log_assert(strstr($res['content'], 'Nu se mai pot face inscrieri')); 
     189log_assert(!strstr($res['content'], 'Runda s-a incheiat')); 
    177190log_assert(strstr($res['content'], '<a href="'. 
    178191            url_textblock('problema/cmmdc'))); 
     
    186199//test_cleanup(); 
    187200 
     201 
    188202?> 
  • trunk/www/controllers/round.php

    r997 r1006  
    2020// with the user-submitted data and their corresponding errors. 
    2121function controller_round_details($round_id) { 
    22     // validate round_id 
     22    global $identity_user; 
     23 
     24    // Validate round_id 
    2325    if (!is_round_id($round_id)) { 
    2426        flash_error('Identificatorul rundei este invalid'); 
     
    3537    // Security check 
    3638    identity_require('round-edit', $round); 
     39 
     40    // Filter for available round types. 
     41    $round_types = array(); 
     42    foreach (round_get_types() as $round_type => $pretty_name) { 
     43        if (identity_can("round-edit", round_init('round_id', $round_type,  
     44            $identity_user))) 
     45            $round_types[$round_type] = $pretty_name; 
     46    } 
    3747 
    3848    // get parameter list for rounds (in general, not for this specific round) 
     
    148158    // If posting with no errors then do the db monkey 
    149159    if (request_is_post() && !$errors) { 
     160        // Don't forget about security. 
     161        identity_require("round-edit", $new_round); 
    150162        // FIXME: error handling? Is that even remotely possible in php? 
    151163        log_print_r($_REQUEST); 
     
    172184    $view['param_infos'] = $param_infos; 
    173185    $view['all_tasks'] = $all_tasks; 
     186    $view['round_types'] = $round_types; 
    174187 
    175188    execute_view_die("views/round_edit.php", $view); 
     
    181194    global $identity_user; 
    182195 
    183     // Security check. FIXME: sort of a hack. 
     196    // Security check. 
    184197    identity_require_login(); 
    185     identity_require("round-create", 
    186             round_init('new_round', 'classic', $identity_user)); 
    187198 
    188199    // Form stuff. 
     
    193204    $values['id'] = request('id', ''); 
    194205    // FIXME: type hidden 
    195     $values['type'] = request('type', 'classic'); 
     206    $values['type'] = request('type', 'user-defined'); 
    196207 
    197208    if (request_is_post()) { 
     
    210221                    $values['type'], 
    211222                    $identity_user); 
     223            identity_require("round-create", $round); 
    212224            $round_params = array(); 
    213225            // FIXME: array_ magic? 
     
    223235            redirect(url_round_edit($round['id'])); 
    224236        } 
     237    } 
     238 
     239    // Filter for available round types. 
     240    $round_types = array(); 
     241    foreach (round_get_types() as $round_type => $pretty_name) { 
     242        if (identity_can("round-create", round_init("round_id", $round_type,  
     243            $identity_user))) 
     244            $round_types[$round_type] = $pretty_name; 
    225245    } 
    226246 
     
    231251    $view['form_values'] = $values; 
    232252    $view['form_errors'] = $errors; 
     253    $view['round_types'] = $round_types; 
    233254 
    234255    execute_view_die("views/round_create.php", $view); 
  • trunk/www/static/css/screen.css

    r997 r1006  
    14851485} 
    14861486 
     1487/* Round status icons */ 
     1488.round-complete, .round-waiting, .round-running { 
     1489    width: 16px; 
     1490    height: 16px; 
     1491    display: block; 
     1492} 
     1493 
     1494.round-complete { 
     1495    width: 14px; 
     1496    height: 11px; 
     1497    background: url("../images/complete.gif"); 
     1498} 
     1499 
     1500.round-waiting { 
     1501    background: url("../images/hourglass.png"); 
     1502} 
     1503 
     1504.round-running { 
     1505    background: url("../images/run.png"); 
     1506} 
  • trunk/www/views/round_create.php

    r934 r1006  
    1212                'name' => 'Tipul rundei', 
    1313                'type' => 'enum', 
    14                 'values' => round_get_types(), 
    15                 'default' => 'classic', 
     14                'values' => $round_types, 
     15                'default' => 'user-defined', 
    1616        ),  
    1717); 
    1818 
    1919?> 
    20  
    2120<h1><?= html_escape($title) ?></h1> 
    2221 
  • trunk/www/views/round_edit.php

    r934 r1006  
    5555                'name' => 'Tipul rundei', 
    5656                'type' => 'enum', 
    57                 'values' => round_get_types(), 
    58                 'default' => 'classic', 
     57                'values' => $round_types, 
     58                'default' => 'user-defined', 
    5959        ), 
    6060        'public_eval' => array( 
     
    7171<?php if ($round['state'] == 'running') { ?> 
    7272    <div class="warning"> 
    73      Atentie! Runda este activa chiar acum. Orice modificare poate avea urmari neplacute. 
     73     Atentie! Runda este activa chiar acum. Orice modificare poate avea urmari neplacute! 
    7474    </div> 
    7575<?php } elseif ($round['state'] == 'waiting') { ?> 
Note: See TracChangeset for help on using the changeset viewer.