generate_learning_data.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use App\Services\LearningAnalyticsService;
  4. echo "=" . str_repeat("=", 60) . "\n";
  5. echo "🚀 LearningAnalytics 学习数据生成器\n";
  6. echo "=" . str_repeat("=", 60) . "\n\n";
  7. // 初始化服务
  8. $service = new LearningAnalyticsService();
  9. // 配置
  10. $kpCodes = ['KP1001', 'KP1101', 'KP9003', 'KP8001', 'KP9002']; // 因式分解相关知识点
  11. $studentsPerTeacher = 5;
  12. $attemptsPerStudent = 30;
  13. try {
  14. // 从LearningAnalytics获取知识点
  15. echo "📚 获取知识点列表...\n";
  16. $knowledgePoints = $service->getKnowledgePoints();
  17. echo "✅ 获取到 " . count($knowledgePoints) . " 个知识点\n\n";
  18. // 获取老师列表
  19. echo "👥 获取老师列表...\n";
  20. $teachers = DB::connection('remote_mysql')
  21. ->table('teachers')
  22. ->get()
  23. ->toArray();
  24. echo "✅ 找到 " . count($teachers) . " 个老师\n\n";
  25. $totalStudents = 0;
  26. $totalAttempts = 0;
  27. foreach ($teachers as $teacher) {
  28. echo "🎓 处理老师: {$teacher->teacher_id} - {$teacher->name}\n";
  29. echo str_repeat("-", 60) . "\n";
  30. // 获取该老师的学生
  31. $students = DB::connection('remote_mysql')
  32. ->table('students')
  33. ->where('teacher_id', $teacher->teacher_id)
  34. ->limit($studentsPerTeacher)
  35. ->get()
  36. ->toArray();
  37. echo " 📝 找到 " . count($students) . " 个学生\n";
  38. foreach ($students as $student) {
  39. echo "\n 👤 学生: {$student->student_id} - {$student->name}\n";
  40. echo " 班级: {$student->grade} {$student->class_name}\n";
  41. // 生成学习数据
  42. $attempts = [];
  43. $mastery = 0.3; // 初始掌握度
  44. // 生成30次答题记录
  45. for ($i = 0; $i < $attemptsPerStudent; $i++) {
  46. // 随机选择知识点
  47. $kpCode = $kpCodes[array_rand($kpCodes)];
  48. // 模拟学习进度:前10次错误较多,后面逐渐变好
  49. $isCorrect = false;
  50. if ($i < 10) {
  51. $isCorrect = (rand(1, 100) <= 30); // 30% 正确率
  52. } elseif ($i < 20) {
  53. $isCorrect = (rand(1, 100) <= 60); // 60% 正确率
  54. } else {
  55. $isCorrect = (rand(1, 100) <= 80); // 80% 正确率
  56. }
  57. // 模拟答题时间(逐渐变快)
  58. $baseTime = 180;
  59. $timeSpent = max(60, $baseTime - ($i * 4) + rand(-20, 20));
  60. // 更新掌握度
  61. if ($isCorrect) {
  62. $mastery = $mastery + (1 - $mastery) * 0.15;
  63. } else {
  64. $mastery = $mastery - ($mastery * 0.08);
  65. }
  66. $mastery = max(0.1, min(0.95, $mastery)); // 限制在0.1-0.95之间
  67. $attempts[] = [
  68. 'kp_code' => $kpCode,
  69. 'is_correct' => $isCorrect,
  70. 'time_spent_seconds' => $timeSpent,
  71. 'difficulty_level' => rand(2, 4),
  72. 'mastery' => $mastery
  73. ];
  74. }
  75. // 提交到LearningAnalytics
  76. echo " 📤 提交学习数据到LearningAnalytics...\n";
  77. $successCount = 0;
  78. foreach ($attempts as $attempt) {
  79. $result = $service->updateMastery([
  80. 'student_id' => $student->student_id,
  81. 'kp_code' => $attempt['kp_code'],
  82. 'is_correct' => $attempt['is_correct'],
  83. 'time_spent_seconds' => $attempt['time_spent_seconds'],
  84. 'difficulty_level' => $attempt['difficulty_level']
  85. ]);
  86. if (isset($result['error']) && !$result['error']) {
  87. $successCount++;
  88. }
  89. }
  90. $finalMastery = $mastery;
  91. echo " ✅ 成功提交 {$successCount} 条记录\n";
  92. echo " 📊 最终掌握度: " . number_format($finalMastery * 100, 2) . "%\n";
  93. $totalStudents++;
  94. $totalAttempts += $successCount;
  95. }
  96. echo "\n";
  97. }
  98. echo "=" . str_repeat("=", 60) . "\n";
  99. echo "📈 生成完成!\n";
  100. echo "=" . str_repeat("=", 60) . "\n";
  101. echo "✅ 处理学生数: {$totalStudents}\n";
  102. echo "✅ 总答题记录: {$totalAttempts}\n";
  103. echo "✅ 平均每人: " . number_format($totalAttempts / max(1, $totalStudents), 1) . " 次\n";
  104. echo "\n🎉 所有学习数据已生成并同步到LearningAnalytics!\n";
  105. } catch (Exception $e) {
  106. echo "❌ 错误: " . $e->getMessage() . "\n";
  107. echo "📍 位置: " . $e->getFile() . ":" . $e->getLine() . "\n";
  108. echo "\n" . str_repeat("=", 60) . "\n";
  109. exit(1);
  110. }