forked from drachels/moodle-mod_mootyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcnext.php
executable file
·153 lines (138 loc) · 6.17 KB
/
gcnext.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
<?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/>.
/**
* This file adds grade and performance info to mdl_mootyper_grades after an exercise.
*
* @package mod_mootyper
* @copyright 2012 Jaka Luthar ([email protected])
* @copyright 2016 onwards AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
use \mod_mootyper\event\exercise_completed;
use \mod_mootyper\event\exam_completed;
use \mod_mootyper\event\lesson_completed;
use \mod_mootyper\local\results;
// Changed to this format 20190301.
require(__DIR__ . '/../../config.php');
// 20200808 Added for integration with Moodle rating/grades.
require_once(__DIR__ . '/lib.php');
global $CFG, $DB;
$cmid = optional_param('cmid', 0, PARAM_INT); // Course_module ID.
$lsnname = optional_param('lsnname', '', PARAM_RAW); // MooTyper lesson name.
$exercisename = optional_param('exercisename', 0, PARAM_INT); // MooTyper exercise name (It is just a number.).
$mtmode = optional_param('mtmode', 0, PARAM_INT); // MooTyper activity mode. 0 = Lesson, 1 = Exam, 2 = Practice.
$count = optional_param('count', 0, PARAM_INT); // Number of exercises in this lesson.
if ($cmid) {
$cm = get_coursemodule_from_id('mootyper', $cmid, 0, false, MUST_EXIST);
$courseid = $cm->course;
$context = context_module::instance($cm->id);
}
require_login(0, true, null, false);
// Set pass flag to control background color when viewing grades.
// Check to see if accuracy was good enough to pass.
if (optional_param('rpAccInput', '', PARAM_FLOAT) >= optional_param('rpGoal', '', PARAM_FLOAT)) {
$passfield = 1;
} else {
$passfield = 0;
}
// Check to see if wpm rate was good enough to pass, only if accuracy passed.
if ($passfield == 1) {
if (optional_param('rpWpmInput', '', PARAM_FLOAT) >= optional_param('rpWPM', '', PARAM_FLOAT)) {
$passfield = 1;
} else {
$passfield = 0;
}
}
$record = new stdClass();
$record->mootyper = optional_param('rpSityperId', '', PARAM_INT);
$record->userid = optional_param('rpUser', '', PARAM_INT);
// 20200915 Changed from float to int.
$record->grade = optional_param('rpAccInput', '', PARAM_INT);
$record->mistakes = optional_param('rpMistakesInput', '', PARAM_INT);
$record->timeinseconds = optional_param('rpTimeInput', '', PARAM_INT);
$record->hitsperminute = optional_param('rpSpeedInput', '', PARAM_FLOAT);
$record->fullhits = optional_param('rpFullHits', '', PARAM_INT);
$record->precisionfield = optional_param('rpAccInput', '', PARAM_FLOAT);
$record->timetaken = time();
$record->exercise = optional_param('rpExercise', '', PARAM_INT);
$record->pass = $passfield;
$record->attemptid = optional_param('rpAttId', '', PARAM_INT);
$record->wpm = (max(0, optional_param('rpWpmInput', '', PARAM_FLOAT)));
$record->mistakedetails = optional_param('rpMistakeDetailsInput', '', PARAM_CLEAN);
// 20200111 Check to see if there were no mistakes made and change undefined to nomistakes string.
if (stripos($record->mistakedetails, "undefined") !== false) {
$record->mistakedetails = get_string('nomistakes', 'mootyper');
}
// 20200808 Added code for using MooTyper exercise grades as Moodle Ratings.
$mootyper = $DB->get_record('mootyper', array('id' => $record->mootyper), '*', MUST_EXIST);
$DB->insert_record('mootyper_grades', $record, false);
// 20200808 Need id of the record we just inserted.
$rec = results::get_grade_entry($mootyper->id, $record->userid, $record->exercise, $record->timetaken);
// 20200808 Make grade entry depending on whether grade or rating.
if ($mootyper->assessed) {
// 20200808 Need code to place the exercise grade into the rating table.
$ratingoptions = new stdClass();
$ratingoptions->contextid = \context_module::instance($cm->id)->id;
$ratingoptions->component = 'mod_mootyper';
$ratingoptions->ratingarea = 'exercises';
$ratingoptions->itemid = $rec->id;
$ratingoptions->scaleid = $mootyper->scale;
$ratingoptions->rating = $record->grade;
$ratingoptions->userid = $record->userid;
$ratingoptions->timecreated = $record->timetaken;
$ratingoptions->timemodified = $record->timetaken;
// 20200808 Place latest exercise grade into the mdl_rating table.
$DB->insert_record('rating', $ratingoptions, false);
// 20200808 Update entry in Moodle Grades.
mootyper_update_grades($mootyper, $record->userid);
} else {
// Otherwise, place a whole grade into the mdl_grade_items table.
mootyper_update_grades($mootyper);
}
// 20191129 Added trigger for exercise_completed event.
// 20191201 Added modification to also trigger exam_completed event.
$params = array(
'objectid' => $cmid,
'context' => $context,
'other' => array(
'exercise' => $record->exercise,
'lessonname' => $lsnname,
'activity' => $cm->name
)
);
// If exam or just an exercise is completed, log the appropriate event.
if ($mtmode === 1) {
$event = exam_completed::create($params);
} else {
$event = exercise_completed::create($params);
}
$event->trigger();
// Added 20191203 If all the exercises in a lesson are complete, trigger lesson_completed event, too.
if (!($mtmode === 1) && ($exercisename === $count)) {
$params = array(
'objectid' => $cmid,
'context' => $context,
'other' => array(
'exercise' => $record->exercise,
'lessonname' => $lsnname,
'activity' => $cm->name
)
);
$event = lesson_completed::create($params);
$event->trigger();
}
$webdir = $CFG->wwwroot . '/mod/mootyper/view.php?n='.$record->mootyper;
echo '<script type="text/javascript">window.location="'.$webdir.'";</script>';