-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathactivityready.php
165 lines (148 loc) · 4.98 KB
/
activityready.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
154
155
156
157
158
159
160
161
162
163
164
165
<?php
// This file is part of the Navigation buttons plugin for 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/>.
/**
* Check if activity is ready to show buttons.
*
* @copyright Davo Smith <[email protected]>
* @package block_navbuttons
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** Always show navigation buttons for this activity */
define('NAVBUTTONS_ACTIVITY_ALWAYS', 0);
/** Show buttons when activity complete */
define('NAVBUTTONS_ACTIVITY_COMPLETE', 1);
/** Show buttons via custom rules for this activity */
define('NAVBUTTONS_ACTIVITY_CUSTOM', 2);
/** Never show activity buttons for this activity */
define('NAVBUTTONS_ACTIVITY_NEVER', 3);
/**
* Check if an activity is configured to only show navbuttons when
* complete and then check if the activity is complete
* @param cm_info $cm the course module for the activity
* @return boolean true if the navbuttons should be shown
*/
function navbuttons_activity_showbuttons($cm) {
$modname = $cm->modname;
$show = get_config('block_navbuttons', 'activity'.$modname);
if ($show === false || $show == NAVBUTTONS_ACTIVITY_ALWAYS) {
return true; // No config or 'always show'.
}
if ($show == NAVBUTTONS_ACTIVITY_NEVER) {
return false;
}
if ($show == NAVBUTTONS_ACTIVITY_COMPLETE) {
$completion = new completion_info($cm->get_course());
if (!$completion->is_enabled($cm)) {
return true; // No completion tracking - show the buttons.
}
$cmcompletion = $completion->get_data($cm);
if ($cmcompletion->completionstate == COMPLETION_INCOMPLETE) {
return false;
}
return true;
}
if (!isloggedin() || isguestuser()) {
return true; // Always show the buttons if not logged in.
}
// NAVBUTTONS_ACTIVITY_CUSTOM.
$funcname = 'navbuttons_mod_'.$modname.'_showbuttons';
if (!function_exists($funcname)) {
return true; // Shouldn't have got to here, but allow the buttons anyway.
}
return $funcname($cm);
}
/**
* Custom rule for mod_assignment buttons.
* @param cm_info $cm
* @return bool
*/
function navbuttons_mod_assignment_showbuttons($cm) {
global $DB, $CFG, $USER;
if (!$assignment = $DB->get_record('assignment', ['id' => $cm->instance])) {
return true; // Not quite sure what went wrong.
}
$type = $assignment->assignmenttype;
if ($type === 'offline') {
return true; // Cannot track 'offline' assignments.
}
$libfile = $CFG->dirroot.'/mod/assignment/type/'.$type.'/assignment.class.php';
if (!file_exists($libfile)) {
return true;
}
require_once($libfile);
$class = 'assignment_'.$type;
$instance = new $class($cm->id, $assignment, $cm, $cm->get_course());
if (!$submission = $instance->get_submission($USER->id)) {
return false; // No submission.
}
if ($type == 'upload' || $type == 'uploadpdf') {
if ($instance->drafts_tracked()) {
if ($instance->is_finalized($submission)) {
return true; // Upload submission is 'finalised'.
} else {
return false;
}
}
}
if ($submission->timemodified > 0) {
return true; // Submission has a 'modified' time.
}
return false;
}
/**
* Custom rule for mod_choice.
* @param object $cm
* @return bool
*/
function navbuttons_mod_choice_showbuttons($cm) {
global $USER, $DB;
return $DB->record_exists('choice_answers', [
'choiceid' => $cm->instance,
'userid' => $USER->id,
]);
}
/**
* Custom rule for mod_quiz.
* @param object $cm
* @return bool
*/
function navbuttons_mod_quiz_showbuttons($cm) {
global $USER, $CFG;
require_once($CFG->dirroot.'/mod/quiz/locallib.php');
if (quiz_get_user_attempt_unfinished($cm->instance, $USER->id)) {
return false; // Unfinished attempt in progress.
}
if (!quiz_get_user_attempts($cm->instance, $USER->id, 'finished', true)) {
return false; // No finished attempts.
}
return true;
}
/**
* Custom rule for mod_questionnaire.
* @param object $cm
* @return bool
*/
function navbuttons_mod_questionnaire_showbuttons($cm) {
global $USER, $DB;
try {
return $DB->record_exists('questionnaire_attempts', [
'qid' => $cm->instance,
'userid' => $USER->id,
]);
} catch (Exception $e) {
return true;
}
}