<?php namespace App\Models;

use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;

class CourseModel extends Model
{
    protected $table = 'courses';

    protected $primaryKey = 'id';

    protected $allowedFields = ['courseID','facultyID','courseName','introText','duration','durationType','targetAudience','methodology','courseObjectives','feeKshs','feeUsd','imgUrl','dateCreated','createdBy','dateModified','modifiedBy','status','meta_keywords','meta_desc'];

public function getCoursesList()
{
    $db      = \Config\Database::connect();
    $builder = $db->table('courses');        // 'mytablename' is the name of your table

    $builder->select('courseID,courseName,duration,feeKshs,feeUsd,typeName');
    $builder->join('durationtypes', 'courses.durationType = durationtypes.typeID');
    $query = $builder->get();

    return $query->getResult();
  }

  public function getCategoryCourses($catId){
      $db      = \Config\Database::connect();
      $builder = $db->table('courses');        // 'mytablename' is the name of your table

      $builder->select('courseID,courseName,duration,feeKshs,feeUsd,typeName,meta_desc,meta_keywords');
      $builder->join('durationtypes', 'courses.durationType = durationtypes.typeID');
      $query = $builder->getWhere(['facultyID' => $catId]);


      return $query->getResult();
    }

    public function getCourse($slug = false)
{
    if ($slug === false)
    {
        return $this->findAll();
    }

    return $this->asArray()
                ->where(['slug' => $slug])
                ->first();
}


  public function getCourseDetails($courseId){
      $db      = \Config\Database::connect();
      $builder = $db->table('courses');        // 'mytablename' is the name of your table

      $builder->select('*');
      $builder->join('durationtypes', 'courses.durationType = durationtypes.typeID');
      $query = $builder->getWhere(['courseID' => $courseId]);

      return $query->getRow();
    }

    public function updateCourse($courseId,$data){

        $db= \Config\Database::connect();
       $builder = $db->table('courses');

        $builder->where('courseID', $courseId);
        $builder->update($data);



    }

    }
