1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
/**
* check_admission.php
*
* @author André Noack <noack@data-quest.de>, Suchi & Berg GmbH <info@data-quest.de>
* @access public
* @since 2.4
*/
class CheckAdmissionJob extends CronJob
{
public static function getName()
{
return _('Losverfahren überprüfen');
}
public static function getDescription()
{
return _('Überprüft, ob Losverfahren anstehen und führt diese aus');
}
public static function getParameters()
{
return [
'verbose' => [
'type' => 'boolean',
'default' => false,
'status' => 'optional',
'description' => _('Sollen Ausgaben erzeugt werden (sind später im Log des Cronjobs sichtbar)'),
],
'send_messages' => [
'type' => 'boolean',
'default' => true,
'status' => 'optional',
'description' => _('Sollen interne Nachrichten an alle betroffenen Nutzer gesendet werden)'),
],
'send_applications_to_owner' => [
'type' => 'boolean',
'default' => false,
'status' => 'optional',
'description' => _('Die Liste mit Anmeldungen an die Person senden, der das Anmeldeset gehört.')
]
];
}
public function setUp()
{
require_once 'lib/classes/admission/CourseSet.php';
if (empty($GLOBALS['ABSOLUTE_URI_STUDIP'])) {
throw new Exception('To use check_admission job you MUST set correct values for $ABSOLUTE_URI_STUDIP in config_local.inc.php!');
}
}
public function execute($last_result, $parameters = [])
{
$verbose = $parameters['verbose'];
$query = "SELECT DISTINCT cr.set_id
FROM courseset_rule AS cr
INNER JOIN coursesets USING(set_id)
WHERE type = 'ParticipantRestrictedAdmission'
AND algorithm_run = 0";
$sets = DBManager::get()->fetchFirst($query);
if (count($sets) > 0) {
if ($verbose) {
echo date('r') . ' - Starting seat distribution ' . chr(10);
$oldLogger = Log::getInstance();
$logdir = $GLOBALS['TMP_PATH'] . '/seat_distribution_logs';
@mkdir($logdir);
$logfile = $logdir . '/' . date('Y-m-d-H-i') . '_seat_distribution.log';
if (is_dir($logdir)) {
Log::setInstance(
new Logger('seat-distributions', [new StreamHandler($logfile, Logger::DEBUG)])
);
echo 'logging to ' . $logfile . chr(10);
} else {
echo 'could not create directory ' . $logdir . chr(10);
}
}
$i = 0;
foreach ($sets as $set_id) {
$courseset = new CourseSet($set_id);
if ($courseset->isSeatDistributionEnabled() && !$courseset->hasAlgorithmRun() && $courseset->getSeatDistributionTime() < time()) {
if ($verbose) {
echo ++$i . ' ' . $courseset->getId() . ' : ' . $courseset->getName() . chr(10);
$applicants = AdmissionPriority::getPriorities($set_id);
$courses = SimpleCollection::createFromArray(Course::findMany($courseset->getCourses()))->toGroupedArray('seminar_id', words('name veranstaltungsnummer'));
$captions = [_("Nachname"), _("Vorname"), _("Nutzername"),_('Nutzer-ID'), _('Veranstaltung-ID'), _("Veranstaltung"), _("Nummer"), _("Priorität")];
$data = [];
$users = User::findEachMany(function($user) use ($courses,$applicants,&$data) {
$app_courses = $applicants[$user->id];
asort($app_courses);
foreach ($app_courses as $course_id => $prio) {
$row = [];
$row[] = $user->nachname;
$row[] = $user->vorname;
$row[] = $user->username;
$row[] = $user->id;
$row[] = $course_id;
$row[] = $courses[$course_id]['name'];
$row[] = $courses[$course_id]['veranstaltungsnummer'];
$row[] = $prio;
$data[] = $row;
}
}, array_keys($applicants), 'ORDER BY Nachname,Vorname');
$applicants_file = $GLOBALS['TMP_PATH'] . '/seat_distribution_logs/applicants_' . $set_id . '.csv';
if (array_to_csv($data, $applicants_file, $captions)) {
echo 'applicants written to ' . $applicants_file . chr(10);
if ($parameters['send_applications_to_owner']) {
//Send a mail to the owner of the course set:
$owner = User::find($courseset->getUserId());
if ($owner) {
setTempLanguage($owner->id);
$mail = new StudipMail();
$mail->addRecipient($owner->email)
->setSubject(
sprintf(_('Das Stud.IP Anmeldeset %s wird gelost'), $courseset->getName()))
->setBodyText(sprintf(
_('Ihr Anmeldeset %s wird jetzt gelost. Im Anhang finden Sie die Liste der Anmeldungen.'),
$courseset->getName()
))
->addFileAttachment($applicants_file)
->send();
restoreLanguage();
}
}
}
}
$courseset->distributeSeats();
}
}
if ($verbose) {
Log::setInstance($oldLogger);
}
} else {
if ($verbose) {
echo date('r') . ' - Nothing to do' . chr(10);
}
}
}
}
|