# tools_manager.py import os import re import json import math from datetime import datetime class ToolsManager: def __init__(self): self.base_dir = "tools_records" os.makedirs(self.base_dir, exist_ok=True) def safe_name(self, value): value = str(value or "").strip() value = re.sub(r"[^a-zA-Z0-9_\-ء-ي]+", "_", value) return value[:80] or "student" def calculate(self, expression): try: expression = str(expression or "").strip() if not expression: return { "success": False, "message": "اكتب العملية الحسابية أولًا." } expression = expression.replace("^", "**") allowed_names = { "sqrt": math.sqrt, "sin": math.sin, "cos": math.cos, "tan": math.tan, "pi": math.pi, "e": math.e, "abs": abs, "round": round, "pow": pow } result = eval(expression, {"__builtins__": {}}, allowed_names) return { "success": True, "message": f"النتيجة: {result}", "result": result } except Exception as e: return { "success": False, "message": f"تعذر الحساب: {e}" } def save_student_tool_note(self, identity, context, note, uploaded_file=None): try: identity = self.safe_name(identity) context = str(context or "عام").strip() note = str(note or "").strip() student_dir = os.path.join(self.base_dir, identity) os.makedirs(student_dir, exist_ok=True) stamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S") uploaded_path = None if uploaded_file is not None: try: uploaded_path = uploaded_file.name except Exception: uploaded_path = str(uploaded_file) payload = { "identity": identity, "context": context, "note": note, "uploaded_file": uploaded_path, "created_at": datetime.utcnow().isoformat(), "status": "saved_for_teacher_review" } path = os.path.join(student_dir, f"tool_note_{stamp}.json") with open(path, "w", encoding="utf-8") as f: json.dump(payload, f, ensure_ascii=False, indent=2) return { "success": True, "message": f"تم حفظ الملاحظة/السؤال لمراجعة الأستاذ: {path}", "path": path } except Exception as e: return { "success": False, "message": f"فشل حفظ الملاحظة: {e}" } def get_whiteboard_html(self): return """

السبورة التجريبية

هذه نسخة خفيفة آمنة. احفظ صورة السبورة، ثم يمكنك رفعها من أدواتي ليشاهدها الأستاذ.

""".strip()