create or replace package menu as
   
   procedure teacher_menu(u_access in varchar2 default NULL,
			  userid in varchar2 default NULL);
   procedure process_teacher_option(opt in varchar2 default null,
                               u_access in varchar2 default null,
			  userid in varchar2 default NULL);
   procedure student_menu(u_access in varchar2 default NULL,
			  teachername in varchar2 default NULL,
                          id in varchar2,
			  last_access in varchar2);
   procedure process_student_option(u_access in varchar2 default NULL,
			  teachername in varchar2 default NULL,
                          opt in varchar2 default null,
                          tcl in varchar2 default null,
                          id in varchar2);
end;
/
show errors

create or replace package body menu as

procedure teacher_menu(u_access in varchar2 default NULL,
		 userid in varchar2 default NULL)

as
   access_check varchar2(30);
   no_access    exception;
begin
   if (u_access is NULL) then
      raise no_access;   
   end if;
   access_check := user_access.check_access(u_access);
   if  (access_check != 'Teacher') then
      raise no_access;
   end if;
   htp.htmlOpen;
   htp.headOpen;
   htp.htitle('Teacher Menu');
   htp.headClose;
   htp.bodyOpen('/ows-img/raj/fleck.jpg');
   htp.formOpen(owa_util.get_owa_service_path ||
               'menu.process_teacher_option');
   htp.formHidden('u_access', u_access);
   htp.formHidden('userid', userid);
   htp.formSubmit('opt','Add Catalog Entry'); htp.br;
--   htp.formSubmit('opt','Add Student'); htp.br;
   htp.formSubmit('opt','Add Course Entry'); htp.br;
   htp.formSubmit('opt','Select a Course'); htp.br;
   htp.formSubmit('opt','Search Userid-Password'); htp.br;
   htp.formSubmit('opt','Change Password'); htp.br;
   htp.formSubmit('opt','Logout'); htp.br;
   htp.formclose;
   htp.bodyClose;
   htp.htmlClose;
Exception
   when no_access then
	Raise_no_access('gradebook');
   when others then
      htp.print('Error in menus.teacher_menu');
      htp.br;
      htp.print(SQLERRM);
end teacher_menu;

procedure process_teacher_option(opt in varchar2 default null,
                               u_access in varchar2 default null,
			       userid in varchar2 default NULL) as
begin
   if (opt = 'Add Catalog Entry') then
     add_all.create_catalog(u_access,userid);
--   elsif (opt = 'Add Student') then
--     add_all.create_student(u_access,userid);
   elsif (opt = 'Add Course Entry') then
     add_all.create_course(u_access,userid);
   elsif (opt = 'Select a Course') then
     add_all.select_course(u_access,userid);
   elsif (opt = 'Search Userid-Password') then
     reports.lookup(u_access, userid);
   elsif (opt = 'Change Password') then
     user_access.change_password(u_access);
   elsif (opt = 'Logout') then
     user_access.logout(u_access);
   end if;
end;

procedure student_menu(u_access in varchar2 default NULL,
	     	       teachername in varchar2 default NULL,
                       id in varchar2,
		       last_access in varchar2) as
   access_check varchar2(30);
   no_access    exception;
   student_fname varchar2(20);
   student_lname varchar2(20);
   no_teachername exception;

   cursor c1 is 
     select courses.term,cno,courses.lineno 
     from courses,enrolls
     where enrolls.sid = id and (enrolls.authority = teachername) and
           courses.term = enrolls.term and
           courses.lineno = enrolls.lineno;
begin

   if (u_access is NULL) then
      raise no_access;   
   end if;
   access_check := user_access.check_access(u_access);
	if  (access_check != 'Student') then
      raise no_access;
   end if;
   if (teachername is NULL) then 
     raise no_teachername;
   end if;
   select students.fname, students.lname
   into student_fname, student_lname
   from students
   where (students.sid = id) and (authority = teachername);
 
   htp.htmlOpen;
   htp.headOpen;
   htp.htitle('Student Menu');
   htp.headClose;
   htp.bodyOpen('/ows-img/raj/bgchalk.jpg');
   htp.print('Welcome, <b>'||initcap(student_fname)||' '||initcap(student_lname)
	 || ' </b>to the Grade Book system.<br>');
   htp.print('Your last accessed was: '||last_access);

   htp.formOpen(owa_util.get_owa_service_path ||
               'menu.process_student_option');
   htp.formSelectOpen('tcl','Select Course: ');
   for c1_rec in c1 loop
     htp.formSelectOption('(' ||c1_rec.term||','||
                          c1_rec.cno||','||
                          c1_rec.lineno || ')');
   end loop;
   htp.formSelectClose;
   htp.nl;
   htp.formHidden('u_access', u_access);
   htp.formHidden('teachername',teachername);
   htp.formHidden('id', id);
   htp.formSubmit('opt','Display Scores');
   htp.formSubmit('opt','Check Your Standing');
   htp.formSubmit('opt','Change Password');
   htp.formSubmit('opt','Logout');
   htp.formclose;
   htp.bodyClose;
   htp.htmlClose;
Exception
   when No_Data_Found then
      htp.print('Either the professor,'||htf.bold(teachername)||', have
not yet added your name as a student in the class nor you do not know your
teacher name! Please check back later.');
   when no_access then
	Raise_no_access('gradebook');
   when no_teachername then
      htp.print('You forgot to select a teacher name. Please go back and
		select one.');
      htp.print('<form><input type=button onclick=history.go(-1)
		value="Back to Previous Menu"></form>');
   when others then
      htp.print('Error in menus.student_menu');
htp.print('sqlerrm='||SQLERRM|| ' id=' || id); htp.br;
htp.print('teachername=' || teachername); htp.br;
htp.print('last_access=' || last_access); htp.br;
htp.print('u_access=' || u_access); htp.br;
end student_menu;

procedure process_student_option(u_access in varchar2 default NULL,
			  teachername in varchar2 default NULL,
                          opt in varchar2 default null,
                          tcl in varchar2 default null,
                          id in varchar2) as
begin
  if (opt = 'Display Scores') then
    display.display_own_scores(id,tcl,u_access,teachername);
  elsif (opt = 'Check Your Standing') then
    display.display_graphs(id,tcl,u_access,teachername);
  elsif (opt = 'Change Password') then
    user_access.change_password(u_access);
  elsif (opt = 'Logout') then
    user_access.logout(u_access);
  end if;
end;

end;
/
show errors
