-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrading_template.php
More file actions
executable file
·189 lines (158 loc) · 5.77 KB
/
grading_template.php
File metadata and controls
executable file
·189 lines (158 loc) · 5.77 KB
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
<?php
// grade.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/>.
/**
* Redirects the user to either a logic or to the logic statistics
*
* @package mod_logic
* @category grade
* @copyright 2023 Dan Nessett <dnessett@yahoo.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
/**
* Require config.php
*/
global $DB;
require_once("../../config.php");
$id = required_param('id', PARAM_INT);
$cm = get_coursemodule_from_id('logic', $id, 0, false, MUST_EXIST);
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
$logic = new logic($DB->get_record('logic', array('id' => $cm->instance), '*', MUST_EXIST));
require_login($course, false, $cm);
$PAGE->set_url('/mod/logic/grade.php', array('id'=>$cm->id));
redirect('view.php?id='.$cm->id);
// the functions below go into mod/logic/lib.php
// functions logic_supports(), logic_grade_item_update(), logic_update_grades(),
// and logic_grade_item_update() are taken from mod/lesson/lib.php, then modified
// for logic
/**
* Return the list if Moodle features this module supports
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, false if not,
* null if doesn't know or string for the module purpose.
*/
function logic_supports($feature) {
switch($feature) {
case FEATURE_GRADE_HAS_GRADE: return true;
default: return null;
}
}
/**
* Create grade item for given assignment.
*
* @param stdClass $logic record with extra cmid
* @param array $grades optional array/object of grade(s);
* 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function logic_grade_item_update($logic, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
$params = array('itemname'=>$logic->name, 'idnumber'=>$logic->cm_id);
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = 100;
$params['grademin'] = 0;
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
return grade_update('mod/logic', $logic->course, 'mod', 'logic', $logic->id,
0, $grades, $params);
}
/**
* Update activity grades.
*
* @param stdClass $logic database record
* @param int $userid specific user only, 0 means all
* @param bool $nullifnone - not used
*/
function logic_update_grades($logic, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
if (!$logic->assessed) {
logic_grade_item_update($logic);
} else if ($grades = logic_get_user_grades($logic, $userid)) {
logic_grade_item_update($logic, $grades);
} else if ($userid and $nullifnone) {
$grade = new stdClass();
$grade->userid = $userid;
$grade->rawgrade = NULL;
logic_grade_item_update($logic, $grade);
} else {
logic_grade_item_update($logic);
}
}
/**
* Create grade item for given logic problem
*
* @category grade
* @uses GRADE_TYPE_VALUE
* @param object $logic object
* @param array|object $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function logic_grade_item_update($logic, $grades=null) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
if (property_exists($logic, 'cm_id')) { //it may not be always present
$params = array('itemname'=>$logic->name, 'idnumber'=>$logic->cm_id);
} else {
$params = array('itemname'=>$logic->name);
}
if (!$logic->practice and $logic->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = 100;
$params['grademin'] = 0;
}
// Make sure current grade fetched correctly from $grades
$currentgrade = null;
if (!empty($grades)) {
if (is_array($grades)) {
$currentgrade = reset($grades);
} else {
$currentgrade = $grades;
}
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
} else if (!empty($grades)) {
// Need to calculate raw grade (Note: $grades has many forms)
if (is_object($grades)) {
$grades = array($grades->userid => $grades);
} else if (array_key_exists('userid', $grades)) {
$grades = array($grades['userid'] => $grades);
}
foreach ($grades as $key => $grade) {
if (!is_array($grade)) {
$grades[$key] = $grade = (array) $grade;
}
//check raw grade isnt null otherwise we erroneously insert a grade of 0
if ($grade['rawgrade'] !== null) {
$grades[$key]['rawgrade'] = ($grade['rawgrade'] * $params['grademax'] / 100);
} else {
//setting rawgrade to null just in case user is deleting a grade
$grades[$key]['rawgrade'] = null;
}
}
}
return grade_update('mod/logic', $logic->course, 'mod', 'logic', $logic->id, 0, $grades, $params);
}