-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoursevalidator.sol
More file actions
64 lines (51 loc) · 1.98 KB
/
Coursevalidator.sol
File metadata and controls
64 lines (51 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract CourseValidator {
struct Module {
uint progress; // progress in percentage (0–100)
}
struct Course {
uint id;
address learner;
bool completed;
Module[] modules;
}
mapping(uint => Course) public courses;
mapping(uint => bool) public certificateIssued;
event CourseCompleted(uint courseId, address learner);
/// @notice Add a new course with a number of modules
function addCourse(uint courseId, address learner, uint moduleCount) public {
require(courses[courseId].id == 0, "Course already exists");
Course storage course = courses[courseId];
course.id = courseId;
course.learner = learner;
for (uint i = 0; i < moduleCount; i++) {
course.modules.push(Module(0));
}
}
/// @notice Update a module's progress
function updateProgress(uint courseId, uint moduleIndex, uint newProgress) public {
Course storage course = courses[courseId];
require(course.id != 0, "Course not found");
require(moduleIndex < course.modules.length, "Invalid module index");
require(newProgress <= 100, "Progress can't exceed 100");
course.modules[moduleIndex].progress = newProgress;
validateCourseCompletion(courseId);
}
/// @notice Check if all modules are complete and issue certificate
function validateCourseCompletion(uint courseId) internal {
Course storage course = courses[courseId];
if (course.completed) return;
for (uint i = 0; i < course.modules.length; i++) {
if (course.modules[i].progress < 100) {
return;
}
}
// All modules are 100% → mark course complete
course.completed = true;
// ✅ Certificate issuance logic
certificateIssued[courseId] = true;
// 🔔 Emit event
emit CourseCompleted(courseId, course.learner);
}
}