findInstanceWithRange($rangeType, $rangeId); } private function findInstanceWithRange(string $rangeType, string $rangeId): Instance { $methods = [ 'course' => 'getCoursewareCourse', 'courses' => 'getCoursewareCourse', 'user' => 'getCoursewareUser', 'users' => 'getCoursewareUser', 'sharedusers' => 'getSharedCoursewareUser', ]; if (!($method = $methods[$rangeType])) { throw new BadRequestException('Invalid range type: "' . $rangeType . '".'); } $root = null; if ($rangeType !== 'sharedusers') { $chunks = explode('_', $rangeId); $courseId = $chunks[0]; $unitId = $chunks[1] ?? null; if ($unitId === '') { throw new BadRequestException('Unit id must not be empty.'); } if ($unitId) { $unit = Unit::findOneBySQL('range_id = ? AND id = ?', [$courseId, $unitId]); } else { $unit = Unit::findOneBySQL('range_id = ?', [$courseId]); } if ($unit) { $root = $unit->structural_element; } } else { $root = StructuralElement::$method($rangeId); } if (!$root) { throw new RecordNotFoundException(); } return new Instance($root); } }