Commit 21eab7ab authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Refactor: reorder code in show_contest()

parent a26c7fd0
...@@ -232,15 +232,6 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token ...@@ -232,15 +232,6 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token
let mut data = json_val::Map::new(); let mut data = json_val::Map::new();
data.insert("contest".to_string(), to_json(&ci)); data.insert("contest".to_string(), to_json(&ci));
if session.is_logged_in() {
data.insert("logged_in".to_string(), to_json(&true));
data.insert("username".to_string(), to_json(&session.username));
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("teacher".to_string(), to_json(&session.is_teacher));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
}
let now = time::get_time(); let now = time::get_time();
let student_grade = session.grade % 100 - if session.grade / 100 == 1 { 1 } else { 0 }; let student_grade = session.grade % 100 - if session.grade / 100 == 1 { 1 } else { 0 };
...@@ -253,37 +244,38 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token ...@@ -253,37 +244,38 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token
let matching_grade = !grade_too_low && !grade_too_high; let matching_grade = !grade_too_low && !grade_too_high;
let can_start = session.is_logged_in() && contest_running && matching_grade; let can_start = session.is_logged_in() && contest_running && matching_grade;
let has_duration = contest.duration > 0;
contest.start.map(|start| { data.insert("has_duration".to_string(), to_json(&has_duration));
if now < start {
let time_until = start - now;
data.insert("time_until_start_d".to_string(), to_json(&(time_until.num_days())));
data.insert("time_until_start_h".to_string(), to_json(&(time_until.num_hours() % 24)));
data.insert("time_until_start_m".to_string(), to_json(&(time_until.num_minutes() % 60)));
}
});
contest.end.map(|end| {
if now < end {
let time_until = end - now;
data.insert("time_until_end_d".to_string(), to_json(&(time_until.num_days())));
data.insert("time_until_end_h".to_string(), to_json(&(time_until.num_hours() % 24)));
data.insert("time_until_end_m".to_string(), to_json(&(time_until.num_minutes() % 60)));
}
});
data.insert("can_start".to_string(), to_json(&can_start)); data.insert("can_start".to_string(), to_json(&can_start));
data.insert("grade_too_high".to_string(), to_json(&grade_too_high)); data.insert("grade_too_high".to_string(), to_json(&grade_too_high));
data.insert("grade_too_low".to_string(), to_json(&grade_too_low)); data.insert("grade_too_low".to_string(), to_json(&grade_too_low));
data.insert("contest_not_yet".to_string(), to_json(&contest_not_yet)); data.insert("contest_not_yet".to_string(), to_json(&contest_not_yet));
data.insert("contest_no_more".to_string(), to_json(&contest_no_more)); data.insert("contest_no_more".to_string(), to_json(&contest_no_more));
// This only checks if a query string is existent, so any query string will if let Some(start) = contest.start {
// lead to the assumption that a bare page is requested. This is useful to if now < start {
// disable caching (via random token) but should be changed if query string let until = start - now;
// can obtain more than only this meaning in the future data.insert("time_until_start".to_string(),
if query_string.is_none() { to_json(&[until.num_days(), until.num_hours() % 24, until.num_minutes() % 60]));
data.insert("not_bare".to_string(), to_json(&true)); }
}
if let Some(end) = contest.end {
if now < end {
let until = end - now;
data.insert("time_until_end".to_string(),
to_json(&[until.num_days(), until.num_hours() % 24, until.num_minutes() % 60]));
}
}
if session.is_logged_in() {
data.insert("logged_in".to_string(), to_json(&true));
data.insert("username".to_string(), to_json(&session.username));
data.insert("firstname".to_string(), to_json(&session.firstname));
data.insert("lastname".to_string(), to_json(&session.lastname));
data.insert("teacher".to_string(), to_json(&session.is_teacher));
data.insert("csrf_token".to_string(), to_json(&session.csrf_token));
} }
if let Some(participation) = opt_part { if let Some(participation) = opt_part {
...@@ -328,8 +320,12 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token ...@@ -328,8 +320,12 @@ pub fn show_contest<T: MedalConnection>(conn: &T, contest_id: i32, session_token
data.insert("seconds_left".to_string(), to_json(&left_secs)); data.insert("seconds_left".to_string(), to_json(&left_secs));
} }
if contest.duration > 0 { // This only checks if a query string is existent, so any query string will
data.insert("duration".to_string(), to_json(&true)); // lead to the assumption that a bare page is requested. This is useful to
// disable caching (via random token) but should be changed if query string
// can obtain more than only this meaning in the future
if query_string.is_none() {
data.insert("not_bare".to_string(), to_json(&true));
} }
Ok(("contest".to_owned(), data)) Ok(("contest".to_owned(), data))
......
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
<div class="columns"> <div class="columns">
<div class="column is-8 is-offset-2"> <div class="column is-8 is-offset-2">
<h3 class="title is-4">{{#if duration}}Wettbewerb:{{else}}Trainingsaufgaben:{{/if}} {{ contest.name }}</h3> <h3 class="title is-4">{{#if has_duration}}Wettbewerb:{{else}}Trainingsaufgaben:{{/if}} {{ contest.name }}</h3>
{{#if contest.duration}}<h4 class="subtitle is-5">Dauer: {{contest.duration}} Minuten</h4>{{/if}} {{#if contest.duration}}<h4 class="subtitle is-5">Dauer: {{contest.duration}} Minuten</h4>{{/if}}
</div> </div>
</div> </div>
...@@ -104,12 +104,12 @@ ...@@ -104,12 +104,12 @@
<div class="column is-6 is-offset-3"> <div class="column is-6 is-offset-3">
<div style="min-height: 400px; margin-bottom:100px;" class="content"> <div style="min-height: 400px; margin-bottom:100px;" class="content">
<!-- <h3 class="title is-4">{{#if duration}}Wettbewerb:{{else}}Trainingsaufgaben:{{/if}} {{ contest.name }}</h3> <!-- <h3 class="title is-4">{{#if has_duration}}Wettbewerb:{{else}}Trainingsaufgaben:{{/if}} {{ contest.name }}</h3>
{{#if contest.duration}}<h4 class="subtitle is-5">Dauer: {{contest.duration}} Minuten</h4>{{/if}} --> {{#if contest.duration}}<h4 class="subtitle is-5">Dauer: {{contest.duration}} Minuten</h4>{{/if}} -->
{{#if participation_start_date}} {{#if participation_start_date}}
<p> <p>
{{#if duration}} {{#if has_duration}}
{{#if time_left}} {{#if time_left}}
Verbleibenden Zeit: <span id="timetime">{{time_left}}</span> Verbleibenden Zeit: <span id="timetime">{{time_left}}</span>
...@@ -183,7 +183,7 @@ ...@@ -183,7 +183,7 @@
{{/if}} {{/if}}
{{#if time_until_end_d}} {{#if time_until_end_d}}
<p>Der Wettbewerb kann noch gestartet werden: {{ time_until_end_d }} Tage, {{ time_until_end_h }} Stunden, {{ time_until_end_m }} Minuten.</p> <p>Der Wettbewerb kann noch gestartet werden: {{ time_until_end.0 }} Tage, {{ time_until_end.1 }} Stunden, {{ time_until_end.2 }} Minuten.</p>
{{/if}} {{/if}}
{{else}} {{else}}
<p>Du kannst diesen Wettbewerb nicht starten.</p> <p>Du kannst diesen Wettbewerb nicht starten.</p>
...@@ -193,7 +193,7 @@ ...@@ -193,7 +193,7 @@
{{#if contest_not_yet}} {{#if contest_not_yet}}
<p>Der Wettbewerb hat noch nicht begonnen.</p> <p>Der Wettbewerb hat noch nicht begonnen.</p>
{{#if time_until_start_d}} {{#if time_until_start_d}}
<p>Der Wettbewerb wird geöffnet in: {{ time_until_start_d }} Tagen, {{ time_until_start_h }} Stunden, {{ time_until_start_m }} Minuten.</p> <p>Der Wettbewerb wird geöffnet in: {{ time_until_start.0 }} Tagen, {{ time_until_start.1 }} Stunden, {{ time_until_start.2 }} Minuten.</p>
{{/if}} {{/if}}
{{else}} {{else}}
{{#if logged_in}} {{#if logged_in}}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment