/** * Execute task. */ public function execute() { try { \local_ousearch\year_tables::task_split_tables(); } catch (\moodle_exception $e) { // It is OK to throw exceptions, but doing so does not actually // display the exception in cron log. mtrace('Exception occurred:'); mtrace($e->getMessage()); mtrace($e->getTraceAsString()); throw $e; } }
/** * Tests the change_dates_chunk function. */ public function test_change_dates_chunk() { global $DB; $this->resetAfterTest(); // Create two test courses. $course1 = $this->getDataGenerator()->create_course(array('startdate' => strtotime('2013-01-04'))); $course2 = $this->getDataGenerator()->create_course(array('startdate' => strtotime('2015-01-04'))); // Turn the system completely on. set_config(year_tables::CONFIG_ENABLED, year_tables::ENABLED_TRANSFERRING, 'local_ousearch'); while (true) { if (year_tables::split_tables_chunk(false)) { break; } } // Add two documents to one course and one to the other. self::add_test_document(1, $course1, 'One'); self::add_test_document(2, $course1, 'Two'); self::add_test_document(3, $course2, 'Three'); $this->assertEquals(4, $DB->count_records('local_ousearch_occurs_2013')); $this->assertEquals(2, $DB->count_records('local_ousearch_occurs_2015')); // Initial search check. $this->check_search($course1, 'title', array(1, 2)); $this->check_search($course2, 'three', array(3)); // Change the date for course1. $DB->set_field('course', 'startdate', strtotime('2015-01-04'), array('id' => $course1->id)); year_tables::handle_updated_course($course1->id); // Move it all. $this->assertTrue(year_tables::change_dates_chunk(false)); $this->assertEquals(0, $DB->count_records('local_ousearch_occurs_2013')); $this->assertEquals(6, $DB->count_records('local_ousearch_occurs_2015')); $this->assertEquals('', $DB->get_field('local_ousearch_courseyears', 'oldyears', array('courseid' => $course1->id))); $this->check_search($course1, 'title', array(1, 2)); // Now move it twice. $DB->set_field('course', 'startdate', strtotime('2017-01-04'), array('id' => $course1->id)); year_tables::handle_updated_course($course1->id); $DB->set_field('course', 'startdate', strtotime('2016-01-04'), array('id' => $course1->id)); year_tables::handle_updated_course($course1->id); // This time it will do it in 2 chunks (2015 then 2017). $this->assertFalse(year_tables::change_dates_chunk(false)); $this->assertTrue(year_tables::change_dates_chunk(false)); $this->assertEquals(2, $DB->count_records('local_ousearch_occurs_2015')); $this->assertEquals(4, $DB->count_records('local_ousearch_occurs_2016')); $this->check_search($course1, 'title', array(1, 2)); // If there are too many to move in one chunk, do it in two. $DB->set_field('course', 'startdate', strtotime('2017-01-04'), array('id' => $course1->id)); year_tables::handle_updated_course($course1->id); $this->assertFalse(year_tables::change_dates_chunk(false, 1)); // Halfway through, one of the documents should have been copied while // the other is in the wrong table. $this->check_search($course1, 'title', array(1)); // Finish the job. $this->assertTrue(year_tables::change_dates_chunk(false, 1)); $this->check_search($course1, 'title', array(1, 2)); }
/** * Gets list of course-modules on the course which have search documents * and for which the user has accessallgroups OR the item is set to * visible groups. * @param int $courseid Course ID to check * @return array Array of course_module objects (id, course only) */ public static function get_group_exceptions($courseid) { global $DB; $year = year_tables::get_year_for_course(get_course($courseid)); $docstable = year_tables::get_docs_table($year); // Get all CMs that have a document. $possible = $DB->get_records_sql("\n SELECT DISTINCT cm.id AS cmid, cm.course AS cmcourse, cm.groupmode AS cmgroupmode, x.*\n FROM {" . $docstable . "} bod\n JOIN {course_modules} cm ON bod.coursemoduleid = cm.id\n JOIN {context} x ON x.instanceid = cm.id AND x.contextlevel = " . CONTEXT_MODULE . "\n WHERE bod.courseid = ?", array($courseid)); // Check accessallgroups on each one. $results = array(); foreach ($possible as $record) { if ($record->cmgroupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', context_course::instance($record->cmcourse))) { $results[] = (object) array('id' => $record->cmid, 'course' => $record->cmcourse); } } return $results; }