CREATE OR REPLACE PACKAGE admin_tools_package
IS
procedure show_missing_grades (p_start_date IN date DEFAULT ADD_MONTHS(SYSDATE,-12),p_end_date IN date DEFAULT SYSDATE);
FUNCTION count_classes_per_course (p_course_id IN classes.course_id%type) return number;
PROCEDURE show_class_offerings (p_start_date date,p_end_date date);
end admin_tools_package;

CREATE OR REPLACE PACKAGE BODY admin_tools_package IS
procedure show_missing_grades (p_start_date IN date DEFAULT ADD_MONTHS(SYSDATE,-12),p_end_date IN date DEFAULT SYSDATE)
as
cursor missing_grades_cur
is
select class_id,stu_id,status
from enrollments
where enrollment_date between p_start_date and p_end_date and
final_numeric_grade is null and final_letter_grade is null
order by enrollment_date;
begin
for v_rec in missing_grades_cur
loop
dbms_output.put_line('class_id '||v_rec.class_id||' student_id '||v_rec.stu_id||' status '||v_rec.status);
end loop;

end show_missing_grades;

FUNCTION compute_avarage_grade (p_class_id IN enrollments.class_id%type)
RETURN NUMBER
IS
v_avg enrollments.final_numeric_grade%type;
begin
select avg(final_numeric_grade)
into v_avg
from enrollments
where class_id=p_class_id;
RETURN NVL(v_avg,0);
end compute_avarage_grade;

FUNCTION count_classes_per_course (p_course_id IN classes.course_id%type)
RETURN NUMBER
IS
v_nr_c number(6);
begin
select count(class_id) into v_nr_c
from classes
where course_id=p_course_id;
RETURN v_nr_c;
end count_classes_per_course;

PROCEDURE show_class_offerings(p_start_date date,p_end_date date)
AS

   CURSOR c1 IS SELECT course_id,class_id,start_date,instr_id FROM classes WHERE start_date BETWEEN p_start_date AND p_end_date;
   CURSOR c2(p_course_id NUMBER,p_instr_id NUMBER) IS SELECT i.first_name,i.last_name,c.title,c.section_code 
       FROM courses c, instructors i 
       WHERE c.course_id = p_course_id AND i.instructor_id=p_instr_id; 
   
BEGIN 
   DBMS_OUTPUT.PUT_LINE('Date range: Between '||p_start_date||' and '||p_end_date||'.'); 
   DBMS_OUTPUT.PUT_LINE('Classes Information.'); 
   FOR v_c1 IN c1 LOOP
     FOR v_c2 in c2(v_c1.course_id,v_c1.instr_id) LOOP
        DBMS_OUTPUT.PUT_LINE('Class ID '||v_c1.class_id||' Start Date'||v_c1.start_date||' Instructor '||v_c2.first_name||' '||v_c2.last_name||' Course Title '||v_c2.title ||' Section Code '||v_c2.section_code||compute_avarage_grade(v_c1.class_id)); 
     END LOOP; 
   END LOOP; 
end show_class_offerings;

end admin_tools_package;
