generate_learning_data.php 5.0 KB

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