/** * 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(); }
/** * Return contents of course_overview block * * @return stdClass contents of block */ public function get_content() { global $USER, $CFG, $DB; require_once $CFG->dirroot . '/user/profile/lib.php'; if ($this->content !== NULL) { return $this->content; } $config = get_config('block_course_overview'); $this->content = new stdClass(); $this->content->text = ''; $this->content->footer = ''; $content = array(); $updatemynumber = optional_param('mynumber', -1, PARAM_INT); if ($updatemynumber >= 0) { block_course_overview_update_mynumber($updatemynumber); } profile_load_custom_fields($USER); list($sortedcourses, $sitecourses, $totalcourses) = block_course_overview_get_sorted_courses(); $overviews = block_course_overview_get_overviews($sitecourses); $renderer = $this->page->get_renderer('block_course_overview'); if (!empty($config->showwelcomearea)) { require_once $CFG->dirroot . '/message/lib.php'; $msgcount = message_count_unread_messages(); $this->content->text = $renderer->welcome_area($msgcount); } // Number of sites to display. if ($this->page->user_is_editing() && empty($config->forcedefaultmaxcourses)) { $this->content->text .= $renderer->editing_bar_head($totalcourses); } if (empty($sortedcourses)) { $this->content->text .= get_string('nocourses', 'my'); } else { // For each course, build category cache. $this->content->text .= $renderer->course_overview($sortedcourses, $overviews); $this->content->text .= $renderer->hidden_courses($totalcourses - count($sortedcourses)); if ($this->page->user_is_editing() && ajaxenabled()) { $this->page->requires->js_init_call('M.block_course_overview.add_handles'); } } return $this->content; }
die("could not send email!"); } } // Reload from db, we need new full name on this page if we do not redirect. $user = $DB->get_record('user', array('id' => $user->id), '*', MUST_EXIST); if ($USER->id == $user->id) { // Override old $USER session variable if needed. foreach ((array) $user as $variable => $value) { if ($variable === 'description' or $variable === 'password') { // These are not set for security nad perf reasons. continue; } $USER->{$variable} = $value; } // Preload custom fields. profile_load_custom_fields($USER); } if (is_siteadmin() and empty($SITE->shortname)) { // Fresh cli install - we need to finish site settings. redirect(new moodle_url('/admin/index.php')); } if (!$emailchanged || !$CFG->emailchangeconfirmation) { redirect($returnurl); } } // Make sure we really are on the https page when https login required. $PAGE->verify_https_required(); // Display page header. $streditmyprofile = get_string('editmyprofile'); $strparticipants = get_string('participants'); $userfullname = fullname($user, true);
/** * Get a complete user record, which includes all the info in the user record. * * Intended for setting as $USER session variable * * @param string $field The user field to be checked for a given value. * @param string $value The value to match for $field. * @param int $mnethostid * @return mixed False, or A {@link $USER} object. */ function get_complete_user_data($field, $value, $mnethostid = null) { global $CFG, $DB; if (!$field || !$value) { return false; } // Build the WHERE clause for an SQL query. $params = array('fieldval' => $value); $constraints = "{$field} = :fieldval AND deleted <> 1"; // If we are loading user data based on anything other than id, // we must also restrict our search based on mnet host. if ($field != 'id') { if (empty($mnethostid)) { // If empty, we restrict to local users. $mnethostid = $CFG->mnet_localhost_id; } } if (!empty($mnethostid)) { $params['mnethostid'] = $mnethostid; $constraints .= " AND mnethostid = :mnethostid"; } // Get all the basic user data. if (!($user = $DB->get_record_select('user', $constraints, $params))) { return false; } // Get various settings and preferences. // Preload preference cache. check_user_preferences_loaded($user); // Load course enrolment related stuff. $user->lastcourseaccess = array(); // During last session. $user->currentcourseaccess = array(); // During current session. if ($lastaccesses = $DB->get_records('user_lastaccess', array('userid' => $user->id))) { foreach ($lastaccesses as $lastaccess) { $user->lastcourseaccess[$lastaccess->courseid] = $lastaccess->timeaccess; } } $sql = "SELECT g.id, g.courseid\n FROM {groups} g, {groups_members} gm\n WHERE gm.groupid=g.id AND gm.userid=?"; // This is a special hack to speedup calendar display. $user->groupmember = array(); if (!isguestuser($user)) { if ($groups = $DB->get_records_sql($sql, array($user->id))) { foreach ($groups as $group) { if (!array_key_exists($group->courseid, $user->groupmember)) { $user->groupmember[$group->courseid] = array(); } $user->groupmember[$group->courseid][$group->id] = $group->id; } } } // Add the custom profile fields to the user record. $user->profile = array(); if (!isguestuser($user)) { require_once $CFG->dirroot . '/user/profile/lib.php'; profile_load_custom_fields($user); } // Rewrite some variables if necessary. if (!empty($user->description)) { // No need to cart all of it around. $user->description = true; } if (isguestuser($user)) { // Guest language always same as site. $user->lang = $CFG->lang; // Name always in current language. $user->firstname = get_string('guestuser'); $user->lastname = ' '; } return $user; }
/** * Return the value for a user's profile field * * @param int $userid User ID * @return string|bool Value, or false if user does not have a value for this field */ protected function get_cached_user_profile_field($userid) { global $USER, $DB, $CFG; $iscurrentuser = $USER->id == $userid; if (isguestuser($userid) || $iscurrentuser && !isloggedin()) { // Must be logged in and can't be the guest. return false; } // Custom profile fields will be numeric, there are no numeric standard profile fields so this is not a problem. $iscustomprofilefield = $this->customfield ? true : false; if ($iscustomprofilefield) { // As its a custom profile field we need to map the id back to the actual field. // We'll also preload all of the other custom profile fields just in case and ensure we have the // default value available as well. if (!array_key_exists($this->customfield, self::get_custom_profile_fields())) { // No such field exists. // This shouldn't normally happen but occur if things go wrong when deleting a custom profile field // or when restoring a backup of a course with user profile field conditions. return false; } $field = $this->customfield; } else { $field = $this->standardfield; } // If its the current user than most likely we will be able to get this information from $USER. // If its a regular profile field then it should already be available, if not then we have a mega problem. // If its a custom profile field then it should be available but may not be. If it is then we use the value // available, otherwise we load all custom profile fields into a temp object and refer to that. // Noting its not going be great for performance if we have to use the temp object as it involves loading the // custom profile field API and classes. if ($iscurrentuser) { if (!$iscustomprofilefield) { if (property_exists($USER, $field)) { return $USER->{$field}; } else { // Unknown user field. This should not happen. throw new \coding_exception('Requested user profile field does not exist'); } } // Checking if the custom profile fields are already available. if (!isset($USER->profile)) { // Drat! they're not. We need to use a temp object and load them. // We don't use $USER as the profile fields are loaded into the object. $user = new \stdClass(); $user->id = $USER->id; // This should ALWAYS be set, but just in case we check. require_once $CFG->dirroot . '/user/profile/lib.php'; profile_load_custom_fields($user); if (array_key_exists($field, $user->profile)) { return $user->profile[$field]; } } else { if (array_key_exists($field, $USER->profile)) { // Hurrah they're available, this is easy. return $USER->profile[$field]; } } // The profile field doesn't exist. return false; } else { // Loading for another user. if ($iscustomprofilefield) { // Fetch the data for the field. Noting we keep this query simple so that Database caching takes care of performance // for us (this will likely be hit again). // We are able to do this because we've already pre-loaded the custom fields. $data = $DB->get_field('user_info_data', 'data', array('userid' => $userid, 'fieldid' => self::$customprofilefields[$field]->id), IGNORE_MISSING); // If we have data return that, otherwise return the default. if ($data !== false) { return $data; } else { return self::$customprofilefields[$field]->defaultdata; } } else { // Its a standard field, retrieve it from the user. return $DB->get_field('user', $field, array('id' => $userid), MUST_EXIST); } } return false; }
/** * Post authentication hook. * This method is called from authenticate_user_login() for all enabled auth plugins. * * @param object $user user object, later used for $USER * @param string $username (with system magic quotes) * @param string $password plain text password (with system magic quotes) */ function user_authenticated_hook(&$user, $username, $password) { global $DB, $SESSION; $context = context_system::instance(); $uid = $user->id; // Ignore users from don't_touch list $ignore = explode(",", $this->config->donttouchusers); if (!empty($ignore) and array_search($username, $ignore) !== false) { $SESSION->mcautoenrolled = TRUE; return true; } // Ignore guests if ($uid < 2) { $SESSION->mcautoenrolled = TRUE; return true; } // ********************** Get COHORTS data $clause = array('contextid' => $context->id); if ($this->config->enableunenrol == 1) { $clause['component'] = self::COMPONENT_NAME; } $cohorts = $DB->get_records('cohort', $clause); $cohorts_list = array(); foreach ($cohorts as $cohort) { $cid = $cohort->id; $cname = format_string($cohort->name); $cohorts_list[$cid] = $cname; } // Get advanced user data profile_load_data($user); profile_load_custom_fields($user); $user_profile_data = mcae_prepare_profile_data($user, $this->config->secondrule_fld); // Additional values for email list($email_username, $email_domain) = explode("@", $user_profile_data['email']); // email root domain $email_domain_array = explode('.', $email_domain); if (count($email_domain_array) > 2) { $email_rootdomain = $email_domain_array[count($email_domain_array) - 2] . '.' . $email_domain_array[count($email_domain_array) - 1]; } else { $email_rootdomain = $email_domain; } $user_profile_data['email'] = array('full' => $user_profile_data['email'], 'username' => $email_username, 'domain' => $email_domain, 'rootdomain' => $email_rootdomain); // Delimiter $delimiter = $this->config->delim; $delim = strtr($delimiter, array('CR+LF' => chr(13) . chr(10), 'CR' => chr(13), 'LF' => chr(10))); // Calculate a cohort names for user $replacements_tpl = $this->config->replace_arr; $replacements = array(); if (!empty($replacements_tpl)) { $replacements_pre = explode($delim, $replacements_tpl); foreach ($replacements_pre as $rap) { list($key, $val) = explode("|", $rap); $replacements[$key] = $val; } } // Generate cohorts array $main_rule = $this->config->mainrule_fld; $templates_tpl = array(); $templates = array(); if (!empty($main_rule)) { $templates_tpl = explode($delim, $main_rule); } else { $SESSION->mcautoenrolled = TRUE; return; //Empty mainrule } // Find %split function foreach ($templates_tpl as $item) { if (preg_match('/(?<full>%split\\((?<fld>\\w*)\\|(?<delim>.{1,5})\\))/', $item, $split_params)) { // Split! $splitted = explode($split_params['delim'], $user_profile_data[$split_params['fld']]); foreach ($splitted as $key => $val) { $user_profile_data[$split_params['fld'] . "_{$key}"] = $val; $templates[] = strtr($item, array("{$split_params['full']}" => "{{ {$split_params['fld']}_{$key} }}")); } } else { $templates[] = $item; } } $processed = array(); // Process templates with Mustache foreach ($templates as $cohort) { $cohortname = $this->mustache->render($cohort, $user_profile_data); $cohortname = !empty($replacements) ? strtr($cohortname, $replacements) : $cohortname; if ($cohortname == '') { continue; // We don't want an empty cohort name } $cid = array_search($cohortname, $cohorts_list); if ($cid !== false) { if (!$DB->record_exists('cohort_members', array('cohortid' => $cid, 'userid' => $user->id))) { cohort_add_member($cid, $user->id); } } else { // Cohort not exist so create a new one $newcohort = new stdClass(); $newcohort->name = $cohortname; $newcohort->description = "created " . date("d-m-Y"); $newcohort->contextid = $context->id; if ($this->config->enableunenrol == 1) { $newcohort->component = "auth_mcae"; } $cid = cohort_add_cohort($newcohort); cohort_add_member($cid, $user->id); // Prevent creation new cohorts with same names $cohorts_list[$cid] = $cohortname; } $processed[] = $cid; } $SESSION->mcautoenrolled = TRUE; //Unenrol user if ($this->config->enableunenrol == 1) { //List of cohorts where this user enrolled $sql = "SELECT c.id AS cid FROM {cohort} c JOIN {cohort_members} cm ON cm.cohortid = c.id WHERE c.component = 'auth_mcae' AND cm.userid = {$uid}"; $enrolledcohorts = $DB->get_records_sql($sql); foreach ($enrolledcohorts as $ec) { if (array_search($ec->cid, $processed) === false) { cohort_remove_member($ec->cid, $uid); } } } }
public function __construct($templatename, $options = array()) { global $USER, $SESSION, $COURSE, $DB; $user = array_key_exists('user', $options) ? $options['user'] : null; $course = array_key_exists('course', $options) ? $options['course'] : null; $this->invoice = array_key_exists('invoice', $options) ? $options['invoice'] : null; $sender = array_key_exists('sender', $options) ? $options['sender'] : null; $approveuser = array_key_exists('approveuser', $options) ? $options['approveuser'] : null; $event = array_key_exists('event', $options) ? $options['event'] : null; $this->classroom = array_key_exists('classroom', $options) ? $options['classroom'] : null; $this->license = array_key_exists('license', $options) ? $options['license'] : null; $this->headers = array_key_exists('headers', $options) ? $options['headers'] : null; if (!isset($user)) { $user =& $USER; } if (!isset($course)) { $course =& $COURSE; } if (!isset($sender)) { if ($USER->id == 0) { // We are being run from cron. $sender =& self::get_sender($user); } else { // Not been defined explicitly, use the current user. $sender = $USER; } } // Set the sender to the default site one if use real sender is not true. if (empty($CFG->iomad_email_senderisreal)) { $sender = core_user::get_support_user(); } $this->user = $this->get_user($user); $this->approveuser = $this->get_user($approveuser); // Check if we are being passed a password and add it if so. if (isset($user->newpassword)) { $this->user->newpassword = $user->newpassword; } $this->sender = $this->get_user($sender); if (!isset($this->user->email)) { print_error("No user was specified or the specified user has no email to send {$templatename} to."); } if (isset($this->user->id) && !isset($this->user->profile)) { profile_load_custom_fields($this->user); } // Check if we are an admin with a company set. if (!empty($SESSION->currenteditingcompany)) { $this->company = new company($SESSION->currenteditingcompany); // Otherwise use the creating users company. } else { // Code by sumit $userid = isset($user->id) ? $user->id : $USER->id; // End of code $this->company = $DB->get_record_sql("SELECT * FROM {company}\n WHERE id = (\n SELECT companyid FROM {company_users}\n WHERE userid = :userid\n )", array('userid' => $userid)); } $this->course = $this->get_course($course); $this->event = $event; $this->templatename = $templatename; $this->template = $this->get_template($templatename); }
function sendConfirmMailToManagers($instance,$info,$applydescription){ global $DB; global $CFG; global $USER; $courseid = $instance->courseid; $apply_setting = $DB->get_records_sql("select name,value from ".$CFG->prefix."config_plugins where plugin='enrol_apply'"); $show_standard_user_profile = $show_extra_user_profile = false; if($instance->customint1 != ''){ ($instance->customint1 == 0)?$show_standard_user_profile = true:$show_standard_user_profile = false; }else{ ($apply_setting['show_standard_user_profile']->value == 0)?$show_standard_user_profile = true:$show_standard_user_profile = false; } if($instance->customint2 != ''){ ($instance->customint2 == 0)?$show_extra_user_profile = true:$show_extra_user_profile = false; }else{ ($apply_setting['show_extra_user_profile']->value == 0)?$show_extra_user_profile = true:$show_extra_user_profile = false; } if($apply_setting['sendmailtomanager']->value == 1){ $course = get_course($courseid); $context = context_system::instance(); $managerType = $DB->get_record('role',array("shortname"=>"manager")); $managers = $DB->get_records('role_assignments', array('contextid'=>$context->id,'roleid'=>$managerType->id)); foreach($managers as $manager){ $userWithManagerRole = $DB->get_record('user',array('id'=>$manager->userid)); $body = '<p>'. get_string('coursename', 'enrol_apply') .': '.format_string($course->fullname).'</p>'; $body .= '<p>'. get_string('applyuser', 'enrol_apply') .': '.$USER->firstname.' '.$USER->lastname.'</p>'; $body .= '<p>'. get_string('comment', 'enrol_apply') .': '.$applydescription.'</p>'; if($show_standard_user_profile){ $body .= '<p><strong>'. get_string('user_profile', 'enrol_apply').'</strong></p>'; $body .= '<p>'. get_string('firstname') .': '.$info->firstname.'</p>'; $body .= '<p>'. get_string('lastname') .': '.$info->lastname.'</p>'; $body .= '<p>'. get_string('email') .': '.$info->email.'</p>'; $body .= '<p>'. get_string('city') .': '.$info->city.'</p>'; $body .= '<p>'. get_string('country') .': '.$info->country.'</p>'; $body .= '<p>'. get_string('preferredlanguage') .': '.$info->lang.'</p>'; $body .= '<p>'. get_string('description') .': '.$info->description_editor['text'].'</p>'; $body .= '<p>'. get_string('firstnamephonetic') .': '.$info->firstnamephonetic.'</p>'; $body .= '<p>'. get_string('lastnamephonetic') .': '.$info->lastnamephonetic.'</p>'; $body .= '<p>'. get_string('middlename') .': '.$info->middlename.'</p>'; $body .= '<p>'. get_string('alternatename') .': '.$info->alternatename.'</p>'; $body .= '<p>'. get_string('url') .': '.$info->url.'</p>'; $body .= '<p>'. get_string('icqnumber') .': '.$info->icq.'</p>'; $body .= '<p>'. get_string('skypeid') .': '.$info->skype.'</p>'; $body .= '<p>'. get_string('aimid') .': '.$info->aim.'</p>'; $body .= '<p>'. get_string('yahooid') .': '.$info->yahoo.'</p>'; $body .= '<p>'. get_string('msnid') .': '.$info->msn.'</p>'; $body .= '<p>'. get_string('idnumber') .': '.$info->idnumber.'</p>'; $body .= '<p>'. get_string('institution') .': '.$info->institution.'</p>'; $body .= '<p>'. get_string('department') .': '.$info->department.'</p>'; $body .= '<p>'. get_string('phone') .': '.$info->phone1.'</p>'; $body .= '<p>'. get_string('phone2') .': '.$info->phone2.'</p>'; $body .= '<p>'. get_string('address') .': '.$info->address.'</p>'; } if($show_extra_user_profile){ require_once($CFG->dirroot.'/user/profile/lib.php'); $user = $DB->get_record('user',array('id'=>$USER->id)); profile_load_custom_fields($user); foreach ($user->profile as $key => $value) { $body .= '<p>'. $key .': '.$value.'</p>'; } } $body .= '<p>'. html_writer::link(new moodle_url('/enrol/apply/manage.php'), get_string('applymanage', 'enrol_apply')).'</p>'; $contact = core_user::get_support_user(); $info = $userWithManagerRole; $info->coursename = $course->fullname; email_to_user($info, $contact, get_string('mailtoteacher_suject', 'enrol_apply'), html_to_text($body), $body); } } }
defined('MOODLE_INTERNAL') || die; global $USER; require_once $CFG->dirroot . '/user/profile/lib.php'; require_once $CFG->dirroot . '/auth/mcae/lib.php'; if ($hassiteconfig) { // needs this condition or there is error on login page $ADMIN->add('accounts', new admin_externalpage('cohorttoolmcae', get_string('auth_cohorttoolmcae', 'auth_mcae'), new moodle_url('/auth/mcae/convert.php'))); $ADMIN->add('accounts', new admin_externalpage('cohortviewmcae', get_string('auth_cohortviewmcae', 'auth_mcae'), new moodle_url('/auth/mcae/view.php'))); } if ($ADMIN->fulltree) { $settings->add(new admin_setting_configtextarea('auth_mcae/mainrule_fld', get_string('auth_mainrule_fld', 'auth_mcae'), '', '')); // Profile field helper $fldlist = array(); $usr_helper = $DB->get_record('user', array('id' => 2)); profile_load_data($usr_helper); profile_load_custom_fields($usr_helper); $fldlist = mcae_prepare_profile_data($usr_helper); // Additional values for email list($email_username, $email_domain) = explode("@", $fldlist['email']); // Email root domain $email_domain_array = explode('.', $email_domain); if (count($email_domain_array) > 2) { $email_rootdomain = $email_domain_array[count($email_domain_array) - 2] . '.' . $email_domain_array[count($email_domain_array) - 1]; } else { $email_rootdomain = $email_domain; } $fldlist['email'] = array('full' => $fldlist['email'], 'username' => $email_username, 'domain' => $email_domain, 'rootdomain' => $email_rootdomain); //print_r($fldlist); $help_array = array(); mcae_print_profile_data($fldlist, '', $help_array); //print_r($usr_helper);