This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
226 lines (171 loc) · 9.12 KB
/
hook.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
defined('MOODLE_INTERNAL') || die();
require_once $CFG->dirroot . '/blocks/gradetracker/lib.php';
/**
* This adds the gradetracker box to the assignment or activity creation form
* mod/assign/mod_form.php - line 168
* @param type $mform
*/
function gt_mod_hook(&$mform, $cm = false){
global $COURSE, $PAGE, $OUTPUT;
// Make sure the gradetracker block is installed
if (!block_instance('gradetracker')) return false;
$add = optional_param('add', false, PARAM_TEXT);
$cmID = optional_param('update', false, PARAM_INT);
$ModuleLink = false;
if ($add){
$ModuleLink = \GT\ModuleLink::getByModName($add);
}
// Bring javascript in
$PAGE->requires->jquery();
$PAGE->requires->js( '/blocks/gradetracker/js/mod.js', true );
$PAGE->requires->js_init_call("gt_mod_hook_bindings", null, true);
$GT = new \GT\GradeTracker();
$Course = new \GT\Course($COURSE->id);
$activity = $Course->getActivity($cmID);
$quals = $Course->getCourseQualifications(true);
// Create the box
$mform->addElement('header', $GT->getPluginTitle(), $GT->getPluginTitle());
$output = "";
// If this ModuleLink has Parts, but this is a NEW activity not yet saved, then we can't link it up yet
if ($ModuleLink && $ModuleLink->hasParts() && !$cmID){
$output .= \gt_info_alert_box( get_string('activitylinkaftersave', 'block_gradetracker') );
} else {
$output .= "<br>";
if ($quals)
{
$unitArray = array();
foreach($quals as $qual)
{
// If the qual doesn't have any units, don't do anything
if ($qual->isLevelEnabled("Units") && $qual->getUnits())
{
$unitsLinked = \GT\Activity::getUnitsLinkedToCourseModule($cmID, $qual->getID());
$output .= "<b>{$qual->getDisplayName()}</b><br>";
$output .= "<select id='gt_mod_hook_{$qual->getID()}_units_select' class='gt_mod_hook_units' qualID='{$qual->getID()}'>";
$output .= "<option value=''></option>";
if ($qual->getUnits())
{
foreach($qual->getUnits() as $unit)
{
$disabled = (in_array($unit->getID(), $unitsLinked)) ? 'disabled' : '';
$output .= "<option value='{$unit->getID()}' {$disabled} >{$unit->getDisplayName()}</option>";
}
}
$output .= "</select> ";
$output .= "<span id='gt_mod_hook_loader_{$qual->getID()}' class='gt_hidden'><img src='".gt_image_url('i/loading_small')."' alt='loading' /></span>";
$output .= "<br><br>";
$output .= "<div id='gt_mod_hook_qual_units_{$qual->getID()}'>";
// Ones that are already linked
if ($unitsLinked)
{
foreach($unitsLinked as $unitID)
{
$unit = new \GT\Unit($unitID);
$criteria = $unit->sortCriteria(false, true);
$output .= "<div id='gt_hooked_unit_{$qual->getID()}_{$unit->getID()}' class='gt_hooked_unit'>";
$output .= "{$unit->getDisplayName()} <a href='#' class='gt_mod_hook_delete_unit' qualID='{$qual->getID()}' unitID='{$unit->getID()}'><img src='{gt_image_url('t/delete')}' /></a><br>";
$output .= "<table class='gt_c gt_hook_unit_criteria'>";
$output .= "<tr>";
if ($criteria)
{
foreach($criteria as $criterion)
{
$output .= "<th>{$criterion->getName()}</th>";
}
}
$output .= "</tr>";
$output .= "<tr>";
if ($criteria)
{
foreach($criteria as $criterion)
{
if ($activity->getRecordParts())
{
$output .= "<td><select name='gt_criteria[{$qual->getID()}][{$unit->getID()}][{$criterion->getID()}]'>";
$output .= "<option value='0'></option>";
foreach($activity->getRecordParts() as $part)
{
$sel = (\GT\Activity::checkExists($cmID, $qual->getID(), $unit->getID(), $criterion->getID(), $part->id)) ? 'selected' : '';
$output .= "<option value='{$part->id}' {$sel}>{$part->name}</option>";
}
$output .= "</select></td>";
}
else
{
$chk = (\GT\Activity::checkExists($cmID, $qual->getID(), $unit->getID(), $criterion->getID())) ? 'checked' : '';
$output .= "<td><input type='checkbox' name='gt_criteria[{$qual->getID()}][{$unit->getID()}][{$criterion->getID()}]' {$chk} /></td>";
}
}
}
$output .= "</tr>";
$output .= "</table>";
$output .= "</div>";
}
}
$output .= "</div>";
$output .= "<br><br>";
}
}
// If course module passed through, put id in hidden field
if ($cm && $cm->id > 0){
$output .= "<input type='hidden' id='gt_cid' value='{$cm->course}' />";
$output .= "<input type='hidden' id='gt_cmid' value='{$cm->id}' />";
}
}
else
{
$output .= get_string('coursenoquals', 'block_gradetracker');
}
}
$mform->addElement('html', $output);
}
/**
* Process the GT parts of the module add/edit form
* @param type $mod
* @param type $course
*/
function gt_mod_hook_process($mod, $course){
$modID = (is_object($mod)) ? $mod->id : $mod;
$linkedCriteria = (isset($_POST['gt_criteria'])) ? $_POST['gt_criteria'] : false;
$criteriaArray = array();
// If there are criteria we want to link, process them
if ($linkedCriteria)
{
foreach($linkedCriteria as $qualID => $units)
{
foreach($units as $unitID => $criteria)
{
foreach($criteria as $critID => $value)
{
// If the value is greater than 0, try and create a link to it
if ( (is_numeric($value) && $value > 0) || !is_numeric($value) )
{
$activity = new \GT\Activity();
$activity->setCourseModuleID($modID);
$activity->setQualID($qualID);
$activity->setUnitID($unitID);
$activity->setCritID($critID);
// If the value is an int > 0, then it must be a partID
if (is_numeric($value) && $value > 0){
$activity->setPartID($value);
}
$activity->create();
$criteriaArray[$qualID][] = $critID;
}
}
// ------------ Logging Info
$Log = new \GT\Log();
$Log->context = \GT\Log::GT_LOG_CONTEXT_CONFIG;
$Log->details = \GT\Log::GT_LOG_DETAILS_UPDATED_COURSE_ACTIVITY_LINKS;
$Log->afterjson = array($modID => $criteria);
$Log->addAttribute(\GT\Log::GT_LOG_ATT_QUALID, $qualID)
->addAttribute(\GT\Log::GT_LOG_ATT_UNITID, $unitID);
$Log->save();
// ------------ Logging Info
}
}
}
// Now remove any that are currently linked to this course module that were not submitted in the form
\GT\Activity::removeNonSubmittedLinks($modID, $criteriaArray);
}