<commit_before><?php
defined('SYSPATH') or die('No direct script access.');
/**
 * Model representing resource
 *
 */
class Resource_Model extends Table_Model
{
    protected $primary_val = 'short_title';
    protected $sorting = array('title' => 'asc');
    public $headers = array('short_title', 'icon', 'url', 'publisher');
    protected $belongs_to =
    array('contact', 'creator' => 'curator', 'curator' => 'curator', 'publisher', 'contract', 'conspectus',
          'conspectus_subcategory', 'crawl_freq', 'resource_status', 'suggested_by');
    protected $has_one = array('nomination');
    protected $has_many = array('seeds', 'ratings', 'correspondence', 'qa_checks');
    // to speed up loading of forms
    public $formo_ignores = array('contact_id', 'publisher_id', 'contract_id');

    public function __construct($id = NULL)
    {
        parent::__construct($id);
        if (is_null($id)) {
            $this->date = date_helper::mysql_date_now();
        }
    }

    public function __set($key, $value)
    {
        if ($key === 'catalogued') {
            if ($this->__isset('catalogued')) {
                if ($value == 0) {
                    $this->catalogued = NULL;
                } else {
                    return;
                }
            }
            $date_format = Kohana::config('wadmin.date_format');
            if ($value === TRUE or $value == 1) {
                $value = date($date_format);
            }
        }
        if ($key == 'date' or $key == 'reevaluate_date') {
            if ($value == '') {
                $value = NULL;
            } else {
                $date = new DateTime($value);
                $value = $date->format(DATE_ATOM);
            }
        }
        if ($key == 'rating_result') {
            if ($value == 'NULL') {
                $value = NULL;
            }
        }
        parent::__set($key, $value);
    }

    public function __get($column)
    {
        if ($column == 'icon') {
            return $this->get_icon();
        }
        if ($column == 'short_title') {
            $value = parent::__get('title');
            $length = Kohana::config('wadmin.title_length');
            return text::limit_chars($value, $length, '');
        }
        $value = parent::__get($column);
        if ($column === 'date' or $column === 'catalogued' or $column === 'reevaluate_date') {
            if (!is_null($value)) {
                return date_helper::short_date($value);
            }
        }
        if ($column == 'rating_result' and $value != NULL) {
            $rating_array = Rating_Model::get_final_array();
            $value = $rating_array [$value];
        }
        return $value;
    }

    public static function get_rated_resources($round = 1, $limit = NULL, $offset = NULL, $pattern = NULL)
    {
        $conditions = array('ratings.round' => $round);
        if (!is_null($pattern)) {
            $conditions ['resources.title'] = $pattern;
        }
        if (!is_null($limit) and  !is_null($offset)) {
            $result = ORM::factory('resource')->join('ratings', 'resources.id = ratings.resource_id')->like($conditions)
                    ->groupby('resources.id')->find_all($limit, $offset);
        } else {
            $result = ORM::factory('resource')->join('ratings', 'resources.id = ratings.resource_id')->like($conditions)
                    ->groupby('resources.id')->find_all();
        }
        return $result;
    }

    /**
     * Get Resources which have new contract from the last crawl or were evaluated as unsatisfactory
     * @param $curator_id
     */
    public static function get_to_checkQA($curator_id = NULL)
    {
        // get the date of the last crawl
        $date = Crawl_Model::get_last_crawl()->date;

        $where = "contract_id IS NOT NULL
                        AND date_signed < '{$date}'
                        AND curator_id = {$curator_id}
                        AND (
                        resources.id NOT
                        IN (
                            SELECT r.id
                            FROM resources r, qa_checks q
                            WHERE r.id = q.resource_id
                            AND q.date_checked > '{$date}'
                            AND q.result != -1
                            )
                        )";
        if (!is_null($curator_id)) {
            $where .= " AND curator_id = {$curator_id}";
        }
        $resources = ORM::factory('resource')->join('contracts', 'contracts.id', 'resources.contract_id')->where($where)
                ->find_all();
        return $resources;
    }

    public static function get_new_nominations($curator_id)
    {
        return ORM::factory('resource')->with('nomination')->where(
            array('resources.curator_id' => $curator_id, 'nomination.resource_id IS NOT' => NULL))->find_all();<commit_msg>FIX - opravena kontrola kvality (contract_id byl v podmince nejednoznacny)<commit_after><?php
defined('SYSPATH') or die('No direct script access.');
/**
 * Model representing resource
 *
 */
class Resource_Model extends Table_Model
{
    protected $primary_val = 'short_title';
    protected $sorting = array('title' => 'asc');
    public $headers = array('short_title', 'icon', 'url', 'publisher');
    protected $belongs_to =
    array('contact', 'creator' => 'curator', 'curator' => 'curator', 'publisher', 'contract', 'conspectus',
          'conspectus_subcategory', 'crawl_freq', 'resource_status', 'suggested_by');
    protected $has_one = array('nomination');
    protected $has_many = array('seeds', 'ratings', 'correspondence', 'qa_checks');
    // to speed up loading of forms
    public $formo_ignores = array('contact_id', 'publisher_id', 'contract_id');

    public function __construct($id = NULL)
    {
        parent::__construct($id);
        if (is_null($id)) {
            $this->date = date_helper::mysql_date_now();
        }
    }

    public function __set($key, $value)
    {
        if ($key === 'catalogued') {
            if ($this->__isset('catalogued')) {
                if ($value == 0) {
                    $this->catalogued = NULL;
                } else {
                    return;
                }
            }
            $date_format = Kohana::config('wadmin.date_format');
            if ($value === TRUE or $value == 1) {
                $value = date($date_format);
            }
        }
        if ($key == 'date' or $key == 'reevaluate_date') {
            if ($value == '') {
                $value = NULL;
            } else {
                $date = new DateTime($value);
                $value = $date->format(DATE_ATOM);
            }
        }
        if ($key == 'rating_result') {
            if ($value == 'NULL') {
                $value = NULL;
            }
        }
        parent::__set($key, $value);
    }

    public function __get($column)
    {
        if ($column == 'icon') {
            return $this->get_icon();
        }
        if ($column == 'short_title') {
            $value = parent::__get('title');
            $length = Kohana::config('wadmin.title_length');
            return text::limit_chars($value, $length, '');
        }
        $value = parent::__get($column);
        if ($column === 'date' or $column === 'catalogued' or $column === 'reevaluate_date') {
            if (!is_null($value)) {
                return date_helper::short_date($value);
            }
        }
        if ($column == 'rating_result' and $value != NULL) {
            $rating_array = Rating_Model::get_final_array();
            $value = $rating_array [$value];
        }
        return $value;
    }

    public static function get_rated_resources($round = 1, $limit = NULL, $offset = NULL, $pattern = NULL)
    {
        $conditions = array('ratings.round' => $round);
        if (!is_null($pattern)) {
            $conditions ['resources.title'] = $pattern;
        }
        if (!is_null($limit) and  !is_null($offset)) {
            $result = ORM::factory('resource')->join('ratings', 'resources.id = ratings.resource_id')->like($conditions)
                    ->groupby('resources.id')->find_all($limit, $offset);
        } else {
            $result = ORM::factory('resource')->join('ratings', 'resources.id = ratings.resource_id')->like($conditions)
                    ->groupby('resources.id')->find_all();
        }
        return $result;
    }

    /**
     * Get Resources which have new contract from the last crawl or were evaluated as unsatisfactory
     * @param $curator_id
     */
    public static function get_to_checkQA($curator_id = NULL)
    {
        // get the date of the last crawl
        $date = Crawl_Model::get_last_crawl()->date;

        $where = "resources.contract_id IS NOT NULL
                        AND date_signed < '{$date}'
                        AND curator_id = {$curator_id}
                        AND (
                        resources.id NOT
                        IN (
                            SELECT r.id
                            FROM resources r, qa_checks q
                            WHERE r.id = q.resource_id
                            AND q.date_checked > '{$date}'
                            AND q.result != -1
                            )
                        )";
        if (!is_null($curator_id)) {
            $where .= " AND curator_id = {$curator_id}";
        }
        $resources = ORM::factory('resource')->join('contracts', 'contracts.id', 'resources.contract_id')->where($where)
                ->find_all();
        return $resources;
    }

    public static function get_new_nominations($curator_id)
    {
        return ORM::factory('resource')->with('nomination')->where(
            array('resources.curator_id' => $curator_id, 'nomination.resource_id IS NOT' => NULL))->find_all();
{'fraction_non_alphanumeric': 0.14790669247224203, 'fraction_numerical': 0.0008149129061831516, 'mean_word_length': 1.8131805157593124, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3721659', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2716, 'n_words': 591}