Source Code

;; Fiqh Course - Islamic jurisprudence courses
;; Halal - learning fiqh
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var course-count uint u0)
(define-map courses uint { instructor: principal, title: (string-utf8 100), madhab: (string-ascii 20), fee: uint, students: uint })
(define-map enrollments { course-id: uint, student: principal } { paid: uint, completed: bool })
(define-public (create-course (title (string-utf8 100)) (madhab (string-ascii 20)) (fee uint))
  (let ((id (+ (var-get course-count) u1)))
    (map-set courses id { instructor: tx-sender, title: title, madhab: madhab, fee: fee, students: u0 })
    (var-set course-count id) (ok id)))
(define-public (enroll (course-id uint))
  (let ((c (unwrap! (map-get? courses course-id) ERR-NONE)))
    (try! (stx-transfer? (get fee c) tx-sender (get instructor c)))
    (map-set enrollments { course-id: course-id, student: tx-sender } { paid: (get fee c), completed: false })
    (map-set courses course-id (merge c { students: (+ (get students c) u1) })) (ok true)))
(define-public (mark-complete (course-id uint) (student principal))
  (let ((c (unwrap! (map-get? courses course-id) ERR-NONE)) (e (unwrap! (map-get? enrollments { course-id: course-id, student: student }) ERR-NONE)))
    (asserts! (is-eq tx-sender (get instructor c)) ERR-AUTH)
    (map-set enrollments { course-id: course-id, student: student } (merge e { completed: true })) (ok true)))
(define-read-only (get-course (id uint)) (map-get? courses id))
(define-read-only (get-enrollment (id uint) (who principal)) (map-get? enrollments { course-id: id, student: who }))
(define-read-only (get-course-count) (ok (var-get course-count)))

Functions (6)

FunctionAccessArgs
create-coursepublictitle: (string-utf8 100
enrollpubliccourse-id: uint
mark-completepubliccourse-id: uint, student: principal
get-courseread-onlyid: uint
get-enrollmentread-onlyid: uint, who: principal
get-course-countread-only