Ejemplo n.º 1
0
 public function addUserAsMember($user)
 {
     global $USER;
     if ($this->isFull()) {
         $this->send_admin_institution_is_full_message();
         die_info(get_string('institutionmaxusersexceeded', 'admin'));
     }
     if (is_numeric($user)) {
         $user = get_record('usr', 'id', $user);
     }
     // The user hasn't been added yet, so we have to manually use this institution's lang
     if ($this->lang != 'default') {
         $lang = $this->lang;
     } else {
         $lang = get_user_language($user->id);
     }
     $userinst = new StdClass();
     $userinst->institution = $this->name;
     $studentid = get_field('usr_institution_request', 'studentid', 'usr', $user->id, 'institution', $this->name);
     if (!empty($studentid)) {
         $userinst->studentid = $studentid;
     } else {
         if (!empty($user->studentid)) {
             $userinst->studentid = $user->studentid;
         }
     }
     $userinst->usr = $user->id;
     $now = time();
     $userinst->ctime = db_format_timestamp($now);
     $defaultexpiry = $this->defaultmembershipperiod;
     if (!empty($defaultexpiry)) {
         $userinst->expiry = db_format_timestamp($now + $defaultexpiry);
     }
     $message = (object) array('users' => array($user->id), 'subject' => get_string_from_language($lang, 'institutionmemberconfirmsubject'), 'message' => get_string_from_language($lang, 'institutionmemberconfirmmessage', 'mahara', $this->displayname));
     db_begin();
     if (!get_config('usersallowedmultipleinstitutions')) {
         delete_records('usr_institution', 'usr', $user->id);
         delete_records('usr_institution_request', 'usr', $user->id);
     }
     insert_record('usr_institution', $userinst);
     delete_records('usr_institution_request', 'usr', $userinst->usr, 'institution', $this->name);
     execute_sql("\n            DELETE FROM {usr_tag}\n            WHERE usr = ? AND tag " . db_ilike() . " 'lastinstitution:%'", array($user->id));
     // Copy institution views and collection to the user's portfolio
     $checkviewaccess = empty($user->newuser) && !$USER->get('admin');
     $userobj = new User();
     $userobj->find_by_id($user->id);
     $userobj->copy_institution_views_collections_to_new_member($this->name);
     require_once 'activity.php';
     activity_occurred('maharamessage', $message);
     handle_event('updateuser', $userinst->usr);
     // Give institution members access to user's profile page
     require_once 'view.php';
     if ($profileview = $userobj->get_profile_view()) {
         $profileview->add_owner_institution_access(array($this->name));
     }
     db_commit();
 }
Ejemplo n.º 2
0
 public function addUserAsMember($user)
 {
     global $USER;
     if ($this->isFull()) {
         throw new SystemException('Trying to add a user to an institution that already has a full quota of members');
     }
     if (is_numeric($user)) {
         $user = get_record('usr', 'id', $user);
     }
     if ($user instanceof User) {
         $lang = $user->get_account_preference('lang');
         if (empty($lang) || $lang == 'default') {
             $lang = get_config('lang');
         }
     } else {
         // stdclass object
         $lang = get_user_language($user->id);
     }
     $userinst = new StdClass();
     $userinst->institution = $this->name;
     $studentid = get_field('usr_institution_request', 'studentid', 'usr', $user->id, 'institution', $this->name);
     if (!empty($studentid)) {
         $userinst->studentid = $studentid;
     } else {
         if (!empty($user->studentid)) {
             $userinst->studentid = $user->studentid;
         }
     }
     $userinst->usr = $user->id;
     $now = time();
     $userinst->ctime = db_format_timestamp($now);
     $defaultexpiry = $this->defaultmembershipperiod;
     if (!empty($defaultexpiry)) {
         $userinst->expiry = db_format_timestamp($now + $defaultexpiry);
     }
     $message = (object) array('users' => array($user->id), 'subject' => get_string_from_language($lang, 'institutionmemberconfirmsubject'), 'message' => get_string_from_language($lang, 'institutionmemberconfirmmessage', 'mahara', $this->displayname));
     db_begin();
     if (!get_config('usersallowedmultipleinstitutions')) {
         delete_records('usr_institution', 'usr', $user->id);
         delete_records('usr_institution_request', 'usr', $user->id);
     }
     insert_record('usr_institution', $userinst);
     delete_records('usr_institution_request', 'usr', $userinst->usr, 'institution', $this->name);
     execute_sql("\n            DELETE FROM {usr_tag}\n            WHERE usr = ? AND tag " . db_ilike() . " 'lastinstitution:%'", array($user->id));
     // Copy institution views and collection to the user's portfolio
     $checkviewaccess = empty($user->newuser) && !$USER->get('admin');
     $userobj = new User();
     $userobj->find_by_id($user->id);
     $userobj->copy_institution_views_collections_to_new_member($this->name, $checkviewaccess);
     require_once 'activity.php';
     activity_occurred('maharamessage', $message);
     handle_event('updateuser', $userinst->usr);
     // Give institution members access to user's profile page
     require_once 'view.php';
     if ($profileview = $userobj->get_profile_view()) {
         $profileview->add_owner_institution_access(array($this->name));
     }
     db_commit();
 }