rest-based-web-service / classes / Course.class.php
Course.class.php
Raw
<?php

class Course {
    /*--------properties--------*/
        // database
        private $db;
        private $title;
        private $code;
        private $progression;
        private $syllabus;
        private $id;
    
        /*---Constructor----*/ 
        function __construct(){
            $this->db = new mysqli('studentmysql.miun.se', 'alza2100', 'nZVgHJsAnn', 'alza2100');
            ; 
            if($this->db->connect_errno > 0 ){
                if($db->connect_error > 0){
                    die('Connection failed: ' . $this->db->connect_errno);
                }
            }
        }
    
        /*--- get all courses ----*/
        function getAllCourses() : array {
            // sql query 
            $sql = "SELECT * FROM course order by Id DESC; "; 
            $result = mysqli_query($this->db, $sql);
            
            return mysqli_fetch_all($result, MYSQLI_ASSOC) ;
        }
    
        /*--- get spesific course by id ----*/
        function getCourseById(int $id): array {
            $id = intval($id);
            $sql = "SELECT * FROM course WHERE id=$id ";
            $result = mysqli_query($this->db, $sql);
            $row = $result-> fetch_assoc();
            return $row;
        }
    
        /*---Setter for the title, code, progression, syllabus ----*/ 
        function setCourse(string $title, string $code,string $progression, string $syllabus ) : bool{
            // Check if is not empty string 
            if( ($title != '') and ($code != '') and ($progression != '') and ($syllabus != '') ) {
                $this->title = $title;
                $this->code = $code;
                $this->progression = $progression;
                $this->syllabus = $syllabus;
                return true;
            }
            else {
                return false;
            }
        }
    
    
        /*---Setter for the title, code, progression, syllabus with id ----*/ 
        function setCourseWithID( int $id , string $title, string $code,string $progression, string $syllabus) : bool{
            $id = intval($id);
            if( ($title != '') and ($code != '') and ($progression != '') and ($syllabus != '') ) {
                $this->title = $title;
                $this->code = $code;
                $this->progression = $progression;
                $this->syllabus = $syllabus;
                $this->id = $id;
                return true;
            }
            else {
                return false;
            }
        }
    
        // check if the course created
        function createCourse( ) : bool {
            if(!$this-> setCourse($this->title, $this->code, $this->progression, $this->syllabus )) return false;
            $sql =" INSERT INTO course SET title= '" .$this->title . "', code= '" .$this->code. "', progression= '" .$this->progression . "', syllabus= '" .$this->syllabus. "'; ";
            return mysqli_query($this->db, $sql);
    
        }
    
        /*--- update course ----*/
        function updateCourse( ) : bool {
            if(!$this-> setCourseWithID($this->id, $this->title, $this->code, $this->progression, $this->syllabus)) return false;
            // sql query
            $sql =" UPDATE course SET title= '" .$this->title . "', code= '" .$this->code. "', progression= '" .$this->progression . "', syllabus= '" .$this->syllabus. "' WHERE id= $this->id; ";
            // send query
            return mysqli_query($this->db, $sql);
        }
    
        /*--- delete course by its id ----*/
        function deleteCourse(int $id): bool {
            $id = intval($id);
            //sql query
            $sql = "DELETE FROM course WHERE id= $id;";
            // send query
            return mysqli_query($this->db, $sql);

    }
        
    
    }

?>