File size: 2,057 Bytes
72996f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import uuid

MODULES_DIR = os.path.join(os.path.dirname(__file__), 'modules')

class ModuleManager:
    def __init__(self):
        self.modules = {}
        self.load_modules()

    def load_modules(self):
        """Scan the modules directory and load all valid JSON module definitions."""
        self.modules = {}
        if not os.path.exists(MODULES_DIR):
            os.makedirs(MODULES_DIR)
            
        for filename in os.listdir(MODULES_DIR):
            if filename.endswith('.json'):
                filepath = os.path.join(MODULES_DIR, filename)
                try:
                    with open(filepath, 'r') as f:
                        module_data = json.load(f)
                        
                        # Validate required fields
                        required = ['id', 'title', 'category', 'description']
                        if all(k in module_data for k in required):
                            self.modules[module_data['id']] = module_data
                        else:
                            print(f"Warning: Module {filename} missing required fields.")
                except Exception as e:
                    print(f"Error loading module {filename}: {e}")

    def get_all_modules(self):
        """Return a list of all loaded module metadata."""
        return list(self.modules.values())

    def get_module(self, module_id):
        """Retrieve a specific module by ID."""
        return self.modules.get(module_id)

    def create_module_session(self, module_id):
        """Initialize a new assessment session for a module."""
        module = self.get_module(module_id)
        if not module:
            return None
            
        session_id = f"sess_{uuid.uuid4().hex[:8]}"
        
        return {
            'session_id': session_id,
            'module_id': module_id,
            'category': module['category'],
            'title': module['title'],
            'config': module.get('config', {})
        }

# Global instance
module_manager = ModuleManager()