1
0
Fork 0
mirror of https://github.com/Oreolek/kangana.git synced 2024-06-16 23:11:07 +03:00
kangana/application/classes/Model/Course.php

160 lines
3.8 KiB
PHP
Raw Normal View History

2014-01-17 13:39:34 +02:00
<?php defined('SYSPATH') OR die('No direct access allowed.');
2014-01-18 08:03:47 +02:00
/**
* Course model.
* Course is an ordered collection of letters.
2014-01-18 08:03:47 +02:00
* It has a period in days. Every <period> days a client receives a letter from the collection.
* @package Models
* @author Oreolek
**/
class Model_Course extends ORM {
2014-01-28 09:50:18 +02:00
protected $_has_many = array(
2014-03-12 08:57:57 +02:00
'clients' => array(
2014-01-28 09:50:18 +02:00
'model' => 'Client',
'through' => 'clients_courses'
2014-01-28 13:07:36 +02:00
),
'letters' => array(
'model' => 'Letter'
2016-10-12 06:22:02 +03:00
),
'group' => array(
'model' => 'Group',
'through' => 'courses_groups'
),
2014-01-28 09:50:18 +02:00
);
2014-01-18 08:03:47 +02:00
/**
* @return array validation rules
**/
public function rules()
{
return array(
2014-01-18 08:03:47 +02:00
'title' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 100)),
2014-01-18 10:02:16 +02:00
),
2014-01-18 08:03:47 +02:00
'description' => array(
array('not_empty'),
array('min_length', array(':value', 20)),
2014-01-18 08:03:47 +02:00
),
'period' => array(
array('numeric')
),
'price' => array(
array('numeric')
2016-10-12 08:48:39 +03:00
),
);
}
2014-01-18 08:03:47 +02:00
/**
* Array of field labels.
* Used in forms.
**/
protected $_labels = array(
'title' => 'Title',
'price' => 'Subscription price',
'period' => 'Mailing period (in days)',
'description' => 'Description (for the clients)'
);
2014-01-18 10:02:16 +02:00
public function customize()
{
if ($this->period < 1)
{
$this->period = 1;
}
2014-02-15 05:53:52 +02:00
if (empty($this->price))
{
$this->price = 0;
}
2014-01-18 10:02:16 +02:00
}
2014-01-29 10:52:33 +02:00
public static function count_letters($id)
{
2014-02-15 07:41:05 +02:00
$query = DB::select(array(DB::expr('COUNT(*)'), 'cnt'))->from('letters')->where('course_id', '=', $id);
return $query->execute()->get('cnt');
2014-01-29 10:52:33 +02:00
}
2014-02-02 05:25:58 +02:00
/**
* Return subscriber count
**/
public function count_clients()
{
return DB::select(array(DB::expr('COUNT(client_id)'), 'cnt'))->from('clients_courses')->where('course_id', '=', $this->id)->execute()->get('cnt');
2014-02-02 05:25:58 +02:00
}
2014-01-29 10:52:33 +02:00
public static function exists($id)
{
$count = DB::select(array(DB::expr('COUNT(*)'), 'cnt'))->from('courses')->where('id', '=', $id)->execute()->get('cnt');
2014-01-29 10:52:33 +02:00
return ($count == 1);
}
2014-01-29 14:13:04 +02:00
2014-02-15 07:41:05 +02:00
/**
* Get ID of all courses which have subscribers
**/
2014-01-31 10:15:50 +02:00
public static function get_ids()
{
2014-02-27 10:07:00 +02:00
return DB::select('course_id')->distinct(TRUE)->from('clients_courses')->execute()->as_array(NULL, 'course_id');
2014-01-31 10:15:50 +02:00
}
public static function get_period($course_id)
{
2014-02-15 07:41:05 +02:00
$query = DB::select('period')
->from('courses')
2014-02-15 07:41:05 +02:00
->where('id', '=', $course_id);
return $query->execute()->get('period');
}
public static function get_letter_ids($course_id)
2014-01-31 10:15:50 +02:00
{
2014-02-15 07:41:05 +02:00
$query = DB::select('id')
2014-01-31 10:15:50 +02:00
->from('letters')
->where('course_id', '=', $course_id)
2014-02-15 07:41:05 +02:00
->order_by('order');
return $query->execute()->as_array(NULL, 'id');
2014-01-31 10:15:50 +02:00
}
public static function get_client_ids($course_id)
2014-01-31 10:15:50 +02:00
{
return DB::select('client_id')
->from('clients_courses')
->where('course_id', '=', $course_id)
2014-01-31 10:15:50 +02:00
->execute()
->get('client_id');
}
2014-01-29 14:13:04 +02:00
/**
* Get next letter in course
2014-01-29 14:13:04 +02:00
* @param int $offset search offset (typically number of already sent letters)
**/
public function next_letter($offset = 0)
{
return ORM::factory('Letter')
->where('course_id', '=', $this->id)
2014-01-29 14:13:04 +02:00
->order_by('order', 'ASC')
->limit(1)
->offset($offset)
->find();
}
2014-02-04 11:01:29 +02:00
public function delete()
{
$letter_ids = $this->get_letter_ids($this->id);
$query = DB::delete('tasks');
if (is_array($letter_ids))
{
$query->where('letter_id', 'IN', $letter_ids);
}
else
{
$query->where('letter_id', '=', $letter_ids);
}
$query->execute();
DB::delete('letters')
->where('course_id', '=', $this->id)
2014-02-04 11:01:29 +02:00
->execute();
DB::delete('clients_courses')
->where('course_id', '=', $this->id)
2014-02-04 11:01:29 +02:00
->execute();
return parent::delete();
}
2014-01-17 13:39:34 +02:00
}