public static function handleCompetenciesRequest(ContentArea $ContentArea) { // get input students if (empty($_REQUEST['students'])) { $studentIds = []; } else { $studentIds = is_string($_REQUEST['students']) ? explode(',', $_REQUEST['students']) : $_REQUEST['students']; } $students = []; foreach ($studentIds as $studentId) { if (!ctype_digit($studentId)) { return static::throwInvalidRequestError('students must be identified by integer ID'); } if (!($students[] = Student::getByID($studentId))) { return static::throwInvalidRequestError('student ID not found'); } } // get data for all competencie $competencies = []; foreach ($ContentArea->Competencies as $Competency) { $competencyData = $Competency->getDetails(['totalDemonstrationsRequired', 'minimumAverage']); foreach ($students as $Student) { $competencyData['studentCompletions'][$Student->ID] = $Competency->getCompletionForStudent($Student); } $competencies[] = $competencyData; } # // query per-student and per-competency completed demonstrations # try { # $studentCompetencies = DB::allRecords( # 'SELECT Demonstration.StudentID, Skill.ID, Skill.CompetencyID, LEAST(Skill.DemonstrationsRequired, COUNT(Demonstration.ID)) AS demonstrations' # .' FROM (SELECT ID, CompetencyID, DemonstrationsRequired FROM `%s` WHERE CompetencyID IN (%s)) Skill' # .' JOIN `%s` DemonstrationSkill' # .' ON DemonstrationSkill.SkillID = Skill.ID' # .' JOIN (SELECT ID, StudentID FROM `%s` WHERE StudentID IN (%s)) Demonstration' # .' ON Demonstration.ID = DemonstrationSkill.DemonstrationID' # .' GROUP BY StudentID, Skill.ID' # ,[ # Skill::$tableName # ,implode(',', array_keys($competencies)) # ,DemonstrationSkill::$tableName # ,Demonstration::$tableName # ,implode(',', $students) # ] # ); # } catch (TableNotFoundException $e) { # $studentCompetencies = []; # } # # // sort demonstrations into competencies array and index by student id # foreach ($studentCompetencies AS $studentCompetency) { # $competencies[$studentCompetency['CompetencyID']]['studentDemonstrations'][$studentCompetency['StudentID']] += (int)$studentCompetency['demonstrations']; # } return static::respond('competencies', ['data' => $competencies]); }