Option Explicit On 
Option Strict On
Imports System.Data
Imports System.Data.SqlClient

Public Class ClassAssignments
	Inherits System.Web.UI.Page
	Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents daClassAssignments As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents DG_Assignments As System.Web.UI.WebControls.DataGrid
	Protected WithEvents DsClassAssignments1 As AllSaints.dsClassAssignments
	Protected WithEvents hlnkNewAssignment As System.Web.UI.WebControls.HyperLink
	Protected WithEvents lnkBack2eClass As System.Web.UI.WebControls.LinkButton
	Private _PageID As Integer
#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.daClassAssignments = New System.Data.SqlClient.SqlDataAdapter()
		Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
		Me.SqlConn = New System.Data.SqlClient.SqlConnection()
		Me.DsClassAssignments1 = New AllSaints.dsClassAssignments()
		CType(Me.DsClassAssignments1, System.ComponentModel.ISupportInitialize).BeginInit()
		'
		'daClassAssignments
		'
		Me.daClassAssignments.SelectCommand = Me.SqlSelectCommand1
		Me.daClassAssignments.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetClassAssignment", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("AsignID", "AsignID"), New System.Data.Common.DataColumnMapping("ClassPageID", "ClassPageID"), New System.Data.Common.DataColumnMapping("AsignTitle", "AsignTitle"), New System.Data.Common.DataColumnMapping("Subject", "Subject"), New System.Data.Common.DataColumnMapping("StartDate", "StartDate"), New System.Data.Common.DataColumnMapping("EndDate", "EndDate"), New System.Data.Common.DataColumnMapping("SubmitDate", "SubmitDate"), New System.Data.Common.DataColumnMapping("Attachment", "Attachment"), New System.Data.Common.DataColumnMapping("AssignmentHTML", "AssignmentHTML")}), New System.Data.Common.DataTableMapping("Table1", "Table1", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("AsignID", "AsignID"), New System.Data.Common.DataColumnMapping("ClassPageID", "ClassPageID"), New System.Data.Common.DataColumnMapping("AsignTitle", "AsignTitle"), New System.Data.Common.DataColumnMapping("Subject", "Subject"), New System.Data.Common.DataColumnMapping("StartDate", "StartDate"), New System.Data.Common.DataColumnMapping("EndDate", "EndDate"), New System.Data.Common.DataColumnMapping("SubmitDate", "SubmitDate"), New System.Data.Common.DataColumnMapping("Attachment", "Attachment"), New System.Data.Common.DataColumnMapping("AssignmentHTML", "AssignmentHTML")})})
		'
		'SqlSelectCommand1
		'
		Me.SqlSelectCommand1.CommandText = "[GetClassAssignment]"
		Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure
		Me.SqlSelectCommand1.Connection = Me.SqlConn
		Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@AID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ClassID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		'
		'SqlConn
		'
		Me.SqlConn.ConnectionString = "data source=localhost;initial catalog=Allsaints;integrated security=SSPI;persist " & _
		"security info=False;workstation id=GALAXY;packet size=4096"
		'
		'DsClassAssignments1
		'
		Me.DsClassAssignments1.DataSetName = "dsClassAssignments"
		Me.DsClassAssignments1.Locale = New System.Globalization.CultureInfo("en-US")
		Me.DsClassAssignments1.Namespace = "http://www.tempuri.org/dsClassAssignments.xsd"
		CType(Me.DsClassAssignments1, System.ComponentModel.ISupportInitialize).EndInit()

	End Sub

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
		If SqlConn.State = ConnectionState.Closed Then SqlConn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")
	End Sub

#End Region

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		If SqlConn.State = ConnectionState.Closed Then SqlConn.ConnectionString = ConfigurationSettings.AppSettings("sConnection")

		If Not Page.IsPostBack Then
			'if the posted id is not valid then
			If Session("ClassPageID") Is Nothing OrElse Not IsNumeric(Session("ClassPageID").ToString) Then
				Response.Redirect("default.aspx", True)
			End If
			Dim thisPage As Integer = CType(Session("ClassPageID"), Integer)
			Dim cmd As New SqlCommand()
			Dim sPageType As String = ""
			With cmd
				.CommandText = "Select PageType from as_pages where ID=" & thisPage
				.CommandType = CommandType.Text
				.Connection = SqlConn
				If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
				Dim oValue As Object = .ExecuteScalar()
				If Not IsDBNull(oValue) Then
					sPageType = oValue.ToString.Trim
				End If
			End With
			'if the posted id is not an eClass type pgae then
			If (sPageType.Trim <> "Class") Then Response.Redirect("default.aspx", True)
			daClassAssignments.SelectCommand.Parameters("@ClassID").Value = Session("ClassPageID")
			daClassAssignments.SelectCommand.Parameters("@AID").Value = 0
			daClassAssignments.Fill(DsClassAssignments1)
			DG_Assignments.DataSource = New DataView(DsClassAssignments1.Tables("GetClassAssignment"))
			DG_Assignments.DataBind()
		End If
	End Sub

	'**************************************************************************
	'	Data gird has a new item created now
	'**************************************************************************
	Private Sub DG_Assignments_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_Assignments.ItemCreated
		If e.Item.ItemType = ListItemType.Footer Then
			Dim btnDel As New Button()
			With btnDel
				.ID = "DeleteSelected"
				.Attributes.Add("onclick", "return confirm('Are you sure ?');")
				.CommandName = "DeleteAllSelected"
				.Text = "Delete Selected"
				.Font.Name = "Verdana"
				.ToolTip = "Click this to delete all selected assignments"
			End With
			e.Item.Cells(1).Controls.Add(btnDel)
		End If

		If e.Item.ItemType = ListItemType.Pager Then
			If (DsClassAssignments1 Is Nothing) AndAlso (DsClassAssignments1.Tables("GetClassAssignment").Rows.Count < DG_Assignments.PageSize) Then
				e.Item.Visible = False
			End If
		End If
	End Sub

	'**************************************************************************
	'	Get this page Data to display is now 
	'**************************************************************************
	Private Sub DG_Assignments_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DG_Assignments.PageIndexChanged
		DG_Assignments.CurrentPageIndex = e.NewPageIndex
		daClassAssignments.SelectCommand.Parameters("@ClassID").Value = CType(Session("ClassPageID"), Integer)
		daClassAssignments.SelectCommand.Parameters("@AID").Value = 0
		daClassAssignments.Fill(DsClassAssignments1)
		DG_Assignments.DataSource = New DataView(DsClassAssignments1.Tables("GetClassAssignment"), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Assignments.DataBind()
	End Sub

	'**************************************************************************
	'	Data gird sorting is now cliched
	'**************************************************************************
	Private Sub DG_Assignments_SortCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DG_Assignments.SortCommand
		daClassAssignments.SelectCommand.Parameters("@ClassID").Value = CType(Session("ClassPageID"), Integer)
		daClassAssignments.SelectCommand.Parameters("@AID").Value = 0
		daClassAssignments.Fill(DsClassAssignments1)
		ViewState("SortBy") = e.SortExpression
		If CType(ViewState("Direction"), String) = "" Then
			ViewState("Direction") = " ASC "
		ElseIf CType(ViewState("Direction"), String).Trim = "ASC" Then
			ViewState("Direction") = " DESC "
		Else
			ViewState("Direction") = " ASC "
		End If
		DG_Assignments.DataSource = New DataView(DsClassAssignments1.Tables("GetClassAssignment"), "", e.SortExpression & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
		DG_Assignments.DataBind()
	End Sub

	'**************************************************************************
	'	A child command has been fired up by a control in the Data gird
	'**************************************************************************
	Private Sub DG_Assignments_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Assignments.ItemCommand
		If e.CommandName = "DeleteAllSelected" Then
			Dim dgi As DataGridItem
			For Each dgi In DG_Assignments.Items
				If dgi.ItemType = ListItemType.Item Or dgi.ItemType = ListItemType.AlternatingItem Then
					Dim cb As CheckBox = CType(dgi.Cells(0).Controls(1), CheckBox)
					If cb.Checked Then
						Dim cmd As New SqlCommand()
						With cmd
							.CommandText = "Select Attachment from as_ClassAssignments where AsignID=" & CType(cb.ToolTip, Integer)
							.CommandType = CommandType.Text
							.Connection = SqlConn
							If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
							Dim oValue As Object = .ExecuteScalar()
							If Not IsDBNull(oValue) Then
								If oValue.ToString.Trim.Length > 0 Then
									Try
										System.IO.File.Delete(Server.MapPath("..") & "\contents\Assignments\" & oValue.ToString)
									Catch ex As Exception
										'do nothing
									End Try
								End If
							End If
							.CommandText = "Delete from as_ClassAssignments where AsignID=" & CType(cb.ToolTip, Integer)
							.ExecuteNonQuery()
						End With
					End If
				End If
			Next
			daClassAssignments.SelectCommand.Parameters("@ClassID").Value = CType(Session("ClassPageID"), Integer)
			daClassAssignments.SelectCommand.Parameters("@AID").Value = 0
			daClassAssignments.Fill(DsClassAssignments1)
			DG_Assignments.DataSource = New DataView(DsClassAssignments1.Tables("GetClassAssignment"), "", CType(ViewState("SortBy"), String) & CType(ViewState("Direction"), String), DataViewRowState.CurrentRows)
			DG_Assignments.DataBind()
		Else
			'Response.Write(e.CommandName)
		End If
	End Sub

	Private Sub DG_Assignments_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DG_Assignments.SelectedIndexChanged

	End Sub

	Private Sub lnkBack2eClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkBack2eClass.Click
		Response.Redirect("MgrContent.aspx?Level=2&ID=" & Session("ClassPageID").ToString)
	End Sub
End Class
