Comment 3 for bug 1705162

Revision history for this message
Nelson (nmoller-c) wrote :

I'll propose something like:

class ActivityTypeInstitutionmessage extends ActivityType {

    protected $messagetype;
    protected $institution;
    protected $username;
    protected $fullname;

    public function __construct($data, $cron=false) {
        parent::__construct($data, $cron);
        if ($this->messagetype == 'request') {
            $this->url = 'admin/users/institutionusers.php';
            $this->users = activity_get_users($this->get_id(), null, null, null,
                                              array($this->institution->name));
            $this->add_urltext(array('key' => 'institutionmembers', 'section' => 'admin'));
        } else if ($this->messagetype == 'invite') {
            $this->url = 'account/institutions.php';
            $this->users = activity_get_users($this->get_id(), $this->users);
            $this->add_urltext(array('key' => 'institutionmembership', 'section' => 'mahara'));
        }
        $this->institution = new Institution($this->institution->name);
    }

    private function get_language($user, $institution) {
        if (!isset($user->lang) || $user->lang === '' || $user->lang === 'default') {
            if (!isset($institution->lang) || $institution->lang === '' || $institution->lang === 'default') {
                return get_config('lang') ? get_config('lang') : '';
            }
            else return $institution->lang;
        }
        else return $user->lang;
    }

    public function get_subject($user) {
        $lang = $this->get_language($user, $this->institution);
        if ($this->messagetype == 'request') {
            $userstring = $this->fullname . ' (' . $this->username . ')';
            return get_string_from_language($lang, 'institutionrequestsubject', 'activity', $userstring,
                                            $this->institution->displayname);
        } else if ($this->messagetype == 'invite') {
            return get_string_from_language($lang, 'institutioninvitesubject', 'activity',
                                            $this->institution->displayname);
        }
    }

    public function get_message($user) {
        $lang = $this->get_language($user, $this->institution);
        if ($this->messagetype == 'request') {
            return $this->get_subject($user) .' '. get_string_from_language($lang, 'institutionrequestmessage', 'activity', $this->url);
        } else if ($this->messagetype == 'invite') {
            return $this->get_subject($user) .' '. get_string_from_language($lang, 'institutioninvitemessage', 'activity', $this->url);
        }
    }

    public function get_required_parameters() {
        return array('messagetype', 'institution');
    }
}