diff --git a/application/classes/Model/Course.php b/application/classes/Model/Course.php index db52b82..29ce5fb 100644 --- a/application/classes/Model/Course.php +++ b/application/classes/Model/Course.php @@ -111,7 +111,24 @@ class Model_Course extends ORM { **/ public static function get_ids() { - return DB::select('course_id')->distinct(TRUE)->from('clients_courses')->execute()->as_array(NULL, 'course_id'); + $use_groups = Kohana::$config->load('common.groupmode'); + if( ! $use_groups) + { + return DB::select('course_id') + ->distinct(TRUE) + ->from('clients_courses') + ->execute() + ->as_array(NULL, 'course_id'); + } + else + { + $groups = Model_Group::get_ids(); + return DB::select('id') + ->from('courses') + ->where('group_id', 'IN', $groups) + ->execute() + ->as_array(NULL, 'id'); + } } public static function get_period($course_id) @@ -130,13 +147,31 @@ class Model_Course extends ORM { ->order_by('order'); return $query->execute()->as_array(NULL, 'id'); } + public static function get_client_ids($course_id) { - return DB::select('client_id') - ->from('clients_courses') - ->where('course_id', '=', $course_id) - ->execute() - ->get('client_id'); + $use_groups = Kohana::$config->load('common.groupmode'); + if( ! $use_groups) + { + return DB::select('client_id') + ->from('clients_courses') + ->where('course_id', '=', $course_id) + ->execute() + ->get('client_id'); + } + else + { + $group_id = DB::select('group_id') + ->from('courses') + ->where('id', '=', $course_id) + ->execute() + ->get('group_id'); + return DB::select('client_id') + ->from('clients_groups') + ->where('group_id', '=', $group_id) + ->execute() + ->get('client_id'); + } } /** diff --git a/application/classes/Model/Group.php b/application/classes/Model/Group.php index 88c9027..b496c11 100644 --- a/application/classes/Model/Group.php +++ b/application/classes/Model/Group.php @@ -48,4 +48,12 @@ class Model_Group extends ORM { { return self::count($this->id); } + + /** + * Get ID of all groups which have subscribers + **/ + public static function get_ids() + { + return DB::select('group_id')->distinct(TRUE)->from('clients_groups')->execute()->as_array(NULL, 'group_id'); + } } diff --git a/application/classes/Task/Prepare.php b/application/classes/Task/Prepare.php index f2853d8..659430d 100644 --- a/application/classes/Task/Prepare.php +++ b/application/classes/Task/Prepare.php @@ -17,10 +17,12 @@ class Task_Prepare extends Minion_Task if (is_null($course)) { Log::instance()->add(Log::ERROR, 'Course ID is NULL when preparing'); + echo "Course ID is NULL when preparing\n"; return; } $count = Model_Course::count_letters($course); if ($count == 0) + echo "No letters found in course $course; skipping.\n"; return; $period = Model_Course::get_period($course); $clients = Model_Course::get_client_ids($course, $period); @@ -46,6 +48,8 @@ class Task_Prepare extends Minion_Task if ($letter !== FALSE) { Model_Task::prepare($client_id, $letter); + } else { + echo "No letters to prepare.\n"; } } else { echo "Letter won't be prepared because it's too early to send it.\n"; @@ -83,6 +87,8 @@ class Task_Prepare extends Minion_Task } catch(Database_Exception $e) { + echo $e->getMessage(); + Log::instance()->add(Log::ERROR, $e->getMessage()); $db->rollback(); } } diff --git a/application/tests/CoursesTest.php b/application/tests/CoursesTest.php index 2f9a8e4..b28322d 100644 --- a/application/tests/CoursesTest.php +++ b/application/tests/CoursesTest.php @@ -15,6 +15,20 @@ class CoursesTest extends Unittest_Database_TestCase return $this->createXMLDataSet(Kohana::find_file('tests', 'test_data/courses', 'xml')); } + function test_get_ids() + { + $courses = Model_Course::get_ids(); + $this->assertNotEmpty($courses); + $this->assertContains(1, $courses); + } + + function test_get_clients() + { + $clients = Model_Course::get_client_ids(1); + $this->assertNotEmpty($clients); + $this->assertContains('1', $clients); + } + /** * @group Mail **/ diff --git a/application/tests/test_data/courses.xml b/application/tests/test_data/courses.xml index 03bd0e8..846c775 100644 --- a/application/tests/test_data/courses.xml +++ b/application/tests/test_data/courses.xml @@ -1,17 +1,27 @@ + + id + name + + 1 + Test Group + +
idtitledescriptionperiodprice + group_id 1 Test course Application testing purposes 1 0 + 1
@@ -36,6 +46,11 @@ 1
+ + id + client_id + group_id +
idtext