Description:
[web] better ui for announcement and prob stat
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@195 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r103:c7b351b3b685 - - 3 files changed: 6 inserted, 2 deleted
@@ -1,91 +1,92 | |||
|
1 | 1 | class AnnouncementsController < ApplicationController |
|
2 | 2 | |
|
3 | 3 | before_filter :authenticate |
|
4 | 4 | before_filter { |controller| controller.authorization_by_roles(['admin'])} |
|
5 | 5 | |
|
6 | 6 | in_place_edit_for :announcement, :published |
|
7 | 7 | |
|
8 | 8 | # GET /announcements |
|
9 | 9 | # GET /announcements.xml |
|
10 | 10 | def index |
|
11 |
- @announcements = Announcement.find(:all |
|
|
11 | + @announcements = Announcement.find(:all, | |
|
12 | + :order => "created_at DESC") | |
|
12 | 13 | |
|
13 | 14 | respond_to do |format| |
|
14 | 15 | format.html # index.html.erb |
|
15 | 16 | format.xml { render :xml => @announcements } |
|
16 | 17 | end |
|
17 | 18 | end |
|
18 | 19 | |
|
19 | 20 | # GET /announcements/1 |
|
20 | 21 | # GET /announcements/1.xml |
|
21 | 22 | def show |
|
22 | 23 | @announcement = Announcement.find(params[:id]) |
|
23 | 24 | |
|
24 | 25 | respond_to do |format| |
|
25 | 26 | format.html # show.html.erb |
|
26 | 27 | format.xml { render :xml => @announcement } |
|
27 | 28 | end |
|
28 | 29 | end |
|
29 | 30 | |
|
30 | 31 | # GET /announcements/new |
|
31 | 32 | # GET /announcements/new.xml |
|
32 | 33 | def new |
|
33 | 34 | @announcement = Announcement.new |
|
34 | 35 | |
|
35 | 36 | respond_to do |format| |
|
36 | 37 | format.html # new.html.erb |
|
37 | 38 | format.xml { render :xml => @announcement } |
|
38 | 39 | end |
|
39 | 40 | end |
|
40 | 41 | |
|
41 | 42 | # GET /announcements/1/edit |
|
42 | 43 | def edit |
|
43 | 44 | @announcement = Announcement.find(params[:id]) |
|
44 | 45 | end |
|
45 | 46 | |
|
46 | 47 | # POST /announcements |
|
47 | 48 | # POST /announcements.xml |
|
48 | 49 | def create |
|
49 | 50 | @announcement = Announcement.new(params[:announcement]) |
|
50 | 51 | |
|
51 | 52 | respond_to do |format| |
|
52 | 53 | if @announcement.save |
|
53 | 54 | flash[:notice] = 'Announcement was successfully created.' |
|
54 | 55 | format.html { redirect_to(@announcement) } |
|
55 | 56 | format.xml { render :xml => @announcement, :status => :created, :location => @announcement } |
|
56 | 57 | else |
|
57 | 58 | format.html { render :action => "new" } |
|
58 | 59 | format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } |
|
59 | 60 | end |
|
60 | 61 | end |
|
61 | 62 | end |
|
62 | 63 | |
|
63 | 64 | # PUT /announcements/1 |
|
64 | 65 | # PUT /announcements/1.xml |
|
65 | 66 | def update |
|
66 | 67 | @announcement = Announcement.find(params[:id]) |
|
67 | 68 | |
|
68 | 69 | respond_to do |format| |
|
69 | 70 | if @announcement.update_attributes(params[:announcement]) |
|
70 | 71 | flash[:notice] = 'Announcement was successfully updated.' |
|
71 | 72 | format.html { redirect_to(@announcement) } |
|
72 | 73 | format.xml { head :ok } |
|
73 | 74 | else |
|
74 | 75 | format.html { render :action => "edit" } |
|
75 | 76 | format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } |
|
76 | 77 | end |
|
77 | 78 | end |
|
78 | 79 | end |
|
79 | 80 | |
|
80 | 81 | # DELETE /announcements/1 |
|
81 | 82 | # DELETE /announcements/1.xml |
|
82 | 83 | def destroy |
|
83 | 84 | @announcement = Announcement.find(params[:id]) |
|
84 | 85 | @announcement.destroy |
|
85 | 86 | |
|
86 | 87 | respond_to do |format| |
|
87 | 88 | format.html { redirect_to(announcements_url) } |
|
88 | 89 | format.xml { head :ok } |
|
89 | 90 | end |
|
90 | 91 | end |
|
91 | 92 | end |
@@ -1,147 +1,148 | |||
|
1 | 1 | class Submission < ActiveRecord::Base |
|
2 | 2 | |
|
3 | 3 | belongs_to :language |
|
4 | 4 | belongs_to :problem |
|
5 | 5 | belongs_to :user |
|
6 | 6 | |
|
7 | 7 | before_validation :assign_problem |
|
8 | 8 | before_validation :assign_language |
|
9 | 9 | |
|
10 | 10 | validates_presence_of :source |
|
11 | 11 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
12 | 12 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
13 | 13 | validate :must_have_valid_problem |
|
14 | 14 | validate :must_specify_language |
|
15 | 15 | |
|
16 | 16 | before_save :assign_latest_number_if_new_recond |
|
17 | 17 | |
|
18 | 18 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
19 | 19 | last_sub = find(:first, |
|
20 | 20 | :conditions => {:user_id => user_id, |
|
21 | 21 | :problem_id => problem_id}, |
|
22 | 22 | :order => 'number DESC') |
|
23 | 23 | return last_sub |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | def self.find_all_last_by_problem(problem_id) |
|
27 | 27 | # need to put in SQL command, maybe there's a better way |
|
28 | 28 | Submission.find_by_sql("SELECT * FROM submissions " + |
|
29 | 29 | "WHERE id = " + |
|
30 | 30 | "(SELECT MAX(id) FROM submissions AS subs " + |
|
31 | 31 | "WHERE subs.user_id = submissions.user_id AND " + |
|
32 | 32 | "problem_id = " + problem_id.to_s + " " + |
|
33 |
- "GROUP BY user_id)" |
|
|
33 | + "GROUP BY user_id) " + | |
|
34 | + "ORDER BY user_id") | |
|
34 | 35 | end |
|
35 | 36 | |
|
36 | 37 | def self.find_last_for_all_available_problems(user_id) |
|
37 | 38 | submissions = Array.new |
|
38 | 39 | problems = Problem.find_available_problems |
|
39 | 40 | problems.each do |problem| |
|
40 | 41 | sub = Submission.find_last_by_user_and_problem(user_id, problem.id) |
|
41 | 42 | submissions << sub if sub!=nil |
|
42 | 43 | end |
|
43 | 44 | submissions |
|
44 | 45 | end |
|
45 | 46 | |
|
46 | 47 | def self.find_by_user_problem_number(user_id, problem_id, number) |
|
47 | 48 | Submission.find(:first, |
|
48 | 49 | :conditions => { |
|
49 | 50 | :user_id => user_id, |
|
50 | 51 | :problem_id => problem_id, |
|
51 | 52 | :number => number |
|
52 | 53 | }) |
|
53 | 54 | end |
|
54 | 55 | |
|
55 | 56 | def self.find_all_by_user_problem(user_id, problem_id) |
|
56 | 57 | Submission.find(:all, |
|
57 | 58 | :conditions => { |
|
58 | 59 | :user_id => user_id, |
|
59 | 60 | :problem_id => problem_id, |
|
60 | 61 | }) |
|
61 | 62 | end |
|
62 | 63 | |
|
63 | 64 | protected |
|
64 | 65 | |
|
65 | 66 | def self.find_option_in_source(option, source) |
|
66 | 67 | if source==nil |
|
67 | 68 | return nil |
|
68 | 69 | end |
|
69 | 70 | i = 0 |
|
70 | 71 | source.each_line do |s| |
|
71 | 72 | if s =~ option |
|
72 | 73 | words = s.split |
|
73 | 74 | return words[1] |
|
74 | 75 | end |
|
75 | 76 | i = i + 1 |
|
76 | 77 | if i==10 |
|
77 | 78 | return nil |
|
78 | 79 | end |
|
79 | 80 | end |
|
80 | 81 | return nil |
|
81 | 82 | end |
|
82 | 83 | |
|
83 | 84 | def self.find_language_in_source(source) |
|
84 | 85 | langopt = find_option_in_source(/^LANG:/,source) |
|
85 | 86 | if language = Language.find_by_name(langopt) |
|
86 | 87 | return language |
|
87 | 88 | elsif language = Language.find_by_pretty_name(langopt) |
|
88 | 89 | return language |
|
89 | 90 | else |
|
90 | 91 | return nil |
|
91 | 92 | end |
|
92 | 93 | end |
|
93 | 94 | |
|
94 | 95 | def self.find_problem_in_source(source) |
|
95 | 96 | prob_opt = find_option_in_source(/^TASK:/,source) |
|
96 | 97 | if problem = Problem.find_by_name(prob_opt) |
|
97 | 98 | return problem |
|
98 | 99 | else |
|
99 | 100 | return nil |
|
100 | 101 | end |
|
101 | 102 | end |
|
102 | 103 | |
|
103 | 104 | def assign_problem |
|
104 | 105 | if self.problem_id!=-1 |
|
105 | 106 | begin |
|
106 | 107 | self.problem = Problem.find(self.problem_id) |
|
107 | 108 | rescue ActiveRecord::RecordNotFound |
|
108 | 109 | self.problem = nil |
|
109 | 110 | end |
|
110 | 111 | else |
|
111 | 112 | self.problem = Submission.find_problem_in_source(self.source) |
|
112 | 113 | end |
|
113 | 114 | end |
|
114 | 115 | |
|
115 | 116 | def assign_language |
|
116 | 117 | self.language = Submission.find_language_in_source(self.source) |
|
117 | 118 | end |
|
118 | 119 | |
|
119 | 120 | # validation codes |
|
120 | 121 | def must_specify_language |
|
121 | 122 | return if self.source==nil |
|
122 | 123 | |
|
123 | 124 | # for output_only tasks |
|
124 | 125 | return if self.problem!=nil and self.problem.output_only |
|
125 | 126 | |
|
126 | 127 | if self.language==nil |
|
127 | 128 | errors.add('source',"must specify programming language") unless self.language!=nil |
|
128 | 129 | end |
|
129 | 130 | end |
|
130 | 131 | |
|
131 | 132 | def must_have_valid_problem |
|
132 | 133 | return if self.source==nil |
|
133 | 134 | if self.problem==nil |
|
134 | 135 | errors.add('problem',"must be specified.") |
|
135 | 136 | elsif (!self.problem.available) and (self.new_record?) |
|
136 | 137 | errors.add('problem',"must be valid.") |
|
137 | 138 | end |
|
138 | 139 | end |
|
139 | 140 | |
|
140 | 141 | # callbacks |
|
141 | 142 | def assign_latest_number_if_new_recond |
|
142 | 143 | return if !self.new_record? |
|
143 | 144 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
144 | 145 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
145 | 146 | end |
|
146 | 147 | |
|
147 | 148 | end |
@@ -1,30 +1,32 | |||
|
1 | 1 | <% content_for :head do %> |
|
2 | 2 | <%= stylesheet_link_tag 'scaffold' %> |
|
3 | 3 | <%= javascript_include_tag :defaults %> |
|
4 | 4 | <% end %> |
|
5 | 5 | |
|
6 | 6 | <h1>Listing announcements</h1> |
|
7 | 7 | |
|
8 | + <%= link_to 'New announcement', new_announcement_path %> | |
|
9 | + | |
|
8 | 10 | <table> |
|
9 | 11 | <tr> |
|
10 | 12 | <th>Body</th> |
|
11 | 13 | <th>Author</th> |
|
12 | 14 | <th>Published</th> |
|
13 | 15 | </tr> |
|
14 | 16 | |
|
15 | 17 | <% for announcement in @announcements %> |
|
16 | 18 | <tr> |
|
17 | 19 | <% @announcement = announcement %> |
|
18 | 20 | <td><%=h announcement.body %></td> |
|
19 | 21 | <td><%=h announcement.author %></td> |
|
20 | 22 | <td><%= in_place_editor_field :announcement, :published, {}, :rows => 1 %></td> |
|
21 | 23 | <td><%= link_to 'Show', announcement %></td> |
|
22 | 24 | <td><%= link_to 'Edit', edit_announcement_path(announcement) %></td> |
|
23 | 25 | <td><%= link_to 'Destroy', announcement, :confirm => 'Are you sure?', :method => :delete %></td> |
|
24 | 26 | </tr> |
|
25 | 27 | <% end %> |
|
26 | 28 | </table> |
|
27 | 29 | |
|
28 | 30 | <br /> |
|
29 | 31 | |
|
30 | 32 | <%= link_to 'New announcement', new_announcement_path %> |
You need to be logged in to leave comments.
Login now