-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
193 lines (165 loc) · 6.49 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
<?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/>.
/**
* Moodle interface library for masks
*
* @copyright 2016 Edunao SAS ([email protected])
* @author Sadge ([email protected])
* @package mod_masks
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// this is lib.php - add code here for interfacing this module to Moodle internals
defined('MOODLE_INTERNAL') || die;
/**
* List of features supported in masks 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 masks_supports($feature) {
switch($feature) {
case FEATURE_GROUPS: return false;
case FEATURE_GROUPINGS: return false;
case FEATURE_GROUPMEMBERSONLY: return false;
case FEATURE_MOD_INTRO: return false;
case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_GRADE_OUTCOMES: return false;
case FEATURE_BACKUP_MOODLE2: return true;
case FEATURE_SHOW_DESCRIPTION: return false;
default: return null;
}
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
* @param $data the data submitted from the reset course.
* @return array status array
*/
function masks_reset_userdata($data) {
global $DB;
// Delete records from user_state
$subSelect = "SELECT q.id FROM {course_modules} cm JOIN {masks_question} q ON q.parentcm=cm.id WHERE cm.course = ?";
$DB->delete_records_select('masks_user_state', "question IN ($subSelect)", array($data->courseid));
return array();
}
/**
* Add masks instance.
* @param object $data
* @param object $mform
* @return int new url instance id
*/
function masks_add_instance($data, $mform) {
global $CFG, $DB;
// write new record to the database
$data->id = $DB->insert_record('masks', $data);
// add the 2 grading columns to the grade book
require_once($CFG->libdir.'/gradelib.php');
$graderesult = grade_update('mod/masks', $data->course, 'mod', 'masks', $data->id, 0, null, array( 'itemname' => $data->name ) );
if ( $graderesult != GRADE_UPDATE_OK ){
throw new \moodle_exception('Failed to set gradebook meta data: for new masks instance');
}
return $data->id;
}
/**
* Update masks instance.
* @param object $data
* @param object $mform
* @return bool true
*/
function masks_update_instance($data, $mform) {
global $CFG, $DB;
$parameters = array();
for ($i = 0; $i < 100; $i++) {
$parameter = "parameter_$i";
$variable = "variable_$i";
if (empty($data->$parameter) or empty($data->$variable)) {
continue;
}
$parameters[$data->$parameter] = $data->$variable;
}
$data->parameters = serialize($parameters);
$data->timemodified = time();
$data->id = $data->instance;
$DB->update_record('masks', $data);
// update the 2 grading columns for the grade book
require_once($CFG->libdir.'/gradelib.php');
$graderesult = grade_update('mod/masks', $data->course, 'mod', 'masks', $data->id, 0, null, array( 'itemname' => $data->name ) );
if ( $graderesult != GRADE_UPDATE_OK ){
echo "Grade Result: $graderesult\n";
echo "Data:\n";
print_r($data);
throw new \moodle_exception('Failed to set gradebook meta data: for new masks instance');
}
return true;
}
/**
* Delete masks instance.
* @param int $id
* @return bool true
*/
function masks_delete_instance($id) {
global $DB;
if (!$instance = $DB->get_record('masks', array('id' => $id))) {
return false;
}
if (!$cm = get_coursemodule_from_instance('masks', $id)) {
return false;
}
// Delete records from question, mask and user_state
$subSelect = "SELECT id FROM {masks_question} WHERE parentcm = ?";
$DB->delete_records_select('masks_user_state', "question IN ($subSelect)", array($cm->id));
$DB->delete_records_select('masks_mask', "question IN ($subSelect)", array($cm->id));
$DB->delete_records('masks_question', array('parentcm' => $cm->id));
// Delete records from doc_page, page and doc
$subSelect = "SELECT id FROM {masks_doc} WHERE parentcm = ?";
$DB->delete_records('masks_page', array('parentcm' => $cm->id));
$DB->delete_records_select('masks_doc_page', "doc IN ($subSelect)", array($cm->id));
$DB->delete_records('masks_doc', array('parentcm' => $cm->id));
// note: all context files are deleted automatically
$DB->delete_records('masks', array('id' => $id));
return true;
}
/**
* Serve image files
*
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - just send the file
*/
function mod_masks_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login( $course, true, $cm );
$relativepath = implode( '/', $args );
$fullpath = "/$context->id/mod_masks/masks_doc_page/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash( sha1( $fullpath ) );
if ( !$file or $file->is_directory() ) {
if ($filearea === 'content') { //return file not found straight away to improve performance.
send_header_404();
die;
}
return false;
}
// finally send the file
$lifetime = null;
send_stored_file($file, $lifetime, 0, false, $options);
}