source: trunk/common/round.php @ 1076

Revision 1076, 6.3 KB checked in by gcosmin, 2 years ago (diff)

Constraints for virtual contests.

Solves ticket #372.
Review URL:  http://reviewboard.infoarena.ro/r/116/

  • Property svn:eol-style set to native
Line 
1<?php
2// This module implements round and round-param related stuff.
3
4// Get valid round types.
5function round_get_types() {
6    return array(
7            'classic' => 'Concurs clasic',
8            'archive' => 'Arhiva de pregatire',
9            'user-defined' => 'Concurs virtual',
10    );
11}
12
13// Get parameter infos.
14function round_get_parameter_infos() {
15    return array(
16            'classic' => array(
17                    'duration' => array(
18                            'name' => 'Durata',
19                            'description' => "Durata concursului, in ore",
20                            'default' => '4.5',
21                            'type' => 'float',
22                    ),
23                    'rating_update' => array(
24                            'name' => 'Afecteaza rating-urile',
25                            'description' => "Daca rezultatele din acest concurs ".
26                                "afecteaza rating-urile concurentilor",
27                            'default' => '1',
28                            'type' => 'bool',
29                    ),
30                ),
31            'archive' => array(
32                    'duration' => array(
33                            'name' => 'Durata',
34                            'description' => "Infinit. Nu schimba.",
35                            'default' => '10000000',
36                            'type' => 'float',
37                    ),
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            );
48}
49
50// Validate parameters. Return erros as $form_errors convention.
51function round_validate_parameters($round_type, $parameters) {
52    $errors = array();
53    if ($round_type == 'classic' or $round_type == 'user-defined'
54            or $round_type == 'archive') {
55        // Check duration
56        $duration = getattr($parameters, 'duration');
57        if (is_null($duration)) {
58            $errors['duration'] = "Durata trebuie specificata";
59        }
60
61        if ($round_type == 'user-defined') {
62            if ($duration > IA_USER_DEFINED_ROUND_DURATION_LIMIT) {
63                $errors['duration'] = "Durata maxim admisa este de " .
64                    IA_USER_DEFINED_ROUND_DURATION_LIMIT . " ore";
65            }
66        }
67
68        if (!is_numeric($duration) || $duration < 0) {
69            $errors['duration'] = "Durata trebuie sa fie un numar pozitiv";
70        }
71    } else {
72        log_error("Bad round_type");
73    }
74
75    return $errors;
76}
77
78// Initialize a round object
79function round_init($round_id, $round_type, $user = null) {
80    $round = array(
81            'id' => $round_id,
82            'type' => $round_type,
83            'title' => $round_id,
84            'page_name' => IA_ROUND_TEXTBLOCK_PREFIX . $round_id,
85            'state' => 'waiting',
86            'start_time' => NULL,
87            'public_eval' => (($round_type == 'archive') ? 1 : 0),
88            'user_id' => $user['id']
89    );
90
91    log_assert_valid(round_validate($round));
92    return $round;
93}
94
95// Validates a round.
96// NOTE: this might be incomplete, so don't rely on it exclusively.
97// Use this to check for a valid model. It's also usefull in controllers.
98function round_validate($round) {
99    $errors = array();
100
101    // If you can't pass a fucking array you don't deserve to live.
102    log_assert(is_array($round), "You didn't even pass an array");
103
104    if (!is_round_id(getattr($round, 'id', ''))) {
105        $errors['id'] = 'ID de runda invalid';
106    }
107
108    if (!is_page_name($round['page_name'])) {
109        $errors['page_name'] = "Homepage invalid";
110    }
111
112    if (!array_key_exists(getattr($round, 'type'), round_get_types())) {
113        $errors['type'] = "Tipul rundei este invalid";
114    }
115
116    if (!in_array(getattr($round, 'state'),
117            array('running', 'waiting', 'complete'))) {
118        $errors['state'] = "Starea rundei este invalida";
119    }
120
121    if (!is_whole_number(getattr($round, 'public_eval'))) {
122        $errors['public_eval'] = "public_eval este invalid";
123    }
124
125    // NULL is ok here.
126    if (!is_db_date(getattr($round, 'start_time', db_date_format()))) {
127        $errors['start_time'] = "Timpul trebuie specificat ca YYYY-MM-DD HH:MM:SS";
128    } else {
129        if ($round['type'] == 'user-defined') {
130            $current_time = db_date_parse(date("Y-m-d H:i:s"));
131            $round_time = db_date_parse($round['start_time']);
132
133            if (($round_time - $current_time) >
134                    IA_USER_DEFINED_ROUND_DAYSBEFORE_LIMIT * 60 * 60 * 24) {
135                $errors['start_time'] = "Nu poti creea o runda cu mai mult de "
136                    . IA_USER_DEFINED_ROUND_DAYSBEFORE_LIMIT . " zile inainte";
137            }
138        }
139    }
140
141    if (!is_user_id(getattr($round, 'user_id', ''))) {
142        $errors['user_id'] = "ID-ul userului este invalid";
143    }
144
145    return $errors;
146}
147
148// Called by the eval when a round starts.
149function round_event_start($round) {
150    log_assert_valid(round_validate($round));
151    log_print("CONTEST LOGIC: Starting round {$round['id']}.");
152    $round['state'] = 'running';
153    round_update($round);
154    // User defined rounds always contain already visible tasks.
155    if ($round["type"] != "user-defined") {
156        round_unhide_all_tasks($round['id']);
157    }
158}
159
160// Called when a round is stopped.
161function round_event_stop($round) {
162    log_assert_valid(round_validate($round));
163    log_print("CONTEST LOGIC: Stopping round {$round['id']}.");
164    $round['state'] = 'complete';
165    // Results should be immediately visible after a round ends
166    // if it's type is not classic.
167    if ($round["type"] != "classic") {
168        $round['public_eval'] = 1;
169    }
170    round_update($round);
171}
172
173// Called when a round should be in waiting.
174function round_event_wait($round) {
175    log_assert_valid(round_validate($round));
176    log_print("CONTEST LOGIC: Stand-by for round {$round['id']}.");
177    $round['state'] = 'waiting';
178    round_update($round);
179    // User defined rounds always contain already visible tasks.
180    // If such a round is postponed, do not (re)hide the tasks
181    // like for classic rounds, where the tasks are not visible
182    // before the round starts.
183    if ($round["type"] != "user-defined") {
184        round_hide_all_tasks($round['id']);
185    }
186}
187
188?>
Note: See TracBrowser for help on using the repository browser.