0

当我想根据 CourseId CourseWorkId 和 StudentSubmissionId 使用 API 从 Google Classroom 的学生提交列表中逐一获取草稿等级时,需要 60 到 100 多秒。

对于获得单个草稿成绩,我需要 courseId、courseWork Id 和 Id。因此,为了获取请求参数(courseId、courseWork Id 和 Id),我需要遍历 CourseWork 和 StudentSubmission。直到获得单个课程的所有草稿成绩在此期间,每次都会重复该过程。

显示代码以了解为什么加载时间更多用于检索不到 40 到 50 个草稿等级。

<?php

 $client = getClient();
 $service = new Google_Service_Classroom($client);

function test($courseID)
{
   $client = getClient();
   $service = new Google_Service_Classroom($client);

   $assignedGrades = [];
   $result = [];
   $course_type = [];

   // Get CourseWork 
   $courseWorkID = getCourseWorkID($service, $courseID);
     
   //Get StudentSubmissions
   $studentSubmissions = getStudentSubmissions($service, $courseID, $courseWorkID); 

   for ($i = 0; $i < count($studentSubmissions); $i++) {
       $assignedGrades[$i] = getAssignedGrades($service, $courseID, $courseWorkID, $studentSubmissions[$i]);
   }

   foreach ($assignedGrades as $assignedGrade) {

       foreach ($assignedGrade as $grade) {

           $result[] = $grade;
       }
   }
   return $result;
}



function get_CourseWorkTypes($service,$courseID){
    $client = getClient();
    $service = new Google_Service_Classroom($client);
    $courseWorks = $service->courses_courseWork->listCoursesCourseWork($courseID)->getCourseWork();
  
    $courseWorkTypes = [];
    for ($i = 0; $i < count($courseWorks); $i++){
        $courseWorkTypes[$i] = $courseWorks[$i]->getTitle();
    }
    return $courseWorkTypes;
   }
 
   //Get AssignedGrades
function getAssignedGrades($service, $courseID, $courseWorkID, $studentSubmissions)
{
   
   $assignedGrades = [];
   if (is_null(getCourseWorkType($service,$courseID,$courseWorkID))){
       $courseWorkType = "NULL";
   }
   else {
       $courseWorkType = getCourseWorkType($service,$courseID, $courseWorkID);
   }

   for ($i = 0; $i < count($studentSubmissions); $i++) {
      

       $assignedGrades[$i] = array(
           "userID" => getStudentByID($service, $studentSubmissions[$i]->getUserID()),
           "grade" => $studentSubmissions[$i]->getAssignedGrade(),
           "CourseId" => $studentSubmissions[$i]->getCourseId(),
           "CourseWorkId" => $studentSubmissions[$i]->getCourseWorkId(),
           "id" => $studentSubmissions[$i]->getId(),
           "title" => getCourseWorkTitle($service,$studentSubmissions[$i]->getCourseId(),$studentSubmissions[$i]->getCourseWorkId()),          

       );

   }

   return $assignedGrades;
}
function getCourseWorkTitle($service, $courseID, $courseWorkID){
    return $courseWorks = $service->courses_courseWork->get($courseID, $courseWorkID)->getTitle();
}

function getCourseWorkType($service, $courseID, $courseWorkID){
    $client = getClient();
    $service = new Google_Service_Classroom($client);
    $courseWorks = $service->courses_courseWork->listCoursesCourseWork($courseID)->getCourseWork();
   
    $courseWorkTypes = [];
    for ($i = 0; $i < count($courseWorks); $i++){
        $courseWorkTypes[$i] = $courseWorks[$i]->getTitle();
    }
   
    return $courseWorkTypes;
   }

function getCourseWorkID($service, $courseID)
{
   return $service->courses_courseWork->listCoursesCourseWork($courseID)->getCourseWork();
   
}

function getStudentSubmissions($service, $courseID, $courseWorkID)
{

   $studentSubmissions = [];

   for ($i = 0; $i < count($courseWorkID); $i++) {
       $studentSubmissions[$i] = $service->courses_courseWork_studentSubmissions->
       listCoursesCourseWorkStudentSubmissions($courseID, $courseWorkID[$i]->getId())->getStudentSubmissions();
   }

  return $studentSubmissions;

}

function CourseWorkType($courseID){
    $client = getClient();
    $service = new Google_Service_Classroom($client);
    $courseWorks = $service->courses_courseWork->listCoursesCourseWork($courseID)->getCourseWork();
   
    $courseWorkTypes = [];
    for ($i = 0; $i < count($courseWorks); $i++){
        $courseWorkTypes[$i] = $courseWorks[$i]->getTitle();
    }

    return $courseWorkTypes;

  }

$st =[];

if(isset($_REQUEST["couse"])){
   $courseID = $_REQUEST["couse"];   
}
else{
    echo "No couse Found";
}

   $st = test($courseID);

?>

直到获得一门课程的所有草稿成绩需要 60 到 100 多秒。如何减少页面加载时间以获取单个课程的所有草稿成绩。

4

0 回答 0