cron-mephi.php 2.6 KB
<?php

require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/AbstractApi.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/SimpleXMLElement.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/IssueStatus.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/User.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Issue.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Attachment.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Client.php";

require_once "config.php";

function tempdir_cleanup($dir) {
	system('rm -rf "'.$dir.'"');
}

function tempdir($dir=FALSE, $prefix='php') {
	$tempfile = tempnam(sys_get_temp_dir(),'');

	if (file_exists($tempfile)) 
		unlink($tempfile);

	mkdir($tempfile);
	if (is_dir($tempfile)) {
//		register_shutdown_function("tempdir_cleanup", $tempfile);
		return $tempfile;
	}

	return FALSE;
}


$redmine = new Redmine\Client('https://redmine.ut.mephi.ru', REDMINE_API);

$configdb = new SQLite3("/var/www/rs.ut.mephi.ru/db/db");

/* NRNU Auto-report */

$answer = $redmine->api('issue')->all(array(
		'project_id'		=> '96', 	// project "news"
		'status_id'		=> 'open',	// only opened task
	));

if (!isset($answer['issues']))
	exit(0);

$time = time();

foreach ($answer['issues'] as &$issue) {
	if ($issue['status']['id'] == STATUSID_SOLVED) {
		$redmine->api('issue')->addWatcher ($issue['id'], USERID_NVMALYH);
		$redmine->api('issue')->addWatcher ($issue['id'], USERID_AATITOV);
		$redmine->api('issue')->update     ($issue['id'], array('status_id' => STATUSID_CLOSED));
		continue;
	}

	if (($issue['status']['id'] != STATUSID_NOTCONFIRMED) && ($issue['status']['id'] != STATUSID_NEW))
		continue;

	$timediff_c =  $time-strtotime($issue['created_on']);
	$timediff_u =  $time-strtotime($issue['updated_on']);

	$changes = array();

	$stages = $configdb->query('SELECT * FROM newsstages ORDER BY timeout');

	$assigned_to_new = $issue['assigned_to']['id'];

	$isInInterval = false;
	while ($row = $stages->fetchArray()) {
		if ($timediff_c < $row['timeout']) {
			if ($row['assignee'] > 0) {
				$assigned_to_new = $row['assignee'];
			}
			if ($row['watcher']  > 0) {
				$redmine->api('issue')->addWatcher ($issue['id'], $row['watcher']);
			}
			$isInInterval = true;
			break;
		}
	}
	if (!$isInInterval) {
		if ($timediff_u > 3600)
			$changes['notes'] = ((int)($timediff_c / 3600))." hours elapsed";
	}

	if (!isset($issue['assigned_to']) || ($issue['assigned_to']['id'] != $assigned_to_new))
		$changes['assigned_to_id'] = $assigned_to_new;

	if (count($changes) > 0)
		$redmine->api('issue')->update($issue['id'], $changes);
}

?>