Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make buttons work #13473

Merged
merged 7 commits into from Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,7 +6,20 @@ import {TwoPartBanner} from './twoPartBanner';

const CsFundamentalsSection = React.createClass({
propTypes: {
lastWorkshopSurveyUrl: React.PropTypes.string
lastWorkshopSurveyUrl: React.PropTypes.string,
printCsfCertificateUrl: React.PropTypes.string
},

onStartSurveyClick() {
window.location = this.props.lastWorkshopSurveyUrl;
},

onPrintCertificateClick() {
window.location = this.props.printCsfCertificateUrl;
},

onOnlineLearningClick() {
window.location = '../s/K5PD';
},

renderCompletedCourseBox() {
Expand All @@ -20,17 +33,17 @@ const CsFundamentalsSection = React.createClass({
Thank you for taking a CS Fundamentals workshop! Please complete this survey about your experience and you
will be able to order supplies for your classroom. You can print a certificate for completing our workshop.
</p>
<Button bsStyle="primary">
<Button bsStyle="primary" onClick={this.onStartSurveyClick}>
Start survey
</Button>
<Button>
<Button onClick={this.onPrintCertificateClick}>
Print certificate
</Button>
</div>
);
} else {
return (
<div id="csfPrintCertificate">
<div id="csfPrintCertificate" onClick={this.onPrintCertificateClick}>
<h3>
Thank you
</h3>
Expand All @@ -54,7 +67,7 @@ const CsFundamentalsSection = React.createClass({
<p>
Supplement your in-person session with this online K-5 Professional Learning course.
</p>
<Button bsStyle="primary">
<Button bsStyle="primary" onClick={this.onOnlineLearningClick}>
Learn online
</Button>
</div>
Expand Down
Expand Up @@ -17,6 +17,7 @@ const LandingPage = React.createClass({
coursesTaught: React.PropTypes.arrayOf(React.PropTypes.string),
lastWorkshopSurveyUrl: React.PropTypes.string,
lastWorkshopSurveyCourse: React.PropTypes.string,
printCsfCertificateUrl: React.PropTypes.string,
professionalLearningCourseData: React.PropTypes.array
},

Expand Down Expand Up @@ -66,6 +67,7 @@ const LandingPage = React.createClass({
{this.shouldRenderCSFSection() && (
<CsFundamentalsSection
lastWorkshopSurveyUrl={this.props.lastWorkshopSurveyCourse === 'CS Fundamentals' ? this.props.lastWorkshopSurveyUrl : null}
printCsfCertificateUrl={this.props.printCsfCertificateUrl}
/>
)
}
Expand Down
Expand Up @@ -12,6 +12,7 @@ ReactDOM.render(
coursesCompleted={landingPageData['courses_completed']}
lastWorkshopSurveyUrl={landingPageData['last_workshop_survey_url']}
lastWorkshopSurveyCourse={landingPageData['last_workshop_survey_course']}
printCsfCertificateUrl={landingPageData['print_csf_certificate_url']}
professionalLearningCourseData={landingPageData['summarized_plc_enrollments']}
/>,
document.getElementById('landing-page-container')
Expand Down
Expand Up @@ -14,10 +14,11 @@ describe("Tests for Professional Learning Landing Page", () => {

describe("Tests related to the initial state of the landing page for given teacher", () => {
it("page is as expected for CSF teacher", () => {
const landingPage = generateLandingPage({coursesTaught: ['CS Fundamentals']});
const landingPage = generateLandingPage({coursesTaught: ['CS Fundamentals'], printCsfCertificateUrl: 'certificateUrl'});
const csFundamentalsSection = landingPage.find('CsFundamentalsSection');
expect(csFundamentalsSection).to.have.length(1);
expect(csFundamentalsSection.prop('lastWorkshopSurveyUrl')).to.equal(null);
expect(csFundamentalsSection.prop('printCsfCertificateUrl')).to.equal('certificateUrl');
expect(landingPage.find('CsPrinciplesAndDiscoveriesSection')).to.have.length(0);
});

Expand Down
15 changes: 15 additions & 0 deletions dashboard/app/controllers/pd/csf_certificate_controller.rb
@@ -0,0 +1,15 @@
class Pd::CsfCertificateController < ApplicationController
before_action :authenticate_user!
load_resource :enrollment, class: 'Pd::Enrollment', find_by: :code, id_param: :enrollment_code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some basic tests for this controller:

  • authenticated users can access
  • non-users cannot
  • valid enrollment code works
  • invalid enrollment code gets not found

def generate_certificate
image = create_certificate_image2(
dashboard_dir('app', 'assets', 'images', 'pd_workshop_certificate_csf.png'),
@enrollment.try(:full_name) || '',
y: 444,
height: 100
)

send_data image.to_blob, type: 'image/png', disposition: 'inline'
end
end
Expand Up @@ -15,13 +15,21 @@ def index

summarized_plc_enrollments = Plc::UserCourseEnrollment.where(user: current_user).map(&:summarize)

if courses_completed.include?(Pd::Workshop::COURSE_CSF)
enrollment = Pd::Enrollment.where(pd_workshop_id: ended_workshops.where(course: Pd::Workshop::COURSE_CSF)).
for_user(current_user).order(:survey_sent_at).last

print_csf_certificate_url = CDO.studio_url("/pd/generate_csf_certificate/#{enrollment.try(:code)}")
end

# Link to the certificate
@landing_page_data = {
courses_teaching: courses_teaching,
courses_completed: courses_completed,
last_workshop_survey_url: last_pending_enrollment && CDO.code_org_url("/pd-workshop-survey/#{last_pending_enrollment.code}"),
last_workshop_survey_course: last_pending_enrollment.try(:workshop).try(:course),
print_csf_certificate_url: print_csf_certificate_url,
summarized_plc_enrollments: summarized_plc_enrollments
}
}.compact
end
end
4 changes: 4 additions & 0 deletions dashboard/app/models/pd/enrollment.rb
Expand Up @@ -49,6 +49,10 @@ class Pd::Enrollment < ActiveRecord::Base
validate :validate_school_name, unless: :created_before_school_info?
validates_presence_of :school_info, unless: :created_before_school_info?

def self.for_user(user)
where('email = ? OR user_id = ?', user.email, user.id)
end

# Name split (https://github.com/code-dot-org/code-dot-org/pull/11679) was deployed on 2016-11-09
def created_before_name_split?
persisted? && created_at < '2016-11-10'
Expand Down
2 changes: 2 additions & 0 deletions dashboard/config/routes.rb
Expand Up @@ -350,6 +350,8 @@ module OPS

get 'mimeo/:enrollment_code', controller: 'mimeo_sso', action: 'authenticate_and_redirect'
get 'mimeo/:enrollment_code/error', controller: 'mimeo_sso', action: 'error'

get 'generate_csf_certificate/:enrollment_code', controller: 'csf_certificate', action: 'generate_certificate'
end

get '/dashboardapi/section_progress/:section_id', to: 'api#section_progress'
Expand Down
27 changes: 27 additions & 0 deletions dashboard/test/controllers/pd/csf_certificate_controller_test.rb
@@ -0,0 +1,27 @@
require 'test_helper'

class Pd::CsfCertificateControllerTest < ::ActionController::TestCase
setup do
@user = create :teacher
sign_in(@user)
@enrollment = create :pd_enrollment
end

test 'Generates certificate for a real user' do
get :generate_certificate, params: {enrollment_code: @enrollment.code}
assert_response :success
end

test 'Generates no certificate for an invalid enrollment' do
assert_raise ActiveRecord::RecordNotFound do
get :generate_certificate, params: {enrollment_code: "garbage code"}
end
end

test 'Redirects if user is signed out' do
sign_out(@user)

get :generate_certificate, params: {enrollment_code: @enrollment.code}
assert_response :redirect
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized, after seeing these tests, that any enrollment code will work on this url, not just those for ended workshops. Not necessary in this PR, but we might want to add some more validation.

Expand Up @@ -30,6 +30,7 @@ class Pd::ProfessionalLearningLandingControllerTest < ::ActionController::TestCa
assert_equal [Pd::Workshop::COURSE_CSF, Pd::Workshop::COURSE_CSD, Pd::Workshop::COURSE_CSP], response[:courses_teaching]
assert_equal [Pd::Workshop::COURSE_CSF, Pd::Workshop::COURSE_CSD], response[:courses_completed]
assert_equal "#{CDO.code_org_url}/pd-workshop-survey/#{@ended_enrollment.code}", response[:last_workshop_survey_url]
assert_equal CDO.studio_url("/pd/generate_csf_certificate/#{@ended_enrollment.code}"), response[:print_csf_certificate_url]
assert_equal Pd::Workshop::COURSE_CSF, response[:last_workshop_survey_course]
end

Expand Down
9 changes: 9 additions & 0 deletions dashboard/test/models/pd/enrollment_test.rb
Expand Up @@ -10,6 +10,15 @@ class Pd::EnrollmentTest < ActiveSupport::TestCase
refute_equal enrollment1.code, enrollment2.code
end

test 'enrollment.for_user' do
user = create :teacher
enrollment1 = create :pd_enrollment, user_id: nil, email: user.email
enrollment2 = create :pd_enrollment, user_id: user.id, email: 'someoneelse@example.com'

enrollments = Pd::Enrollment.for_user(user).to_a
assert_equal Set.new([enrollment1, enrollment2]), Set.new(enrollments)
end

test 'find by code' do
enrollment = create :pd_enrollment

Expand Down