Merge branch 'MDL-53140-master' of git://github.com/rezaies/moodle
[moodle.git] / completion / classes / api.php
CommitLineData
45d00aab
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains class containing completion API.
19 *
20 * @package core_completion
21 * @copyright 2017 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_completion;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Class containing completion API.
31 *
32 * @package core_completion
33 * @copyright 2017 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class api {
37
38 /**
39 * @var string The completion expected on event.
40 */
41 const COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED = 'expectcompletionon';
42
43 /**
44 * Creates, updates or deletes an event for the expected completion date.
45 *
46 * @param int $cmid The course module id
47 * @param string $modulename The name of the module (eg. assign, quiz)
65f15cd2 48 * @param \stdClass|int $instanceorid The instance object or ID.
45d00aab
MN
49 * @param int|null $completionexpectedtime The time completion is expected, null if not set
50 * @return bool
51 */
1930749f 52 public static function update_completion_date_event($cmid, $modulename, $instanceorid, $completionexpectedtime) {
4477520e
MN
53 global $CFG, $DB;
54
55 // Required for calendar constant CALENDAR_EVENT_TYPE_ACTION.
56 require_once($CFG->dirroot . '/calendar/lib.php');
45d00aab 57
1930749f
AG
58 $instance = null;
59 if (is_object($instanceorid)) {
60 $instance = $instanceorid;
61 } else {
a66de3ca
DW
62 $instance = $DB->get_record($modulename, array('id' => $instanceorid), '*', IGNORE_MISSING);
63 }
64 if (!$instance) {
65 return false;
1930749f
AG
66 }
67 $course = get_course($instance->course);
45d00aab
MN
68
69 $completion = new \completion_info($course);
70
71 // Can not create/update an event if completion is disabled.
72 if (!$completion->is_enabled() && $completionexpectedtime !== null) {
73 return true;
74 }
75
76 // Create the \stdClass we will be using for our language strings.
77 $lang = new \stdClass();
78 $lang->modulename = get_string('pluginname', $modulename);
79 $lang->instancename = $instance->name;
80
81 // Create the calendar event.
82 $event = new \stdClass();
83 $event->type = CALENDAR_EVENT_TYPE_ACTION;
84 $event->eventtype = self::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED;
85 if ($event->id = $DB->get_field('event', 'id', array('modulename' => $modulename,
86 'instance' => $instance->id, 'eventtype' => $event->eventtype))) {
87 if ($completionexpectedtime !== null) {
88 // Calendar event exists so update it.
89 $event->name = get_string('completionexpectedfor', 'completion', $lang);
90 $event->description = format_module_intro($modulename, $instance, $cmid);
91 $event->timestart = $completionexpectedtime;
92 $event->timesort = $completionexpectedtime;
93 $event->visible = instance_is_visible($modulename, $instance);
94 $event->timeduration = 0;
95
e1cd93ce 96 $calendarevent = \calendar_event::load($event->id);
2363b99e 97 $calendarevent->update($event, false);
45d00aab
MN
98 } else {
99 // Calendar event is no longer needed.
e1cd93ce 100 $calendarevent = \calendar_event::load($event->id);
45d00aab
MN
101 $calendarevent->delete();
102 }
103 } else {
104 // Event doesn't exist so create one.
105 if ($completionexpectedtime !== null) {
106 $event->name = get_string('completionexpectedfor', 'completion', $lang);
107 $event->description = format_module_intro($modulename, $instance, $cmid);
108 $event->courseid = $instance->course;
109 $event->groupid = 0;
110 $event->userid = 0;
111 $event->modulename = $modulename;
112 $event->instance = $instance->id;
113 $event->timestart = $completionexpectedtime;
114 $event->timesort = $completionexpectedtime;
115 $event->visible = instance_is_visible($modulename, $instance);
116 $event->timeduration = 0;
117
2363b99e 118 \calendar_event::create($event, false);
45d00aab
MN
119 }
120 }
121
122 return true;
123 }
124}