======================
Intervention Dashboard
======================

We provide an Intervention tab that takes the user to a kind of dashboard with
two fieldsets.  The first serves as a launch point for the search student
intervention view where the user can find a student to which they have access.
The second fieldset functions similar to an email inbox, listing all messages
and goals that the user is responsible for, most recently created first.

First we need to set up the school.

    >>> from schooltool.app.browser.ftests import setup
    >>> setup.setUpBasicSchool()

We'll create a principal, a teacher and three students as well as browser
instances for each.

    >>> from schooltool.intervention.browser.ftests import ftests
    >>> ftests.addPerson('Principal', 'Principal', 'principal', 'pwd',
    ...                  groups=['administrators'])
    >>> ftests.addPerson('Teacher1', 'Teacher1', 'teacher1', 'pwd')
    >>> ftests.addPerson('Student1', 'Student1', 'student1', 'pwd')
    >>> ftests.addPerson('Student2', 'Student2', 'student2', 'pwd')
    >>> ftests.addPerson('Student3', 'Student3', 'student3', 'pwd')
    >>> principal = setup.logIn('principal', 'pwd')
    >>> teacher1 = setup.logIn('teacher1', 'pwd')
    >>> student1 = setup.logIn('student1', 'pwd')
    >>> student2 = setup.logIn('student2', 'pwd')
    >>> student3 = setup.logIn('student3', 'pwd')

The teacher will advise the first and third students only.  The principal,
by virtue of being in the administrators group, will already have access to
all students.  Keep in mind that all users, including staff, can be
considered students in this system.

    >>> ftests.addAdvisors('Student1', ['teacher1'])
    >>> ftests.addAdvisors('Student3', ['teacher1'])

Now we can create variables with urls for the student intervention center as
well as add messages and goals.

    >>> schoolyear_url = 'http://localhost/schooltool.interventions/2005-2006'

    >>> student1_url = schoolyear_url + '/student1'
    >>> student1_messages = student1_url + '/messages'
    >>> student1_goals = student1_url + '/goals'
    >>> add_msg_student1 = student1_messages + '/+/addMessage.html'
    >>> add_goal_student1 = student1_goals + '/+/addGoal.html'

    >>> student2_url = schoolyear_url + '/student2'
    >>> student2_messages = student2_url + '/messages'
    >>> student2_goals = student2_url + '/goals'
    >>> add_msg_student2 = student2_messages + '/+/addMessage.html'
    >>> add_goal_student2 = student2_goals + '/+/addGoal.html'

    >>> student3_url = schoolyear_url + '/student3'
    >>> student3_messages = student3_url + '/messages'
    >>> student3_goals = student3_url + '/goals'
    >>> add_msg_student3 = student3_messages + '/+/addMessage.html'
    >>> add_goal_student3 = student3_goals + '/+/addGoal.html'

We need to visit the student intervention centers using the traversal adapter
to auto-vivify the InterventionStudent objects.

    >>> for student in ['Student1', 'Student2', 'Student3']:
    ...     principal.open('http://localhost/persons')
    ...     principal.getLink(student).click()
    ...     principal.getLink('Intervention Center').click()

We'll have the principal add messages and goals for all three students with
varying sets of persons responsible selected.  This will allow us to
test the two search fieldsets of the dashboard.

    >>> ftests.addMessage(principal, add_msg_student1, ['principal', 'student1'])
    From: ...
    >>> ftests.addMessage(principal, add_msg_student1, ['principal', 'teacher1'])
    From: ...

    >>> ftests.addMessage(principal, add_msg_student2, ['principal', 'student2'])
    From: ...
    >>> ftests.addMessage(principal, add_msg_student2, ['principal', 'teacher1'])
    From: ...
    >>> ftests.addEditGoal(principal, add_goal_student2, ['principal', 'student2'])
    From: ...

    >>> ftests.addEditGoal(principal, add_goal_student3, ['principal', 'teacher1', 'student3'])
    From: ...


Search Students Fieldset
------------------------

The first fieldset has a Find button that lists all the students the user has
the right to access.  The princiapl will be able to access all users.  The
teacher will only be able to access the students he advises, student1.  Both
students will have no access to any students, including themselves.

    >>> principal.getLink('Intervention').click()
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Default</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Manager</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/principal">Principal</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/principal">Principal</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/teacher1">Teacher1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/teacher1">Teacher1</a>

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>

    >>> student1.getLink('Intervention').click()
    >>> student1.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> student1.printQuery("id('content-body')//table/tbody//a")

    >>> student2.getLink('Intervention').click()
    >>> student2.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> student2.printQuery("id('content-body')//table/tbody//a")

    >>> student3.getLink('Intervention').click()
    >>> student3.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> student3.printQuery("id('content-body')//table/tbody//a")

We note that the url has changed because we process the student searchs with
another view that doesn't even have am Inbox fieldset.  This is becuase we
didn't want a large result set to push the inbox down in the view.  Better
that the Inbox dominate the inbox view and the student result set be returned
separately.

    >>> principal.url
    'http://localhost/persons/principal/intervention_tab/search_students.html'
    >>> principal.printQuery("id('content-body')//fieldset//legend/b")
    <b>Students</b>

Since the principal has access to more than one student, we'll test the search
and filter student widget with him as user.  The first filter will be for any
user with 'student' in their name.

    >>> principal.getControl(name='SEARCH_STUDENT_NAME').value = 'student'
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>

The name filter also matches usernames, so that even though 'manager' does not
match any user's first or last name, it will bring up the manager's intervenion
center.

    >>> principal.getControl(name='SEARCH_STUDENT_NAME').value = 'manager'
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Default</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Manager</a>

We have a Clear button that restores the search criteria to empty, thus
returning all users again.

    >>> principal.getControl(name='CLEAR_STUDENT_SEARCH').click()
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Default</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/manager">Manager</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/principal">Principal</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/principal">Principal</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/teacher1">Teacher1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/teacher1">Teacher1</a>

Finally, by clicking the Goals Only checkbox the search results will be limited
to students that have goals, in our case, student2 and student3.

    >>> principal.getControl(name='WITH_GOALS_ONLY').value = 'checked'
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>

We will provide a button for the user to return from the search student view
back to the inbox in case it is not obvious to use the Intervention tab.

    >>> principal.getLink('Return to Inbox').click()
    >>> principal.url
    'http://localhost/persons/principal/intervention_tab'
    >>> principal.printQuery("id('content-body')//fieldset//legend/b")
    <b>Search Students</b>
    <b>Inbox</b>

We can also search for students with filtering directly from the inbox view.

    >>> principal.getLink('Intervention').click()
    >>> principal.getControl(name='SEARCH_STUDENT_NAME').value = 'student'
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student1">Student1</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>

    >>> principal.getLink('Intervention').click()
    >>> principal.getControl(name='WITH_GOALS_ONLY').value = 'checked'
    >>> principal.getControl(name='SEARCH_STUDENT_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student2">Student2</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>
    <a href="http://localhost/schooltool.interventions/2005-2006/student3">Student3</a>


Inbox Fieldset
--------------

The second fieldset lists all messages and goals that the user is allowed to
view.  It can also be filtereed.  In the case of a student, any message or
goal that the creator intended him to see will appear in this list.
The links are for the view only views.

    >>> principal.getLink('Intervention').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/1...

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...

    >>> student1.getLink('Intervention').click()
    >>> student1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/1...

    >>> student2.getLink('Intervention').click()
    >>> student2.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/1...

    >>> student3.getLink('Intervention').click()
    >>> student3.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...

There is a name filter that can limit the list to students whose name contains
a search string.

    >>> principal.getControl(name='SEARCH_NAME').value = 'student1'
    >>> principal.getControl(name='SEARCH_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/1...

    >>> principal.getControl(name='SEARCH_NAME').value = 'student2'
    >>> principal.getControl(name='SEARCH_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/1...

    >>> principal.getControl(name='SEARCH_NAME').value = 'student3'
    >>> principal.getControl(name='SEARCH_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...

We have a Clear button that restores the search criteria to empty, thus
returning all messages and goals again.

    >>> principal.getControl(name='CLEAR_SEARCH').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/messages/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/1...

Finally, by clicking the Goals Only checkbox the search results will be limited
to students that have goals, in our case, student2 and student3.

    >>> principal.getControl(name='GOALS_ONLY').value = 'checked'
    >>> principal.getControl(name='SEARCH_BUTTON').click()
    >>> principal.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student2/schoolyears/2005-2006/goals/1...


UpdatePersonResponsibleIndex() tests
------------------------------------

We want to funcitonally test the fact that our persons_responsible index is
properly updated when the attribute is changed in a goal.  A simple test will
suffice.  We'll remove the teacher from a goal to see that it is no longer
visable in the teacher's inbox.  Then we'll put the teacher back and see it
reappear.

    >>> edit_goal_url = student3_goals + '/1/editGoal.html'
    >>> ftests.addEditGoal(principal, edit_goal_url, ['principal', 'student3'])

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...

    >>> ftests.addEditGoal(principal, edit_goal_url, ['principal', 'teacher1', 'student3'])

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...

Now we need to test that the same behaviour will occur if an as yet non-existent
view (we'll provide a test-only version) changes a message's recipients or a
goal's persons_responsible.

    >>> message_url = 'http://localhost/persons/student1/schoolyears/2005-2006/messages/2/update_responsible.html'
    >>> goal_url = 'http://localhost/persons/student3/schoolyears/2005-2006/goals/1/update_responsible.html'

First, we'll remove the teacher from the message.  The teacher's inbox should
now have the message missing.

    >>> principal.open(message_url + '?remove=teacher1')
    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...

We'll add the teacher back to the message.  The teacher's inbox should now
have the message again.

    >>> principal.open(message_url + '?add=teacher1')
    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...

We'll do the same two sets of tests for the goal.

    >>> principal.open(goal_url + '?remove=teacher1')
    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...

    >>> principal.open(goal_url + '?add=teacher1')
    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.printQuery("id('content-body')//table/tbody//a")
    <a href="http://localhost/persons/student3/schoolyears/2005-2006/goals/1...
    <a href="http://localhost/persons/student1/schoolyears/2005-2006/messages/2...


Traversal adapter tests
-----------------------

Because the links presented to the user in the Inbox fieldset are built for
using a set of traversal adapters, we need to test visiting them.  Additionaly,
once we have visited a message or goal, there is an Intervention Center link
present that adds an addition to the traversal of the link, so we need to
visit them as well.

First, we'll visit the goal and hit the Intervention Center button.

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.getLink('x').click()
    >>> teacher1.printQuery("id('content-body')//h3")
    <h3>Goal 1 for Student3 Student3 added ...</h3>

    >>> teacher1.getLink('Intervention Center').click()
    >>> teacher1.printQuery("id('content-body')//table/tr/td[3]/a/text()")
    Student3 Student3
    >>> teacher1.printQuery("id('content-body')//legend/b")
    <b>Messages and Observations</b>
    <b>Goals and Interventions</b>
    <b>Report Sheets</b>
    <b>Change of Status Messages</b>

We'll do the same with the message.

    >>> teacher1.getLink('Intervention').click()
    >>> teacher1.getLink('x', index=1).click()
    >>> teacher1.printQuery("id('content-body')//h3")
    <h3>Message from: Principal Principal sent ...</h3>

    >>> teacher1.getLink('Intervention Center').click()
    >>> teacher1.printQuery("id('content-body')//table/tr/td[3]/a/text()")
    Student1 Student1
    >>> teacher1.printQuery("id('content-body')//legend/b")
    <b>Messages and Observations</b>
    <b>Goals and Interventions</b>
    <b>Report Sheets</b>
    <b>Change of Status Messages</b>

