-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.php
125 lines (111 loc) · 3.67 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
<?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/>.
/**
* Library of functions for local_differentiator.
*
* @package local_differentiator
* @copyright 2019 Luca Bösch <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Saves a new differentiator instance 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 new instance.
*
* @param object $differentiator an object from the form in mod_form.php
* @return int the id of the newly inserted record
*/
function differentiator_add_instance($differentiator) {
global $DB;
$differentiator->timecreated = time();
$differentiator->timemodified = time();
$id = $DB->insert_record('differentiator', $differentiator);
return $id;
}
/**
* Updates a differentiator instance.
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $differentiator an object from the form in mod_form.php
* @return boolean Success/Fail
*/
function differentiator_update_instance($differentiator) {
global $DB;
$differentiator->timemodified = time();
$differentiator->id = $differentiator->instance;
$ret = $DB->update_record('differentiator', $differentiator);
return $ret;
}
/**
* List of features supported in differentiator.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return bool True if feature is supported
*/
function differentiator_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return false;
case FEATURE_GROUPINGS:
return false;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* View or submit an mform.
*
* Returns the HTML to view an mform.
* If form data is delivered and the data is valid, this returns 'ok'.
*
* @param array $args
* @return string
* @throws moodle_exception
*/
function local_differentiator_output_fragment_mform($args) {
$differentiator = new \local_differentiator\differentiator();
$formdata = [];
if (!empty($args['jsonformdata'])) {
$serialiseddata = json_decode($args['jsonformdata']);
if (is_string($serialiseddata)) {
parse_str($serialiseddata, $formdata);
}
}
$moreargs = (isset($args['moreargs'])) ? json_decode($args['moreargs']) : new stdClass;
$formname = $args['form'] ?? '';
$form = \local_differentiator\form\form_controller::get_controller($formname, $differentiator, $formdata, $moreargs);
if ($form->success()) {
$ret = 'ok';
if ($msg = $form->get_message()) {
$ret .= ' ' . $msg;
}
return $ret;
} else {
return $form->render();
}
}