Merge branch 'MDL-73646-master-v2' of https://github.com/sharidas/moodle
[moodle.git] / mod / choice / locallib.php
bloba00641d034add90befb5ce072cd39de42459fcd3
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, false);
56 $event->format = FORMAT_HTML;
57 $event->timestart = $choice->timeopen;
58 $event->timesort = $choice->timeopen;
59 $event->visible = instance_is_visible('choice', $choice);
60 $event->timeduration = 0;
61 $calendarevent = calendar_event::load($event->id);
62 $calendarevent->update($event, false);
63 } else {
64 // Calendar event is on longer needed.
65 $calendarevent = calendar_event::load($event->id);
66 $calendarevent->delete();
68 } else {
69 // Event doesn't exist so create one.
70 if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
71 $event->name = get_string('calendarstart', 'choice', $choice->name);
72 $event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
73 $event->format = FORMAT_HTML;
74 $event->courseid = $choice->course;
75 $event->groupid = 0;
76 $event->userid = 0;
77 $event->modulename = 'choice';
78 $event->instance = $choice->id;
79 $event->timestart = $choice->timeopen;
80 $event->timesort = $choice->timeopen;
81 $event->visible = instance_is_visible('choice', $choice);
82 $event->timeduration = 0;
83 calendar_event::create($event, false);
87 // Choice end calendar events.
88 $event = new stdClass();
89 $event->type = CALENDAR_EVENT_TYPE_ACTION;
90 $event->eventtype = CHOICE_EVENT_TYPE_CLOSE;
91 if ($event->id = $DB->get_field('event', 'id',
92 array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => $event->eventtype))) {
93 if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
94 // Calendar event exists so update it.
95 $event->name = get_string('calendarend', 'choice', $choice->name);
96 $event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
97 $event->format = FORMAT_HTML;
98 $event->timestart = $choice->timeclose;
99 $event->timesort = $choice->timeclose;
100 $event->visible = instance_is_visible('choice', $choice);
101 $event->timeduration = 0;
102 $calendarevent = calendar_event::load($event->id);
103 $calendarevent->update($event, false);
104 } else {
105 // Calendar event is on longer needed.
106 $calendarevent = calendar_event::load($event->id);
107 $calendarevent->delete();
109 } else {
110 // Event doesn't exist so create one.
111 if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
112 $event->name = get_string('calendarend', 'choice', $choice->name);
113 $event->description = format_module_intro('choice', $choice, $choice->coursemodule, false);
114 $event->format = FORMAT_HTML;
115 $event->courseid = $choice->course;
116 $event->groupid = 0;
117 $event->userid = 0;
118 $event->modulename = 'choice';
119 $event->instance = $choice->id;
120 $event->timestart = $choice->timeclose;
121 $event->timesort = $choice->timeclose;
122 $event->visible = instance_is_visible('choice', $choice);
123 $event->timeduration = 0;
124 calendar_event::create($event, false);