Option Strict On
Option Explicit On 

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.IO.File

Public Class ClassAssignment
	Inherits System.Web.UI.Page
	Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtSubject As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtStartDate As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtEndDate As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtSubmitDate As System.Web.UI.WebControls.TextBox
	Protected WithEvents btnNewAssignment As System.Web.UI.WebControls.Button
	Protected WithEvents lblError As System.Web.UI.WebControls.Label
	Protected WithEvents daClassAssignment As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlConn As System.Data.SqlClient.SqlConnection
	Protected WithEvents eweDetails As EasyWebEditorControl.EasyWebEdit
	Protected WithEvents AttachementFile As System.Web.UI.HtmlControls.HtmlInputFile
	Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
	Protected WithEvents lnkRemoveAttachment As System.Web.UI.WebControls.LinkButton
	Protected WithEvents pnlAttachment As System.Web.UI.WebControls.Panel
	Protected WithEvents imgbtnAttachement As System.Web.UI.WebControls.ImageButton
	Protected WithEvents lnkAttachment As System.Web.UI.WebControls.LinkButton
	Protected WithEvents lblAsignID As System.Web.UI.WebControls.Label
	Protected WithEvents hlnkViewAllAssignments As System.Web.UI.WebControls.HyperLink
	Protected WithEvents lnkBack2eClass As System.Web.UI.WebControls.LinkButton
	Protected WithEvents DsClassAssignments1 As AllSaints.dsClassAssignments

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.daClassAssignment = New System.Data.SqlClient.SqlDataAdapter()
		Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand()
		Me.SqlConn = New System.Data.SqlClient.SqlConnection()
		Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
		Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand()
		Me.DsClassAssignments1 = New AllSaints.dsClassAssignments()
		CType(Me.DsClassAssignments1, System.ComponentModel.ISupportInitialize).BeginInit()
		'
		'daClassAssignment
		'
		Me.daClassAssignment.InsertCommand = Me.SqlInsertCommand1
		Me.daClassAssignment.SelectCommand = Me.SqlSelectCommand1
		Me.daClassAssignment.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "as_ClassAssignments", 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")})})
		Me.daClassAssignment.UpdateCommand = Me.SqlUpdateCommand1
		'
		'SqlInsertCommand1
		'
		Me.SqlInsertCommand1.CommandText = "[InsClassAssignment]"
		Me.SqlInsertCommand1.CommandType = System.Data.CommandType.StoredProcedure
		Me.SqlInsertCommand1.Connection = Me.SqlConn
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ClassPageID", System.Data.SqlDbType.Int, 4, "ClassPageID"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@AsignTitle", System.Data.SqlDbType.VarChar, 255, "AsignTitle"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Subject", System.Data.SqlDbType.VarChar, 255, "Subject"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@StartDate", System.Data.SqlDbType.VarChar, 25, "StartDate"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EndDate", System.Data.SqlDbType.VarChar, 25, "EndDate"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SubmitDate", System.Data.SqlDbType.VarChar, 25, "SubmitDate"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Attachment", System.Data.SqlDbType.VarChar, 255, "Attachment"))
		Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@AssignmentHTML", System.Data.SqlDbType.Text, 0, "AssignmentHTML"))
		'
		'SqlConn
		'
		Me.SqlConn.ConnectionString = "data source=localhost;initial catalog=Allsaints;integrated security=SSPI;persist " & _
		"security info=False;workstation id=GALAXY;packet size=4096"
		'
		'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(0, 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))
		'
		'SqlUpdateCommand1
		'
		Me.SqlUpdateCommand1.CommandText = "[SetClassAssignment]"
		Me.SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure
		Me.SqlUpdateCommand1.Connection = Me.SqlConn
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ClassPageID", System.Data.SqlDbType.Int, 4, "ClassPageID"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@AsignTitle", System.Data.SqlDbType.VarChar, 255, "AsignTitle"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Subject", System.Data.SqlDbType.VarChar, 255, "Subject"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@StartDate", System.Data.SqlDbType.VarChar, 25, "StartDate"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@EndDate", System.Data.SqlDbType.VarChar, 25, "EndDate"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@SubmitDate", System.Data.SqlDbType.VarChar, 25, "SubmitDate"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Attachment", System.Data.SqlDbType.VarChar, 255, "Attachment"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@AssignmentHTML", System.Data.SqlDbType.Text, 0, "AssignmentHTML"))
		Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_AsignID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "AsignID", System.Data.DataRowVersion.Original, Nothing))
		'
		'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")
		lnkRemoveAttachment.Attributes.Add("onclick", "return confirm('Are you sure?');")

		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)

			If Request("AsignID") = "" Then
				lblMessage.Text = "<strong>Create New Class Assignment</strong>"
			Else
				lblMessage.Text = "<Strong>Edit Class Assignment</Strong>"
				'set the assignment id in lable
				lblAsignID.Text = Request("AsignID").ToString.Trim
				daClassAssignment.SelectCommand.Parameters("@AID").Value = CType(lblAsignID.Text, Integer)
				daClassAssignment.SelectCommand.Parameters("@ClassID").Value = CType(Session("ClassPageID"), Integer)
				daClassAssignment.Fill(DsClassAssignments1)

				If DsClassAssignments1.Tables.Count > 0 AndAlso DsClassAssignments1.Tables(0).Rows.Count > 0 Then
					eweDetails.HTMLValue = DsClassAssignments1.Tables(0).Rows(0).Item("AssignmentHTML").ToString
					'get the attachment file name
					Dim sFilename As String = DsClassAssignments1.Tables(0).Rows(0).Item("Attachment").ToString
					'if any attachment found then
					If sFilename.Trim.Length > 0 Then
						lnkAttachment.CommandArgument = sFilename
						imgbtnAttachement.CommandArgument = sFilename
						sFilename = sFilename.Replace("_" & DsClassAssignments1.Tables(0).Rows(0).Item("AsignID").ToString, "")
						lnkAttachment.Text = sFilename
						pnlAttachment.Visible = True
					End If
					Page.DataBind()
				Else
					Response.Redirect("ClassAssignment.aspx", True)
				End If
			End If
		End If
	End Sub
	'************************************************************************
	Private Sub btnNewAssignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewAssignment.Click
		If txtTitle.Text.Trim.Length <= 0 Then
			lblError.Text = "<p>Assignment Title was not entered!</p>"
			lblError.Visible = True
			Exit Sub
		End If
		'label AsignID will not have assignment id or any text if new creation
		If lblAsignID.Text.Trim.Length <= 0 Then
			Dim cmd As New SqlCommand()
			Dim AMaxID As Integer
			With cmd
				.CommandText = "Select Max(AsignID) from as_ClassAssignments"
				.CommandType = CommandType.Text
				.Connection = SqlConn
				If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
				Dim oValue As Object = .ExecuteScalar
				If IsDBNull(oValue) Then
					AMaxID = 1
				Else
					AMaxID = CType(oValue, Integer) + 1
				End If
			End With
			cmd.Dispose()

			Dim sFileName As String = ""
			Dim sFileExt As String = ""
			With AttachementFile.PostedFile
				If .FileName <> "" AndAlso .ContentLength > 0 Then
					sFileName = System.IO.Path.GetFileNameWithoutExtension(.FileName)
					sFileExt = System.IO.Path.GetExtension(.FileName)
					sFileName = sFileName & "_" & AMaxID.ToString & sFileExt
					Try
						.SaveAs(Server.MapPath("..") & "\contents\Assignments\" & sFileName)
					Catch ex As Exception
						lblError.Text = "<p>Error Occured, Unable to upload image!</p>"
						lblError.Visible = True
						Exit Sub
					End Try
				End If
			End With

			With daClassAssignment.InsertCommand
				.Parameters("@ClassPageID").Value = CType(Session("ClassPageID"), Integer)
				.Parameters("@AsignTitle").Value = txtTitle.Text
				.Parameters("@Subject").Value = txtSubject.Text
				.Parameters("@StartDate").Value = txtStartDate.Text
				.Parameters("@EndDate").Value = txtEndDate.Text
				.Parameters("@SubmitDate").Value = txtSubmitDate.Text
				.Parameters("@Attachment").Value = sFileName
				.Parameters("@AssignmentHTML").Value = eweDetails.HTMLValue
				Try
					Dim aid As Integer = CType(.ExecuteScalar, Integer)
					Response.Redirect("ClassAssignment.aspx?AsignID=" & aid.ToString, True)
				Catch ex As Exception
					lblError.Text = "<p>Error Occured, Unable to create new assignment!</p>"
					lblError.Visible = True
				End Try
			End With
			'else it is the editing of the assignment
		Else
			Dim sFileName As String = ""
			Dim sFileExt As String = "'"
			'remove the previouse attached file if new is posted/given
			With AttachementFile.PostedFile
				Dim cmd As New SqlCommand()
				If .FileName <> "" AndAlso .ContentLength > 0 Then
					With cmd
						.CommandText = "Select Attachment from as_ClassAssignments where AsignID=" & lblAsignID.Text & " AND ClassPageID=" & CType(Session("ClassPageID"), Integer)
						.CommandType = CommandType.Text
						.Connection = SqlConn
						If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
						Dim oValue As Object = .ExecuteScalar
						'if some previouse attachement file name exits then remove that file
						If Not IsDBNull(oValue) AndAlso oValue.ToString.Trim.Length > 0 Then
							sFileName = CType(oValue.ToString, String)
							If sFileName.Trim.Length > 0 Then
								Try
									System.IO.File.Delete(Server.MapPath("..") & "\contents\Assignments\" & sFileName)
								Catch ex As Exception
									'do nothing
								End Try
							End If
						End If
					End With
					sFileName = System.IO.Path.GetFileNameWithoutExtension(.FileName)
					sFileExt = System.IO.Path.GetExtension(.FileName)
					sFileName = sFileName & "_" & lblAsignID.Text & sFileExt
					Try
						.SaveAs(Server.MapPath("..") & "\contents\Assignments\" & sFileName)
					Catch ex As Exception
						'lblError.Text = "Error Occured, Unable to upload image!"
						'lblError.Visible = True
						'Exit Sub
					End Try
				Else
					With cmd
						.CommandText = "Select Attachment from as_ClassAssignments where AsignID=" & lblAsignID.Text & " AND ClassPageID=" & CType(Session("ClassPageID"), Integer)
						.CommandType = CommandType.Text
						.Connection = SqlConn
						If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
						Dim oValue As Object = .ExecuteScalar
						sFileName = ""
						'preserve the attachement file name
						If Not IsDBNull(oValue) AndAlso oValue.ToString.Trim.Length > 0 Then
							sFileName = CType(oValue.ToString, String)
						End If
					End With
				End If
			End With
			With daClassAssignment.UpdateCommand
				.Parameters("@ClassPageID").Value = CType(Session("ClassPageID"), Integer)
				.Parameters("@AsignTitle").Value = txtTitle.Text
				.Parameters("@Subject").Value = txtSubject.Text
				.Parameters("@StartDate").Value = txtStartDate.Text
				.Parameters("@EndDate").Value = txtEndDate.Text
				.Parameters("@SubmitDate").Value = txtSubmitDate.Text
				.Parameters("@Attachment").Value = sFileName
				.Parameters("@AssignmentHTML").Value = eweDetails.HTMLValue
				.Parameters("@Original_AsignID").Value = CType(lblAsignID.Text, Integer)
				If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
				.Connection = SqlConn
				Try
					Dim aid As Integer = CType(.ExecuteScalar, Integer)
					Response.Redirect("ClassAssignment.aspx?AsignID=" & lblAsignID.Text, True)
				Catch ex As Exception
					lblError.Text = "<p>Error Occured, Unable to update assignment!</p>"
					lblError.Visible = True
				End Try
			End With
		End If
	End Sub
	'*****************************************************************************
	Private Sub lnkRemoveAttachment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkRemoveAttachment.Click
		Dim cmd As New SqlCommand()
		With cmd
			.CommandText = "Select Attachment from as_ClassAssignments where AsignID=" & lblAsignID.Text & " AND ClassPageID=" & CType(Session("ClassPageID"), Integer)
			.CommandType = CommandType.Text
			.Connection = SqlConn
			If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
			Dim oValue As Object = .ExecuteScalar
			'if some attachement file name exits then remove that file
			If Not IsDBNull(oValue) AndAlso oValue.ToString.Trim.Length > 0 Then
				Dim sFilename As String = CType(oValue.ToString, String)
				If sFilename.Trim.Length > 0 Then
					Try
						System.IO.File.Delete(Server.MapPath("..") & "\contents\Assignments\" & sFilename)
					Catch ex As Exception
						'do nothing
					End Try
					.CommandText = "Update as_ClassAssignments Set Attachment='' where AsignID=" & lblAsignID.Text & " AND ClassPageID=" & CType(Session("ClassPageID"), Integer)
					.CommandType = CommandType.Text
					.Connection = SqlConn
					If SqlConn.State = ConnectionState.Closed Then SqlConn.Open()
					.ExecuteNonQuery()
					pnlAttachment.Visible = False
					'lnkAttachment.Visible = False
					'imgbtnAttachement.Visible = False
					'lnkRemoveAttachment.Visible = False
					'referesh the page data
					'Call GetContactData()
					'Call GetData()
				End If
			End If
		End With
	End Sub

	'***************************************************************************
	'view the attachemnt by cliking image file
	'***************************************************************************
	Private Sub imgbtnAttachement_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnAttachement.Click
		Response.Redirect("../contents/Assignments/" & lnkAttachment.CommandArgument, True)
	End Sub

	Private Sub lnkAttachment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkAttachment.Click
		Response.Redirect("../contents/Assignments/" & lnkAttachment.CommandArgument, True)
	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
