-
Notifications
You must be signed in to change notification settings - Fork 172
CRUD for Questionnaire #246
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # app/controllers/items_controller.rb | ||
| class ItemsController < ApplicationController | ||
| def index | ||
| questionnaire = Questionnaire.find(params[:questionnaire_id]) | ||
|
|
||
| Rails.logger.info "Items for Questionnaire #{questionnaire.id}:" | ||
| questionnaire.items.each do |item| | ||
| Rails.logger.info item.attributes.inspect | ||
| end | ||
|
|
||
| items_json = questionnaire.items.as_json | ||
| Rails.logger.info "JSON being rendered: #{items_json.inspect}" | ||
|
|
||
| render json: items_json | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class QuestionTypesController < ApplicationController | ||
| # GET /item_types | ||
| def index | ||
| question_types = QuestionType.all | ||
| render json: question_types | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class QuestionnaireTypesController < ApplicationController | ||
| # GET /questionnaire_types | ||
| def index | ||
| questionnaire_types = QuestionnaireType.all | ||
| render json: questionnaire_types | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class QuestionType < ApplicationRecord | ||
|
||
| # Validations | ||
| validates :name, presence: true, uniqueness: true | ||
|
|
||
| # Associations (if any later) | ||
| # has_many :questionnaires, foreign_key: :questionnaire_type, primary_key: :name | ||
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,13 @@ | |
| class Questionnaire < ApplicationRecord | ||
| belongs_to :instructor | ||
| has_many :items, class_name: "Item", foreign_key: "questionnaire_id", dependent: :destroy # the collection of questions associated with this Questionnaire | ||
| accepts_nested_attributes_for :items, allow_destroy: true | ||
| before_destroy :check_for_question_associations | ||
|
|
||
| validate :validate_questionnaire | ||
| validates :name, presence: true | ||
| validates :max_question_score, :min_question_score, numericality: true | ||
|
|
||
|
|
||
| # after_initialize :post_initialization | ||
| # @print_name = 'Review Rubric' | ||
|
|
@@ -28,7 +29,7 @@ def symbol | |
| def get_assessments_for(participant) | ||
| participant.reviews | ||
| end | ||
|
|
||
| # validate the entries for this questionnaire | ||
| def validate_questionnaire | ||
| errors.add(:max_question_score, 'The maximum item score must be a positive integer.') if max_question_score < 1 | ||
|
|
@@ -64,9 +65,9 @@ def self.copy_questionnaire_details(params) | |
| questionnaire | ||
| end | ||
|
|
||
| # Check_for_question_associations checks if questionnaire has associated questions or not | ||
| # Check_for_question_associations checks if questionnaire has associated items or not | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check_for_item_associations |
||
| def check_for_question_associations | ||
| if questions.any? | ||
| if items.any? | ||
| raise ActiveRecord::DeleteRestrictionError.new( "Cannot delete record because dependent questions exist") | ||
| end | ||
| end | ||
|
|
@@ -82,4 +83,4 @@ def as_json(options = {}) | |
| hash['instructor'] ||= { id: nil, name: nil } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class QuestionnaireType < ApplicationRecord | ||
| # Validations | ||
| validates :name, presence: true, uniqueness: true | ||
|
|
||
| # Associations (if any later) | ||
| # has_many :questionnaires, foreign_key: :questionnaire_type, primary_key: :name | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class AddTextareaWidthToItems < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :items, :textarea_width, :integer | ||
| add_column :items, :textarea_height, :integer | ||
| add_column :items, :textbox_width, :integer | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class AddColsRowsToItems < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :items, :col_names, :string | ||
| add_column :items, :row_names, :string | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| begin | ||
| # Create an instritution | ||
| inst_id = Institution.create!( | ||
| name: 'North Carolina State University' | ||
| ).id | ||
|
|
||
| Role.create!(id: 1, name: 'Super Administrator') | ||
| Role.create!(id: 2, name: 'Administrator') | ||
| Role.create!(id: 3, name: 'Instructor') | ||
| Role.create!(id: 4, name: 'Teaching Assistant') | ||
| Role.create!(id: 5, name: 'Student') | ||
|
|
||
| # Create an admin user | ||
| User.create!( | ||
| name: 'admin', | ||
| email: '[email protected]', | ||
| password: 'password123', | ||
| full_name: 'admin admin', | ||
| institution_id: 1, | ||
| role_id: 1 | ||
| ) | ||
| # Create an institution | ||
| inst = Institution.find_or_create_by!( | ||
| name: 'North Carolina State University' | ||
| ) | ||
| inst_id = inst.id | ||
|
|
||
| # Create Roles | ||
| Role.find_or_create_by!(id: 1, name: 'Super Administrator') | ||
| Role.find_or_create_by!(id: 2, name: 'Administrator') | ||
| Role.find_or_create_by!(id: 3, name: 'Instructor') | ||
| Role.find_or_create_by!(id: 4, name: 'Teaching Assistant') | ||
| Role.find_or_create_by!(id: 5, name: 'Student') | ||
|
|
||
| # Create an admin user | ||
| User.find_or_create_by!(name: 'admin') do |user| | ||
| user.email = '[email protected]' | ||
| user.password = 'password123' | ||
| user.full_name = 'admin admin' | ||
| user.institution_id = 1 | ||
| user.role_id = 1 | ||
| end | ||
|
|
||
| # Check if we should generate random data | ||
| # We assume if instructors exist, we've already seeded random data | ||
| if User.where(role_id: 3).exists? | ||
| puts "Random data already seeded (Instructors found). Skipping..." | ||
| else | ||
| # Generate Random Users | ||
| num_students = 48 | ||
| num_assignments = 8 | ||
|
|
@@ -122,7 +127,44 @@ | |
| team_id: team_ids[i%num_teams], | ||
| ).id | ||
| end | ||
| end | ||
|
|
||
| questionnaire_type_names = [ | ||
| 'Review', | ||
| 'Author feedback', | ||
| 'Teammate review', | ||
| 'Survey', | ||
| 'Quiz', | ||
| 'Bookmark rating', | ||
| 'Teammate review', | ||
| 'Assignment survey', | ||
| 'Course evaluation', | ||
| 'Global survey' | ||
| ] | ||
|
|
||
| questionnaire_types = {} | ||
| questionnaire_type_names.each do |type_name| | ||
| questionnaire_types[type_name] = QuestionnaireType.find_or_create_by!(name: type_name) | ||
| end | ||
| puts "Created questionnaire types: #{questionnaire_types.keys.join(', ')}" | ||
|
|
||
| question_type_names = [ | ||
| 'Section header', | ||
| 'Table header', | ||
| 'Column header', | ||
| 'Criterion', | ||
| 'Text field', | ||
| 'Text area', | ||
| 'Dropdown', | ||
| 'Multiple choice', | ||
| 'Scale', | ||
| 'Grid', | ||
| 'Checkbox', | ||
| 'Upload', | ||
| ] | ||
|
|
||
| rescue ActiveRecord::RecordInvalid => e | ||
| puts e, 'The db has already been seeded' | ||
| question_types = {} | ||
| question_type_names.each do |type_name| | ||
| question_types[type_name] = QuestionType.find_or_create_by!(name: type_name) | ||
| end | ||
| puts "Created item types: #{question_types.keys.join(', ')}" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ItemTypesController