submissiongoogle-classroom

Problem listing assignments of a student in Google Classroom


I am starting to use Classroom API to enhance local apps in our school. In order to make a report for a class, I want to list all student assignments and gradings. I use loops to go through all courses for a student, then all coursework for every course, and then all submissions for every coursework. Here is the piece of code that I use:

function fListWorkStudent(idStudent)
{
  // Variables 
  var pageToken = null;
  var optionalArgs =
  {
    pageToken: pageToken,
    courseStates: 'ACTIVE',
    studentId: idStudent,
    pageSize: 0
  };
  var optionalArgs2 =
  {
    pageToken: pageToken,
    userId: idStudent,
    pageSize: 0
  };

  // Courses for a student
  var response = Classroom.Courses.list(optionalArgs);
  var sCourses = response.courses;
  if (sCourses.length === 0)
    Logger.log("No courses");
  else 
  {
    for (course in sCourses)
    {
      var idCourse=sCourses[course].id;
      var nomprof=getUserName(sCourses[course].ownerId);

      // Coursework for every course
      var responseW = Classroom.Courses.CourseWork.list(idCourse);
      var works = responseW.courseWork;
      if (works && (works.length > 0))
      {
        for work in works)
        {
          var idWork=works[work].id;

          // Submissions for every coursework
          var responseS = Classroom.Courses.CourseWork.StudentSubmissions.list(idCourse, idWork, optionalArgs2);
          var submissions = responseS.studentSubmissions;
          if (submissions && submissions.length >0)
          {
            for (submission in submissions)
            {
              // Prepare report here
            }
          }
        }
      }
    }
  }
}

The problem with this code is that when I call Classroom.Courses.CourseWork.StudentSubmissions.list(idCourse, idWork, optionalArgs2) to get the submissions filtered of selected student, and the loop reaches a coursework not assigned to that student, the call fails with error 'classroom.courses.courseWork.studentSubmissions.list; error: Requested entity was not found.' I could solve it by checking in the loop if the coursework is not assigned to that student before calling the API function, or maybe using a try..catch clause to catch the possible error, but I would like to know if there is a smarter solution to this issue.

Regards Rafael


Solution

  • Unfortunately the API does not give you an endpoint to list directly all assignment / submissions of a given student

    However, you are not alone with this problem, there is already a feature request for this functionality on Google's Public Issue Tracker.

    I recommend you to give it a "star" in order to increase visibility.

    In the mean time, indeed you either need to implement a try...catch statement, or a conditonal statement, something like:

    
     if(works[work].assigneeMode == "ALL_STUDENTS" || (works[work].assigneeMode == "INDIVIDUAL_STUDENTS" && works[work].individualStudentsOptions.studentIds.indexOf(idStudent)!=-1))
      {
       var responseS = Classroom.Courses.CourseWork.StudentSubmissions.list(idCourse, idWork, optionalArgs2);
       ...
       }