MDL-63086 block_rss_client: Move skip time calculation to task
[moodle.git] / mod / choice / locallib.php
blobab33373c14ef53f9c945a1b43c936e7bb6e6b5ab
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.
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 * 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!
22 * @package mod_choice
23 * @copyright 2016 Stephen Bourget
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * This creates new calendar events given as timeopen and timeclose by $choice.
31 * @param stdClass $choice
32 * @return void
34 function choice_set_events($choice) {
35 global $DB, $CFG;
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);
62 } else {
63 // Calendar event is on longer needed.
64 $calendarevent = calendar_event::load($event->id);
65 $calendarevent->delete();
67 } else {
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;
73 $event->groupid = 0;
74 $event->userid = 0;
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);
101 } else {
102 // Calendar event is on longer needed.
103 $calendarevent = calendar_event::load($event->id);
104 $calendarevent->delete();
106 } else {
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;
112 $event->groupid = 0;
113 $event->userid = 0;
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);