diff --git a/app/controllers/course/members.php b/app/controllers/course/members.php index 360e32ba8b34abfbc96ac11465b508524a3efa64..8c841ab5223d96df2c76a6a2a41c0b1b99a6fd57 100644 --- a/app/controllers/course/members.php +++ b/app/controllers/course/members.php @@ -1423,15 +1423,13 @@ class Course_MembersController extends AuthenticatedController if ($this->is_tutor || $this->config->COURSE_STUDENT_MAILING) { $widget->addLink( - _('Nachricht an alle eingetragenen Teilnehmenden (Rundmail)'), - URLHelper::getURL('dispatch.php/messages/write', [ - 'course_id' => $this->course_id, - 'default_subject' => $this->subject, - 'filter' => 'all', - 'emailrequest' => 1 + _('Rundmail schreiben'), + URLHelper::getURL('dispatch.php/course/members/circular_mail', [ + 'course_id' => $this->course_id, + 'default_subject' => $this->subject ]), Icon::create('inbox') - )->asDialog(); + )->asDialog('size=auto'); } if ($this->is_tutor) { //Calculate the course institutes here since they are needed @@ -1820,4 +1818,132 @@ class Course_MembersController extends AuthenticatedController $this->redirect('course/members'); } + + + public function circular_mail_action() + { + if (!$this->is_tutor || + ($this->config->COURSE_STUDENT_MAILING && !$this->is_autor)) { + throw new AccessDeniedException(); + } + + //Calculate the amount of recipients for each group: + $this->user_count = CourseMember::countByCourseAndStatus($this->course_id, 'user'); + $this->autor_count = CourseMember::countByCourseAndStatus($this->course_id, 'autor'); + $this->tutor_count = CourseMember::countByCourseAndStatus($this->course_id, 'tutor'); + $this->dozent_count = CourseMember::countByCourseAndStatus($this->course_id, 'dozent'); + + //Use the correct names for thte four status groups: + $sem = Seminar::GetInstance($this->course_id); + $this->user_name = get_title_for_status('user', 0, $sem->status); + $this->autor_name = get_title_for_status('autor', 0, $sem->status); + $this->tutor_name = get_title_for_status('tutor', 0, $sem->status); + $this->dozent_name = get_title_for_status('dozent', 0, $sem->status); + + if ($this->is_tutor) { + $this->awaiting_count = AdmissionApplication::countBySql( + "seminar_id = :course_id AND status = 'awaiting'", + [ + 'course_id' => $this->course_id + ] + ); + $this->accepted_count = AdmissionApplication::countBySql( + "seminar_id = :course_id AND status = 'accepted'", + [ + 'course_id' => $this->course_id + ] + ); + } + $this->default_selected_groups = ['dozent', 'tutor', 'autor', 'user']; + $this->all_available_groups = $this->default_selected_groups; + if ($this->is_tutor) { + //The user has at least tutor permissions: + if ($this->accepted_count) { + $this->all_available_groups[] = 'accepted'; + } + if ($this->awaiting_count) { + $this->all_available_groups[] = 'awaiting'; + } + } + if (Request::submitted('write')) { + CSRFProtection::verifyUnsafeRequest(); + + $this->selected_groups = Request::getArray('selected_groups'); + //Filter all selected groups by the list of all available groups: + $filtered_groups = []; + foreach ($this->selected_groups as $group) { + if (in_array($group, $this->all_available_groups)) { + $filtered_groups[] = $group; + } + } + if ($filtered_groups == $this->default_selected_groups) { + $this->redirect(URLHelper::getURL( + 'dispatch.php/messages/write', + [ + 'course_id' => $this->course_id, + 'default_subject' => $this->subject, + 'filter' => 'all', + 'emailrequest' => 1 + ] + )); + } elseif ($filtered_groups == $this->all_available_groups) { + $this->redirect(URLHelper::getURL( + 'dispatch.php/messages/write', + [ + 'course_id' => $this->course_id, + 'default_subject' => $this->subject, + 'filter' => 'really_all', + 'emailrequest' => 1 + ] + )); + } else { + //Do custom filtering. + $filters = []; + $who_param = []; + + foreach ($filtered_groups as $group) { + if ($group === 'awaiting') { + $filters[] = 'awaiting'; + } elseif ($group === 'accepted') { + $filters[] = 'prelim'; + } elseif ($group === 'user') { + $filters[] = 'all'; + $who_param[] = 'user'; + } elseif ($group === 'autor') { + $filters[] = 'all'; + $who_param[] = 'autor'; + } elseif ($group === 'tutor') { + $filters[] = 'all'; + $who_param[] = 'tutor'; + } elseif ($group === 'dozent') { + $filters[] = 'all'; + $who_param[] = 'dozent'; + } + } + $filters = array_unique($filters); + if (!$filters) { + PageLayout::postError( + _('Es wurde keine Gruppe ausgewählt!') + ); + return; + } + + $url_params = [ + 'course_id' => $this->course_id, + 'default_subject' => $this->subject, + 'filter' => implode(',', array_unique($filters)), + 'emailrequest' => 1 + ]; + if ($who_param) { + $url_params['who'] = implode(',', $who_param); + } + //print_r($url_params);die(); + + $this->redirect(URLHelper::getURL( + 'dispatch.php/messages/write', + $url_params + )); + } + } + } } diff --git a/app/controllers/messages.php b/app/controllers/messages.php index 5066882a818f5770283d53984ec7251e8d674934..9bac911a81052f42c7826ee1c72bdde751ebadfa 100644 --- a/app/controllers/messages.php +++ b/app/controllers/messages.php @@ -287,57 +287,83 @@ class MessagesController extends AuthenticatedController { } //check if the message shall be sent to all (or some) members of a course: - $filter = Request::get('filter'); - $course = Course::find(Request::option('course_id')); - if ($filter && $course) { - if ($GLOBALS['perm']->have_studip_perm('tutor', $course->id) - || ($GLOBALS['perm']->have_studip_perm('autor', $course->id) - && ($course->getSemClass()['studygroup_mode'] || CourseConfig::get($course->id)->COURSE_STUDENT_MAILING))) { - $this->default_message->receivers = []; + $filters = explode(',', Request::get('filter', '')); + if ($filters && Request::option('course_id')) { + $additional = ''; + $course = new Course(Request::option('course_id')); + $allow_tutor_filters = false; + if ($GLOBALS['perm']->have_studip_perm('tutor', $course->id) || $course->getSemClass()['studygroup_mode'] || CourseConfig::get($course->id)->COURSE_STUDENT_MAILING) { + $allow_tutor_filters = true; + $additional = " AND seminar_user.visible != 'no'"; + } + $this->default_message->receivers = []; + $all_recipients = []; + foreach ($filters as $filter) { $query = ''; - $params = [$course->id, Request::option('who')]; - if ($GLOBALS['perm']->have_studip_perm('tutor', $course->id)) { - if ($filter === 'send_sms_to_all') { - $query = "SELECT user_id, 'rec' AS snd_rec + $params = ['course_id' => $course->id]; + if (Request::get('who')) { + $params['status'] = explode(',', Request::get('who', '')); + } + + if ($filter === 'send_sms_to_all' && $allow_tutor_filters) { + $query = "SELECT user_id, 'rec' AS snd_rec FROM seminar_user JOIN auth_user_md5 USING (user_id) - WHERE Seminar_id = ? AND status = ? + WHERE Seminar_id = :course_id AND status IN ( :status ) {$additional} ORDER BY Nachname, Vorname"; - } elseif ($filter === 'all') { - $query = "SELECT user_id, 'rec' AS snd_rec + } elseif ($filter === 'all') { + if ($params['status']) { + $additional .= ' AND seminar_user.status IN ( :status )'; + } + $query = "SELECT user_id, 'rec' AS snd_rec FROM seminar_user JOIN auth_user_md5 USING (user_id) - WHERE Seminar_id = ? + WHERE Seminar_id = :course_id {$additional} ORDER BY Nachname, Vorname"; - } elseif ($filter === 'prelim') { - $query = "SELECT user_id, 'rec' AS snd_rec + } elseif ($filter === 'really_all' && $allow_tutor_filters) { + $query = "SELECT user_id, 'rec' as snd_rec + FROM seminar_user + WHERE seminar_id = :course_id + UNION SELECT user_id, 'rec' as snd_rec FROM admission_seminar_user WHERE seminar_id = :course_id + UNION SELECT user_id, 'rec' as snd_rec FROM priorities WHERE seminar_id = :course_id"; + } elseif ($filter === 'prelim' && $allow_tutor_filters) { + $query = "SELECT user_id, 'rec' AS snd_rec FROM admission_seminar_user JOIN auth_user_md5 USING (user_id) - WHERE seminar_id = ? AND status = 'accepted' + WHERE Seminar_id = :course_id AND status = 'accepted' ORDER BY Nachname, Vorname"; - } elseif ($filter === 'awaiting') { - $query = "SELECT user_id, 'rec' AS snd_rec + } elseif ($filter === 'awaiting' && $allow_tutor_filters) { + $query = "SELECT user_id, 'rec' AS snd_rec FROM admission_seminar_user JOIN auth_user_md5 USING (user_id) - WHERE seminar_id = ? AND status = 'awaiting' + WHERE Seminar_id = :course_id AND status = 'awaiting' + ORDER BY Nachname, Vorname"; + } elseif ($filter === 'inst_status') { + $query = "SELECT user_id, 'rec' AS snd_rec + FROM user_inst + JOIN auth_user_md5 USING (user_id) + WHERE Institut_id = :course_id AND inst_perms IN ( :status ) + {$additional} ORDER BY Nachname, Vorname"; - } elseif ($filter === 'not_grouped') { - $query = "SELECT seminar_user.user_id, 'rec' as snd_rec + } elseif ($filter === 'not_grouped' && $allow_tutor_filters) { + $query = "SELECT seminar_user.user_id, 'rec' as snd_rec FROM seminar_user JOIN auth_user_md5 USING (user_id) LEFT JOIN statusgruppen ON range_id = seminar_id LEFT JOIN statusgruppe_user ON statusgruppen.statusgruppe_id = statusgruppe_user.statusgruppe_id AND seminar_user.user_id = statusgruppe_user.user_id - WHERE seminar_id = ? + WHERE seminar_id = :course_id GROUP BY seminar_user.user_id HAVING COUNT(statusgruppe_user.statusgruppe_id) = 0 ORDER BY Nachname, Vorname"; - } elseif ($filter === 'claiming') { - $cs = CourseSet::getSetForCourse($course->id); - if (is_object($cs) && !$cs->hasAlgorithmRun()) { - foreach (AdmissionPriority::getPrioritiesByCourse($cs->getId(), $course->id) as $user_id => $p) { - $this->default_message->receivers[] = MessageUser::build(['user_id' => $user_id, 'snd_rec' => 'rec']); - } + } elseif ($filter === 'claiming' && $allow_tutor_filters) { + $cs = CourseSet::getSetForCourse($course->id); + if (is_object($cs) && !$cs->hasAlgorithmRun()) { + foreach (AdmissionPriority::getPrioritiesByCourse($cs->getId(), $course->id) as $user_id => $p) { + $all_recipients = array_merge( + $all_recipients, + MessageUser::build(['user_id' => $user_id, 'snd_rec' => 'rec']) + ); } } } else { @@ -348,11 +374,16 @@ class MessagesController extends AuthenticatedController { ORDER BY Nachname, Vorname"; } if ($query) { - $this->default_message->receivers = DBManager::get()->fetchAll($query, $params, 'MessageUser::build'); + $all_recipients = array_merge( + $all_recipients, + DBManager::get()->fetchAll($query, $params, 'MessageUser::build') + ); } } + $this->default_message->receivers = $all_recipients; } + if (Request::option('prof_id') && Request::option('deg_id') && $GLOBALS['perm']->have_perm('root')) { $query = "SELECT DISTINCT user_id,'rec' as snd_rec FROM user_studiengang diff --git a/app/views/course/members/circular_mail.php b/app/views/course/members/circular_mail.php new file mode 100644 index 0000000000000000000000000000000000000000..1ebdb980118aba35681bd1eee10e45b2b14c2ed5 --- /dev/null +++ b/app/views/course/members/circular_mail.php @@ -0,0 +1,101 @@ +<form class="default" method="post" action="<?= $controller->link_for('course/members/circular_mail') ?>" + data-dialog> + <?= CSRFProtection::tokenTag() ?> + <fieldset> + <legend><?= _('An wen möchten Sie eine Rundmail schreiben?') ?></legend> + <p aria-hidden="true"><?= _('An wen möchten Sie eine Rundmail schreiben?') ?></p> + <? if (in_array('dozent', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="dozent" + <? if (!$dozent_count): echo 'disabled'; ?> + <? elseif (in_array('dozent', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= htmlready($dozent_name) ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $dozent_count), + $dozent_count + ) ?>) + </em> + </label> + <? endif ?> + <? if (in_array('tutor', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="tutor" + <? if (!$tutor_count): echo 'disabled'; ?> + <? elseif (in_array('tutor', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= htmlReady($tutor_name) ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $tutor_count), + $tutor_count + ) ?>) + </em> + </label> + <? endif ?> + <? if (in_array('autor', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="autor" + <? if (!$autor_count): echo 'disabled'; ?> + <? elseif (in_array('autor', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= htmlReady($autor_name) ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $autor_count), + $autor_count + ) ?>) + </em> + </label> + <? endif ?> + <? if (in_array('user', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="user" + <? if (!$user_count): echo 'disabled'; ?> + <? elseif (in_array('user', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= htmlReady($user_name) ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $user_count), + $user_count + ) ?>) + </em> + </label> + <? endif ?> + <? if (in_array('accepted', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="accepted" + <? if (!$accepted_count): echo 'disabled'; ?> + <? elseif (in_array('accepted', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= _('Alle vorläufig akzeptierten Teilnehmende der Veranstaltung') ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $accepted_count), + $accepted_count + ) ?>) + </em> + </label> + <? endif ?> + <? if (in_array('awaiting', $all_available_groups)) : ?> + <label> + <input type="checkbox" name="selected_groups[]" value="awaiting" + <? if (!$awaiting_count): echo 'disabled'; ?> + <? elseif (in_array('awaiting', $default_selected_groups)): echo 'checked'; ?> + <? endif; ?>> + <?= _('Alle Personen auf der Warteliste der Veranstaltung') ?> + <em> + (<?= sprintf( + ngettext('%u Person', '%u Personen', $awaiting_count), + $awaiting_count + ) ?>) + </em> + </label> + <? endif ?> + </fieldset> + <div data-dialog-button> + <?= \Studip\Button::create(_('Rundmail schreiben'), 'write') ?> + </div> +</form>