Select Git revision
my_tandems.php
-
Moritz Strohm authoredMoritz Strohm authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
my_tandems.php 10.75 KiB
<?php
/**
* This file is part of the TandemPlugin for Stud.IP
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Plugin
**/
require_once(__DIR__ . '/../models/TandemProfile.class.php');
require_once(__DIR__ . '/../models/TandemPair.class.php');
require_once(__DIR__ . '/../models/TandemLanguage.class.php');
require_once(__DIR__ . '/../models/TandemUserMotherLanguage.class.php');
class MyTandemsController extends PluginController
{
protected function buildSidebar() : void
{
$sidebar = Sidebar::get();
$actions = new ActionsWidget();
$actions->addLink(
dgettext('TandemPlugin', 'Meine beherrschten Sprachen'),
PluginEngine::getURL(
$this->plugin,
[],
'my_tandems/manage_mother_languages'
),
Icon::create('vcard', 'clickable'),
['data-dialog' => '1']
);
$actions->addLink(
dgettext('TandemPlugin', 'Gesuch hinzufügen'),
PluginEngine::getURL(
$this->plugin,
[],
'profile/add'
),
Icon::create('add', 'clickable'),
['data-dialog' => '1']
);
$sidebar->addWidget($actions);
}
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if(Navigation::hasItem('/profile/my_tandems')) {
Navigation::activateItem('/profile/my_tandems');
}
$this->user = User::findCurrent();
if (!$this->plugin->userHasAccess($this->user->id)) {
throw new AccessDeniedException(
dgettext('TandemPlugin', 'Sie befinden sich auf der Blockliste und dürfen daher das TandemPlugin nicht nutzen!')
);
}
$this->buildSidebar();
}
public function index_action()
{
if(Navigation::hasItem('/profile/my_tandems/index')) {
Navigation::activateItem('/profile/my_tandems/index');
}
$this->first_run = false;
$this->mother_languages = TandemUserMotherLanguage::findByUser_id(
$this->user->id
);
$sql = 'user_id = :user_id ';
$sql_params = ['user_id' => $this->user->id];
if(!empty($pair_user_profile_ids)) {
$sql .= 'AND id NOT IN ( :profile_ids )';
$sql_params['profile_ids'] = $pair_user_profile_ids;
}
//get the user's tandem profiles
$this->tandem_requests = TandemProfile::findBySql($sql, $sql_params);
//get the user's established and all requested tandem pairs:
$this->tandem_pairs = TandemPair::findByUserId($this->user->id, [0, 1]);
//get offers matching the user's tandem profiles:
$this->matches = [];
foreach($this->tandem_requests as $request) {
$matches = TandemMatching::findMatches($request);
//Remove duplicates by indexing the matches by their ID:
foreach($matches as $match) {
if (Config::get()->TANDEMPLUGIN_USE_LEVEL) {
if ($match->matchesSpokenLanguages($this->user->id)) {
$this->matches[$match->id] = $match;
}
} else {
$this->matches[$match->id] = $match;
}
}
}
if((count($this->mother_languages) == 0) and count($this->tandem_requests) == 0) {
$this->first_run = true;
}
}
/**
* Retrieves the user's tandem profiles.
*/
public function profiles_action()
{
if(Navigation::hasItem('/profile/my_tandems/profiles')) {
Navigation::activateItem('/profile/my_tandems/profiles');
}
$this->tandem_requests = TandemProfile::findByUser_id($this->user->id);
$this->matches = [];
foreach($this->tandem_requests as $request) {
$this->matches[$request->id] = TandemMatching::countMatches($request);
}
}
/**
* Retrieves all tandem pairs of a user.
*/
public function pairs_action()
{
if(Navigation::hasItem('/profile/my_tandems/pairs')) {
Navigation::activateItem('/profile/my_tandems/pairs');
}
$this->tandem_pairs = TandemPair::findByUserId($this->user->id);
$this->show_message = true;
}
/**
*
*/
public function manage_mother_languages_action()
{
$this->mother_languages = TandemUserMotherLanguage::findSortedByName(
$this->user->id
);
$this->levels = $this->plugin->getLevels();
$this->all_languages = TandemLanguage::findBySql('TRUE');
if (Request::submitted('manage')) {
//check for added, edited and deleted entries:
$ids = Request::getArray('ids');
$language_ids = Request::getArray('language_ids');
$countries = Request::getArray('countries');
$regions = Request::getArray('regions');
$status = Request::getArray('status');
$levels = Request::getArray('level');
$processed_language_ids = []; //array of languages that were processed
$duplicate_language = false;
$profile_exists_for_language = null;
foreach($ids as $key => $id) {
//Check if a tandem profile for the mother language exists.
//In that case we can't continue:
if ($status[$key] != 'T') {
$profile_exists = TandemProfile::countBySql(
'user_id = :user_id AND target_language_id = :language_id',
[
'user_id' => $this->user->id,
'language_id' => $language_ids[$key]
]
);
if($profile_exists) {
$profile_exists_for_language = TandemLanguage::find($language_ids[$key]);
if($profile_exists_for_language instanceof TandemLanguage) {
$profile_exists_for_language = $profile_exists_for_language->getLocalName();
} else {
//In case the language wasn't found show the language-ID
//(its ISO 639-2 3 letter code):
$profile_exists_for_language = $profile_exists;
}
break;
}
}
if(!$id and ($status[$key] != 'T')) {
//No ID is set and the status is not T (template):
//It's a new entry: store it, if it doesn't exist yet!
if(in_array($language_ids[$key], $processed_language_ids)) {
$duplicate_language = true;
break;
} else {
$new_language = new TandemUserMotherLanguage();
$new_language->user_id = $this->user->id;
$new_language->language_id = $language_ids[$key];
$new_language->country = $countries[$key];
$new_language->region = $regions[$key];
if (Config::get()->TANDEMPLUGIN_USE_LEVEL) {
$new_language->level = $levels[$key];
}
$new_language->store();
$processed_language_ids[] = $language_ids[$key];
}
} else {
//existing entry: modify or delete it
$language = TandemUserMotherLanguage::find($id);
if($language) {
//Assure that only the owner may edit or delete the entry:
if($language->user_id == $this->user->id) {
if($status[$key] == 'D') {
//delete the entry:
//Pairs are not deleted since they store the
//language separately.
$language->delete();
} else {
//modify the entry, if the language isn't duplicated:
if(in_array($language_ids[$key], $processed_language_ids)) {
$duplicate_language = true;
break;
} else {
$language->language_id = $language_ids[$key];
$language->country = $countries[$key];
$language->region = $regions[$key];
if (Config::get()->TANDEMPLUGIN_USE_LEVEL) {
$language->level = $levels[$key];
}
$language->store();
$processed_language_ids[] = $language_ids[$key];
}
}
}
}
}
}
$this->error_occured = $duplicate_language | ($profile_exists_for_language != null);
//reload list of mother languages:
$this->mother_languages = TandemUserMotherLanguage::findSortedByName(
$this->user->id
);
if($duplicate_language) {
//A language is more than one time in the list of mother languages:
PageLayout::postError(
dgettext('TandemPlugin', 'Eine Sprache kann nicht mehrfach zur Liste der beherrschten Sprachen hinzugefügt werden!')
);
} elseif($profile_exists_for_language) {
//A tandem profile of the current user exists for the mother language:
PageLayout::postError(
sprintf(
dgettext('TandemPlugin', 'Für die Sprache %s wurde bereits ein Gesuch erstellt, sodass diese nicht als beherrschte Sprache hinzugefügt werden kann!'),
$profile_exists_for_language
)
);
} else {
//Everything went fine:
PageLayout::postSuccess(
dgettext('TandemPlugin', 'Die beherrschten Sprachen wurden aktualisiert!')
);
}
}
}
}