import re with open('includes/cycles.php', 'r') as f: content = f.read() new_func = """function import_global_center_assessment_to_center(int $globalAssessmentId, int $centerApplicationId, int $cycleId): int { $pdo = db(); $globalAssessment = get_global_center_assessment_type($globalAssessmentId); if (!$globalAssessment) { throw new InvalidArgumentException("Global assessment template not found."); } $globalCriteria = list_global_center_assessment_criteria_by_assessment($globalAssessmentId, true); try { $pdo->beginTransaction(); $stmt = $pdo->prepare('INSERT INTO center_assessment_types (center_application_id, cycle_id, global_template_id, title, category, scale_type, max_score, weight_percentage, is_active, notes) VALUES (:center_application_id, :cycle_id, :global_template_id, :title, :category, :scale_type, :max_score, :weight_percentage, :is_active, :notes)'); $stmt->bindValue(':center_application_id', $centerApplicationId, PDO::PARAM_INT); $stmt->bindValue(':cycle_id', $cycleId, PDO::PARAM_INT); $stmt->bindValue(':global_template_id', $globalAssessmentId, PDO::PARAM_INT); $stmt->bindValue(':title', $globalAssessment['title'], PDO::PARAM_STR); $stmt->bindValue(':category', $globalAssessment['category'], PDO::PARAM_STR); $stmt->bindValue(':scale_type', $globalAssessment['scale_type'], PDO::PARAM_STR); $stmt->bindValue(':max_score', $globalAssessment['max_score'], PDO::PARAM_STR); $stmt->bindValue(':weight_percentage', $globalAssessment['weight_percentage'], PDO::PARAM_STR); $stmt->bindValue(':is_active', $globalAssessment['is_active'], PDO::PARAM_INT); $stmt->bindValue(':notes', $globalAssessment['notes'] ?? null, PDO::PARAM_STR); $stmt->execute(); $newAssessmentId = (int) $pdo->lastInsertId(); if ($globalCriteria !== []) { $stmtCrit = $pdo->prepare('INSERT INTO center_assessment_criteria (center_application_id, cycle_id, assessment_type_id, title, notes, max_score, sort_order, is_active) VALUES (:center_application_id, :cycle_id, :assessment_type_id, :title, :notes, :max_score, :sort_order, :is_active)'); foreach ($globalCriteria as $criteria) { $stmtCrit->bindValue(':center_application_id', $centerApplicationId, PDO::PARAM_INT); $stmtCrit->bindValue(':cycle_id', $cycleId, PDO::PARAM_INT); $stmtCrit->bindValue(':assessment_type_id', $newAssessmentId, PDO::PARAM_INT); $stmtCrit->bindValue(':title', $criteria['title'], PDO::PARAM_STR); $stmtCrit->bindValue(':notes', $criteria['notes'] ?? null, PDO::PARAM_STR); $stmtCrit->bindValue(':max_score', $criteria['max_score'], PDO::PARAM_STR); $stmtCrit->bindValue(':sort_order', $criteria['sort_order'] ?? 0, PDO::PARAM_INT); $stmtCrit->bindValue(':is_active', $criteria['is_active'], PDO::PARAM_INT); $stmtCrit->execute(); } } $pdo->commit(); return $newAssessmentId; } catch (PDOException $e) { $pdo->rollBack(); error_log("Failed to import global center assessment: " . $e->getMessage()); throw $e; } }""" content = re.sub(r'function import_global_center_assessment_to_center.*?catch \(PDOException \$e\) \{\s*\$pdo->rollBack\(\);\s*error_log\("Failed to import global center assessment: " \. \$e->getMessage\(\)\);\s*throw \$e;\s*\}\s*\}', new_func, content, flags=re.DOTALL) with open('includes/cycles.php', 'w') as f: f.write(content) print("Fixed import function.")