Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Student Interests can now be recorded Issue

  • Loading branch information...
commit 1dba597a8de26ed7d687e90d67cf57772edb9501 1 parent 24355b9
Nathan Neff authored
16 grails-app/controllers/org/bworks/bworksdb/ContactController.groovy
View
@@ -1,9 +1,8 @@
-
-
package org.bworks.bworksdb
class ContactController {
+ def studentService
def index = { redirect(action:list,params:params) }
// the delete, save and update actions only accept POST requests
@@ -123,14 +122,21 @@ class ContactController {
if (params.id) {
contactInstance = Contact.get(params.id)
+ // println 'foo' * 1000;
+ // println params['students[0]']
contactInstance.properties = params
contactInstance.validate()
//Loop through students, validate and save
- contactInstance.students.each {student ->
+ contactInstance.students.eachWithIndex {student, idx ->
if (student.validate()) {
student.save()
+ println "params are: " + params
+ // println "index is: " * 10
+ println idx
+ // println params.students[idx]
+ studentService.saveInterests(student, params["studentInterests[${idx}]"])
}
else {
student.errors.allErrors.each {
@@ -151,12 +157,12 @@ class ContactController {
def newInlineStudent = {
// Available interests are all programs
def interests = Program.findAll()
- render(template: 'newInlineStudent', model: ['idx': params.idx, interests:interests])
+ render(template: 'newInlineStudent', model: ['idx': params.idx, interests:interests])
}
//Replace inline div with "New student" button
def cancelInlinestudent = {
- render(template: 'newInlinestudentButton', model: ['studentidx': params.studentidx])
+ render(template: 'newInlinestudentButton', model: ['studentidx': params.studentidx])
}
/**
76 grails-app/services/org/bworks/bworksdb/StudentService.groovy
View
@@ -0,0 +1,76 @@
+package org.bworks.bworksdb
+
+class StudentService {
+
+ boolean transactional = true
+
+ // Loop through interests, and if an interest is present,
+ // make sure the student has that interest active.
+ // Otherwise, make sure that any interests that are *not* present are rendered
+ // inactive if they exist for that student. Read on. You'll get it.
+ // Interests is an array of goofy Grails checkboxes
+ def saveInterests(student, interests) {
+ println "--" * 200
+ if (interests == null ) return;
+ println "Milf" * 10
+ def allPrograms = Program.findAll()
+ allPrograms.each { prog ->
+ println "Checking program: " + prog.id
+ // println interests.className
+ // See if there's a checkbox in the array that's selected
+ if (interests && interests instanceof java.lang.String) {
+ println "Trying to match " + interests.toString() + " with " + prog.id.toString()
+ if (interests.toString() == prog.id.toString()) {
+ println "Matched" * 10
+ if (!Interest.findWhere(student:student, program:prog, active:true)) {
+ println "adding" * 10
+ student.addToInterests(new Interest(program:prog, active:true))
+ } else {
+ println "Exists already, mofo!"
+ }
+ }
+ else {
+ def i = Interest.findWhere(student:student, program:prog, active:true)
+ if (i) {
+ println "Blasting from foomanchu " + i
+ i.active = false
+ i.save()
+ }
+ }
+ }
+ else if (interests && interests instanceof java.lang.String[]) {
+ def found = false
+ interests.each {
+ println "it : " + it
+ println "prog.id : " + prog.id
+ if (it.toString() == prog.id.toString()) {
+ found = true
+ println "Yes sld"
+ if (!Interest.findWhere(student:student, program:prog, active:true)) {
+ println "mached array" * 10
+ student.addToInterests(new Interest(program:prog, active:true))
+ }
+ }
+ }
+ if (!found) {
+ def i = Interest.findWhere(student:student, program:prog, active:true)
+ if (i) {
+ println "Blasting from interests[]" + i
+ i.active = false
+ i.save()
+ }
+ }
+
+ }
+ else {
+ // Momma said knock you out!
+ def i = Interest.findWhere(student:student, program:prog, active:true)
+ if (i) {
+ println "Blasting from wherever" + i
+ i.active = false
+ i.save()
+ }
+ }
+ }
+ }
+}
18 grails-app/taglib/MiscTagLib.groovy
View
@@ -14,17 +14,23 @@ class MiscTagLib {
programs.each { prog ->
// Note: Need to search for active == true, also
def checkBoxName = "studentInterests[${idx}]"
- out << "<label for='${checkBoxName}'>${prog.name}</label>"
- println "Searching for ${student} interested in ${prog}"
- if (Interest.findByStudentAndProgram(student, prog)) {
- println "Jar Jar! " * 1000
+ // TODO Find out why I can't put AndActive at the end of
+ // this dynamic query
+ def c = Interest.createCriteria()
+ def i = c.list {
+ eq("student", student)
+ eq("program", prog)
+ eq("active", true)
+ }
+
+ if (i) {
out << g.checkBox(value:prog.id, checked:true, name:checkBoxName)
- out << '<br />'
}
else {
out << g.checkBox(value:prog.id, checked:false, name:checkBoxName)
- out << '<br />'
}
+ out << "<label for='${checkBoxName}'>${prog.name}</label>"
+ out << '<br />'
}
return out
}
17 test/unit/org/bworks/bworksdb/StudentServiceTests.groovy
View
@@ -0,0 +1,17 @@
+package org.bworks.bworksdb
+
+import grails.test.*
+
+class StudentServiceTests extends GrailsUnitTestCase {
+ protected void setUp() {
+ super.setUp()
+ }
+
+ protected void tearDown() {
+ super.tearDown()
+ }
+
+ void testSomething() {
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.