-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlocallib.php
executable file
·2266 lines (2002 loc) · 76.1 KB
/
locallib.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* @package mod_tracker
* @category mod
* @author Clifford Tham, Valery Fremaux > 1.8
*
* Library of internal functions and constants for module tracker
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/lib/uploadlib.php');
require_once($CFG->dirroot.'/mod/tracker/mailtemplatelib.php');
// Status defines.
define('POSTED', 0);
define('OPEN', 1);
define('RESOLVING', 2);
define('WAITING', 3);
define('RESOLVED', 4);
define('ABANDONNED', 5);
define('TRANSFERED', 6);
define('TESTING', 7);
define('PUBLISHED', 8);
define('VALIDATED', 9);
// Status defines.
define('ENABLED_POSTED', 1);
define('ENABLED_OPEN', 2);
define('ENABLED_RESOLVING', 4);
define('ENABLED_WAITING', 8);
define('ENABLED_RESOLVED', 16);
define('ENABLED_ABANDONNED', 32);
define('ENABLED_TRANSFERED', 64);
define('ENABLED_TESTING', 128);
define('ENABLED_PUBLISHED', 256);
define('ENABLED_VALIDATED', 512);
define('ENABLED_ALL', 1023);
function tracker_get_context($cmid, $instanceid) {
global $DB;
if ($cmid) {
if (! $cm = get_coursemodule_from_id('tracker', $cmid)) {
print_error('errorcoursemodid', 'tracker');
}
if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('errorcoursemisconfigured', 'tracker');
}
if (! $tracker = $DB->get_record('tracker', array('id' => $cm->instance))) {
print_error('errormoduleincorrect', 'tracker');
}
} else {
if (! $tracker = $DB->get_record('tracker', array('id' => $instanceid))) {
print_error('errormoduleincorrect', 'tracker');
}
if (! $course = $DB->get_record('course', array('id' => $tracker->course))) {
print_error('errorcoursemisconfigured', 'tracker');
}
if (! $cm = get_coursemodule_from_instance("tracker", $tracker->id, $course->id)) {
print_error('errorcoursemodid', 'tracker');
}
}
return array($cm, $tracker, $course);
}
function tracker_requires($view, $screen) {
global $PAGE;
if ($view == 'profile') {
$PAGE->requires->js('/mod/tracker/js/watchsview.js');
}
}
// Major roles against status keys.
function tracker_get_role_definition(&$tracker, $role) {
if ($role == 'report') {
if ($tracker->supportmode == 'bugtracker') {
return ENABLED_POSTED | ENABLED_VALIDATED;
} else {
return ENABLED_POSTED | ENABLED_RESOLVED | ENABLED_VALIDATED;
}
} else if ($role == 'develop') {
if ($tracker->supportmode == 'bugtracker') {
$switches = ENABLED_OPEN | ENABLED_RESOLVING | ENABLED_WAITING | ENABLED_ABANDONNED;
$switches -= ENABLED_TESTING | ENABLED_PUBLISHED | ENABLED_VALIDATED;
return $switches;
} else {
$switches = ENABLED_OPEN | ENABLED_RESOLVING | ENABLED_WAITING | ENABLED_ABANDONNED;
$switches |= ENABLED_TESTING | ENABLED_PUBLISHED;
return $switches;
}
} else if ($role == 'resolve') {
return ENABLED_RESOLVED | ENABLED_VALIDATED | ENABLED_ABANDONNED | ENABLED_TRANSFERED;
} else if ($role == 'manage') {
return ENABLED_POSTED | ENABLED_RESOLVING | ENABLED_WAITING | ENABLED_ABANDONNED | ENABLED_TESTING | ENABLED_PUBLISHED | ENABLED_VALIDATED;
}
return 0;
}
// States && eventmasks.
define('EVENT_POSTED', 1);
define('EVENT_OPEN', 2);
define('EVENT_RESOLVING', 4);
define('EVENT_WAITING', 8);
define('EVENT_RESOLVED', 16);
define('EVENT_ABANDONNED', 32);
define('EVENT_TRANSFERED', 64);
define('EVENT_TESTING', 128);
define('EVENT_PUBLISHED', 256);
define('EVENT_VALIDATED', 512);
define('ON_COMMENT', 1024);
define('ALL_EVENTS', 2047);
/**
* loads all elements in memory
* @param reference $tracker the tracker object
* @param reference $elementsobj
*/
function tracker_loadelements(&$tracker, &$elementsobj) {
global $COURSE, $CFG, $DB;
// First get shared elements.
$elements = $DB->get_records('tracker_element', array('course' => 0));
if (!$elements) {
$elements = array();
}
// Get course scope elements.
$courseelements = $DB->get_records('tracker_element', array('course' => $COURSE->id));
if ($courseelements) {
$elements = array_merge($elements, $courseelements);
}
// Make a set of element objet with records.
if (!empty($elements)) {
foreach ($elements as $element) {
// This get the options by the constructor.
include_once($CFG->dirroot.'/mod/tracker/classes/trackercategorytype/'.$element->type.'/'.$element->type.'.class.php');
$constructorfunction = "{$element->type}element";
$elementsobj[$element->id] = new $constructorfunction($tracker, $element->id);
$elementsobj[$element->id]->name = $element->name;
$elementsobj[$element->id]->description = $element->description;
$elementsobj[$element->id]->type = $element->type;
$elementsobj[$element->id]->course = $element->course;
}
}
}
/**
* get all available types which are plugins in classes/trackercategorytype
* @return an array of known element types
*/
function tracker_getelementtypes() {
global $CFG;
$typedir = $CFG->dirroot.'/mod/tracker/classes/trackercategorytype';
$dir = opendir($typedir);
while ($entry = readdir($dir)) {
if (strpos($entry, '.') === 0) {
continue;
}
if ($entry == 'CVS') {
continue;
}
if (!is_dir("$typedir/$entry")) {
continue;
}
$types[] = $entry;
}
closedir($dir);
return $types;
}
/**
* tells if at least one used element is a file element
* @param int $trackerid the current tracker
*/
function tracker_requiresfile($trackerid) {
global $DB;
$sql = "
SELECT
COUNT(*)
FROM
{tracker_element} e,
{tracker_elementused} eu
WHERE
eu.elementid = e.id AND
eu.trackerid = {$trackerid} AND
e.type = 'file'
";
$count = $DB->count_records_sql($sql);
return $count;
}
/**
* loads elements as objects array in a reference
* @param int $trackerid the current tracker
* @param reference $used a reference to an array of used elements
*/
function tracker_loadelementsused(&$tracker, &$used) {
global $DB;
$cm = get_coursemodule_from_instance('tracker', $tracker->id);
$context = context_module::instance($cm->id);
$fields = 'id, elementid, sortorder';
$usedelements = $DB->get_records('tracker_elementused', array('trackerid' => $tracker->id), 'sortorder', $fields);
$used = array();
$sortorder = 1;
if (!empty($usedelements)) {
foreach ($usedelements as $ueid => $ue) {
// Normalize sortorder indexes.
if ($ue->sortorder != $sortorder) {
$ue->sortorder = $sortorder;
$DB->update_record('tracker_elementused', $ue);
}
$elementused = trackerelement::find_instance_by_usedid($tracker, $ueid);
if ($elementused) {
$used[$ue->elementid] = $elementused;
$used[$ue->elementid]->context = $context;
$sortorder++;
}
}
}
}
/**
* quite the same as above, but not loading objects, and
* mapping hash keys by "name"
* @param int $trackerid
*/
function tracker_getelementsused_by_name(&$tracker) {
global $DB;
$sql = "
SELECT
e.name,
e.description,
e.type,
eu.id AS usedid,
eu.sortorder,
eu.trackerid,
eu.canbemodifiedby,
eu.active
FROM
{tracker_element} e,
{tracker_elementused} eu
WHERE
eu.elementid = e.id AND
eu.trackerid = {$tracker->id}
ORDER BY
eu.sortorder ASC
";
if (!$usedelements = $DB->get_records_sql($sql)) {
return array();
}
return $usedelements;
}
/**
* checks if an element is used somewhere in the tracker. It must be in used list
* @param int $trackerid the current tracker
* @param int $elementid the element
* @return boolean
*/
function tracker_iselementused($trackerid, $elementid) {
global $DB;
$select = 'elementid = ? AND trackerid = ? ';
$inusedelements = $DB->count_records_select('tracker_elementused', $select, array($elementid, $trackerid));
return $inusedelements;
}
/**
* print additional user defined elements in several contexts
* @param int $trackerid the current tracker
* @param array $fields the array of fields to be printed
*/
function tracker_printelements(&$tracker, $fields = null, $dest = false) {
tracker_loadelementsused($tracker, $used);
if (!empty($used)) {
if (!empty($fields)) {
foreach ($used as $element) {
if (isset($fields[$element->id])) {
foreach ($fields[$element->id] as $value) {
$element->value = $value;
}
}
}
}
foreach ($used as $element) {
if (!$element->active) {
continue;
}
echo '<tr>';
echo '<td align="right" valign="top">';
echo '<b>' . format_string($element->description).':</b>';
echo '</td>';
echo '<td align="left" colspan="3">';
if ($dest == 'search') {
if ($element->type == 'file') {
continue;
}
$element->viewsearch();
} else if ($dest == 'query') {
if ($element->type == 'file') {
continue;
}
$element->viewquery();
} else {
$element->view(true);
}
echo '</td>';
echo '</tr>';
}
}
}
/**
* get how many issues in this tracker
* @param int $trackerid
* @param int $status if status is positive or null, filters by status
* @return integer
*/
function tracker_getnumissuesreported($trackerid, $status='*', $reporterid = '*', $resolverid='*', $developerids='', $adminid='*') {
global $DB;
$statusclause = ($status !== '*') ? " AND i.status = $status " : '';
$reporterclause = ($reporterid != '*') ? " AND i.reportedby = $reporterid " : '';
$resolverclause = ($resolverid != '*') ? " AND io.userid = $resolverid " : '';
$developerclause = ($developerids != '') ? " AND io.userid IN ($developerids) " : '';
$adminclause = ($adminid != '*') ? " AND io.bywhomid IN ($adminid) " : '';
$sql = "
SELECT
COUNT(DISTINCT(i.id))
FROM
{tracker_issue} i
LEFT JOIN
{tracker_issueownership} io
ON
i.id = io.issueid
WHERE
i.trackerid = {$trackerid}
$statusclause
$reporterclause
$developerclause
$resolverclause
$adminclause
";
return $DB->count_records_sql($sql);
}
// User related.
/**
* get available managers/tracker administrators
* @param object $context
*/
function tracker_getadministrators($context) {
$allnames = get_all_user_name_fields(true, 'u');
return get_users_by_capability($context, 'mod/tracker:manage', 'u.id,'.$allnames, 'lastname', '', '', '', '', false);
}
/**
* get available resolvers
* @param object $context
*/
function tracker_getresolvers($context) {
$allnames = get_all_user_name_fields(true, 'u');
return get_users_by_capability($context, 'mod/tracker:resolve', 'u.id,'.$allnames, 'lastname', '', '', '', '', false);
}
/**
* get actual reporters from records
* @param int $trackerid
* @return arrzay of user records
*/
function tracker_getreporters($trackerid) {
global $DB;
$allnames = get_all_user_name_fields(true, 'u');
$sql = "
SELECT
DISTINCT(reportedby) AS id,
{$allnames},
u.imagealt
FROM
{tracker_issue} i,
{user} u
WHERE
i.reportedby = u.id AND
i.trackerid = ?
";
return $DB->get_records_sql($sql, array($trackerid));
}
/**
* Get all developpers in a course module
* @param object $context the associated context
*/
function tracker_getdevelopers($context) {
$allnames = get_all_user_name_fields(true, 'u');
return get_users_by_capability($context, 'mod/tracker:develop', 'u.id,'.$allnames, 'lastname', '', '', '', '', false);
}
/**
* get the assignees of a manager
* @param int $userid the manager's id.
* @return array of records
*/
function tracker_getassignees($userid) {
global $DB;
$allnames = get_all_user_name_fields(true, 'u');
$sql = "
SELECT DISTINCT
u.id,
{$allnames},
u.picture,
u.email,
u.emailstop,
u.maildisplay,
u.imagealt,
COUNT(i.id) as issues
FROM
{tracker_issue} i,
{user} u
WHERE
i.assignedto = u.id AND
i.bywhomid = ?
GROUP BY
u.id,
u.firstname,
u.lastname,
u.picture,
u.email,
u.emailstop,
u.maildisplay,
u.imagealt
";
return $DB->get_records_sql($sql, array($userid));
}
/**
* submits an issue in the current tracker
* @param int $trackerid the current tracker
* @return the issue object
* @throws DB exception id canno insert record
*/
function tracker_submitanissue(&$tracker, &$data) {
global $DB, $USER;
$cm = get_coursemodule_from_instance('tracker', $tracker->id);
$context = context_module::instance($cm->id);
$issue = new StdClass();
$issue->datereported = time();
$issue->summary = $data->summary;
$issue->description = $data->description_editor['text'];
$issue->descriptionformat = $data->description_editor['format'];
if (isset($data->resolution_editor)) {
$issue->resolution = $data->resolution_editor['text'];
$issue->resolutionformat = $data->resolution_editor['format'];
} else {
$issue->resolution = '';
$issue->resolutionformat = FORMAT_MOODLE;
}
if (!empty($data->assignedto)) {
$issue->assignedto = 0 + $data->assignedto;
} else if (!empty($tracker->defaultassignee)) {
$issue->assignedto = $tracker->defaultassignee;
} else {
$issue->assignedto = 0;
}
$issue->bywhomid = 0;
$issue->trackerid = $tracker->id;
$issue->reportedby = $USER->id;
if (empty($data->issueid)) {
$issue->status = POSTED;
// Fetch max actual priority.
$select = " trackerid = ? GROUP BY trackerid ";
$maxpriority = $DB->get_field_select('tracker_issue', 'MAX(resolutionpriority)', $select, array($tracker->id));
$issue->resolutionpriority = $maxpriority + 1;
$issue->id = $DB->insert_record('tracker_issue', $issue);
$data->issueid = $issue->id;
// If not CCed, the assignee should be.
tracker_register_cc($tracker, $issue, $issue->reportedby);
} else {
$issue->oldstatus = $DB->get_field('tracker_issue', 'status', array('id' => $data->issueid));
$issue->id = $data->issueid;
$issue->status = $data->status;
$issue->resolution = @$data->resolution_editor['text'];
$issue->resolutionformat = @$data->resolution_editor['format'];
$DB->update_record('tracker_issue', $issue);
}
return $issue;
}
/**
* fetches all issues a user is assigned to as resolver
* @uses $USER
* @param int $trackerid the current tracker
* @param int $userid an eventual userid
*/
function tracker_getownedissuesforresolve($trackerid, $userid = null) {
global $USER, $DB;
if (empty($userid)) {
$userid = $USER->id;
}
$select = " trackerid = ? AND assignedto = ? ";
return $DB->get_records_select('tracker_issue', $select, array($trackerid, $userid));
}
/**
* stores in database the element values
* @param object $issue
* @param object $data full form return
*/
function tracker_recordelements(&$issue, $data) {
global $DB;
$tracker = $DB->get_record('tracker', array('id' => $issue->trackerid));
$usedelements = $DB->get_records('tracker_elementused', array('trackerid' => $issue->trackerid), 'id', 'id,elementid');
foreach ($usedelements as $ueid => $ue) {
if ($ueinstance = trackerelement::find_instance_by_usedid($tracker, $ueid)) {
$ueinstance->form_process($data);
}
}
}
/**
* clears element recordings for an issue
* @see view.controller.php / updateissue
* @param int $issueid the issue
* @param int $withfiles if true, the attached files will be deleted too (full deletion)
*/
function tracker_clearelements($issueid, $withfiles = false) {
global $DB;
if (!$issue = $DB->get_record('tracker_issue', array('id' => "$issueid"))) {
return;
}
$attributeids = $DB->get_records('tracker_issueattribute', array('issueid' => $issueid), 'id', 'id,id');
if (!$DB->delete_records('tracker_issueattribute', array('issueid' => $issueid))) {
print_error('errorcannotlearelementsforissue', 'tracker', $issueid);
}
// delete issue attribute fields
if ($withfiles && !empty($attributeids)) {
$fs = get_file_storage();
foreach ($attributeids as $attid => $void) {
$fs->delete_area_files($context->id, 'mod_tracker', 'issueattribute', $attid);
}
}
}
if (!function_exists('print_error_class')) {
/**
* adds an error css marker in case of matching error
* @param array $errors the current error set
* @param string $errorkey
*/
function print_error_class($errors, $errorkeylist) {
$out = '';
if ($errors) {
foreach ($errors as $anerror) {
if ($anerror->on == '') {
continue;
}
if (preg_match("/\\b{$anerror->on}\\b/" , $errorkeylist)) {
$out .= 'class="formerror" ';
$out .= 'title="'.$anerror->message.'" ';
}
}
}
return $out;
}
}
/**
* registers a user as cced for an issue in a tracker
* @param reference $tracker the current tracker
* @param reference $issue the issue to watch
* @param int $userid the cced user's ID
*/
function tracker_register_cc(&$tracker, &$issue, $userid) {
global $DB;
$params = array('trackerid' => $tracker->id, 'issueid' => $issue->id, 'userid' => $userid);
if ($userid && !$DB->get_record('tracker_issuecc', $params)) {
// Add new the assignee as new CC !!
// We do not discard the old one as he may be still concerned.
$eventmask = ALL_EVENTS;
$params = array('trackerid' => $tracker->id, 'userid' => $userid, 'name' => 'eventmask');
if ($userprefs = $DB->get_record('tracker_preferences', $params)) {
$eventmask = $userprefs->value;
}
$cc = new StdClass;
$cc->trackerid = $tracker->id;
$cc->issueid = $issue->id;
$cc->userid = $userid;
$cc->events = $eventmask;
$DB->insert_record('tracker_issuecc', $cc);
}
}
function tracker_get_issues(&$tracker, $resolved, $screen, $sort, $limitfrom, $pagesize) {
global $DB, $USER;
$params = array($tracker->id);
// Check we display only resolved tickets or working.
if ($resolved) {
$resolvedclause = " AND
(status = ".RESOLVED." OR
status = ".ABANDONNED.")
";
} else {
$resolvedclause = " AND
status <> ".RESOLVED." AND
status <> ".ABANDONNED."
";
}
$userclause = '';
switch ($screen) {
case 'mytickets': {
$userclause = " AND reportedby = ? ";
$params[] = $USER->id;
break;
}
case 'mywork': {
$userclause = " AND assignedto = ? ";
$params[] = $USER->id;
break;
}
}
$sql = "
SELECT
i.id,
i.summary,
i.datereported,
i.reportedby,
i.assignedto,
i.status,
i.resolutionpriority,
u.firstname firstname,
u.lastname lastname,
COUNT(ic.issueid) watches
FROM
{tracker_issue} i
LEFT JOIN
{tracker_issuecc} ic
ON
ic.issueid = i.id
LEFT JOIN
{user} u
ON
i.reportedby = u.id
WHERE
i.reportedby = u.id AND
i.trackerid = ?
$resolvedclause
$userclause
GROUP BY
i.id,
i.summary,
i.datereported,
i.reportedby,
i.assignedto,
i.status,
i.resolutionpriority,
u.firstname,
u.lastname
";
$sqlcount = "
SELECT
COUNT(*)
FROM
{tracker_issue} i
WHERE
i.trackerid = ?
$resolvedclause
$userclause
";
$numrecords = $DB->count_records_sql($sqlcount, $params);
if (!empty($sort)) {
$sql .= " ORDER BY $sort";
} else {
$sql .= " ORDER BY resolutionpriority ASC";
}
$issues = $DB->get_records_sql($sql, $params, $limitfrom, $pagesize);
return array($issues, $numrecords);
}
/**
* get list of possible parents. Note that none can be in the subdependancies.
* @param int $trackerid
* @param int $issueid
* @return array of records.
*/
function tracker_getpotentialdependancies($trackerid, $issueid) {
global $DB;
$subtreelist = tracker_get_subtree_list($trackerid, $issueid);
$subtreeclause = (!empty($subtreelist)) ? "AND i.id NOT IN ({$subtreelist}) " : '';
$sql = "
SELECT
i.id,
id.parentid,
id.childid as isparent,
summary
FROM
{tracker_issue} i
LEFT JOIN
{tracker_issuedependancy} id
ON
i.id = id.parentid
WHERE
i.trackerid = {$trackerid} AND
((id.childid IS NULL) OR (id.childid = $issueid)) AND
((id.parentid != $issueid) OR (id.parentid IS NULL)) AND
i.id != $issueid
$subtreeclause
GROUP BY
i.id,
id.parentid,
id.childid,
summary
";
return $DB->get_records_sql($sql);
}
/**
* get the full list of dependencies in a tree // revamped from techproject/treelib.php
* @param table the table-tree
* @param id the node from where to start of
* @return a comma separated list of nodes
*/
function tracker_get_subtree_list($trackerid, $id) {
global $DB;
$res = $DB->get_records_menu('tracker_issuedependancy', array('parentid' => $id), '', 'id,childid');
$ids = array();
if (is_array($res)) {
foreach (array_values($res) as $asub) {
$ids[] = $asub;
$subs = tracker_get_subtree_list($trackerid, $asub);
if (!empty($subs)) {
$ids[] = $subs;
}
}
}
return(implode(',', $ids));
}
/**
* prints all childs of an issue treeshaped
* @param object $tracker
* @param int $issueid
* @param boolean $return if true, returns the HTML, prints it to output elsewhere
* @param int $indent the indent value
* @return the HTML
*/
function tracker_get_children(&$tracker, $issueid) {
global $DB;
$statuskeys = tracker_get_statuskeys($tracker);
$statuscodes = tracker_get_statuscodes();
$str = '';
$sql = "
SELECT
childid,
summary,
status
FROM
{tracker_issuedependancy} id,
{tracker_issue} i
WHERE
i.id = id.childid AND
id.parentid = {$issueid} AND
i.trackerid = {$tracker->id}
";
$res = $DB->get_records_sql($sql);
return $res;
}
/**
* prints all parents of an issue tree shaped
* @param object $tracker
* @param int $issueid
* @return the HTML
*/
function tracker_get_parents(&$tracker, $issueid, $return = false, $indent='') {
global $DB;
$str = '';
$sql = "
SELECT
parentid,
summary,
status
FROM
{tracker_issuedependancy} id,
{tracker_issue} i
WHERE
i.id = id.parentid AND
id.childid = ? AND
i.trackerid = ?
";
$res = $DB->get_records_sql($sql, array($issueid, $tracker->id));
return $res;
}
/**
* return watch list for a user
* @param int trackerid the current tracker
* @param int userid the user
*/
function tracker_getwatches($trackerid, $userid) {
global $DB;
$sql = "
SELECT
w.*,
i.summary
FROM
{tracker_issuecc} w,
{tracker_issue} i
WHERE
w.issueid = i.id AND
i.trackerid = ? AND
w.userid = ?
";
$watches = $DB->get_records_sql($sql, array($trackerid, $userid));
if ($watches) {
foreach ($watches as $awatch) {
$people = $DB->count_records('tracker_issuecc', array('issueid' => $awatch->issueid));
$watches[$awatch->id]->people = $people;
}
}
return $watches;
}
/**
* sends required notifications when requiring raising priority
* @uses $COURSE
* @param object $issue
* @param object $cm
* @param object $tracker
*/
function tracker_notify_raiserequest($issue, &$cm, $reason, $urgent, $tracker = null) {
global $COURSE, $SITE, $CFG, $USER, $DB;
if (empty($tracker)) {
// Database access optimization in case we have a tracker from somewhere else.
$tracker = $DB->get_record('tracker', array('id' => $issue->trackerid));
}
$fields = 'u.id,'.get_all_user_name_fields(true, 'u').',username,lang,email,emailstop,mailformat,mnethostid';
$context = context_module::instance($cm->id);
$managers = get_users_by_capability($context, 'mod/tracker:manage', $fields, 'lastname', '', '', '', '', true);
$by = $DB->get_record('user', array('id' => $issue->reportedby));
$urgentrequest = '';
if ($urgent) {
$urgentrequest = get_string('urgentsignal', 'tracker');
}
$params = array('t' => $tracker->id, 'view' => 'view', 'screen' => 'viewanissue', 'issueid' => $issue->id);
$issueurl = new moodle_url('/mod/tracker/view.php', $params);
$vars = array('COURSE_SHORT' => $COURSE->shortname,
'COURSENAME' => format_string($COURSE->fullname),
'TRACKERNAME' => format_string($tracker->name),
'ISSUE' => $tracker->ticketprefix.$issue->id,
'SUMMARY' => format_string($issue->summary),
'REASON' => stripslashes($reason),
'URGENT' => $urgentrequest,
'BY' => fullname($by),
'REQUESTEDBY' => fullname($USER),
'ISSUEURL' => $issueurl);
include_once($CFG->dirroot."/mod/tracker/mailtemplatelib.php");
if (!empty($managers)) {
foreach ($managers as $manager) {
$notification = tracker_compile_mail_template('raiserequest', $vars, $manager->lang);
$notificationhtml = tracker_compile_mail_template('raiserequest_html', $vars, $manager->lang);
if ($CFG->debugsmtp) {
echo "Sending Raise Request Mail Notification to ".fullname($manager).'<br/>'.$notificationhtml;
}
$subject = get_string('raiserequestcaption', 'tracker', $SITE->shortname.':'.format_string($tracker->name));
if ($CFG->debugsmtp) {
$msg = "Sending Raise Request Mail Notification to ".fullname($ccuser).'<br/>';
$msg .= "<pre>Subject: $subject\n########\n".shorten_text($notification, 160).'</pre>';
mtrace($msg);
}
email_to_user($manager, $USER, $subject, $notification, $notificationhtml);
}
}
$systemcontext = context_system::instance();
$admins = get_users_by_capability($systemcontext, 'moodle/site:doanything', $fields, 'lastname', '', '', '', '', true);
if (!empty($admins)) {
foreach ($admins as $admin) {
$notification = tracker_compile_mail_template('raiserequest', $vars, $admin->lang);
$notificationhtml = tracker_compile_mail_template('raiserequest_html', $vars, $admin->lang);
if ($CFG->debugsmtp) {
echo "Sending Raise Request Mail Notification to " . fullname($admin) . '<br/>'.$notificationhtml;
}
$subject = get_string('urgentraiserequestcaption', 'tracker', $SITE->shortname.':'.format_string($tracker->name));
email_to_user($admin, $USER, $subject, $notification, $notificationhtml);
}
}
}
/**
* sends required notifications by the watchers when first submit
* @uses $COURSE
* @param object $issue
* @param object $cm
* @param object $tracker
*/
function tracker_notify_submission($issue, &$cm, $tracker = null) {
global $COURSE, $SITE, $CFG, $USER, $DB;
if (empty($tracker)) {
// Database access optimization in case we have a tracker from somewhere else.
$tracker = $DB->get_record('tracker', array('id' => $issue->trackerid));
}
$fields = 'u.id,'.get_all_user_name_fields(true, 'u').',username,lang,email,emailstop,mailformat,mnethostid';
$context = context_module::instance($cm->id);
$managers = get_users_by_capability($context, 'mod/tracker:manage', $fields, 'lastname');
$by = $DB->get_record('user', array('id' => $issue->reportedby));
$params = array('t' => $tracker->id, 'view' => 'view', 'screen' => 'viewanissue', 'issueid' => $issue->id);
$issueurl = new moodle_url('/mod/tracker/view.php', $params);
$params = array('t' => $tracker->id,
'view' => 'profile',
'screen' => 'mywatches',
'issueid' => $issue->id,
'what' => 'register');
$ccurl = new moodle_url('/mod/tracker/view.php', $params);
if (!empty($managers)) {