Merge branch 'MDL-56627-master' of git://github.com/damyon/moodle
[moodle.git] / mod / choice / locallib.php
blob4dd6418eae06335011f3ab6ab3368c0b65173f30
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;
44 // Choice start calendar events.
45 $event = new stdClass();
46 if ($event->id = $DB->get_field('event', 'id',
47 array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'open'))) {
48 if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
49 // Calendar event exists so update it.
50 $event->name = get_string('calendarstart', 'choice', $choice->name);
51 $event->description = format_module_intro('choice', $choice, $choice->coursemodule);
52 $event->timestart = $choice->timeopen;
53 $event->visible = instance_is_visible('choice', $choice);
54 $event->timeduration = 0;
55 $calendarevent = calendar_event::load($event->id);
56 $calendarevent->update($event);
57 } else {
58 // Calendar event is on longer needed.
59 $calendarevent = calendar_event::load($event->id);
60 $calendarevent->delete();
62 } else {
63 // Event doesn't exist so create one.
64 if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) {
65 $event->name = get_string('calendarstart', 'choice', $choice->name);
66 $event->description = format_module_intro('choice', $choice, $choice->coursemodule);
67 $event->courseid = $choice->course;
68 $event->groupid = 0;
69 $event->userid = 0;
70 $event->modulename = 'choice';
71 $event->instance = $choice->id;
72 $event->eventtype = 'open';
73 $event->timestart = $choice->timeopen;
74 $event->visible = instance_is_visible('choice', $choice);
75 $event->timeduration = 0;
76 calendar_event::create($event);
80 // Choice end calendar events.
81 $event = new stdClass();
82 if ($event->id = $DB->get_field('event', 'id',
83 array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'close'))) {
84 if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
85 // Calendar event exists so update it.
86 $event->name = get_string('calendarend', 'choice', $choice->name);
87 $event->description = format_module_intro('choice', $choice, $choice->coursemodule);
88 $event->timestart = $choice->timeclose;
89 $event->visible = instance_is_visible('choice', $choice);
90 $event->timeduration = 0;
91 $calendarevent = calendar_event::load($event->id);
92 $calendarevent->update($event);
93 } else {
94 // Calendar event is on longer needed.
95 $calendarevent = calendar_event::load($event->id);
96 $calendarevent->delete();
98 } else {
99 // Event doesn't exist so create one.
100 if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) {
101 $event = new stdClass();
102 $event->name = get_string('calendarend', 'choice', $choice->name);
103 $event->description = format_module_intro('choice', $choice, $choice->coursemodule);
104 $event->courseid = $choice->course;
105 $event->groupid = 0;
106 $event->userid = 0;
107 $event->modulename = 'choice';
108 $event->instance = $choice->id;
109 $event->eventtype = 'close';
110 $event->timestart = $choice->timeclose;
111 $event->visible = instance_is_visible('choice', $choice);
112 $event->timeduration = 0;
113 calendar_event::create($event);