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