Skip to content
Snippets Groups Projects
Select Git revision
  • 46e5f0088e50b5a9fb800e431a80eafb2a81422f
  • master default protected
2 results

TandemManager.class.php

Blame
  • Moritz Strohm's avatar
    Moritz Strohm authored
    TandemPlugin: Implemented check for existing pairs before deleting a profile, fixed bug in TandemManager class
    
    git-svn-id: https://server2.data-quest.de/svn/studip-plugins/TandemPlugin@2421 6a1f69d7-6018-4d13-bf90-b098e98c258d
    46e5f008
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TandemManager.class.php 6.05 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/TandemPair.class.php');
     
    
    class TandemManager
    {
        
        /**
         * Handles applying for a tandem pair.
         * 
         * @param TandemProfile $request The profile of the user who is applying for a tandem.
         * @param TandemProfile $offer The profile where the user applies for building a tandem.
         * @param User $user The user who applies for a tandem.
         */
        public static function applyForPair(TandemProfile $request, TandemProfile $offer)
        {
            $pair = new TandemPair();
            $pair->request_profile_id = $request->id;
            $pair->offer_profile_id = $offer->id;
            $pair->status = 0;
            
            if($pair->store()) {
                //Send system message to offerer:
                
                $message_text = sprintf(
                    '%s bewirbt sich für ein Tandem in der Sprache %s!',
                    $request->user->getFullName(),
                    $request->getTargetName()
                );
                
                $m = new messaging();
                
                //send mail (with e-mail)
                $m->insert_message(
                    $message_text,
                    $offer->user['username'],
                    '____%system%____',
                    false,
                    false,
                    '1',
                    false,
                    'Bewerbung zur Tandem-Bildung!',
                    '1'
                );
                
                //All went fine:
                return true;
            }
            
            //if something went wrong:
            return false;
        }
        
        
        /**
         * This method handles accepting an application for a tandem.
         */
        public static function acceptApplication(TandemPair $pair)
        {
            //set the tandem pair's status to 1 (accepted):
            $pair->status = 1;
            
            //get all applications for the tandem offer that is referenced
            //in the pair:
            $other_offer_applications = TandemPair::findBySql(
                'offer_profile_id = :offer_profile_id AND request_profile_id <> :request_profile_id',
                [
                    'offer_profile_id' => $pair->offer_profile_id,
                    'request_profile_id' => $pair->request_profile_id
                ]
            );
            
            
            //store the pair's status:
            $pair->store();
            
            //decline all other applications for the tandem offer:
            foreach($other_offer_applications as $application) {
                $application->status = -1;
                $application->store();
            }
            
            
            //Withdraw (delete) all applications for other tandem offers
            //made with the same request profile:
            TandemPair::deleteBySql(
                'offer_profile_id <> :offer_profile_id AND request_profile_id = :request_profile_id',
                [
                    'offer_profile_id' => $pair->offer_profile_id,
                    'request_profile_id' => $pair->request_profile_id
                ]
            );
            
            $message_text = sprintf(
                'Ihre Bewerbung für ein Tandem mit %s für die Sprache %s wurde angenommen!',
                $pair->offer->user->getFullName(),
                $pair->request->getTargetName()
            );
            
            $m = new messaging();
                
            //send mail (with e-mail)
            $m->insert_message(
                $message_text,
                $pair->request->user['username'],
                '____%system%____',
                false,
                false,
                '1',
                false,
                'Bewerbung für Tandem wurde angenommen!',
                '1'
            );
                
            return true;
        }
        
        
        /**
         * This method handles termination of a tandem pair.
         * 
         * @return True on success, false on failure.
         */
        public static function terminatePair(TandemPair $pair, User $user, $delete_users_profile = false)
        {
            //Additionally to terminating (deleting) the tandem pair
            //we may also have to delete the tandem profile of the current user
            //that is bound to the pair.
            
            if($delete_users_profile) {
                $user_profile = null;
                if($pair->request->user_id == $user->id) {
                    $user_profile = $pair->request;
                } elseif($pair->offer->user_id == $user->id) {
                    $user_profile = $pair->offer;
                }
                
                if($user_profile) {
                    if($user_profile->delete() === false) {
                        return false;
                    }
                }
            }
            
            $message_text = '';
            $message_target_user = null;
            
            if($pair->request->user_id == $user->id) {
                $message_text = sprintf(
                    '%s hat das Tandem für die Sprache %s aufgelöst!',
                    $user->getFullName(),
                    $pair->request->getTargetName()
                );
                $message_target_user = $pair->offer->user;
            } elseif($pair->offer->user_id == $user->id) {
                $message_text = sprintf(
                    '%s hat das Tandem für die Sprache %s aufgelöst!',
                    $user->getFullName(),
                    $pair->offer->getTargetName()
                );
                $message_target_user = $pair->request->user;
            }
            
            if($pair->delete()) {
                
                $m = new messaging();
                
                //send mail (with e-mail)
                $m->insert_message(
                    $message_text,
                    $message_target_user['username'],
                    '____%system%____',
                    false,
                    false,
                    '1',
                    false,
                    'Tandem wurde aufgelöst!',
                    '1'
                );
                
                return true;
            }
            
            return false;
        }
    }