You are on page 1of 7

public with sharing class ExamSubjectwise{ /* Description: * ============ * ExamSubjectwise is the Extension Class for the VF page ExaminationSubject vf * This

class is used to display the list of students belong to this class and Section * This class allow to enter Marks of students (subjectwise) * Author: * ======== * Svimtech * * Revision History: * ================= * Date Remarks * 11/08/2011 Initial development built */ //variable Declaration.... String TabURL ; Public integer year; Public Date selectdate; Public Date Examdate; Public String Months ; Public String Examtype; Public String strSubject; public String Academicyear; public String selectclass; Public String Subjects; Public String Section; //get set properties.... public string subjectclass{get;set;} public ExamSetup_ClassWise__c examMarks {get;set;} public List<AggregateResult> getResults{get;set;} public List<AggregateResult> getAggregate{get;set;} public List<AggregateResult> AggregateMarks{get;set;} //List declaration........ Public List<String> subParts = new List<String>(); List<Class__c> subjectlist = new List<Class__c>(); List<Subject__c>SubjectNames= new List<Subject__c>(); List<SelectOption> SelectOptionList= new List<SelectOption>(); List<School_Setup__c> schoolList = new List<School_Setup__c>(); List<Student_Profile__c> studentlist=new List<Student_Profile__c>(); List<Examination_Setup__c> examList=new List<Examination_Setup__c>(); List<ExamSetup_ClassWise__c> ResultList= new List<ExamSetup_ClassWise__c> ( ); List<ExamSetup_ClassWise__c> existingRecords =new List<ExamSetup_ClassWise__ c>(); List<ExamSetup_ClassWise__c> ExamListclasswise=new List<ExamSetup_ClassWise_ _c>(); //controller public ExamSubjectwise(ApexPages.StandardController controller) { examMarks = (ExamSetup_ClassWise__c)controller.getRecord(); } //for Subject Select Option.......... public void SubjectsProperty(){ SelectOptionList.clear();

selectclass= examMarks.Class__c; SelectOptionList.add(new SelectOption('none', '--Select Subject--')); if(selectclass != null){ subjectlist =[SELECT Subjects__c,Name FROM Class__c WHERE (Id =:sele ctclass)]; for(Class__c af : subjectlist){ strSubject= af.Subjects__c; if (strSubject <> null ){ subParts = SvimUtil.splitString(strSubject,','); } } SubjectNames = [Select Id,Name From Subject__c Where Id In:subParts] ; for(Integer j=0;j<SubjectNames .size();j++){ SelectOptionList.add(new SelectOption(SubjectNames [j].id,Subjec tNames [j].Name)); } } } public string getsubjectclass() { return subjectclass; } public void setsubjectclass(string subjectclass){ this.subjectclass = subjectclass; } public List<SelectOption> getSubjects(){ return SelectOptionList; } //method to show the records........... Public Void ShowRecords(){ ResultList.clear(); existingRecords.clear(); string Subjects = subjectclass; selectclass=examMarks.Class__c; Examtype = examMarks.Examination_Types__c; Examdate=examMarks.Date__c; Section = examMarks.Section__c; if(selectclass == null Examtype == null Section == null (Subje cts =='none' Subjects == null)){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'All fields are required......')); return ; }else{ studentlist=[Select Id,Name,Student_Name__c,Class_new__c FROM Studen t_Profile__c WHERE Class_new__c=:selectclass and Sections__c = :Section and St atus__c ='Active'ORDER BY Student_Name__c ASC]; if(studentlist.IsEmpty() == true){ ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ER ROR,'There is no Student in this class.')); return; } // this list is used to fetch all the records from classwise without compare with subjects......... ExamListclasswise =[select Student_Name__c,Marks__c,Student_Id__c,Ex amination_Types__c,Subjects__c,Total_marks__c,Date__c,Obtained_Marks__c From Exa mSetup_ClassWise__c where Class__c=:selectclass And Date__c=:Examdate And Examination_Ty pes__c=:Examtype and Section__c=:Section]; // this list is used to fetch all the records from classwise compare with subjects.........

existingRecords =[Select Student_Name__c,Marks__c,Total_marks__c,Sub ject_Marks__c,Student_Id__c,Date__c,Examination_Types__c, Obtained_Marks__c,Subjects__c From ExamSetup_Class Wise__c where Class__c=:selectclass And Examination_Types_ _c=:Examtype And Subjects__c=:subjectclass and Date__c=:Examdate and Section__c= :Section]; // fetch finacial year from school setup............ schoolList =[ Select Name,Finacial_Year_Start__c from School_Setup__c LIMIT 1]; if(schoolList.isEmpty() == false && schoolList.size( )<> 0){ selectdate= schoolList[0].Finacial_Year_Start__c ; } year = selectdate.year(); ExamSetup_ClassWise__c exam = new ExamSetup_ClassWise__c (); exam.AcademicYear__c = year; // if exciting records doesn't exit........... if(existingRecords.isEmpty() == True){ examList=[SELECT id,Name,Class__c,Examination_Types__c,Total_Mar ks__c,Subjects__c FROM Examination_Setup__c where Examination_Types__c=:Examtype And Class__c=:selectclass AND Subjects__c =:subjectclass]; for(Student_Profile__c student :studentlist){ For(Examination_Setup__c examMarks :examList){ ExamSetup_ClassWise__c studentdata=new ExamSetup_Cla ssWise__c (); studentdata.Student_Name__c=student.Student_Name__c; studentdata.Date__c=Examdate; studentdata.Section__c=Section; studentdata.Subjects__c=subjectclass; studentdata.Student_Id__c=student.Id; studentdata.Subject_Marks__c=examMarks.Total_Marks__ c; studentdata.Examination_Types__c=Examtype; studentdata.Class__c=student.Class_new__c; ResultList.add(studentdata); } } } //end of if loop..... } } // end of show records method..... public List<ExamSetup_ClassWise__c> getRecords(){ if(!existingRecords.isEmpty()&& existingRecords.size()<>null){ return existingRecords ; }else{ return ResultList; } } //method to save the records........ public PageReference Save(){ Schema.DescribeSObjectResult SObjectResult = Schema.SObjectType.ExamSetu p_ClassWise__c; String ObjectKey = SObjectResult.getKeyPrefix(); TabURL = '/'+ ObjectKey +'/o'; selectclass=examMarks.Class__c;

string Subjects = subjectclass; Examtype=examMarks.Examination_Types__c; Examdate=examMarks.Date__c; try{ // if we want to update exciting records..... if(existingRecords<>null && existingRecords.size()>0){ // these two loops are used to compare with subjects and student Id. .... for(ExamSetup_ClassWise__c Extmarks: ExamListclasswise){ for(ExamSetup_ClassWise__c Exams :existingRecords ){ if(Extmarks.Student_Id__c== Exams.Student_Id__c && Extma rks.Subjects__c== Exams.Subjects__c){ Extmarks.Marks__c=Exams.Marks__c; } } } update ExamListclasswise; // this aggregate list is used to find total of all the subject s.......... getResults = [select Student_Id__c,SUM(Marks__c)Total from Exam Setup_ClassWise__c Where Class__c=:selectclass And Examination_Types_ _c=:Examtype GROUP BY Student_Id__c]; for(ExamSetup_ClassWise__c Extmarks: ExamListclasswise){ for(AggregateResult agr:getResults){ Object StudentID = agr.get('Student_Id__c'); if(StudentID == Extmarks.Student_Id__c){ Object TotalMarks = agr.get('Total'); Extmarks.Obtained_Marks__c = Integer.valueOf(Tot alMarks ); } } } update ExamListclasswise; }else{ // insert new list........if records doesn't exit upsert ResultList; // this aggregate list is used to find the total Marks of new list ......... getAggregate=[select Student_Id__c,SUM(Marks__c)Total from ExamSet up_ClassWise__c Where Class__c=:selectclass And Examination_Types__c=:Exam type GROUP BY Student_Id__c]; for(ExamSetup_ClassWise__c Extmarks: ResultList){ for(AggregateResult agr:getAggregate){ Object StudentID = agr.get('Student_Id__c'); if(StudentID == Extmarks.Student_Id__c){ Object TotalMarks = agr.get('Total'); Extmarks.Obtained_Marks__c = Integer.valueOf(TotalMarks

); } } } // this aggregate list is used to find the total Marks of subject s(Combined)....... AggregateMarks=[select SUM(Total_Marks__c)Total from Examination_ Setup__c Where Class__c=:selectclass And Examination_Ty pes__c=:Examtype]; for(ExamSetup_ClassWise__c Extmarks: ResultList){ for(AggregateResult agr:AggregateMarks){ Object TotalMarks = agr.get('Total'); Extmarks.Total_Marks__c= Integer.valueOf(TotalMarks ); } } } upsert ResultList; PageReference home = new PageReference('/apex/ExaminationSubjectvf') ; home.setRedirect(true); return home; }catch (System.DmlException e) { for (Integer i = 0; i < e.getNumDml(); i++){ ApexPages.addmessage(new ApexPages.message(ApexPages.sev erity.ERROR,e.getDmlMessage(i))); } } return null; }

//test method........... static testMethod void TestShowRecords(){ PageReference PageRef=Page.ExaminationSubjectvf; // Setting up the data to pass it in Controller // Creating the subjects for this class Subject__c dummySubject = new Subject__c(name = 'dummySubject'); Insert dummySubject ; // Creating new dummay class Class__c dummyclass= new Class__c(Class_Name__c = 'Dummyclass',Subjects_ _c=dummySubject.ID); Insert dummyclass; Type_Setup__c dummyType = new Type_Setup__c(Name ='dummyType ', Rela ted_Type__c = 'Allowances '); Insert dummyType; Type_Setup__c dummyType1 = new Type_Setup__c(Name ='dummyType1 ', Relate d_Type__c = 'Fee Category'); Insert dummyType1; //creating new Sections......... Type_Setup__c dummySectionType = new Type_Setup__c(Name ='dummySectionTy pe ', Related_Type__c = 'Sections '); Insert dummySectionType; SvimUtil dummyutil=new SvimUtil(); SvimUtil.getSubjects('Math'); List<Voucher__c> voucher=new List<Voucher__c>(); CreateAccountEntities entities=new CreateAccountEntities (); CreateAccountEntities.generateVouher(voucher,'dbCal');

// Need to create student who belong to this class to get the results. Student_Profile__c dummyStudent = new Student_Profile__c(Student_Name__c ='dummyStudent',Class_new__c = dummyclass.ID,Sections__c = dummySectionType.ID) ; insert dummyStudent ; // Creating new examination type........... Type_Setup__c dummyExamType = new Type_Setup__c(name ='dummyExamType', R elated_Type__c = 'Examination Type'); Insert dummyExamType;

// adding subjects to the examination type Examination_Setup__c dummyExamSetup= new Examination_Setup__c(Class__c = dummyclass.ID,Examination_Types__c = dummyExamType.ID, Subjects__c=dummySubject.ID,Total_Marks__c=9 0); insert dummyExamSetup; //creating new finacial start year.............. datetime myDateTime = datetime.now(); integer Month1=myDateTime.Month(); String Month=String.valueOf(Month1); ExamSetup_ClassWise__c dummydata=new ExamSetup_ClassWise__c(Student_Id__ c=dummyStudent.ID,Student_Name__c='steffi',Class__c=dummyclass.ID, Subjects__c=dummySubject.ID,Examination_Types__c=dummyExamType.I D,Date__c=system.Today(),Marks__c=30,Total_marks__c=200, Subject_Marks__c=50,Obtained_Marks__c=150,Section__c =dummySectionTy pe.ID); Apexpages.Standardcontroller emptySc = new Apexpages.Standardcontroller (dummydata); ExamSubjectwise emptyController = new ExamSubjectwise(emptySc); // setting the value of paramteres for empty records. emptyController.SubjectsProperty(); emptyController.setsubjectclass(dummySubject.id); emptyController.getsubjectclass(); System.assertequals(emptyController.getsubjectclass(),dummySubject.id) ; emptyController.getSubjects(); System.assertequals(emptyController.getSubjects(),emptyController.Sele ctOptionList); emptyController.ShowRecords(); emptyController.getRecords(); System.assertequals(emptyController.getRecords(),emptyController.Resul tList); emptyController.save(); insert dummydata; Apexpages.Standardcontroller Sc = new Apexpages.Standardcontroller(dumm ydata); ExamSubjectwise Controller = new ExamSubjectwise(Sc); Controller.setsubjectclass(dummySubject.id); Controller.SubjectsProperty(); Controller.getsubjectclass(); Controller.getSubjects(); Controller.getRecords(); Controller.ShowRecords(); Controller.save(); ExamSetup_ClassWise__c dummydata1 =new ExamSetup_ClassWise__c(Student_Na me__c= 'steffi', Student_ID__c = dummyStudent.ID,Examination_Types__c=dummyExamT

ype.ID,Date__c=system.Today(),Class__c=dummyclass.ID, Section_ _c=dummySectionType.ID,Marks__c=100); insert dummydata1 ; Apexpages.Standardcontroller emptySc1 = new Apexpages.Standardcontrolle r(dummydata1); ExamSubjectwise emptyController1 = new ExamSubjectwise(emptySc1); emptyController1.getRecords(); emptyController1.ShowRecords(); emptyController1.save(); } }