-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanageattempts.php
128 lines (103 loc) · 4.62 KB
/
manageattempts.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
<?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/>.
/**
* Action for adding/editing a minilesson attempt.
*
* @package mod_minilesson
* @copyright 2014 Justin Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
**/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
use mod_minilesson\constants;
global $USER, $DB;
// first get the info passed in to set up the page
$attemptid = optional_param('attemptid', 0 , PARAM_INT);
$source = optional_param('source', 'attempts', PARAM_TEXT);
$n = required_param('n', PARAM_INT); // instance ID
$action = required_param('action', PARAM_TEXT);
// get the objects we need
$moduleinstance = $DB->get_record('minilesson', ['id' => $n], '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $moduleinstance->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('minilesson', $moduleinstance->id, $course->id, false, MUST_EXIST);
// set up the page object url
$PAGE->set_url('/mod/minilesson/manageattempts.php', ['attemptid' => $attemptid, 'n' => $n, 'action' => $action]);
// make sure we are logged in and can see this form
require_login($course, false, $cm);
$context = context_module::instance($cm->id);
require_capability('mod/minilesson:canmanageattempts', $context);
// is the attempt if OK?
if ($action == 'delete' && $attemptid > 0) {
$attempt = $DB->get_record(constants::M_ATTEMPTSTABLE, ['id' => $attemptid, 'moduleid' => $cm->instance], '*', MUST_EXIST);
if (!$attempt) {
print_error('could not find attempt of id:' . $attemptid);
}
} else {
$edit = false;
}
// we always head back to the minilesson attempts page
switch($source){
case 'attempts':
$redirecturl = new moodle_url('/mod/minilesson/reports.php', ['report' => 'attempts', 'id' => $cm->id, 'n' => $n]);
break;
case 'incompleteattempts':
$redirecturl = new moodle_url('/mod/minilesson/reports.php', ['report' => 'incompleteattempts', 'id' => $cm->id, 'n' => $n]);
break;
case 'grading':
$redirecturl = new moodle_url('/mod/minilesson/grading.php', ['id' => $cm->id, 'action' => 'grading']);
break;
case 'gradingbyuser':
$redirecturl = new moodle_url('/mod/minilesson/grading.php', ['action' => 'gradingbyuser', 'userid' => $attempt->userid, 'n' => $n]);
break;
}
// handle delete actions
switch($action){
/////// Delete attempt NOW////////
case 'delete':
require_sesskey();
// Check user has group access.
$attempt = $DB->get_record(constants::M_ATTEMPTSTABLE, ['id' => $attemptid, 'moduleid' => $cm->instance], '*', MUST_EXIST);
if (!groups_user_groups_visible($course, $attempt->userid, $cm)) {
print_error("You do not have permssion to delete this user");
} else if ($DB->delete_records(constants::M_ATTEMPTSTABLE, ['id' => $attemptid])) {
if ($attempt) {
minilesson_update_grades($moduleinstance, $attempt->userid, true);
}
} else {
print_error("Could not delete attempt");
}
redirect($redirecturl);
return;
// Delete ALL attempts ////////
case 'deleteall':
require_sesskey();
$groupsmode = groups_get_activity_groupmode($cm, $course);
$context = empty($cm) ? \context_course::instance($course->id) : \context_module::instance($cm->id);
$supergrouper = has_capability('moodle/site:accessallgroups', $context, $USER->id);
$result = false;
// if no groups, or can see all groups then the SQL is simple
if($supergrouper || $groupsmode != SEPARATEGROUPS) {
$result = $DB->delete_records(constants::M_ATTEMPTSTABLE, ['moduleid' => $moduleinstance->id]);
}
if ($result) {
minilesson_update_grades($moduleinstance, 0, true);
} else {
print_error("Could not delete attempts (all)");
}
redirect($redirecturl);
return;
}
// we should never get here
echo "You should not get here";