2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Internal library of functions for choice module.
19 * All the choice specific functions, needed to implement the module
20 * logic, should go here. Never include this file from your lib.php!
23 * @copyright 2016 Stephen Bourget
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * This creates new calendar events given as timeopen and timeclose by $choice.
31 * @param stdClass $choice
34 function choice_set_events($choice) {
37 require_once($CFG->dirroot
.'/calendar/lib.php');
39 // Get CMID if not sent as part of $choice.
40 if (!isset($choice->coursemodule
)) {
41 $cm = get_coursemodule_from_instance('choice', $choice->id
, $choice->course
);
42 $choice->coursemodule
= $cm->id
;
45 // Choice start calendar events.
46 $event = new stdClass();
47 $event->eventtype
= CHOICE_EVENT_TYPE_OPEN
;
48 // The CHOICE_EVENT_TYPE_OPEN event should only be an action event if no close time is specified.
49 $event->type
= empty($choice->timeclose
) ? CALENDAR_EVENT_TYPE_ACTION
: CALENDAR_EVENT_TYPE_STANDARD
;
50 if ($event->id
= $DB->get_field('event', 'id',
51 array('modulename' => 'choice', 'instance' => $choice->id
, 'eventtype' => $event->eventtype
))) {
52 if ((!empty($choice->timeopen
)) && ($choice->timeopen
> 0)) {
53 // Calendar event exists so update it.
54 $event->name
= get_string('calendarstart', 'choice', $choice->name
);
55 $event->description
= format_module_intro('choice', $choice, $choice->coursemodule
);
56 $event->timestart
= $choice->timeopen
;
57 $event->timesort
= $choice->timeopen
;
58 $event->visible
= instance_is_visible('choice', $choice);
59 $event->timeduration
= 0;
60 $calendarevent = calendar_event
::load($event->id
);
61 $calendarevent->update($event);
63 // Calendar event is on longer needed.
64 $calendarevent = calendar_event
::load($event->id
);
65 $calendarevent->delete();
68 // Event doesn't exist so create one.
69 if ((!empty($choice->timeopen
)) && ($choice->timeopen
> 0)) {
70 $event->name
= get_string('calendarstart', 'choice', $choice->name
);
71 $event->description
= format_module_intro('choice', $choice, $choice->coursemodule
);
72 $event->courseid
= $choice->course
;
75 $event->modulename
= 'choice';
76 $event->instance
= $choice->id
;
77 $event->timestart
= $choice->timeopen
;
78 $event->timesort
= $choice->timeopen
;
79 $event->visible
= instance_is_visible('choice', $choice);
80 $event->timeduration
= 0;
81 calendar_event
::create($event);
85 // Choice end calendar events.
86 $event = new stdClass();
87 $event->type
= CALENDAR_EVENT_TYPE_ACTION
;
88 $event->eventtype
= CHOICE_EVENT_TYPE_CLOSE
;
89 if ($event->id
= $DB->get_field('event', 'id',
90 array('modulename' => 'choice', 'instance' => $choice->id
, 'eventtype' => $event->eventtype
))) {
91 if ((!empty($choice->timeclose
)) && ($choice->timeclose
> 0)) {
92 // Calendar event exists so update it.
93 $event->name
= get_string('calendarend', 'choice', $choice->name
);
94 $event->description
= format_module_intro('choice', $choice, $choice->coursemodule
);
95 $event->timestart
= $choice->timeclose
;
96 $event->timesort
= $choice->timeclose
;
97 $event->visible
= instance_is_visible('choice', $choice);
98 $event->timeduration
= 0;
99 $calendarevent = calendar_event
::load($event->id
);
100 $calendarevent->update($event);
102 // Calendar event is on longer needed.
103 $calendarevent = calendar_event
::load($event->id
);
104 $calendarevent->delete();
107 // Event doesn't exist so create one.
108 if ((!empty($choice->timeclose
)) && ($choice->timeclose
> 0)) {
109 $event->name
= get_string('calendarend', 'choice', $choice->name
);
110 $event->description
= format_module_intro('choice', $choice, $choice->coursemodule
);
111 $event->courseid
= $choice->course
;
114 $event->modulename
= 'choice';
115 $event->instance
= $choice->id
;
116 $event->timestart
= $choice->timeclose
;
117 $event->timesort
= $choice->timeclose
;
118 $event->visible
= instance_is_visible('choice', $choice);
119 $event->timeduration
= 0;
120 calendar_event
::create($event);