#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SmartInstitute.Client.Automation.Views;
using SmartInstitute.Client.Automation.Commands.Students;
using SmartInstitute.Automation.Commands.Framework;
using SmartInstitute;
using SmartInstitute.ObjectModel;
using SmartInstitute.Client.Automation.Helpers;
using SmartInstitute.Client.Automation.Commands.UI;
using SmartInstitute.Automation.Commands.Students;
#endregion
namespace SmartInstitute.Client.Automation.Documents.StudentDocuments
{
public partial class StudentRegistrationUI : StudentDocumentUIBase
{
public StudentRegistrationUI()
{
InitializeComponent();
}
private void StudentRegistrationUI_Load(object sender, EventArgs e)
{
if (!base.DesignMode)
{
this.ShowRegistration();
this.BindView();
this.profileView.ShowProfile(base.Document.StudentModel);
}
}
private void BindView()
{
base.Document.OnAddCourse += new StudentDocument.AddCourseHandler(_View_OnAddCourse);
base.Document.OnAddSection += new StudentDocument.AddSectionHandler(_View_OnAddSection);
}
bool _View_OnAddCourse( Course course )
{
return false;
}
bool _View_OnAddSection( CourseSection courseSection )
{
this.AddSection(courseSection);
return true;
}
private void mapCourseControl_OnAccept( CourseSection CourseSection, Course countedCourse, Course attendedCourse )
{
this.AddNewCourse(CourseSection);
}
#region Actions
private void dropToolStripButton_Click(object sender, EventArgs e)
{
this.DropSelectedCourse();
}
/// <summary>
/// Matk selected courses as dropped
/// </summary>
private void DropSelectedCourse()
{
decimal dropRate;
string percent = dropRateToolStripTextBox.Text.TrimEnd('%');
if (decimal.TryParse(percent, out dropRate))
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
// if not previously dropped
if (!regCourse.Dropped)
{
regCourse.Dropped = true;
}
else
{
regCourse.Dropped = false;
// undrop
}
}
}
else
{
}
this.UpdateUI();
}
private void deleteToolStripButton_Click(object sender, EventArgs e)
{
this.DeleteSelectedCourse();
}
private void nonCreditToolStripButton_Click(object sender, EventArgs e)
{
if (nonCreditToolStripButton.CheckState == CheckState.Checked)
this.MakeSelectedCourseNonCredit();
else if (nonCreditToolStripButton.CheckState == CheckState.Unchecked)
this.MakeSelectedCourseCredited();
// update the tool strip states
this.UpdateState();
}
/// <summary>
/// Mark selected courses as dropped
/// </summary>
private void DeleteSelectedCourse()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
regCourse.MarkToDelete();
}
this.UpdateUI();
}
private void UpdateUI()
{
this.ShowCourses();
}
/// <summary>
/// Mark selected courses as non credit
/// </summary>
private void MakeSelectedCourseNonCredit()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
regCourse.NonCredit = true;
}
this.UpdateUI();
}
/// <summary>
/// Mark selected courses as credited
/// </summary>
private void MakeSelectedCourseCredited()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
regCourse.NonCredit = false;
}
this.UpdateUI();
}
private void validToolStripButton_Click(object sender, EventArgs e)
{
if (validToolStripButton.CheckState == CheckState.Checked)
this.ValidateSelectedCourses();
else if (validToolStripButton.CheckState == CheckState.Unchecked)
this.InvalidateSelectedCourses();
}
private void validateAllToolStripButton_Click(object sender, EventArgs e)
{
grdCourses.SelectAll();
this.ValidateSelectedCourses();
grdCourses.ClearSelection();
}
private void grdCourses_SelectionChanged(object sender, EventArgs e)
{
//this.UpdateToolbarState();
this.UpdateState();
}
private void UpdateState()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
if (regCourse != null)
{
if (regCourse.NonCredit)
{
nonCreditToolStripButton.CheckState = CheckState.Checked;
nonCreditToolStripButton.Text = "Credit";
}
else
{
nonCreditToolStripButton.CheckState = CheckState.Unchecked;
nonCreditToolStripButton.Text = "Non-Credit";
}
if (regCourse.Dropped)
{
dropToolStripButton.Text = "Undrop";
}
else
{
dropToolStripButton.Text = "Drop";
}
}
}
}
private void UpdateToolbarState()
{
// first reset to original state
validateAllToolStripButton.CheckState = CheckState.Unchecked;
bool anyNonCredit = false;
bool allNonCredit = true;
bool anyValid = false;
bool allValid = true;
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
anyNonCredit |= regCourse.NonCredit;
allNonCredit &= regCourse.NonCredit;
anyValid |= regCourse.Valid;
allValid &= regCourse.Valid;
}
if (allNonCredit)
nonCreditToolStripButton.CheckState = CheckState.Checked;
else
{
if (anyNonCredit)
nonCreditToolStripButton.CheckState = CheckState.Indeterminate;
else
nonCreditToolStripButton.CheckState = CheckState.Unchecked;
}
if (allValid)
validToolStripButton.CheckState = CheckState.Checked;
else
{
if (anyValid)
validToolStripButton.CheckState = CheckState.Indeterminate;
else
validToolStripButton.CheckState = CheckState.Unchecked;
}
}
private void ValidateSelectedCourses()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
regCourse.Valid = true;
}
}
private void InvalidateSelectedCourses()
{
foreach (DataGridViewRow row in grdCourses.SelectedRows)
{
CourseTakenByStudent2 regCourse = row.Tag as CourseTakenByStudent2;
regCourse.Valid = false;
}
}
/// <summary>
/// Add the CourseSection to student's course list
/// </summary>
/// <param name="CourseSection"></param>
void AddSection(CourseSection section)
{
this.AddNewCourse(section);
}
private void AddNewCourse(CourseSection section)
{
if (section.Status == (int)SectionStatusEnum.Closed)
{
if (MessageBox.Show(this,
"The Section you are trying to add is --" + section.Status.ToString() + "-- . Are you sure you want to do this?",
"Warning",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.No)
return;
}
if (section.Status == (int)SectionStatusEnum.Reserved || section.Status == (int)SectionStatusEnum.Cancel)
{
MessageBox.Show(this, "You can not add a " + section.Status.ToString() + " CourseSection");
return;
}
// 1 Find the offered course for the class
CourseModel courseModel = _Application.Courses[section.CourseID];
Course course = courseModel.Course;
// 2. Add a new course
CourseTakenByStudent2 courseTaken = new CourseTakenByStudent2(
new CourseTakenByStudent(0, base.Document.StudentModel.Profile.ID,
section.CourseID, section.ID, 1, false, DateTime.Now));
courseTaken.Course = course;
courseTaken.Section = section;
base.Document.StudentModel.Profile.CourseTakenByStudentCollection.Add(courseTaken);
// 4. Refresh UI
this.UpdateUI();
}
#endregion
#region Loading
private void ShowRegistration()
{
this.LoadRegistration();
this.UpdateUI();
}
private void LoadRegistration()
{
// If courses are not loaded yet, try loading it in background
if (null == base.Document.StudentModel.AllCourses)
{
progress.Start();
worker.RunWorkerAsync();
}
}
/// <summary>
/// Loads students registration in background thread
/// Warning! No UI access
/// </summary>
private void LoadRegistrationInBackground()
{
ICommand command = new LoadStudentDetailCommand(base.Document.StudentModel.Profile.ID);
command.Execute();
}
/// <summary>
/// Render the grid from the courses taken by the student
/// It clears and populates the grid from the current registration
/// object's course taken by student list
/// </summary>
private void ShowCourses()
{
grdCourses.Rows.Clear();
Font strikeFont = new Font(base.Font, FontStyle.Strikeout);
// 1. Construct styles for courses of different state
DataGridViewCellStyle dropStyle = new DataGridViewCellStyle(grdCourses.DefaultCellStyle);
dropStyle.BackColor = Color.LightGray;
DataGridViewCellStyle deleteStyle = new DataGridViewCellStyle(grdCourses.DefaultCellStyle);
deleteStyle.ForeColor = Color.Gray;
deleteStyle.Font = strikeFont;
DataGridViewCellStyle noncreditStyle = new DataGridViewCellStyle(grdCourses.DefaultCellStyle);
noncreditStyle.ForeColor = Color.Blue;
// class ID style
DataGridViewCellStyle classIDStyle = new DataGridViewCellStyle( grdCourses.DefaultCellStyle );
classIDStyle.ForeColor = Color.Green;
DataGridViewCellStyle invalidStatusStyle = new DataGridViewCellStyle(grdCourses.DefaultCellStyle);
invalidStatusStyle.ForeColor = Color.DarkRed;
// 2. Populate the grid with current courses
int totalLecCredit = 0, totalSciCredit = 0, totalCompCredit = 0;
foreach (CourseTakenByStudent2 courseTaken in base.Document.StudentModel.Profile.CourseTakenByStudentCollection)
{
// prepare the columns
string courseInfo =
string.Format( "{0} - {1} ({2})",
courseTaken.Section.Description, courseTaken.Section.Title, courseTaken.Section.Capacity )
+ Environment.NewLine;
// Build the routine text
if (null != courseTaken.Section.SectionRoutineCollection)
{
foreach (SectionRoutine routine in courseTaken.Section.SectionRoutineCollection)
{
courseInfo += routine.ToString();
}
}
string creditInfo = courseTaken.Course.LecCredit.ToString() + " " +
courseTaken.Course.SciCredit.ToString() + " " +
courseTaken.Course.CompCredit.ToString();
string status = (courseTaken.Dropped ? "Dropped" : String.Empty ) + Environment.NewLine +
(courseTaken.Valid ? "Valid " : "Invalid ") + Environment.NewLine + ( courseTaken.NonCredit ? "Non-Credit" : String.Empty ) ;
totalLecCredit += courseTaken.Course.LecCredit;
totalSciCredit += courseTaken.Course.SciCredit;
totalCompCredit += courseTaken.Course.CompCredit;
// add the row
int newRowIndex = grdCourses.Rows.Add( courseTaken.Section.ClassID, courseInfo, creditInfo, status );
DataGridViewRow newRow = grdCourses.Rows[newRowIndex];
// Apply different style to cells according to different state
if (courseTaken.Dropped)
newRow.Cells[1].Style.ApplyStyle(dropStyle);
if (courseTaken.IsDeleted)
newRow.Cells[1].Style.ApplyStyle(deleteStyle);
if (courseTaken.NonCredit)
newRow.Cells[1].Style.ApplyStyle(noncreditStyle);
if (!courseTaken.Valid)
newRow.Cells[3].Style.ApplyStyle(invalidStatusStyle);
// class id style
newRow.Cells[0].Style.ApplyStyle( classIDStyle );
newRow.Tag = courseTaken;
}
lecCreditLabel.Text = totalLecCredit.ToString();
scienceCreditLabel.Text = totalSciCredit.ToString();
compCreditLabel.Text = totalCompCredit.ToString();
this.CheckConflict();
strikeFont.Dispose();
}
#endregion
private void tabs_Selected(object sender, TabControlEventArgs e)
{
if (tabs.SelectedTab == this.assessmentTab)
this.assessmentControl.PopulateUI();
if( tabs.SelectedTab == this.courseTab)
this.ShowRegistration();
}
private void refreshToolStripButton_Click(object sender, EventArgs e)
{
this.RefreshRegistration();
}
private void RefreshRegistration()
{
if (MessageBox.Show(this, "Any changes you have made will be discarded, do you want to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
return;
this.ShowRegistration();
}
private void CheckConflict()
{
foreach( DataGridViewRow row1 in this.grdCourses.Rows )
{
CourseTakenByStudent2 course1 = row1.Tag as CourseTakenByStudent2;
if (null != course1.Section.SectionRoutineCollection)
{
foreach (SectionRoutine routine1 in course1.Section.SectionRoutineCollection)
{
foreach (DataGridViewRow row2 in this.grdCourses.Rows)
{
if (row1 != row2)
{
CourseTakenByStudent2 course2 = row2.Tag as CourseTakenByStudent2;
if (null != course2.Section.SectionRoutineCollection)
{
foreach (SectionRoutine routine2 in course2.Section.SectionRoutineCollection)
{
if (routine1.Overlap(routine2))
{
// Conflict
row1.Cells[1].Style.BackColor =
row2.Cells[1].Style.BackColor = Color.Red;
row1.Cells[1].Style.ForeColor =
row2.Cells[1].Style.ForeColor = Color.LightYellow;
}
}
}
}
}
}
}
}
}
private void CloseMe()
{
ICommand command = new CloseCurrentDocumentCommand();
command.Execute();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
// This method will run on a thread other than the UI thread.
// Be sure not to manipulate any Windows Forms controls created
// on the UI thread from this method.
this.LoadRegistrationInBackground();
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (null == e.Error)
{
this.UpdateUI();
}
else
{
MessageBox.Show(this, e.Error.Message,
"Load Registration", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.progress.Stop();
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
this.SaveRegistration();
}
private void SaveRegistration()
{
// Mark this student model as dirty so that it is saved
base.Document.StudentModel.IsDirty = true;
base.Document.StudentModel.NotifyChange("Save");
}
}
}