/**
 * Hook into the task scheduler. Runs a query to find all comments and verification status updates that need
 * to be notified back to the recorder of a record. 
 */
function notify_verifications_and_comments_scheduled_task($last_run_date)
{
    if (!$last_run_date) {
        // first run, so get all records changed in last day. Query will automatically gradually pick up the rest.
        $last_run_date = date('Y-m-d', time() - 60 * 60 * 24 * 50);
    }
    try {
        $db = new Database();
        $notifications = postgreSQL::selectVerificationAndCommentNotifications($last_run_date, $db);
        foreach ($notifications as $notification) {
            $vd = array($notification->date_start, $notification->date_end, $notification->date_type);
            $date = vague_date::vague_date_to_string($vd);
            if (empty($notification->comment)) {
                switch ($notification->record_status) {
                    case 'V':
                        $action = 'verified';
                        break;
                    case 'R':
                        $action = 'rejected';
                        break;
                    case 'D':
                        $action = 'marked dubious';
                        break;
                    case 'S':
                        $action = 'emailed for checking';
                        break;
                }
                $comment = 'The record of ' . $notification->taxon . ' at ' . $notification->public_entered_sref . " on {$date} was {$action}.";
            } else {
                if ($notification->auto_generated === 't') {
                    $comment = 'An automated check using the <a target="_blank" href="http://www.nbn.org.uk/Tools-Resources/Recording-Resources/NBN-Record-Cleaner.aspx" target="_blank">' . 'NBN Record Cleaner</a> rules has highlighted your record of ' . $notification->taxon . ' at ' . $notification->public_entered_sref . ' on ' . $date;
                    $comment .= $notification->generated_by === 'data_cleaner_identification_difficulty' ? ' as being of a species for which identification is not always trivial. <br/><em>' : '. The following information was given: <br/><em>';
                } elseif ($notification->verified_on > $last_run_date and $notification->record_status !== 'I' and $notification->record_status !== 'T' and $notification->record_status !== 'C') {
                    $comment = 'Your record of ' . $notification->taxon . ' at ' . $notification->public_entered_sref . ' on ' . $date . ' was examined by an expert.<br/>"';
                } elseif ($notification->record_owner === 't') {
                    $comment = 'A comment was added to your record of ' . $notification->taxon . ' at ' . $notification->public_entered_sref . ' on ' . $date . '.<br/>"';
                } else {
                    $comment = 'A reply was added to the record of ' . $notification->taxon . ' at ' . $notification->public_entered_sref . ' on ' . $date . ' which you\'ve previously commented on.<br/>"';
                }
                $comment .= $notification->comment;
                if ($notification->auto_generated === 't') {
                    // a difficult ID record is not necessarily important...
                    $thing = $notification->generated_by === 'data_cleaner_identification_difficulty' ? 'identification' : 'important record';
                    $comment .= "</em><br/>You may be contacted by an expert to confirm this {$thing} so if you can supply any more information or photographs it would be useful.";
                } else {
                    $comment .= '"<br/>';
                }
            }
            $theNotificationToInsert = array('source' => 'Verifications and comments', 'source_type' => $notification->source_type, 'data' => json_encode(array('username' => $notification->username, 'occurrence_id' => $notification->id, 'comment' => $comment, 'taxon' => $notification->taxon, 'date' => $date, 'entered_sref' => $notification->public_entered_sref, 'auto_generated' => $notification->auto_generated, 'record_status' => $notification->record_status, 'updated_on' => $notification->updated_on)), 'linked_id' => $notification->id, 'user_id' => $notification->notify_user_id, 'digest_mode' => 'N', 'source_detail' => $notification->source_detail);
            $db->insert('notifications', $theNotificationToInsert);
        }
        echo count($notifications) . ' notifications generated<br/>';
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
예제 #2
0
 public function Index()
 {
     $time = time();
     echo '<ul>';
     echo '<li>Today: ' . vague_date::vague_date_to_string(array(date_create('2008-11-01'), date_create('2008-11-01'), 'D')) . '</li>';
     echo '<li>Day range for 1 week: ' . vague_date::vague_date_to_string(array(date_create('2008-11-01'), date_create('2008-11-08'), 'DD')) . '</li>';
     echo '<li>Month in Year: ' . vague_date::vague_date_to_string(array(date_create('2008-11-01'), date_create('2008-11-30'), 'O')) . '</li>';
     echo '<li>Month in Year range: ' . vague_date::vague_date_to_string(array(date_create('2008-10-01'), date_create('2008-11-30'), 'OO')) . '</li>';
     echo '<li>Season in year: ' . vague_date::vague_date_to_string(array(date_create('2008-09-01'), date_create('2008-11-30'), 'P')) . '</li>';
     echo '<li>Year: ' . vague_date::vague_date_to_string(array(date_create('2008-01-01'), date_create('2008-12-31'), 'Y')) . '</li>';
     echo '<li>Years: ' . vague_date::vague_date_to_string(array(date_create('2006-01-01'), date_create('2008-12-31'), 'YY')) . '</li>';
     echo '<li>From Year: ' . vague_date::vague_date_to_string(array(date_create('2006-01-01'), null, 'Y-')) . '</li>';
     echo '<li>To Year: ' . vague_date::vague_date_to_string(array(null, date_create('2008-12-31'), '-Y')) . '</li>';
     echo '<li>Month: ' . vague_date::vague_date_to_string(array(date_create('2006-01-01'), date_create('2006-01-31'), 'M')) . '</li>';
     echo '<li>Season: ' . vague_date::vague_date_to_string(array(date_create('2008-03-01'), date_create('2008-05-31'), 'S')) . '</li>';
     echo '<li>Unknown: ' . vague_date::vague_date_to_string(array(null, null, 'U')) . '</li>';
     echo '<li>Century: ' . vague_date::vague_date_to_string(array(date_create('1801-01-01'), date_create('1900-12-31'), 'C')) . '</li>';
     echo '<li>Centuries: ' . vague_date::vague_date_to_string(array(date_create('1701-01-01'), date_create('1900-12-31'), 'CC')) . '</li>';
     echo '<li>Century From: ' . vague_date::vague_date_to_string(array(date_create('1701-01-01'), null, 'C-')) . '</li>';
     echo '<li>Century To: ' . vague_date::vague_date_to_string(array(null, date_create('1800-12-31'), '-C')) . '</li>';
     echo '</ul';
 }
 /** 
  * Create a virtual field called default_value from the relevant default value fields, depending on the data type.
  */
 public function __get($column)
 {
     if ($column == 'default_value') {
         $attr = ORM::factory('survey_attribute', $this->survey_attribute_id);
         switch ($attr->data_type) {
             case 'T':
                 return parent::__get('default_text_value');
             case 'F':
                 return parent::__get('default_float_value');
             case 'I':
             case 'L':
                 return parent::__get('default_int_value');
             case 'D':
             case 'V':
                 $vagueDate = array(parent::__get('default_date_start_value'), parent::__get('default_date_end_value'), parent::__get('default_date_type_value'));
                 return vague_date::vague_date_to_string($vagueDate);
         }
     } else {
         return parent::__get($column);
     }
 }
예제 #4
0
 /**
  * Override load_values to add in a vague date field. Also strips out any custom attribute values which don't go into this model.
  * @param   array  values to load
  * @return  ORM
  */
 public function load_values(array $values)
 {
     // clear out any values which match this attribute field prefix
     if (isset($this->attrs_field_prefix)) {
         foreach ($values as $key => $value) {
             if (substr($key, 0, strlen($this->attrs_field_prefix) + 1) == $this->attrs_field_prefix . ':') {
                 unset($values[$key]);
             }
         }
     }
     parent::load_values($values);
     // Add in date field
     if (array_key_exists('date_type', $this->object) && !empty($this->object['date_type'])) {
         $vd = vague_date::vague_date_to_string(array($this->object['date_start'], $this->object['date_end'], $this->object['date_type']));
         $this->object['date'] = $vd;
     }
     return $this;
 }
예제 #5
0
 /**
  * Method to apply a parameterised test on a vague date conversion to a string
  */
 protected function checkConvertVagueDate($from, $to, $type, $expected)
 {
     $fromDate = $from ? new DateTime($from) : null;
     $toDate = $to ? new DateTime($to) : null;
     $vd = array($fromDate, $toDate, $type);
     $s = vague_date::vague_date_to_string($vd);
     $this->assertEquals($expected, $s, 'Failed converting vague date (dates) to ' . $expected);
     // test using strings rather than date objects
     $fromStr = $from ? $from : '';
     $toStr = $to ? $to : '';
     $vd = array($fromStr, $toStr, $type);
     $s = vague_date::vague_date_to_string($vd);
     $this->assertEquals($expected, $s, 'Failed converting vague date (strings) to ' . $expected);
 }
예제 #6
0
 /**
  * Takes the data and columns lists, and looks for a vague date column set.
  * If one is found, inserts a new column for the processed date string.
  */
 private function add_vague_dates(&$data)
 {
     $col_sets = array();
     $columnNames = array_keys($this->columns);
     // First find the additional plaintext columns we need to add
     foreach ($this->columns as $col => $coldef) {
         if (isset($coldef['on_demand']) && $coldef['on_demand'] === "true") {
             continue;
         }
         if (substr($col, -10) == 'date_start') {
             $prefix = substr($col, 0, strlen($col) - 10);
             // check that the report includes date_end and type
             if (in_array($prefix . "date_end", $columnNames) && in_array($prefix . "date_type", $columnNames)) {
                 array_push($col_sets, $prefix);
                 if (!in_array($prefix . 'date', $columnNames)) {
                     $this->columns[$prefix . 'date'] = array('display' => 'Date', 'class' => '', 'style' => '', 'datatype' => 'date');
                 }
                 // Hide the internal vague date columns, unless the report explicitly asks for them (in which case
                 // autodef will not be true).
                 if (!array_key_exists('autodef', $this->columns[$prefix . 'date_start']) || $this->columns[$prefix . 'date_start']['autodef'] == true) {
                     $this->columns[$prefix . 'date_start']['visible'] = 'false';
                 }
                 if (!array_key_exists('autodef', $this->columns[$prefix . 'date_end']) || $this->columns[$prefix . 'date_end']['autodef'] == true) {
                     $this->columns[$prefix . 'date_end']['visible'] = 'false';
                 }
                 if (!array_key_exists('autodef', $this->columns[$prefix . 'date_type']) || $this->columns[$prefix . 'date_type']['autodef'] == true) {
                     $this->columns[$prefix . 'date_type']['visible'] = 'false';
                 }
             }
         }
     }
     // Now we have identified the vague date columns to add, create data columns with the processed date
     // strings.
     $dataCount = count($data);
     // invariant
     foreach ($col_sets as $col_set) {
         for ($r = 0; $r < $dataCount; $r++) {
             $row = $data[$r];
             $data[$r][$col_set . 'date'] = vague_date::vague_date_to_string(array($row[$col_set . 'date_start'], $row[$col_set . 'date_end'], $row[$col_set . 'date_type']));
         }
     }
 }
/**
 * Hook into the task scheduler. Runs a query to find all comments and verification status updates that need
 * to be notified back to the recorder of a record.
 * @param string $last_run_date Date & time that this module was last run.
 * @throws \Kohana_Database_Exception
 */
function notify_verifications_and_comments_scheduled_task($last_run_date)
{
    if (!$last_run_date) {
        // first run, so get all records changed in last day. Query will automatically gradually pick up the rest.
        $last_run_date = date('Y-m-d', time() - 60 * 60 * 24 * 50);
    }
    $db = new Database();
    $notifications = postgreSQL::selectVerificationAndCommentNotifications($last_run_date, $db);
    foreach ($notifications as $notification) {
        $vd = array($notification->date_start, $notification->date_end, $notification->date_type);
        $date = vague_date::vague_date_to_string($vd);
        if (empty($notification->comment)) {
            switch ($notification->record_status . (empty($notification->record_substatus) ? '' : $notification->record_substatus)) {
                case 'V':
                    $action = 'accepted';
                    break;
                case 'V1':
                    $action = 'accepted as correct';
                    break;
                case 'V2':
                    $action = 'accepted as correct';
                    break;
                case 'C3':
                    $action = 'plausible';
                    break;
                case 'D':
                    $action = 'queried';
                    break;
                case 'R':
                    $action = 'not accepted';
                    break;
                case 'R4':
                    $action = 'not accepted as unable to verify';
                    break;
                case 'R5':
                    $action = 'not accepted as incorrect';
                    break;
                default:
                    $action = 'amended';
            }
            $comment = "The record of {$notification->taxon} at {$notification->public_entered_sref} on {$date} was {$action}.";
        } else {
            if ($notification->auto_generated === 't' && substr($notification->generated_by, 0, 12) === 'data_cleaner' && $notification->record_owner === 't') {
                $comment = "The following message was attached to your record of {$notification->taxon} at {$notification->public_entered_sref} on {$date} " . "when it was checked using the <a target=\"_blank\" href=\"http://www.nbn.org.uk/Tools-Resources/Recording-Resources/NBN-Record-Cleaner.aspx\" target=\"_blank\">" . "NBN Record Cleaner</a>. This does not mean the record is incorrect or is being disputed; the information below is merely a flag against the record that " . "might provide useful information for recording and verification purposes.";
            } elseif ($notification->verified_on > $last_run_date && $notification->record_status !== 'I' && $notification->record_status !== 'T' && $notification->record_status !== 'C') {
                if ($notification->record_owner === 't') {
                    $comment = "Your record of {$notification->taxon} at {$notification->public_entered_sref} on {$date} was examined by an expert.";
                } else {
                    $comment = "A record of {$notification->taxon} at {$notification->public_entered_sref} on {$date} which you'd previously commented on was examined by an expert.";
                }
            } elseif ($notification->record_owner === 't') {
                $comment = "A comment was added to your record of {$notification->taxon} at {$notification->public_entered_sref} on {$date}.";
            } else {
                $comment = "A reply was added to the record of {$notification->taxon} at {$notification->public_entered_sref} on {$date} which you've previously commented on.";
            }
            $comment .= "<br/><em>{$notification->comment}</em>";
        }
        $theNotificationToInsert = array('source' => 'Verifications and comments', 'source_type' => $notification->source_type, 'data' => json_encode(array('username' => $notification->username, 'occurrence_id' => $notification->id, 'comment' => $comment, 'taxon' => $notification->taxon, 'date' => $date, 'entered_sref' => $notification->public_entered_sref, 'auto_generated' => $notification->auto_generated, 'record_status' => $notification->record_status, 'record_substatus' => $notification->record_substatus, 'updated_on' => $notification->updated_on)), 'linked_id' => $notification->id, 'user_id' => $notification->notify_user_id, 'digest_mode' => 'N', 'source_detail' => $notification->source_detail);
        $db->insert('notifications', $theNotificationToInsert);
    }
    echo count($notifications) . ' notifications generated<br/>';
}
예제 #8
0
 private function addArrayToEmailTable($occurrenceId, $array, &$emailContent)
 {
     $excludedFields = array('date_end', 'date_type', 'Email me a copy of the record', 'CMS Username', 'CMS User ID', 'Email', 'Happy for Contact?');
     foreach ($array[$occurrenceId] as $field => $value) {
         if ($field == 'date_start') {
             $value = vague_date::vague_date_to_string(array($array[$occurrenceId]->date_start, $array[$occurrenceId]->date_end, $array[$occurrenceId]->date_type));
             $field = 'date';
         }
         if (!empty($value) && !in_array($field, $excludedFields)) {
             $emailContent .= "<tr><td>{$field}</td><td>{$value}</td></tr>";
         }
     }
 }