Skip to content
Snippets Groups Projects
Commit ba9ab143 authored by David Siegfried's avatar David Siegfried
Browse files

count users correctly and rework code, closes #913

Closes #913

Merge request studip/studip!770
parent 0b867f0e
No related branches found
No related tags found
No related merge requests found
......@@ -12,8 +12,8 @@ class Studiengaenge_InformationenController extends MVVController
{
parent::before_filter($action, $args);
if (!($GLOBALS['perm']->have_perm('root') || RolePersistence::isAssignedRole(
$GLOBALS['user']->id, 'MVVAdmin'))) {
if (!($GLOBALS['perm']->have_perm('root')
|| User::findCurrent()->hasRole('MVVAdmin'))) {
throw new AccessDeniedException();
}
PageLayout::setTitle(_('Verwaltung der Studiengänge'));
......@@ -49,21 +49,16 @@ class Studiengaenge_InformationenController extends MVVController
{
$this->studycourse = Fach::find($studycourse_id);
$this->nr = $nr;
$this->degree = Degree::findBySQL('abschluss_id IN (SELECT DISTINCT(abschluss_id) FROM user_studiengang '
. 'WHERE fach_id = :studycourse_id AND abschluss_id IN (:abschluss_ids)) '
. 'ORDER BY name',
['studycourse_id' => $studycourse_id,
'abschluss_ids' => $this->studycourse->degrees->pluck('abschluss_id')]);
}
public function showstudycourse_action($degree_id, $nr = 0)
{
$this->nr = $nr;
$this->degree = Degree::find($degree_id);
if ($GLOBALS['perm']->have_perm("root",$GLOBALS['user']->id)) {
$this->studycourses = StudyCourse::findBySQL('fach_id IN (SELECT DISTINCT(fach_id) FROM user_studiengang '
. 'WHERE abschluss_id = :abschluss_id AND fach_id IN (:studycourse_ids)) ORDER BY name',
[':abschluss_id' => $degree_id, ':studycourse_ids' => $this->degree->professions->pluck('fach_id')]);
$this->degree = Abschluss::find($degree_id);
$this->professions = $this->degree->professions;
if ($GLOBALS['perm']->have_perm('root',$GLOBALS['user']->id)) {
$this->studycourses = $this->degree->professions;
} else {
$inst_ids = SimpleCollection::createFromArray(Institute::findBySQL('Institut_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)
OR fakultaets_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)',
......@@ -78,37 +73,32 @@ class Studiengaenge_InformationenController extends MVVController
public function messagehelper_action()
{
$fach_id = Request::get('fach_id');
$degree_id = Request::get('abschluss_id');
$fach = new Fach($fach_id);
$degree = new Degree($degree_id);
$fach = Fach::find(Request::get('fach_id'));
$degree = Degree::find(Request::get('abschluss_id'));
$user = array();
if (is_null($degree_id) && !is_null($fach_id)) {
$user = UserStudyCourse::findBySql('fach_id = :fach_id',
[':fach_id' => $fach->fach_id]);
} else if (!is_null($degree_id) && !is_null($fach_id)) {
$user = UserStudyCourse::findBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $fach_id, ':abschluss_id' => $degree_id]);
} else if (!is_null($degree_id) && is_null($fach_id)) {
$user = UserStudyCourse::findBySql('abschluss_id = :abschluss_id',
[':abschluss_id' => $degree_id]);
if (!$degree && $fach) {
$users = UserStudyCourse::findBySql('fach_id = :fach_id', [':fach_id' => $fach->id]);
} else if ($degree && !$fach) {
$users = UserStudyCourse::findBySql('abschluss_id = :abschluss_id', [':abschluss_id' => $degree->id]);
} else {
$users = UserStudyCourse::findBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $fach->id, ':abschluss_id' => $degree->id]
);
}
if (empty($user)) {
if (empty($users)) {
PageLayout::postError(_('Keine Studierenden zu den gewählten Angaben gefunden'));
$this->redirect('index');
$this->redirect($this->indexURL());
return;
}
foreach ($user as $u) {
$send_to[] = $u->user->username;
}
$_SESSION['sms_data']['p_rec'] = $send_to;
$_SESSION['sms_data']['p_rec'] = SimpleCollection::createFromArray(
SimpleCollection::createFromArray($users)->pluck('user')
)->pluck('username');
$subject = sprintf(_('Information zum Studiengang: %s %s'),
!$fach->isNew() ? $fach->name: '' , !$degree->isNew() ? $degree->name : '');
$subject = sprintf(
_('Information zum Studiengang: %s %s'),
$fach ? $fach->name: '' , $degree ? $degree->name : ''
);
$this->redirect(URLHelper::getURL('dispatch.php/messages/write',
['default_subject' => $subject, 'emailrequest' => 1]
......@@ -118,29 +108,10 @@ class Studiengaenge_InformationenController extends MVVController
private function createSidebar($view = 'subject' )
{
$widget = new ViewsWidget();
$widget->addLink(_('Gruppieren nach Fächern'), $this->url_for('/index'))
->setActive($view == 'subject');
$widget->addLink(_('Gruppieren nach Abschlüssen'), $this->url_for('/degree'))
->setActive($view == 'degrees');
$widget->addLink(_('Gruppieren nach Fächern'), $this->indexURL())
->setActive($view === 'subject');
$widget->addLink(_('Gruppieren nach Abschlüssen'), $this->degreeURL())
->setActive($view === 'degrees');
Sidebar::Get()->addWidget($widget);
}
public static function getStudyCount($degree_id)
{
if ($GLOBALS['perm']->have_perm('root', $GLOBALS['user']->id)) {
return UserStudyCourse::countBySql('abschluss_id = :abschluss_id',
[':abschluss_id' => $degree_id]);
} else {
$inst_ids = SimpleCollection::createFromArray(Institute::findBySQL('Institut_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)
OR fakultaets_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)',
[':user_id' => $GLOBALS['user']->user_id]))->pluck('institut_id');
return UserStudyCourse::countBySql('JOIN mvv_fach_inst as fach_inst ON (user_studiengang.fach_id = fach_inst.fach_id)
WHERE user_studiengang.abschluss_id = :abschluss_id AND fach_inst.institut_id IN (:inst_ids)',
[':abschluss_id' => $degree_id, ':inst_ids' => $inst_ids]);
}
}
}
\ No newline at end of file
......@@ -12,34 +12,32 @@
</tr>
</thead>
<? foreach ($degree as $key => $deg) : ?>
<? if (($studycount = Studiengaenge_InformationenController::getStudyCount($deg->abschluss_id)) > 0 ) : ?>
<tbody class="collapsed">
<tr class="table-header header-row">
<td class="toggle-indicator">
<a id="<?= $deg->abschluss_id?>" class="mvv-load-in-new-row"
href="<?= $controller->url_for('/showstudycourse', $deg->abschluss_id, $key+1)?>"
onclick="icon_toggle(this)">
<?= htmlReady($deg->name) ?>
</a>
</td>
<td>
<?= $studycount ?>
</td>
<td class="dont-hide actions">
<? if ($GLOBALS['perm']->have_perm("root", $GLOBALS['user']->id)) : ?>
<a href="<?= $controller->url_for('/messagehelper',
['abschluss_id' => $deg->abschluss_id]) ?>" data-dialog >
<?= Icon::create('mail', Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(_('Alle Studierenden mit dem Studienabschluss %s benachrichtigen.'),
$deg->name))]) ?>
<? if ($deg->count_user > 0) : ?>
<tbody class="collapsed">
<tr class="table-header header-row">
<td class="toggle-indicator">
<a id="<?= $deg->abschluss_id ?>" class="mvv-load-in-new-row"
href="<?= $controller->url_for('/showstudycourse', $deg->abschluss_id, $key + 1) ?>">
<?= htmlReady($deg->name) ?>
</a>
<? endif; ?>
</td>
</tr>
</tbody>
<? endif; ?>
<? endforeach; ?>
</td>
<td>
<?= $deg->count_user ?>
</td>
<td class="dont-hide actions">
<? if ($GLOBALS['perm']->have_perm('root', $GLOBALS['user']->id)) : ?>
<a href="<?= $controller->messagehelper(['abschluss_id' => $deg->abschluss_id]) ?>"
data-dialog>
<?= Icon::create('mail')->asImg(
['title' => htmlReady(sprintf(
_('Alle Studierenden mit dem Studienabschluss %s benachrichtigen.'),
$deg->name))
]) ?>
</a>
<? endif ?>
</td>
</tr>
</tbody>
<? endif ?>
<? endforeach ?>
</table>
<table class="default nohover collapsable">
<colgroup>
<col width="70%">
<col width="29%">
<col width="1%">
<col style="width: 70%">
<col style="width: 29%">
<col style="width: 1%">
</colgroup>
<thead>
<tr>
......@@ -14,26 +14,28 @@
<? foreach ($studycourses as $key => $studycourse) : ?>
<? $count = UserStudyCourse::countBySql('fach_id = ?', [$studycourse->fach_id]); ?>
<? if ($count > 0) : ?>
<tbody class="collapsed">
<tr class="table-header header-row">
<td class="toggle-indicator">
<a id="<?= $studycourse->fach_id?>" class="mvv-load-in-new-row"
href="<?= $controller->url_for('/showdegree', $studycourse->fach_id, $key+1)?>">
<?= htmlReady($studycourse->name) ?>
</a>
</td>
<td>
<?= $count ?>
</td>
<td class="dont-hide actions">
<a href="<?= $controller->url_for('/messagehelper', ['fach_id' => $studycourse->fach_id]) ?>" data-dialog >
<?= Icon::create('mail', Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(_('Alle Studierenden des Faches %s benachrichtigen.'),
$studycourse->name))]) ?>
</a>
</td>
</tr>
</tbody>
<? endif; ?>
<? endforeach; ?>
<tbody class="collapsed">
<tr class="table-header header-row">
<td class="toggle-indicator">
<a id="<?= $studycourse->fach_id ?>" class="mvv-load-in-new-row"
href="<?= $controller->showdegree($studycourse->fach_id, $key + 1) ?>">
<?= htmlReady($studycourse->name) ?>
</a>
</td>
<td>
<?= $count ?>
</td>
<td class="dont-hide actions">
<a href="<?= $controller->messagehelper(['fach_id' => $studycourse->fach_id]) ?>" data-dialog>
<?= Icon::create('mail')->asImg(
['title' => sprintf(
_('Alle Studierenden des Faches %s benachrichtigen.'),
htmlReady($studycourse->name))
]) ?>
</a>
</td>
</tr>
</tbody>
<? endif ?>
<? endforeach ?>
</table>
<td colspan="3">
<table class="default">
<colgroup>
<col width="70%">
<col width="29%">
<col width="1%">
<col style="width: 70%">
<col style="width: 29%">
<col style="width: 1%">
</colgroup>
<? foreach ($degree as $key => $deg) : ?>
<tbody>
<tr>
<td><?= htmlReady($deg->name) ?></td>
<td>
<?= UserStudyCourse::countBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $studycourse->fach_id, ':abschluss_id' => $deg->abschluss_id])?>
<? foreach ($studycourse->degrees as $key => $deg) : ?>
<tbody>
<tr>
<td><?= htmlReady($deg->name) ?></td>
<td>
<?= UserStudyCourse::countBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $studycourse->fach_id, ':abschluss_id' => $deg->abschluss_id]) ?>
</td>
<td class="actions">
<? $action =ActionMenu::get()
->setContext($deg->name)
->addLink($controller->url_for('/messagehelper',
['fach_id' => $studycourse->fach_id, 'abschluss_id' => $deg->abschluss_id]),
_('Nachricht an Studierende schreiben'),
Icon::create('mail', Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(_('Nachricht an alle Studierende mit dem Studiengang %s mit dem Abschluss %s'),
$studycourse->name, $deg->name))]),
['data-dialog' => '']) ?>
<?= $action ?>
</td>
</tr>
</tbody>
<? endforeach; ?>
<td class="actions">
<?= $action = ActionMenu::get()
->setContext($deg->name)
->addLink($controller->messagehelperURL(
['fach_id' => $studycourse->fach_id, 'abschluss_id' => $deg->abschluss_id]),
_('Nachricht an Studierende schreiben'),
Icon::create(
'mail',
Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(
_('Nachricht an alle Studierende mit dem Studiengang %s mit dem Abschluss %s'),
$studycourse->name, $deg->name))
]),
['data-dialog' => ''])
->render()
?>
</td>
</tr>
</tbody>
<? endforeach ?>
</table>
</td>
<td colspan="3">
<table class="default">
<colgroup>
<col width="70%">
<col width="29%">
<col width="1%">
<col style="width: 70%">
<col style="width: 29%">
<col style="width: 1%">
</colgroup>
<? foreach ($studycourses as $key => $course) : ?>
<? if (($count = UserStudyCourse::countBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $course->fach_id, ':abschluss_id' => $degree->abschluss_id])) > 0) : ?>
<tr>
<td><?= htmlReady($course->name) ?></td>
<td>
<?= $count ?>
</td>
<td class="actions">
<? $action =ActionMenu::get()->setContext($course->name)
->addLink($controller->url_for('/messagehelper',
['fach_id' => $course->fach_id, 'abschluss_id' => $degree->abschluss_id]),
_('Nachricht an Studierende schreiben'),
Icon::create('mail', Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(_('Nachricht an alle Studierende mit dem Studiengang %s mit dem Abschluss %s'),
$course->name, $degree->name))]),
['data-dialog' => '']) ?>
<?= $action ?>
</td>
</tr>
<? endif; ?>
<? endforeach; ?>
<? foreach ($studycourses as $key => $course) : ?>
<? if (($count = UserStudyCourse::countBySql('fach_id = :fach_id AND abschluss_id = :abschluss_id',
[':fach_id' => $course->fach_id, ':abschluss_id' => $degree->abschluss_id])) > 0) : ?>
<tr>
<td><?= htmlReady($course->name) ?></td>
<td>
<?= $count ?>
</td>
<td class="actions">
<?= $action = ActionMenu::get()->setContext($course->name)
->addLink($controller->messagehelperURL(
['fach_id' => $course->fach_id, 'abschluss_id' => $degree->abschluss_id]),
_('Nachricht an Studierende schreiben'),
Icon::create(
'mail',
Icon::ROLE_CLICKABLE,
['title' => htmlReady(sprintf(
_('Nachricht an alle Studierende mit dem Studiengang %s mit dem Abschluss %s'),
$course->name, $degree->name))
]),
['data-dialog' => ''])->render()
?>
</td>
</tr>
<? endif ?>
<? endforeach ?>
</table>
</td>
......@@ -35,16 +35,27 @@ class Degree extends SimpleORMap
];
$config['additional_fields']['count_user']['get'] = 'countUser';
$config['registered_callbacks']['before_store'][] = "cbUpdateAuthorId";
parent::configure($config);
}
public function countUser()
{
$stmt = DBManager::get()->prepare('SELECT COUNT(DISTINCT user_id) '
. 'FROM user_studiengang WHERE abschluss_id = ?');
$stmt->execute([$this->id]);
return $stmt->fetchColumn();
$sql = 'SELECT COUNT(DISTINCT `user_id`) FROM `user_studiengang`';
$parameters = [':degree_id' => $this->id];
if (!$GLOBALS['perm']->have_perm('root')) {
$inst_ids = SimpleCollection::createFromArray(Institute::findBySQL('Institut_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)
OR fakultaets_id IN (SELECT institut_id FROM roles_user WHERE userid = :user_id)',
[':user_id' => $GLOBALS['user']->user_id]))->pluck('institut_id');
$sql .= 'JOIN `mvv_fach_inst` as `fach_inst` ON (`user_studiengang`.`fach_id` = `fach_inst`.`fach_id`)
WHERE `user_studiengang`.`abschluss_id` = :degree_id AND `fach_inst`.`institut_id` IN (:inst_ids)';
$parameters[':inst_ids'] = $inst_ids;
} else {
$sql .= ' WHERE `user_studiengang`.`abschluss_id` = :degree_id';
}
return DBManager::get()->fetchColumn($sql, $parameters);
}
public function countUserByStudycourse($studycourse_id)
......@@ -56,7 +67,7 @@ class Degree extends SimpleORMap
return $stmt->fetchColumn();
}
public function store()
public function cbUpdateAuthorId()
{
if ($this->isNew() || $this->isDirty()) {
$this->editor_id = $GLOBALS['user']->id;
......@@ -64,8 +75,5 @@ class Degree extends SimpleORMap
$this->author_id = $GLOBALS['user']->id;
}
}
return parent::store();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment