/** * Constructor * * Sets up and retrieves the API objects * **/ public function __construct($company, $user, $course, $invoice, $classroom, $license, $sender, $approveuser) { $this->company =& $company; $this->user =& $user; $this->invoice =& $invoice; $this->classroom =& $classroom; $this->license =& $license; $this->sender =& $sender; $this->approveuser =& $approveuser; if (!isset($this->company)) { if (isset($user->id) && !isset($user->profile)) { profile_load_custom_fields($this->user); } if (isset($user->profile["company"])) { $this->company = company::by_shortname($this->user->profile["company"])->get('*'); } } $this->course =& $course; if (!empty($course->id)) { $this->course->url = new moodle_url('/course/view.php', array('id' => $this->course->id)); } if (!empty($user->id)) { $this->url = new moodle_url('/user/profile.php', array('id' => $this->user->id)); } $this->site = get_site(); }
/** * Creates a user using company user defaults and attaches it to a company * User will be emailed a password when the cron job has run * @param object $data * @return userid */ public static function create($data) { global $DB, $CFG, $USER; if ($data->companyid) { $company = new company($data->companyid); $c = $company->get('shortname'); $data->company = $c->shortname; } else { $company = company::by_shortname($data->company); } $defaults = $company->get_user_defaults(); $user = (object) array_merge((array) $defaults, (array) $data); //$user->username = self::generate_username( $user->email ); //GWL : Remove Generate Usernaame From Email for Phone No. //$user->username = clean_param($user->username, PARAM_USERNAME); //GWL : Remove Generate Usernaame From Email for Phone No. /* GWL : Get Usernamme as phone no. */ $user->username = $user->phone; /* GWL : Get Usernamme as phone no. */ // Deal with the company theme. $user->theme = $company->get_theme(); if ($user->sendnewpasswordemails && !$user->preference_auth_forcepasswordchange) { throw new Exception(get_string('cannotemailnontemporarypasswords', 'local_iomad')); } /* There are 8 possible combinations of password, sendbyemail and forcepasswordchange fields: pwd email yes force change -> temporary password pwd email no force change -> temporary password pwd email no dont force change -> not a temporary password no pwd email yes force change -> create password -> store temp no pwd email no force change -> create password -> store temp no pwd email no dont force change -> create password -> store temp These two combinations shouldn't happen (caught by form validation and exception above): pwd email yes dont force change->needs to be stored as temp password -> not secure no pwd email yes dont force change->create password->store temp->not secure The next set of variables ($sendemail, $passwordentered, $createpassword, $forcepasswordchange, $storetemppassword) are used to distinguish between the first 6 combinations and to take appropriate action. */ $sendemail = $user->sendnewpasswordemails; $passwordentered = !empty($user->newpassword); $createpassword = !$passwordentered; $forcepasswordchange = $user->preference_auth_forcepasswordchange; // Store temp password unless password was entered and it's not going to be send by // email nor is it going to be forced to change. $storetemppassword = !($passwordentered && !$sendemail && !$forcepasswordchange); if ($passwordentered) { $user->password = $user->newpassword; // Don't hash it, user_create_user will do that. } $user->confirmed = 1; $user->mnethostid = 1; $user->maildisplay = 0; // Hide email addresses by default. // Create user record and return id. $id = user_create_user($user); $user->id = $id; // Passwords will be created and sent out on cron. if ($createpassword) { set_user_preference('create_password', 1, $user->id); $user->newpassword = generate_password(); if (!empty($CFG->iomad_email_senderisreal)) { EmailTemplate::send('user_create', array('user' => $user, 'sender' => $USER)); } else { EmailTemplate::send('user_create', array('user' => $user, 'headers' => serialize(array("To:" . $user->email . ", " . $USER->email)))); } $sendemail = false; } if ($forcepasswordchange) { set_user_preference('auth_forcepasswordchange', 1, $user->id); } if ($createpassword) { $DB->set_field('user', 'password', hash_internal_user_password($user->newpassword), array('id' => $user->id)); } if ($storetemppassword) { // Store password as temporary password, sendemail if necessary. self::store_temporary_password($user, $sendemail, $user->newpassword); } // Attach user to company. // Do we have a department? if (empty($data->departmentid)) { $departmentinfo = $DB->get_record('department', array('company' => $company->id, 'parent' => 0)); $data->departmentid = $departmentinfo->id; } // Deal with unset variable. if (empty($data->managertype)) { $data->managertype = 0; } // Create the user association. $DB->insert_record('company_users', array('userid' => $user->id, 'companyid' => $company->id, 'managertype' => $data->managertype, 'departmentid' => $data->departmentid)); if (isset($data->selectedcourses)) { self::enrol($user, array_keys($data->selectedcourses)); } return $user->id; }