Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

lib.php 10.2 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
5
6
7
8
<?php

/**
 * Library of interface functions and constants.
 *
 * @copyright   2019 Université Grenoble Alpes
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
9
10
use mod_labnbook\fetch;

Francois Gannaz's avatar
Francois Gannaz committed
11
\defined('MOODLE_INTERNAL') || die();
Francois Gannaz's avatar
Francois Gannaz committed
12

Francois Gannaz's avatar
Francois Gannaz committed
13
\define('LABNBOOK_TABLE', 'labnbook');
14
15
16

/* @var $DB moodle_database */

Francois Gannaz's avatar
Francois Gannaz committed
17
18
19
/**
 * Return if the plugin supports $feature.
 *
Francois Gannaz's avatar
Francois Gannaz committed
20
21
 * @param string $feature constant representing the feature
 * @return true | null True if the feature is supported, null otherwise
Francois Gannaz's avatar
Francois Gannaz committed
22
 */
Francois Gannaz's avatar
Francois Gannaz committed
23
24
function labnbook_supports($feature)
{
Francois Gannaz's avatar
Francois Gannaz committed
25
26
    $features = [
        //FEATURE_BACKUP_MOODLE2,
Francois Gannaz's avatar
Francois Gannaz committed
27
        //FEATURE_GRADE_HAS_GRADE,
Francois Gannaz's avatar
Francois Gannaz committed
28
29
30
31
32
        //FEATURE_GRADE_OUTCOMES,
        FEATURE_GROUPS,
        FEATURE_MOD_INTRO,
        FEATURE_SHOW_DESCRIPTION,
    ];
Francois Gannaz's avatar
Francois Gannaz committed
33
    return \in_array($feature, $features) ? true : null;
Francois Gannaz's avatar
Francois Gannaz committed
34
35
36
37
38
39
40
41
42
}

/**
 * Saves a new instance of the mod_labnbook into the database.
 *
 * Given an object containing all the necessary data, (defined by the form
 * in mod_form.php) this function will create a new instance and return the id
 * number of the instance.
 *
Francois Gannaz's avatar
Francois Gannaz committed
43
44
45
 * @param object $moduleinstance an object from the form
 * @param mod_labnbook_mod_form $mform the form
 * @return int the id of the newly inserted record
Francois Gannaz's avatar
Francois Gannaz committed
46
 */
Francois Gannaz's avatar
Francois Gannaz committed
47
48
function labnbook_add_instance($moduleinstance, $mform = null)
{
Francois Gannaz's avatar
Francois Gannaz committed
49
50
    global $DB;

51
52
    $fetcher = new fetch\user();
    $groupid = (int) $moduleinstance->groupmode === 0 ? null : (int) $moduleinstance->group;
53
54
55
56
57
58
    $teamconfig = [
        'method' => $moduleinstance->method,
        'teams_max' => $moduleinstance->teams_max,
        'size_opt' => $moduleinstance->size_opt,
        'size_max' => $moduleinstance->size_max,
        'size_min' => $moduleinstance->size_min,
Francois Gannaz's avatar
Francois Gannaz committed
59
60
        'start_datetime' => isset($moduleinstance->startdate) ? \date('Y-m-d H:i:s', $moduleinstance->startdate) : null,
        'end_datetime' => isset($moduleinstance->enddate) ? \date('Y-m-d H:i:s', $moduleinstance->enddate) : null,
61
    ];
62
    try {
63
64
65
66
67
68
69
        $teamConfig = $fetcher->useMission(
            (int) $moduleinstance->labnbook_missionid,
            (int) $moduleinstance->course,
            $mform->get_course()->shortname,
            $groupid,
            $teamconfig
        );
70
    } catch (\Exception $e) {
71
72
73
74
75
76
        $error = $fetcher->getLastError();
        if ($error->code == 409) {
            print_error(get_string('error_duplicate_teaming', 'labnbook'));
        } else {
            die("fatal error: " . $error->message);
        }
77
    }
78
79
80
81
82
    if ($teamConfig->id_team_config) {
        $moduleinstance->labnbook_teamconfigid = (int) $teamConfig->id_team_config;
    } else {
        throw new moodle_exception("LabnBook return an invalid response.");
    }
Francois Gannaz's avatar
Francois Gannaz committed
83

Francois Gannaz's avatar
Francois Gannaz committed
84
    $moduleinstance->timecreated = \time();
85
    $id = $DB->insert_record(LABNBOOK_TABLE, $moduleinstance);
Francois Gannaz's avatar
Francois Gannaz committed
86
87
88
89
90
91
92
93
94
95
96

    return $id;
}

/**
 * Updates an instance of the mod_labnbook in the database.
 *
 * Given an object containing all the necessary data (defined in mod_form.php),
 * this function will update an existing instance with new data.
 *
 * @param object $moduleinstance An object from the form in mod_form.php.
Francois Gannaz's avatar
Francois Gannaz committed
97
98
 * @param mod_labnbook_mod_form $mform the form
 * @return bool true if successful, false otherwise
Francois Gannaz's avatar
Francois Gannaz committed
99
 */
Francois Gannaz's avatar
Francois Gannaz committed
100
101
function labnbook_update_instance($moduleinstance, $mform = null)
{
Francois Gannaz's avatar
Francois Gannaz committed
102
103
    global $DB;

Francois Gannaz's avatar
Francois Gannaz committed
104
    $moduleinstance->timemodified = \time();
Francois Gannaz's avatar
Francois Gannaz committed
105
106
    $moduleinstance->id = $moduleinstance->instance;

107
108
    $fetcher = new fetch\user();
    $groupid = (int) $moduleinstance->groupmode === 0 ? null : (int) $moduleinstance->group;
109
110
111
112
113
114
115
    $teamconfig = [
        'id_team_config' => $moduleinstance->labnbook_teamconfigid,
        'method' => $moduleinstance->method,
        'teams_max' => $moduleinstance->teams_max,
        'size_opt' => $moduleinstance->size_opt,
        'size_max' => $moduleinstance->size_max,
        'size_min' => $moduleinstance->size_min,
Francois Gannaz's avatar
Francois Gannaz committed
116
117
        'start_datetime' => isset($moduleinstance->startdate) ? \date('Y-m-d H:i:s', $moduleinstance->startdate) : null,
        'end_datetime' => isset($moduleinstance->enddate) ? \date('Y-m-d H:i:s', $moduleinstance->enddate) : null,
118
    ];
119
    try {
120
        $fetcher->updateMissionUse(
121
122
123
124
125
            (int) $moduleinstance->labnbook_missionid,
            (int) $moduleinstance->course,
            $mform->get_course()->shortname,
            $groupid,
            $teamconfig
126
        );
127
128
129
130
    } catch (\Exception $e) {
        die("fatal error: " . $e->getMessage()); // TODO
    }

131
    return $DB->update_record(LABNBOOK_TABLE, $moduleinstance);
Francois Gannaz's avatar
Francois Gannaz committed
132
133
134
135
136
}

/**
 * Removes an instance of the mod_labnbook from the database.
 *
Francois Gannaz's avatar
Francois Gannaz committed
137
138
 * @param int $id id of the module instance
 * @return bool true if successful, false on failure
Francois Gannaz's avatar
Francois Gannaz committed
139
 */
Francois Gannaz's avatar
Francois Gannaz committed
140
141
function labnbook_delete_instance($id)
{
Francois Gannaz's avatar
Francois Gannaz committed
142
143
    global $DB;

David Beniamine's avatar
David Beniamine committed
144
145
146
147
148
149
150
    $record = $DB->get_record(LABNBOOK_TABLE, ['id' => $id]);
    if (!$record) {
        return false;
    }
    $fetcher = new fetch\user();
    if (!$fetcher->deleteTeamconfig($record->labnbook_teamconfigid)) {
        throw new moodle_exception("Cannot delete teamconfig : some students have started to work.");
Francois Gannaz's avatar
Francois Gannaz committed
151
152
153
        return false;
    }

Francois Gannaz's avatar
Francois Gannaz committed
154
    $DB->delete_records(LABNBOOK_TABLE, ['id' => $id]);
Francois Gannaz's avatar
Francois Gannaz committed
155
156
}

Francois Gannaz's avatar
Francois Gannaz committed
157
158
159
160
161
162
163
164
165
166
/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 *
 * See {@link get_array_of_activities()} in course/lib.php
 *
 * @param stdClass $coursemodule
 * @return cached_cm_info Info to customise main page display
 */
Francois Gannaz's avatar
Francois Gannaz committed
167
168
function labnbook_get_coursemodule_info($coursemodule)
{
Francois Gannaz's avatar
Francois Gannaz committed
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
    global $CFG, $DB;
    require_once("$CFG->libdir/resourcelib.php");

    $instance = $DB->get_record(
        LABNBOOK_TABLE,
        ['id' => $coursemodule->instance],
        'id, name, intro, introformat'
    );
    if (!$instance) {
        return null;
    }

    $info = new cached_cm_info();
    $info->name = $instance->name;
    if ($coursemodule->showdescription) {
        // Convert intro to html. Do not filter cached version, filters run at display time.
        $info->content = format_module_intro('labnbook', $instance, $coursemodule->id, false);
    }
    /*
    // Configured to open the activity in a new tab
    if ($instance->display === RESOURCELIB_DISPLAY_POPUP) {
        // ...
        $info->target = '_blank';
    }
     */

    return $info;
}

Francois Gannaz's avatar
Francois Gannaz committed
198
199
200
201
202
203
/**
 * Is a given scale used by the instance of mod_labnbook?
 *
 * This function returns if a scale is being used by one mod_labnbook
 * if it has support for grading and scales.
 *
Francois Gannaz's avatar
Francois Gannaz committed
204
205
206
 * @param int $moduleinstanceid ID of an instance of this module
 * @param int $scaleid ID of the scale
 * @return bool true if the scale is used by the given mod_labnbook instance
Francois Gannaz's avatar
Francois Gannaz committed
207
 */
Francois Gannaz's avatar
Francois Gannaz committed
208
209
function labnbook_scale_used($moduleinstanceid, $scaleid)
{
Francois Gannaz's avatar
Francois Gannaz committed
210
211
    global $DB;

Francois Gannaz's avatar
Francois Gannaz committed
212
    if ($scaleid && $DB->record_exists(LABNBOOK_TABLE, ['id' => $moduleinstanceid, 'grade' => -$scaleid])) {
Francois Gannaz's avatar
Francois Gannaz committed
213
214
        return true;
    }
Francois Gannaz's avatar
Francois Gannaz committed
215
    return false;
Francois Gannaz's avatar
Francois Gannaz committed
216
217
218
219
220
221
222
}

/**
 * Checks if scale is being used by any instance of mod_labnbook.
 *
 * This is used to find out if scale used anywhere.
 *
Francois Gannaz's avatar
Francois Gannaz committed
223
224
 * @param int $scaleid ID of the scale
 * @return bool true if the scale is used by any mod_labnbook instance
Francois Gannaz's avatar
Francois Gannaz committed
225
 */
Francois Gannaz's avatar
Francois Gannaz committed
226
227
function labnbook_scale_used_anywhere($scaleid)
{
Francois Gannaz's avatar
Francois Gannaz committed
228
229
    global $DB;

Francois Gannaz's avatar
Francois Gannaz committed
230
    if ($scaleid and $DB->record_exists(LABNBOOK_TABLE, ['grade' => -$scaleid])) {
Francois Gannaz's avatar
Francois Gannaz committed
231
232
        return true;
    }
Francois Gannaz's avatar
Francois Gannaz committed
233
    return false;
Francois Gannaz's avatar
Francois Gannaz committed
234
235
236
237
238
239
240
}

/**
 * Creates or updates grade item for the given mod_labnbook instance.
 *
 * Needed by {@link grade_update_mod_grades()}.
 *
Francois Gannaz's avatar
Francois Gannaz committed
241
242
243
 * @param stdClass $moduleinstance instance object with extra cmidnumber and modname property
 * @param bool $reset reset grades in the gradebook
 * @return void
Francois Gannaz's avatar
Francois Gannaz committed
244
 */
Francois Gannaz's avatar
Francois Gannaz committed
245
246
function labnbook_grade_item_update($moduleinstance, $reset=false)
{
Francois Gannaz's avatar
Francois Gannaz committed
247
    global $CFG;
Francois Gannaz's avatar
Francois Gannaz committed
248
    require_once($CFG->libdir . '/gradelib.php');
Francois Gannaz's avatar
Francois Gannaz committed
249

Francois Gannaz's avatar
Francois Gannaz committed
250
    $item = [];
Francois Gannaz's avatar
Francois Gannaz committed
251
252
253
254
255
256
257
    $item['itemname'] = clean_param($moduleinstance->name, PARAM_NOTAGS);
    $item['gradetype'] = GRADE_TYPE_VALUE;

    if ($moduleinstance->grade > 0) {
        $item['gradetype'] = GRADE_TYPE_VALUE;
        $item['grademax']  = $moduleinstance->grade;
        $item['grademin']  = 0;
Francois Gannaz's avatar
Francois Gannaz committed
258
    } elseif ($moduleinstance->grade < 0) {
Francois Gannaz's avatar
Francois Gannaz committed
259
260
261
262
263
264
265
266
267
        $item['gradetype'] = GRADE_TYPE_SCALE;
        $item['scaleid']   = -$moduleinstance->grade;
    } else {
        $item['gradetype'] = GRADE_TYPE_NONE;
    }
    if ($reset) {
        $item['reset'] = true;
    }

268
    grade_update('/mod/labnbook', $moduleinstance->course, 'mod', LABNBOOK_TABLE, $moduleinstance->id, 0, null, $item);
Francois Gannaz's avatar
Francois Gannaz committed
269
270
271
272
273
}

/**
 * Delete grade item for given mod_labnbook instance.
 *
Francois Gannaz's avatar
Francois Gannaz committed
274
275
 * @param stdClass $moduleinstance instance object
 * @return grade_item
Francois Gannaz's avatar
Francois Gannaz committed
276
 */
Francois Gannaz's avatar
Francois Gannaz committed
277
278
function labnbook_grade_item_delete($moduleinstance)
{
Francois Gannaz's avatar
Francois Gannaz committed
279
    global $CFG;
Francois Gannaz's avatar
Francois Gannaz committed
280
    require_once($CFG->libdir . '/gradelib.php');
Francois Gannaz's avatar
Francois Gannaz committed
281

Francois Gannaz's avatar
Francois Gannaz committed
282
283
284
285
286
287
288
289
290
291
    return grade_update(
        '/mod/labnbook',
        $moduleinstance->course,
        'mod',
        LABNBOOK_TABLE,
        $moduleinstance->id,
        0,
        null,
        ['deleted' => 1]
    );
Francois Gannaz's avatar
Francois Gannaz committed
292
293
294
295
296
297
298
}

/**
 * Update mod_labnbook grades in the gradebook.
 *
 * Needed by {@link grade_update_mod_grades()}.
 *
Francois Gannaz's avatar
Francois Gannaz committed
299
300
 * @param stdClass $moduleinstance instance object with extra cmidnumber and modname property
 * @param int $userid update grade of specific user only, 0 means all participants
Francois Gannaz's avatar
Francois Gannaz committed
301
 */
Francois Gannaz's avatar
Francois Gannaz committed
302
303
function labnbook_update_grades($moduleinstance, $userid = 0)
{
Francois Gannaz's avatar
Francois Gannaz committed
304
    global $CFG;
Francois Gannaz's avatar
Francois Gannaz committed
305
    require_once($CFG->libdir . '/gradelib.php');
Francois Gannaz's avatar
Francois Gannaz committed
306
307

    // Populate array of grade objects indexed by userid.
Francois Gannaz's avatar
Francois Gannaz committed
308
    $grades = [];
309
    grade_update('/mod/labnbook', $moduleinstance->course, 'mod', LABNBOOK_TABLE, $moduleinstance->id, 0, $grades);
Francois Gannaz's avatar
Francois Gannaz committed
310
311
312
313
314
315
316
}

/**
 * Extends the global navigation tree by adding mod_labnbook nodes if there is a relevant content.
 *
 * This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
 *
Francois Gannaz's avatar
Francois Gannaz committed
317
 * @param navigation_node $labnbooknode an object representing the navigation tree node
Francois Gannaz's avatar
Francois Gannaz committed
318
319
320
 * @param stdClass $course
 * @param stdClass $module
 * @param cm_info $cm
Francois Gannaz's avatar
Francois Gannaz committed
321
 */
Francois Gannaz's avatar
Francois Gannaz committed
322
323
function labnbook_extend_navigation($labnbooknode, $course, $module, $cm)
{
Francois Gannaz's avatar
Francois Gannaz committed
324
325
326
327
328
329
330
331
332
333
334
}

/**
 * Extends the settings navigation with the mod_labnbook settings.
 *
 * This function is called when the context for the page is a mod_labnbook module.
 * This is not called by AJAX so it is safe to rely on the $PAGE.
 *
 * @param settings_navigation $settingsnav {@link settings_navigation}
 * @param navigation_node $labnbooknode {@link navigation_node}
 */
Francois Gannaz's avatar
Francois Gannaz committed
335
336
function labnbook_extend_settings_navigation($settingsnav, $labnbooknode = null)
{
Francois Gannaz's avatar
Francois Gannaz committed
337
}