Is it possible to filter enrollments by term?

I'm using this code and works great to pull courses with an End Date, but the problem is, its pulling all courses no matter what semester it is. We do not remove instructors from past courses so to accomplish what I would like is to filter the api call to pull only the current semester.

I think I have this wrong -  const response = await fetch(`ourD2Ldomain/d2l/api/lp/1.42/enrollments/myenrollments/?orgUnitTypeId=3&orgUnitId=${termId}`); but I'm just not sure what it is.

<p></p>

<ul id="codeList"></ul>

<p>

<script>

 async function getCourses() {

   const termId = '2941214'; // Replace with the desired term ID

   const response = await fetch(`ourD2Ldomain/d2l/api/lp/1.42/enrollments/myenrollments/?orgUnitTypeId=3&orgUnitId=${termId}`);

   const data = await response.json();


   const codeList = document.querySelector('#codeList');

data.Items.forEach(item => {

     const code = item.OrgUnit.Code;

     const id = item.OrgUnit.Id;

     const endDate = new Date(item.Access.EndDate); // Parse the date string using the Date object

     endDate.setDate(endDate.getDate() - 7); // Subtract 7 days from the date

     const formattedDate = endDate.toLocaleDateString(); // Format the date string to display only the day, month, and year

     const li = document.createElement('li');

     const a = document.createElement('a');

     a.textContent = code + " (" + formattedDate + ")"; // Use the formatted date string in the link text

     a.href = 'ourD2Ldomain/d2l/home/' + encodeURIComponent(id);

     li.appendChild(a);

     codeList.appendChild(li);

   });

 }


 getCourses();

</script>

</p>

Best Answer

  • Viktor.H.147
    Viktor.H.147 Posts: 44
    Answer ✓

    Currently, you can use the general “myenrollments" action to filter the retrieved enrollments by: org unit type ("only course offerings" for example), whether the org unit is active or not ("filter out enrollments in inactive org units"), and date-based filtering. The date-based filters allow you to specify a date-time window (start and end): only enrollments in org units whose start and end dates fall within this time range will get included.

Answers

  • Justin.B.253
    Justin.B.253 Posts: 20 🌱

    @Viktor Haa62 that worked, it took me a little time to figure it out but I created a widget that will pull a list of courses during a time period, as you pointed out above, and make the course links with the end date listed minus 7 days. As simple as this widget is, we have such varying dates faculty have to submit grades by, this will help them quickly know which day to submit their final grades by. It needs more work but this is what I have so far. The last bit I'm having trouble with is, if the instructor removes the start and end date in Course Offering Information, the query will render it with a 1969 date. Do you know if there is a way that if the END date is not listed then do not show?

    <p>

    <ul id="codeList"></ul>

    </p>

    <script>

    async function getCourses() {

    const apiUrl = 'https://domain_url/d2l/api/lp/1.42/enrollments/myenrollments/';

    const startDateTime = '2023-01-01T13:15:30.067Z';

    const endDateTime = '2023-06-30T13:15:30.067Z';

    const queryParams = new URLSearchParams({

    startDateTime: startDateTime,

    endDateTime: endDateTime,

    orgUnitTypeId: 3

    });

    const response = await fetch(`${apiUrl}?${queryParams.toString()}`);

    const data = await response.json();

    const codeList = document.querySelector('#codeList');

    data.Items.forEach(item => {

    const code = item.OrgUnit.Code;

    const id = item.OrgUnit.Id;

    const endDate = new Date(item.Access.EndDate);

    endDate.setDate(endDate.getDate() - 7);

    const formattedDate = endDate.toLocaleDateString();

    const li = document.createElement('li');

    const a = document.createElement('a');

    a.textContent = code + " (" + formattedDate + ")";

    a.href = 'https://domain_url/d2l/home/' + encodeURIComponent(id);

    li.appendChild(a);

    codeList.appendChild(li);

    });

    }

    getCourses();

    </script>

    </p>

  • Do you know if there is a way that if the END date is not listed then do not show? 

    I don't believe so, no.

  • Justin.B.253
    Justin.B.253 Posts: 20 🌱

    @Viktor Haa62 I was able to find a way by checking for NULL first. If nothing returned, the course wont show in the list. This will help filter out courses that are used for other purposes and not student grades. Thanks for the help in the other questions I had.