-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
236 lines (203 loc) · 7.13 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Common functions for typorepo module.
*
* @package mod_typorepo
* @copyright 2020 bdecent gmbh <https://bdecent.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/filelib.php');
/**
* List of features supported in typorepo module
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, false if not, null if doesn't know
*/
function typorepo_supports($feature) {
switch($feature) {
case FEATURE_MOD_ARCHETYPE:
return MOD_ARCHETYPE_RESOURCE;
case FEATURE_GROUPS:
return false;
case FEATURE_GROUPINGS:
return false;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* List of view style log actions
* @return array
*/
function typorepo_get_view_actions() {
return ['view', 'view all'];
}
/**
* List of update style log actions
* @return array
*/
function typorepo_get_post_actions() {
return ['update', 'add'];
}
/**
* Add new instance of typorepo.
*
* @param \stdClass $instance
* @return bool|int
* @throws dml_exception
*/
function typorepo_add_instance($instance) {
global $DB;
$instance->timemodified = time();
$instance->timecreated = time();
return $DB->insert_record('typorepo', $instance);
}
/**
* Update an existing instance of typorepo.
*
* @param \stdClass $instance
* @return bool
* @throws dml_exception
*/
function typorepo_update_instance($instance) {
global $DB;
$instance->id = $instance->instance;
$instance->timemodified = time();
return $DB->update_record('typorepo', $instance);
}
/**
* Delete an instance of typorepo.
*
* @param int $id
* @return bool
* @throws dml_exception
*/
function typorepo_delete_instance($id) {
global $DB;
if (!$instance = $DB->get_record('typorepo', ['id' => $id])) {
return false;
}
return $DB->delete_records('typorepo', ['id' => $instance->id]);
}
/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $typorepo typorepo object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @throws coding_exception
* @since Moodle 3.0
*/
function typorepo_view($typorepo, $course, $cm, $context) {
// Trigger course_module_viewed event.
$params = [
'context' => $context,
'objectid' => $typorepo->id
];
$event = \mod_typorepo\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('typorepo', $typorepo);
$event->trigger();
// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
}
/**
* Sets dynamic information about a course module
*
* This function is called from cm_info when displaying the module
* mod_folder can be displayed inline on course page and therefore have no course link
*
* @param cm_info $cm
* @throws dml_exception
* @throws coding_exception
*/
function typorepo_cm_info_dynamic(cm_info $cm) {
global $PAGE, $DB, $USER, $OUTPUT;
// Ensure we are on the course view page. This was throwing an error when viewing the module
// because OUTPUT was being used.
if (!$PAGE->context || $PAGE->context->contextlevel != CONTEXT_COURSE) {
return;
}
$instance = $DB->get_record('typorepo', ['id' => $cm->instance], '*', MUST_EXIST);
if ($instance->label_mode) {
// Calculate the url. -- anonymised version with no user data used except for the technical ID from moodle.
$time = time();
$token = md5($USER->id . $USER->id . $USER->id . $cm->course . $time . $USER->id .
get_config('typorepo', 'secret'));
$fullurl = $instance->url . '&token=' . $token . '&time=' . $time . '&moodlemodid=' . $cm->id . '&login=' .
base64_encode($USER->id) . '&firstname=' . base64_encode($USER->id) . '&lastname=' .
base64_encode($USER->id) . '&courseid=' . $cm->course . '&email=' . base64_encode($USER->id);
$iframe = '<iframe style="margin-left: 0px;" src="' . $fullurl . '" frameborder="0" scrolling="' .
get_config('typorepo', 'scrolling') . '" width="100%" height="' .
get_config('typorepo', 'height') . '"> </iframe>';
$content = $OUTPUT->render_from_template('mod_typorepo/view', [
'instance' => $instance,
'cmid' => $cm->id,
'iframe' => $iframe
]);
typorepo_view($instance, $PAGE->course, $cm, context_module::instance($cm->id));
$cm->set_no_view_link();
$cm->set_extra_classes('label_mode');
$cm->set_content($content);
}
}
/**
* Given a course_module object, this function returns any
* "extra" information that may be needed when printing
* this activity in a course listing.
*
* See get_array_of_activities() in course/lib.php
*
* @param stdClass $coursemodule
* @return cached_cm_info info
*/
function typorepo_get_coursemodule_info($coursemodule) {
global $CFG, $DB;
if (!$typorepo = $DB->get_record('typorepo', ['id' => $coursemodule->instance])) {
return null;
}
$info = new cached_cm_info();
$info->name = $typorepo->name;
if ($coursemodule->showdescription) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$info->content = format_module_intro('typorepo', $typorepo, $coursemodule->id, false);
}
if ($typorepo->options == 'newwindow') {
$fullurl = "$CFG->wwwroot/mod/typorepo/view.php?id=$coursemodule->id&redirect=1";
$width = get_config('typorepo', 'width');
$height = get_config('typorepo', 'height');
$wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no," .
"scrollbars=yes,resizable=yes";
$info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
}
return $info;
}