/**
* Handles editing datetime fields.
*
* @param moodleform $mform
*/
public function edit_field_add($mform)
{
// Get the current calendar in use - see MDL-18375.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
// Check if the field is required.
if ($this->field->required) {
$optional = false;
} else {
$optional = true;
}
// Convert the year stored in the DB as gregorian to that used by the calendar type.
$startdate = $calendartype->convert_from_gregorian($this->field->param1, 1, 1);
$stopdate = $calendartype->convert_from_gregorian($this->field->param2, 1, 1);
$attributes = array('startyear' => $startdate['year'], 'stopyear' => $stopdate['year'], 'optional' => $optional);
// Check if they wanted to include time as well.
if (!empty($this->field->param3)) {
$mform->addElement('date_time_selector', $this->inputname, format_string($this->field->name), $attributes);
} else {
$mform->addElement('date_selector', $this->inputname, format_string($this->field->name), $attributes);
}
$mform->setType($this->inputname, PARAM_INT);
$mform->setDefault($this->inputname, time());
}
/**
* Extend the form definition after the data has been parsed.
*/
public function definition_after_data()
{
global $CFG;
$mform = $this->_form;
// If calendar type does not exist, use site default calendar type.
if ($calendarselected = $mform->getElementValue('calendartype')) {
if (is_array($calendarselected)) {
// There are multiple calendar types available.
$calendar = reset($calendarselected);
} else {
// There is only one calendar type available.
$calendar = $calendarselected;
}
// Check calendar type exists.
if (!array_key_exists($calendar, \core_calendar\type_factory::get_list_of_calendar_types())) {
$calendartypeel = $mform->getElement('calendartype');
$calendartypeel->setValue($CFG->calendartype);
}
}
}
/**
* Update a user with a user object (will compare against the ID)
*
* @param stdClass $user the user to update
* @param bool $updatepassword if true, authentication plugin will update password.
* @param bool $triggerevent set false if user_updated event should not be triggred.
*/
function user_update_user($user, $updatepassword = true, $triggerevent = true)
{
global $DB;
// set the timecreate field to the current time
if (!is_object($user)) {
$user = (object) $user;
}
//check username
if (isset($user->username)) {
if ($user->username !== core_text::strtolower($user->username)) {
throw new moodle_exception('usernamelowercase');
} else {
if ($user->username !== clean_param($user->username, PARAM_USERNAME)) {
throw new moodle_exception('invalidusername');
}
}
}
// Unset password here, for updating later, if password update is required.
if ($updatepassword && isset($user->password)) {
//check password toward the password policy
if (!check_password_policy($user->password, $errmsg)) {
throw new moodle_exception($errmsg);
}
$passwd = $user->password;
unset($user->password);
}
// Make sure calendartype, if set, is valid.
if (!empty($user->calendartype)) {
$availablecalendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
// If it doesn't exist, then unset this value, we do not want to update the user's value.
if (empty($availablecalendartypes[$user->calendartype])) {
unset($user->calendartype);
}
} else {
// Unset this variable, must be an empty string, which we do not want to update the calendartype to.
unset($user->calendartype);
}
$user->timemodified = time();
$DB->update_record('user', $user);
if ($updatepassword) {
// Get full user record.
$updateduser = $DB->get_record('user', array('id' => $user->id));
// if password was set, then update its hash
if (isset($passwd)) {
$authplugin = get_auth_plugin($updateduser->auth);
if ($authplugin->can_change_password()) {
$authplugin->user_update_password($updateduser, $passwd);
}
}
}
// Trigger event if required.
if ($triggerevent) {
\core\event\user_updated::create_from_userid($user->id)->trigger();
}
}
/**
* Get per-day basis events
*
* @param array $events list of events
* @param int $month the number of the month
* @param int $year the number of the year
* @param array $eventsbyday event on specific day
* @param array $durationbyday duration of the event in days
* @param array $typesbyday event type (eg: global, course, user, or group)
* @param array $courses list of courses
* @return void
*/
function calendar_events_by_day($events, $month, $year, &$eventsbyday, &$durationbyday, &$typesbyday, &$courses)
{
// Get the calendar type we are using.
$calendartype = \core_calendar\type_factory::get_calendar_instance();
$eventsbyday = array();
$typesbyday = array();
$durationbyday = array();
if ($events === false) {
return;
}
foreach ($events as $event) {
$startdate = $calendartype->timestamp_to_date_array($event->timestart);
// Set end date = start date if no duration
if ($event->timeduration) {
$enddate = $calendartype->timestamp_to_date_array($event->timestart + $event->timeduration - 1);
} else {
$enddate = $startdate;
}
// Simple arithmetic: $year * 13 + $month is a distinct integer for each distinct ($year, $month) pair
if (!($startdate['year'] * 13 + $startdate['mon'] <= $year * 13 + $month) && $enddate['year'] * 13 + $enddate['mon'] >= $year * 13 + $month) {
// Out of bounds
continue;
}
$eventdaystart = intval($startdate['mday']);
if ($startdate['mon'] == $month && $startdate['year'] == $year) {
// Give the event to its day
$eventsbyday[$eventdaystart][] = $event->id;
// Mark the day as having such an event
if ($event->courseid == SITEID && $event->groupid == 0) {
$typesbyday[$eventdaystart]['startglobal'] = true;
// Set event class for global event
$events[$event->id]->class = 'calendar_event_global';
} else {
if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
$typesbyday[$eventdaystart]['startcourse'] = true;
// Set event class for course event
$events[$event->id]->class = 'calendar_event_course';
} else {
if ($event->groupid) {
$typesbyday[$eventdaystart]['startgroup'] = true;
// Set event class for group event
$events[$event->id]->class = 'calendar_event_group';
} else {
if ($event->userid) {
$typesbyday[$eventdaystart]['startuser'] = true;
// Set event class for user event
$events[$event->id]->class = 'calendar_event_user';
}
}
}
}
}
if ($event->timeduration == 0) {
// Proceed with the next
continue;
}
// The event starts on $month $year or before. So...
$lowerbound = $startdate['mon'] == $month && $startdate['year'] == $year ? intval($startdate['mday']) : 0;
// Also, it ends on $month $year or later...
$upperbound = $enddate['mon'] == $month && $enddate['year'] == $year ? intval($enddate['mday']) : calendar_days_in_month($month, $year);
// Mark all days between $lowerbound and $upperbound (inclusive) as duration
for ($i = $lowerbound + 1; $i <= $upperbound; ++$i) {
$durationbyday[$i][] = $event->id;
if ($event->courseid == SITEID && $event->groupid == 0) {
$typesbyday[$i]['durationglobal'] = true;
} else {
if ($event->courseid != 0 && $event->courseid != SITEID && $event->groupid == 0) {
$typesbyday[$i]['durationcourse'] = true;
} else {
if ($event->groupid) {
$typesbyday[$i]['durationgroup'] = true;
} else {
if ($event->userid) {
$typesbyday[$i]['durationuser'] = true;
}
}
}
}
}
}
return;
}
//.........这里部分代码省略.........
if (!in_array($course->format, $courseformats)) {
// this format is disabled. Still display it in the dropdown
$formcourseformats[$course->format] = get_string('withdisablednote', 'moodle', get_string('pluginname', 'format_' . $course->format));
}
}
$mform->addElement('select', 'format', get_string('format'), $formcourseformats);
$mform->addHelpButton('format', 'format');
$mform->setDefault('format', $courseconfig->format);
// Button to update format-specific options on format change (will be hidden by JavaScript).
$mform->registerNoSubmitButton('updatecourseformat');
$mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
// Just a placeholder for the course format options.
$mform->addElement('hidden', 'addcourseformatoptionshere');
$mform->setType('addcourseformatoptionshere', PARAM_BOOL);
// Appearance.
$mform->addElement('header', 'appearancehdr', get_string('appearance'));
if (!empty($CFG->allowcoursethemes)) {
$themeobjects = get_list_of_themes();
$themes = array();
$themes[''] = get_string('forceno');
foreach ($themeobjects as $key => $theme) {
if (empty($theme->hidefromselector)) {
$themes[$key] = get_string('pluginname', 'theme_' . $theme->name);
}
}
$mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
}
$languages = array();
$languages[''] = get_string('forceno');
$languages += get_string_manager()->get_list_of_translations();
$mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
$mform->setDefault('lang', $courseconfig->lang);
// Multi-Calendar Support - see MDL-18375.
$calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
// We do not want to show this option unless there is more than one calendar type to display.
if (count($calendartypes) > 1) {
$calendars = array();
$calendars[''] = get_string('forceno');
$calendars += $calendartypes;
$mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
}
$options = range(0, 10);
$mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
$mform->addHelpButton('newsitems', 'newsitemsnumber');
$mform->setDefault('newsitems', $courseconfig->newsitems);
$mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
$mform->addHelpButton('showgrades', 'showgrades');
$mform->setDefault('showgrades', $courseconfig->showgrades);
$mform->addElement('selectyesno', 'showreports', get_string('showreports'));
$mform->addHelpButton('showreports', 'showreports');
$mform->setDefault('showreports', $courseconfig->showreports);
// Files and uploads.
$mform->addElement('header', 'filehdr', get_string('filesanduploads'));
if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
if (empty($course->legacyfiles)) {
//0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
$choices = array('0' => get_string('no'), '2' => get_string('yes'));
} else {
$choices = array('1' => get_string('no'), '2' => get_string('yes'));
}
$mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
$mform->addHelpButton('legacyfiles', 'courselegacyfiles');
if (!isset($courseconfig->legacyfiles)) {
// in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
$courseconfig->legacyfiles = 0;
}
/**
* Given a $time timestamp in GMT (seconds since epoch), returns an array that represents
* the date in user time.
*
* @param int $time timestamp in GMT
* @param float|int|string $timezone the timezone to use to calculate the time
* {@link http://docs.moodle.org/dev/Time_API#Timezone}
* @return array an array that represents the date in user time
*/
public function timestamp_to_date_array($time, $timezone = 99)
{
$gregoriancalendar = \core_calendar\type_factory::get_calendar_instance('gregorian');
$date = $gregoriancalendar->timestamp_to_date_array($time, $timezone);
$newdate = $this->convert_from_gregorian($date['year'], $date['mon'], $date['mday'], $date['hours'], $date['minutes']);
$date['year'] = $newdate['year'];
$date['mon'] = $newdate['month'];
$date['mday'] = $newdate['day'];
$date['hours'] = $newdate['hour'];
$date['minutes'] = $newdate['minute'];
return $date;
}
请发表评论