From 45d00aab43dfcbd47d2cc06c58eab4b1231154d5 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 16 Mar 2017 14:01:35 +0800 Subject: [PATCH] MDL-57878 core_completion: added expected completion date function Part of MDL-55611 epic. --- completion/classes/api.php | 113 +++++++++++++++++++++++++++++++++++++ lang/en/completion.php | 3 +- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 completion/classes/api.php diff --git a/completion/classes/api.php b/completion/classes/api.php new file mode 100644 index 00000000000..190a6268a7a --- /dev/null +++ b/completion/classes/api.php @@ -0,0 +1,113 @@ +. + +/** + * Contains class containing completion API. + * + * @package core_completion + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_completion; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class containing completion API. + * + * @package core_completion + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class api { + + /** + * @var string The completion expected on event. + */ + const COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED = 'expectcompletionon'; + + /** + * Creates, updates or deletes an event for the expected completion date. + * + * @param int $cmid The course module id + * @param string $modulename The name of the module (eg. assign, quiz) + * @param int $instanceid The instance idLOL + * @param int|null $completionexpectedtime The time completion is expected, null if not set + * @return bool + */ + public static function update_completion_date_event($cmid, $modulename, $instanceid, $completionexpectedtime) { + global $DB; + + $instance = $DB->get_record($modulename, array('id' => $instanceid), '*', MUST_EXIST); + $course = $DB->get_record('course', array('id' => $instance->course), '*', MUST_EXIST); + + $completion = new \completion_info($course); + + // Can not create/update an event if completion is disabled. + if (!$completion->is_enabled() && $completionexpectedtime !== null) { + return true; + } + + // Create the \stdClass we will be using for our language strings. + $lang = new \stdClass(); + $lang->modulename = get_string('pluginname', $modulename); + $lang->instancename = $instance->name; + + // Create the calendar event. + $event = new \stdClass(); + $event->type = CALENDAR_EVENT_TYPE_ACTION; + $event->eventtype = self::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED; + if ($event->id = $DB->get_field('event', 'id', array('modulename' => $modulename, + 'instance' => $instance->id, 'eventtype' => $event->eventtype))) { + if ($completionexpectedtime !== null) { + // Calendar event exists so update it. + $event->name = get_string('completionexpectedfor', 'completion', $lang); + $event->description = format_module_intro($modulename, $instance, $cmid); + $event->timestart = $completionexpectedtime; + $event->timesort = $completionexpectedtime; + $event->visible = instance_is_visible($modulename, $instance); + $event->timeduration = 0; + + $calendarevent = \core_calendar\event::load($event->id); + $calendarevent->update($event); + } else { + // Calendar event is no longer needed. + $calendarevent = \core_calendar\event::load($event->id); + $calendarevent->delete(); + } + } else { + // Event doesn't exist so create one. + if ($completionexpectedtime !== null) { + $event->name = get_string('completionexpectedfor', 'completion', $lang); + $event->description = format_module_intro($modulename, $instance, $cmid); + $event->courseid = $instance->course; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = $modulename; + $event->instance = $instance->id; + $event->timestart = $completionexpectedtime; + $event->timesort = $completionexpectedtime; + $event->visible = instance_is_visible($modulename, $instance); + $event->timeduration = 0; + + \core_calendar\event::create($event); + } + } + + return true; + } +} diff --git a/lang/en/completion.php b/lang/en/completion.php index 11214569ed0..41bc6c2656e 100644 --- a/lang/en/completion.php +++ b/lang/en/completion.php @@ -67,7 +67,8 @@ $string['completiondefault'] = 'Default completion tracking'; $string['completiondisabled'] = 'Disabled, not shown in activity settings'; $string['completionenabled'] = 'Enabled, control via completion and activity settings'; $string['completionexpected'] = 'Expect completed on'; -$string['completionexpected_help'] = 'This setting specifies the date when the activity is expected to be completed. The date is not shown to students and is only displayed in the activity completion report.'; +$string['completionexpected_help'] = 'This setting specifies the date when the activity is expected to be completed.'; +$string['completionexpectedfor'] = 'Expected completion for \'{$a->modulename}\' activity \'{$a->instancename}\''; $string['completionicons'] = 'Completion tick boxes'; $string['completionicons_help'] = 'A tick next to an activity name may be used to indicate when the activity is complete.