MDL-59382 calendar: add modal to create and update events

This commit is contained in:
Ryan Wyllie 2017-07-24 08:01:14 +00:00
parent 6103fd2efe
commit aa0912258d
23 changed files with 2392 additions and 157 deletions

View file

@ -2686,8 +2686,9 @@ function calendar_set_event_type_display($type, $display = null, $user = null) {
*
* @param stdClass $allowed list of allowed edit for event type
* @param stdClass|int $course object of a course or course id
* @param array $groups array of groups for the given course
*/
function calendar_get_allowed_types(&$allowed, $course = null) {
function calendar_get_allowed_types(&$allowed, $course = null, $groups = null) {
global $USER, $DB;
$allowed = new \stdClass();
@ -2695,6 +2696,23 @@ function calendar_get_allowed_types(&$allowed, $course = null) {
$allowed->groups = false;
$allowed->courses = false;
$allowed->site = has_capability('moodle/calendar:manageentries', \context_course::instance(SITEID));
$getgroupsfunc = function($course, $context, $user) use ($groups) {
if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
if (has_capability('moodle/site:accessallgroups', $context)) {
return is_null($groups) ? groups_get_all_groups($course->id) : $groups;
} else {
if (is_null($groups)) {
return groups_get_all_groups($course->id, $user->id);
} else {
return array_filter($groups, function($group) use ($user) {
return isset($group->members[$user->id]);
});
}
}
}
return false;
};
if (!empty($course)) {
if (!is_object($course)) {
@ -2706,27 +2724,84 @@ function calendar_get_allowed_types(&$allowed, $course = null) {
if (has_capability('moodle/calendar:manageentries', $coursecontext)) {
$allowed->courses = array($course->id => 1);
if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
if (has_capability('moodle/site:accessallgroups', $coursecontext)) {
$allowed->groups = groups_get_all_groups($course->id);
} else {
$allowed->groups = groups_get_all_groups($course->id, $USER->id);
}
}
$allowed->groups = $getgroupsfunc($course, $coursecontext, $USER);
} else if (has_capability('moodle/calendar:managegroupentries', $coursecontext)) {
if ($course->groupmode != NOGROUPS || !$course->groupmodeforce) {
if (has_capability('moodle/site:accessallgroups', $coursecontext)) {
$allowed->groups = groups_get_all_groups($course->id);
} else {
$allowed->groups = groups_get_all_groups($course->id, $USER->id);
}
}
$allowed->groups = $getgroupsfunc($course, $coursecontext, $USER);
}
}
}
}
/**
* Get all of the allowed types for all of the courses and groups
* the logged in user belongs to.
*
* The returned array will optionally have 5 keys:
* 'user' : true if the logged in user can create user events
* 'site' : true if the logged in user can create site events
* 'course' : array of courses that the user can create events for
* 'group': array of groups that the user can create events for
* 'groupcourses' : array of courses that the groups belong to (can
* be different from the list in 'course'.
*
* @param array The available types for the logged in user
*/
function calendar_get_all_allowed_types() {
global $CFG, $USER;
require_once($CFG->libdir . '/enrollib.php');
$types = [];
calendar_get_allowed_types($allowed);
if ($allowed->user) {
$types['user'] = true;
}
if ($allowed->site) {
$types['site'] = true;
}
// This function warms the context cache for the course so the calls
// to load the course context in calendar_get_allowed_types don't result
// in additional DB queries.
$courses = enrol_get_users_courses($USER->id, true);
// We want to pre-fetch all of the groups for each course in a single
// query to avoid calendar_get_allowed_types from hitting the DB for
// each separate course.
$groups = groups_get_all_groups_for_courses($courses);
foreach ($courses as $course) {
$coursegroups = isset($groups[$course->id]) ? $groups[$course->id] : null;
calendar_get_allowed_types($allowed, $course, $coursegroups);
if (!empty($allowed->courses)) {
if (!isset($types['course'])) {
$types['course'] = [$course];
} else {
$types['course'][] = $course;
}
}
if (!empty($allowed->groups)) {
if (!isset($types['groupcourses'])) {
$types['groupcourses'] = [$course];
} else {
$types['groupcourses'][] = $course;
}
if (!isset($types['group'])) {
$types['group'] = array_values($allowed->groups);
} else {
$types['group'] = array_merge($types['group'], array_values($allowed->groups));
}
}
}
return $types;
}
/**
* See if user can add calendar entries at all used to print the "New Event" button.
*
@ -3340,3 +3415,70 @@ function calendar_get_legacy_events($tstart, $tend, $users, $groups, $courses, $
return $carry + [$event->get_id() => $mapper->from_event_to_stdclass($event)];
}, []);
}
function calendar_output_fragment_event_form($args) {
global $CFG, $OUTPUT;
require_once($CFG->dirroot.'/calendar/event_form.php');
$html = '';
$data = null;
$eventid = isset($args['eventid']) ? clean_param($args['eventid'], PARAM_INT) : null;
$event = null;
$hasformdata = isset($args['formdata']) && !empty($args['formdata']);
$formoptions = [];
if ($hasformdata) {
parse_str(clean_param($args['formdata'], PARAM_TEXT), $data);
}
if (isset($args['haserror'])) {
$formoptions['haserror'] = clean_param($args['haserror'], PARAM_BOOL);
}
if (is_null($eventid)) {
$mform = new \core_calendar\local\event\forms\create(
null,
$formoptions,
'post',
'',
null,
true,
$data
);
} else {
$event = calendar_event::load($eventid);
$event->count_repeats();
$formoptions['event'] = $event;
$mform = new \core_calendar\local\event\forms\update(
null,
$formoptions,
'post',
'',
null,
true,
$data
);
}
if ($hasformdata) {
$mform->is_validated();
} else if (!is_null($event)) {
$mapper = new \core_calendar\local\event\mappers\create_update_form_mapper();
$data = $mapper->from_legacy_event_to_data($event);
$mform->set_data($data);
// Check to see if this event is part of a subscription or import.
// If so display a warning on edit.
if (isset($event->subscriptionid) && ($event->subscriptionid != null)) {
$renderable = new \core\output\notification(
get_string('eventsubscriptioneditwarning', 'calendar'),
\core\output\notification::NOTIFY_INFO
);
$html .= $OUTPUT->render($renderable);
}
}
$html .= $mform->render();
return $html;
}