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
|
|
|
/**
|
2014-02-05 10:10:05 +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
|
|
|
|
**/
|
2014-02-05 10:10:05 +02:00
|
|
|
class Model_Course extends ORM {
|
2016-10-16 10:34:18 +03:00
|
|
|
/**
|
|
|
|
* A pre-scheduled course - the subscriber gets a new letter once per N days in the series.
|
|
|
|
*/
|
2016-10-19 06:56:43 +03:00
|
|
|
const TYPE_SCHEDULED = 0;
|
2016-10-16 10:34:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A irregular subscription - the letters are sent when the author sends them
|
|
|
|
*/
|
2016-10-19 06:56:43 +03:00
|
|
|
const TYPE_IRREGULAR = 1;
|
2016-10-16 10:34:18 +03:00
|
|
|
|
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',
|
2014-02-05 10:10:05 +02:00
|
|
|
'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()
|
2016-10-05 06:44:39 +03:00
|
|
|
{
|
|
|
|
return array(
|
2014-01-18 08:03:47 +02:00
|
|
|
'title' => array(
|
2016-10-05 06:44:39 +03:00
|
|
|
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(
|
2016-10-05 06:44:39 +03:00
|
|
|
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
|
|
|
),
|
2016-10-05 06:44: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()
|
|
|
|
{
|
2014-02-05 10:10:05 +02:00
|
|
|
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)
|
|
|
|
{
|
2014-02-05 10:10:05 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2014-02-05 10:10:05 +02:00
|
|
|
public static function get_period($course_id)
|
2014-02-03 07:26:56 +02:00
|
|
|
{
|
2014-02-15 07:41:05 +02:00
|
|
|
$query = DB::select('period')
|
2014-02-05 10:10:05 +02:00
|
|
|
->from('courses')
|
2014-02-15 07:41:05 +02:00
|
|
|
->where('id', '=', $course_id);
|
|
|
|
return $query->execute()->get('period');
|
2014-02-03 07:26:56 +02:00
|
|
|
}
|
|
|
|
|
2014-02-05 10:10:05 +02:00
|
|
|
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')
|
2014-02-05 10:10:05 +02:00
|
|
|
->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
|
|
|
}
|
2014-02-05 10:10:05 +02:00
|
|
|
public static function get_client_ids($course_id)
|
2014-01-31 10:15:50 +02:00
|
|
|
{
|
|
|
|
return DB::select('client_id')
|
2014-02-05 10:10:05 +02:00
|
|
|
->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
|
|
|
/**
|
2014-02-05 10:10:05 +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')
|
2014-02-05 10:10:05 +02:00
|
|
|
->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')
|
2014-02-05 10:10:05 +02:00
|
|
|
->where('course_id', '=', $this->id)
|
2014-02-04 11:01:29 +02:00
|
|
|
->execute();
|
2014-02-05 10:10:05 +02:00
|
|
|
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
|
|
|
}
|