From 18e779aeb563af7ecdb7566c816d6e19cbf69856 Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Sun, 4 Jul 2021 10:41:09 +0800 Subject: [PATCH 1/8] creating grade_distribution_graph_data method --- app/controllers/assignments_controller.rb | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index f820efdd5a..6007ba1b80 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -309,6 +309,39 @@ def batch_runs end end + # Return the chart data of assignment grade distributions + def grade_distribution_graph_data + assignment = Assignment.find(params[:id]) + labels = [] + (0..19).each { |i| labels.push((5 * i).to_s + '-' + (5 * i + 5).to_s) } + datasets = [ + { + label: '2', + data: assignment.grade_distribution_array(), + backgroundColor: [ + 'rgba(36, 81, 133, 1)', + ], + borderColor: [ + 'rgba(36, 81, 133, 1)', + ], + borderWidth: 1, + }, + ] + data = {labels: labels, datasets: datasets} + options = { + scales: { + xAxes: [{ + ticks: { + min: 0, + max: 100, + stepSize: 10, + } + }] + } + } + render json: {data: data, options: options} + end + # Refreshes the grade distribution graph def refresh_graph @assignment = Assignment.find(params[:id]) From ac5f97dea0ce309f3e8671f330a606849f09f960 Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Sun, 4 Jul 2021 10:42:28 +0800 Subject: [PATCH 2/8] add routes and correspongding test --- config/routes.rb | 1 + spec/routing/routes_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index f75a68e919..a08c1c7007 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,6 +87,7 @@ put 'start_timed_assignment' get 'starter_file' put 'update_starter_file' + get 'grade_distribution_graph_data' end resources :starter_file_groups do diff --git a/spec/routing/routes_spec.rb b/spec/routing/routes_spec.rb index 8863f230bb..2777ef004a 100644 --- a/spec/routing/routes_spec.rb +++ b/spec/routing/routes_spec.rb @@ -126,6 +126,15 @@ action: 'start_timed_assignment', id: assignment.id.to_s) end + + it 'routes GET grade_distribution_graph_data properly' do + expect(get: path + '/' + assignment.id.to_s + '/grade_distribution_graph_data properly') + .to route_to( + controller: ctrl, + action: 'grade_distribution_graph_data', + id: assignment.id.to_s + ) + end end # end Assignment member route tests From 62c87c91359ebcd955a3fdf97075b724cea84cac Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Sun, 4 Jul 2021 10:43:44 +0800 Subject: [PATCH 3/8] add options status and create ajax for assignment case --- app/assets/javascripts/Components/dashboard.jsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/Components/dashboard.jsx b/app/assets/javascripts/Components/dashboard.jsx index fed358d2c2..52b7065a4b 100644 --- a/app/assets/javascripts/Components/dashboard.jsx +++ b/app/assets/javascripts/Components/dashboard.jsx @@ -30,7 +30,8 @@ class Dashboard extends React.Component { backgroundColor: 'rgb(75, 192, 192)', }, ], - } + }, + options: {} }; } @@ -42,6 +43,10 @@ class Dashboard extends React.Component { // TODO } else if (this.state.assessment_type === 'Assignment') { // TODO + $.ajax({ + url: Routes.grade_distribution_graph_data_assignment_path(this.state.assessment_id), + dataType: 'json', + }).then(res => this.setState({data: res.data, options: res.options})) } } } @@ -50,7 +55,7 @@ class Dashboard extends React.Component { if (this.state.display_course_summary) { return ; } else if (this.state.assessment_type === 'Assignment') { - return ; + return ; } else if (this.state.assessment_type === 'GradeEntryForm') { return (
From 9ec780b8e01c1be510c8fbae573939b5c946e8e2 Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Sun, 4 Jul 2021 11:08:36 +0800 Subject: [PATCH 4/8] fix hound error --- app/controllers/assignments_controller.rb | 26 +++++++++++------------ spec/routing/routes_spec.rb | 10 ++++----- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index 6007ba1b80..acf5c9dd0f 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -317,29 +317,29 @@ def grade_distribution_graph_data datasets = [ { label: '2', - data: assignment.grade_distribution_array(), + data: assignment.grade_distribution_array, backgroundColor: [ - 'rgba(36, 81, 133, 1)', + 'rgba(36, 81, 133, 1)' ], borderColor: [ - 'rgba(36, 81, 133, 1)', + 'rgba(36, 81, 133, 1)' ], - borderWidth: 1, - }, + borderWidth: 1 + } ] - data = {labels: labels, datasets: datasets} + data = { labels: labels, datasets: datasets } options = { scales: { xAxes: [{ - ticks: { - min: 0, - max: 100, - stepSize: 10, - } - }] + ticks: { + min: 0, + max: 100, + stepSize: 10 + } + }] } } - render json: {data: data, options: options} + render json: { data: data, options: options } end # Refreshes the grade distribution graph diff --git a/spec/routing/routes_spec.rb b/spec/routing/routes_spec.rb index 2777ef004a..79cfdedb99 100644 --- a/spec/routing/routes_spec.rb +++ b/spec/routing/routes_spec.rb @@ -128,12 +128,10 @@ end it 'routes GET grade_distribution_graph_data properly' do - expect(get: path + '/' + assignment.id.to_s + '/grade_distribution_graph_data properly') - .to route_to( - controller: ctrl, - action: 'grade_distribution_graph_data', - id: assignment.id.to_s - ) + expect(get: path + '/' + assignment.id.to_s + '/grade_distribution_graph_data') + .to route_to(controller: ctrl, + action: 'grade_distribution_graph_data', + id: assignment.id.to_s) end end # end Assignment member route tests From 998fefbe685a47c3309edacc234201bd40269bf0 Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Wed, 7 Jul 2021 12:43:22 +0800 Subject: [PATCH 5/8] deleted bar labels --- app/controllers/assignments_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index acf5c9dd0f..df637ed82b 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -316,7 +316,6 @@ def grade_distribution_graph_data (0..19).each { |i| labels.push((5 * i).to_s + '-' + (5 * i + 5).to_s) } datasets = [ { - label: '2', data: assignment.grade_distribution_array, backgroundColor: [ 'rgba(36, 81, 133, 1)' From d8c998aa4928b5c12f37dc70c9666b8c209f5cb8 Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Fri, 9 Jul 2021 11:01:54 +0800 Subject: [PATCH 6/8] improve the code based on the feedbacks --- app/assets/javascripts/Components/dashboard.jsx | 5 ++--- app/controllers/assignments_controller.rb | 16 ++-------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/Components/dashboard.jsx b/app/assets/javascripts/Components/dashboard.jsx index 52b7065a4b..744b788ca6 100644 --- a/app/assets/javascripts/Components/dashboard.jsx +++ b/app/assets/javascripts/Components/dashboard.jsx @@ -30,8 +30,7 @@ class Dashboard extends React.Component { backgroundColor: 'rgb(75, 192, 192)', }, ], - }, - options: {} + } }; } @@ -46,7 +45,7 @@ class Dashboard extends React.Component { $.ajax({ url: Routes.grade_distribution_graph_data_assignment_path(this.state.assessment_id), dataType: 'json', - }).then(res => this.setState({data: res.data, options: res.options})) + }).then(res => this.setState({data: res})) } } } diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb index df637ed82b..31214afa8f 100644 --- a/app/controllers/assignments_controller.rb +++ b/app/controllers/assignments_controller.rb @@ -312,8 +312,7 @@ def batch_runs # Return the chart data of assignment grade distributions def grade_distribution_graph_data assignment = Assignment.find(params[:id]) - labels = [] - (0..19).each { |i| labels.push((5 * i).to_s + '-' + (5 * i + 5).to_s) } + labels = (0..19).map { |i| (5 * i).to_s + '-' + (5 * i + 5).to_s } datasets = [ { data: assignment.grade_distribution_array, @@ -327,18 +326,7 @@ def grade_distribution_graph_data } ] data = { labels: labels, datasets: datasets } - options = { - scales: { - xAxes: [{ - ticks: { - min: 0, - max: 100, - stepSize: 10 - } - }] - } - } - render json: { data: data, options: options } + render json: data end # Refreshes the grade distribution graph From efc5eb119f33f810de1d2f30ec1f05682ac9357a Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Fri, 9 Jul 2021 11:02:46 +0800 Subject: [PATCH 7/8] added tests for grade distribution data method --- .../assignments_controller_spec.rb | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/controllers/assignments_controller_spec.rb b/spec/controllers/assignments_controller_spec.rb index 1886156166..649fb771d0 100644 --- a/spec/controllers/assignments_controller_spec.rb +++ b/spec/controllers/assignments_controller_spec.rb @@ -1056,4 +1056,32 @@ end end end + describe '#grade_distribution_graph_data' do + before { get_as user, :grade_distribution_graph_data, params: params } + let(:assignment) { create :assignment } + let(:params) { { id: assignment.id } } + let(:user) { create :admin } + let(:assignment_with_results) { create :assignment_with_criteria_and_results, assignment: assignment } + let(:params) { { id: assignment_with_results.id } } + context 'data' do + it 'should contain the right keys' do + data = JSON.parse(response.body).keys + expect(data).to contain_exactly('labels', 'datasets') + end + end + context 'labels' do + it 'should contain the right values' do + labels = JSON.parse(response.body)['labels'] + expected = (0..19).map { |i| (5 * i).to_s + '-' + (5 * i + 5).to_s } + expect(labels).to eq(expected) + end + end + context 'datasets' do + it 'should contain the right data' do + data = JSON.parse(response.body)['datasets'].first['data'] + expected = assignment_with_results.grade_distribution_array + expect(data).to contain_exactly(*expected) + end + end + end end From d7f6466bf8df2b6a737f5eb5b50c219a5f410f1f Mon Sep 17 00:00:00 2001 From: Junru Lin Date: Fri, 9 Jul 2021 11:09:50 +0800 Subject: [PATCH 8/8] deleted options --- app/assets/javascripts/Components/dashboard.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/Components/dashboard.jsx b/app/assets/javascripts/Components/dashboard.jsx index 744b788ca6..86c2867a59 100644 --- a/app/assets/javascripts/Components/dashboard.jsx +++ b/app/assets/javascripts/Components/dashboard.jsx @@ -54,7 +54,7 @@ class Dashboard extends React.Component { if (this.state.display_course_summary) { return ; } else if (this.state.assessment_type === 'Assignment') { - return ; + return ; } else if (this.state.assessment_type === 'GradeEntryForm') { return (