/**
  * Format a timestamp to send to WebEx. Converts to GMT time.
  *
  * @param int       $time Timestamp to format.
  * @return string   The XML.
  */
 public static function time_to_date_string($time)
 {
     // Convert the time to GMT.
     $dt = new \DateTime(date("o:m:d H:i:s", $time), \core_date::get_server_timezone_object());
     $gmttime = $time - $dt->getOffset();
     return date('m/d/Y H:i:s', $gmttime);
 }
Ejemplo n.º 2
0
 /**
  * Test the temporary table creation and deletion.
  *
  * @depends test_statslib_temp_table_create_and_drop
  */
 public function test_statslib_temp_table_fill()
 {
     global $CFG, $DB, $USER;
     $dataset = $this->load_xml_data_file(__DIR__ . "/fixtures/statslib-test09.xml");
     $this->prepare_db($dataset[0], array('log'));
     // This nonsense needs to be rewritten.
     $date = new DateTime('now', core_date::get_server_timezone_object());
     $start = self::DAY - $date->getOffset();
     $end = $start + 24 * 3600;
     stats_temp_table_create();
     stats_temp_table_fill($start, $end);
     $this->assertEquals(1, $DB->count_records('temp_log1'));
     $this->assertEquals(1, $DB->count_records('temp_log2'));
     stats_temp_table_drop();
     // New log stores.
     $this->preventResetByRollback();
     stats_temp_table_create();
     $course = $this->getDataGenerator()->create_course();
     $context = context_course::instance($course->id);
     $fcontext = context_course::instance(SITEID);
     $user = $this->getDataGenerator()->create_user();
     $this->setUser($user);
     $this->assertFileExists("{$CFG->dirroot}/{$CFG->admin}/tool/log/store/standard/version.php");
     set_config('enabled_stores', 'logstore_standard', 'tool_log');
     set_config('buffersize', 0, 'logstore_standard');
     set_config('logguests', 1, 'logstore_standard');
     get_log_manager(true);
     $DB->delete_records('logstore_standard_log');
     \core_tests\event\create_executed::create(array('context' => $fcontext, 'courseid' => SITEID))->trigger();
     \core_tests\event\read_executed::create(array('context' => $context, 'courseid' => $course->id))->trigger();
     \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
     \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
     \core\event\user_loggedin::create(array('userid' => $USER->id, 'objectid' => $USER->id, 'other' => array('username' => $USER->username)))->trigger();
     $DB->set_field('logstore_standard_log', 'timecreated', 10);
     $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
     \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
     \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
     // Fake the origin of events.
     $DB->set_field('logstore_standard_log', 'origin', 'web', array());
     $this->assertEquals(7, $DB->count_records('logstore_standard_log'));
     stats_temp_table_fill(9, 11);
     $logs1 = $DB->get_records('temp_log1');
     $logs2 = $DB->get_records('temp_log2');
     $this->assertCount(5, $logs1);
     $this->assertCount(5, $logs2);
     // The order of records in the temp tables is not guaranteed...
     $viewcount = 0;
     $updatecount = 0;
     $logincount = 0;
     foreach ($logs1 as $log) {
         if ($log->course == $course->id) {
             $this->assertEquals('view', $log->action);
             $viewcount++;
         } else {
             $this->assertTrue(in_array($log->action, array('update', 'login')));
             if ($log->action === 'update') {
                 $updatecount++;
             } else {
                 $logincount++;
             }
             $this->assertEquals(SITEID, $log->course);
         }
         $this->assertEquals($user->id, $log->userid);
     }
     $this->assertEquals(1, $viewcount);
     $this->assertEquals(3, $updatecount);
     $this->assertEquals(1, $logincount);
     set_config('enabled_stores', '', 'tool_log');
     get_log_manager(true);
     stats_temp_table_drop();
 }
Ejemplo n.º 3
0
 public function test_get_server_timezone_object()
 {
     $this->resetAfterTest();
     $zones = core_date::get_list_of_timezones();
     foreach ($zones as $zone) {
         $this->setTimezone($zone, 'Pacific/Auckland');
         $tz = core_date::get_server_timezone_object();
         $this->assertInstanceOf('DateTimeZone', $tz);
         $this->assertSame($zone, $tz->getName());
     }
 }
Ejemplo n.º 4
0
 /**
  * Works out the next time the automated backup should be run.
  *
  * @param mixed $ignoredtimezone all settings are in server timezone!
  * @param int $now timestamp, should not be in the past, most likely time()
  * @return int timestamp of the next execution at server time
  */
 public static function calculate_next_automated_backup($ignoredtimezone, $now)
 {
     $config = get_config('backup');
     $backuptime = new DateTime('@' . $now);
     $backuptime->setTimezone(core_date::get_server_timezone_object());
     $backuptime->setTime($config->backup_auto_hour, $config->backup_auto_minute);
     while ($backuptime->getTimestamp() < $now) {
         $backuptime->add(new DateInterval('P1D'));
     }
     // Get number of days from backup date to execute backups.
     $automateddays = substr($config->backup_auto_weekdays, $backuptime->format('w')) . $config->backup_auto_weekdays;
     $daysfromnow = strpos($automateddays, "1");
     // Error, there are no days to schedule the backup for.
     if ($daysfromnow === false) {
         return 0;
     }
     if ($daysfromnow > 0) {
         $backuptime->add(new DateInterval('P' . $daysfromnow . 'D'));
     }
     return $backuptime->getTimestamp();
 }