-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
4894 lines (4593 loc) · 226 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/>.
/**
* Library of functions and constants for module evaluation
* includes the main-part of evaluation-functions
*
* @package mod_evaluation
* @copyright Andreas Grabs for mod_evaluation
* @copyright by Harry.Bleckert@ASH-Berlin.eu for ASH Berlin
* + forked from mod_feedback 12/2021
*
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/*
To Do:
add configuration settings for category tree of course_of_studies
*/
// ASH specific organisation of course of studies (Studiengänge) = Level 2
// Semester is level 3
define('COURSE_OF_STUDIES_PATH', 2);
//define('EVALUATION_DEBUG', TRUE );
define('EVALUATION_DEBUG', false);
function evaluation_debug($msg = false): bool {
if (EVALUATION_DEBUG and is_siteadmin()) {
if ($msg) {
print "<hr>EVALUATION_DEBUG<br>\n$msg<br><hr>";
}
//if ( !defined('READ_ONLY_SESSION') ) { define('READ_ONLY_SESSION', true); }
return true;
}
return false;
}
function ev_get_plugin_version($component = "mod_evaluation") {
list($plugintype, $pluginname) = core_component::normalize_component($component);
$pluginpath = core_component::get_plugin_directory($plugintype, $pluginname);
$plugin = new \stdClass();
require $pluginpath.'/version.php';
//return $plugin->version;
return $plugin;
}
// Wrapper for PHP 8.x to catch uncountable parameters of safeCount()
function safeCount($value) {
if (is_numeric($value)) {
return $value;
}
if (is_countable($value)) {
return count($value);
}
return 0;
}
// get_string for get_string('show_user', 'mod_evaluarion');
function ev_get_string($string, $param = "") {
$plugin = 'evaluation';
$trstring = get_string($string, $plugin, $param);
/*if ( !empty($param) )
{ $trstring = get_string($string, $plugin, $param ); }
else
{ $trstring = get_string($string, $plugin ); }
*/
if (stristr($trstring, ']')) {
return $string;
}
return $trstring;
}
// Moodle return clode for addidional html settings
function evaluation_additional_html() {
global $CFG, $USER;
if (substr($CFG->release, 0, 1) > "3") {
$LoggedInAs = empty($_SESSION["LoggedInAs"]) ? "" : '
/*if ( document.getElementById("usernavigation").length > 0 )
{ document.getElementById("usernavigation").style.display="none"; }*/
if ( document.getElementsByClassName("primary-navigation").length > 0 )
{ document.getElementsByClassName("primary-navigation")[0].style.display="none"; }
if ( document.getElementsByClassName("fixed-top").length > 0 )
{ var nav = document.getElementsByClassName("fixed-top"); for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; } }
';
$html = '<script>
if ( document.getElementsByClassName("page-header-image").length > 0 )
{ document.getElementsByClassName("page-header-image")[0].style.display="none"; }
if ( document.getElementsByClassName("page-header-headings").length > 0 )
{ document.getElementsByClassName("page-header-headings")[0].style.display="none"; }
if ( document.getElementsByClassName("page-context-header").length > 0 )
{ document.getElementsByClassName("page-context-header")[0].style.display="none"; }
if ( document.getElementsByClassName("secondary-navigation").length > 0 )
{ document.getElementsByClassName("secondary-navigation")[0].style.display="none"; }
if ( document.getElementsByClassName("activity-description").length > 0 )
{ document.getElementsByClassName("activity-description")[0].style.display="none"; }'
. $LoggedInAs . '</script>';
} else {
set_user_preference("drawer-open-nav", false, $USER);
$LoggedInAs = empty($_SESSION["LoggedInAs"]) ? "" : '
if ( document.getElementsByClassName("usermenu").length > 0 )
{ var nav = document.getElementsByClassName("usermenu");
for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; };
}
';
$html = '<style>.container-fluid.navbar-nav > div { display: none; }</style>
<script>
// document.getElementById("nav-drawer").style.display="none";
if ( document.getElementsByClassName("nav").length > 0 )
{ var nav = document.getElementsByClassName("nav"); for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; } }
if ( document.getElementsByClassName("list-group").length > 0 )
{ var nav = document.getElementsByClassName("list-group"); for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; } }
if ( document.getElementsByClassName("fixed-top").length > 0 )
{ var nav = document.getElementsByClassName("fixed-top"); for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; } }
if ( document.getElementsByClassName("breadcrumb-item").length > 0 )
{ var nav = document.getElementsByClassName("breadcrumb-item"); nav[0].style.display="none"; }
if ( document.getElementById("page-footer").length > 0 )
{ document.getElementById("page-footer").style.display="none"; }
if ( document.getElementsByClassName("footnote").length > 0 )
{ var nav = document.getElementsByClassName("footnote"); for (var i = 0; i < nav.length; i++) { nav[i].style.display="none"; }; }
// document.getElementById("page-navbar").style.display="block";
// document.getElementByClassName("breadcrumb-item").style.display="block";
' . $LoggedInAs . '
</script>';
/* obsolete
document.getElementById("page-footer").style.display="none";
document.getElementsByClassName("logininfo")[0].style.display="none";
*/
if (empty($_SESSION["LoggedInAs"])) // need remove elements only if logged in as role
{
return "";
}
}
return $html;
}
// Moodle hide classes that show settings menu items and activity title
function evHideSettings() {
print evaluation_additional_html();
}
// set Page layout for Evaluation menu pages
function evSetPage($url, $url2 = false, $anker = false) {
global $CFG, $PAGE, $OUTPUT, $id, $course, $evaluation, $courseid, $downloading;
//$pagelayout = 'report';
$pagelayout = empty($_SESSION["LoggedInAs"]) ? 'report' : 'popup';
$PAGE->set_pagelayout($pagelayout); // report incourse popup base standard
//$PAGE->set_context(context_course::instance($course->id));
//$PAGE->set_context(context_module::instance($cm->id));
$evurl = new moodle_url('/mod/evaluation/');
//navigation_node::override_active_url($evurl);
$PAGE->navbar->ignore_active();
$PAGE->navbar->ignore_active();
/*doesn't work
$previewnode = $PAGE->navigation->add(get_string("modulenameplural", "evaluation"),$evurl, navigation_node::TYPE_CONTAINER);
$thingnode = $previewnode->add($evaluation->name, new moodle_url('/mod/evaluation/view.php', array('id'=>$id )));
$thingnode->make_active();
*/
// add navbar /mod/evaluation/
$PAGE->navbar->add(get_string("modulenameplural", "evaluation"), $evurl);
// view page settings
$evurl = new moodle_url('/mod/evaluation/view.php', array('id' => $id));
$PAGE->navbar->add($evaluation->name, $evurl);
// current page settings
if ($url2) {
$PAGE->navbar->add($anker, $url2);
} // show settings cog for admin
else if (is_siteadmin()) {
$PAGE->force_settings_menu(false);
}
// add empty navbar for Moodle > 3.n
if (substr($CFG->release, 0, 1) > "3") {
$evurl = new moodle_url('/mod/evaluation/view.php', array('id' => $id));
$PAGE->navbar->add("", $evurl);
}
$PAGE->set_url($url);
$PAGE->set_title($evaluation->name);
$PAGE->set_heading($course->fullname);
if (!$downloading) { // Print the page header
echo $OUTPUT->header();
// Moodle 4 Boost: hide classes that show settings menu items and activity title
if (true or stristr($CFG->dataroot, 'dev')) {
evHideSettings();
//$CFG->additionalhtmlfooter = evaluation_additional_html();
}
print '<div id="LoginAs" class="LoginAs d-print-none"></div><span style="clear:both;"><br></span>';
}
}
function evaluation_set_results($evaluation, $forceGlobal = false, $forceCourse = false, $forceUsers = false) {
global $DB, $CFG;
if (!defined('NO_OUTPUT_BUFFERING')) {
define('NO_OUTPUT_BUFFERING', true);
}
ini_set("output_buffering", 350);
@ob_flush();@ob_end_flush();@flush();@ob_start();
// only siteadmin should call this
if (isset($_SESSION['set_results_' . $evaluation->id]) or evaluation_is_open($evaluation)
or intval(date("Ymd", $evaluation->timeopen)) > intval(date("Ymd"))) {
return false;
}
if (evaluation_is_closed($evaluation)) { // create item Studiengang
$timecloseSaved = $evaluation->timeclose;
$timeopen = ($evaluation->timeopen ? $evaluation->timeopen : time() - 86400);
if (is_siteadmin()) {
if ($CFG->ash and !evaluation_is_item_course_of_studies($evaluation->id)) {
evaluation_autofill_item_studiengang($evaluation);
}
// shuffle userids per evaluation and course
if ($evaluation->anonymous and !$evaluation->anonymized) {
ev_shuffle_completed_userids($evaluation);
}
}
$teamteaching = $evaluation->teamteaching;
//$DB->update_record('evaluation', $evaluation);
// store global evaluation details in table evaluation
if ($forceGlobal or empty($evaluation->possible_evaluations)) {
if (is_siteadmin()) {
print "<br><br>\nSaving evaluation results to database table evaluation<br>\n";
}
// set evaluation to Open for allowing set_results
$evaluation->timeclose = time() + 86400;
if ( $forceGlobal OR !safeCount($_SESSION["distinct_s"]) OR !isset($_SESSION["distinct_s_active"]) )
{
list($_SESSION["participating_courses"], $_SESSION["participating_empty_courses"],
$_SESSION["distinct_s"], $_SESSION["distinct_s_active"], $_SESSION["students"],
$_SESSION["students_active"],
$_SESSION["distinct_t"], $_SESSION["distinct_t_active"], $_SESSION["Teachers"], $_SESSION["Teachers_active"]
)
= get_evaluation_participants($evaluation);
}
//$_SESSION["num_courses_of_studies"] = safeCount(evaluation_get_course_studies($evaluation));
//$_SESSION["duplicated"] = evaluation_count_duplicated_replies($evaluation);
$_SESSION["teamteaching_courses"] = evaluation_count_teamteaching_courses($evaluation);
$courses_of_studies = $_SESSION["num_courses_of_studies"];
$duplicated_replies = $_SESSION["duplicated"];
$teamteaching_courses = $_SESSION["teamteaching_courses"];
$participating_students = $_SESSION["distinct_s"];
$participating_active_students = $_SESSION["distinct_s_active"];
$participating_teachers = $_SESSION["distinct_t"];
$participating_active_teachers = $_SESSION["distinct_t_active"];
$participating_courses = $_SESSION["participating_courses"];
$participating_active_courses = $_SESSION["participating_courses"] - $_SESSION["participating_empty_courses"];
$possible_evaluations = $_SESSION["students"]; // possible_evaluations( $evaluation );
$possible_active_evaluations = $_SESSION["students_active"];
if ($teamteaching and $participating_courses) {
$possible_evaluations = possible_evaluations($evaluation);
// WHEN Open: MUST divide by all teachers and all courses because teachers of empty courses and empty courses may get evaluated!
$possible_active_evaluations = possible_active_evaluations($evaluation);
//$possible_active_evaluations = round($_SESSION["students_active"] * ($_SESSION["Teachers_active"]/$participating_active_courses), 0);
}
// only up to 15 days
if ($forceGlobal and
(!empty($evaluation->participating_active_students) and ($timecloseSaved + (15 * 86400)) < time())) {
$participating_students = $evaluation->participating_students;
$participating_active_students = $evaluation->participating_active_students;
$participating_teachers = $evaluation->participating_teachers;
$participating_active_teachers = $evaluation->participating_active_teachers;
//$_SESSION["distinct_t_active"];
$participating_courses = $evaluation->participating_courses;
$participating_active_courses = $evaluation->participating_active_courses;
$possible_evaluations = $evaluation->possible_evaluations;
$possible_active_evaluations = $evaluation->possible_active_evaluations;
$DB->execute("UPDATE {evaluation} SET courses_of_studies=$courses_of_studies,
participating_students=$participating_students, participating_active_students=$participating_active_students,
participating_courses=$participating_courses, participating_active_courses=$participating_active_courses,
possible_evaluations=$possible_evaluations, possible_active_evaluations=$possible_active_evaluations,
duplicated_replies=$duplicated_replies, teamteaching_courses=$teamteaching_courses,
participating_teachers=$participating_teachers, participating_active_teachers=$participating_active_teachers
WHERE id=$evaluation->id");
} else {
$DB->execute("UPDATE {evaluation} SET courses_of_studies=$courses_of_studies,
participating_students=$participating_students, participating_active_students=$participating_active_students,
participating_courses=$participating_courses, participating_active_courses=$participating_active_courses,
possible_evaluations=$possible_evaluations, possible_active_evaluations=$possible_active_evaluations,
duplicated_replies=$duplicated_replies, teamteaching_courses=$teamteaching_courses,
participating_teachers=$participating_teachers, participating_active_teachers=$participating_active_teachers
WHERE id=$evaluation->id");
}
}
// store per course evaulation details in tables evaluation_enrolments
if ($forceCourse or
!$DB->count_records_sql("SELECT COUNT(*) from {evaluation_enrolments} WHERE evaluation=$evaluation->id")
) { // set evaluation to Open for allowing set_results
$evaluation->timeclose = time() + 86400;
$courses = evaluation_participating_courses($evaluation);
ini_set("output_buffering", 256);
if (true) //evaluation_debug() )
{
print "<br>\nUpdating table evaluation_enrolments<br>\n";
}
$active_students = $active_teachers = 0;
$possible_evaluations = $possible_active_evaluations = 0;
foreach ($courses as $courseid) {
$numTeachersCourse = $numStudentsCourse = $numTeachersActiveCourse = $numStudentsActiveCourse = 0;
$students = get_evaluation_participants($evaluation, false, $courseid, false, true);
$teachers = get_evaluation_participants($evaluation, false, $courseid, true, false);
$course_of_studies = evaluation_get_course_of_studies($courseid, false);
$department = get_department_from_cos($course_of_studies);
$teacherids = $active_teacherids = array();
foreach ($teachers as $teacher) {
$teacherids[] = $teacher['id'];
$numTeachersCourse++;
if ($teacher["lastaccess"] > $timeopen) {
$active_teachers++;
$numTeachersActiveCourse++;
$active_teacherids[] = $teacher['id'];
}
//evaluation_user_lastaccess($evaluation, $teacher["id"], $teacher["lastaccess"], "teacher", $courseid);
}
$teacherids = implode(",", $teacherids);
$active_teacherids = implode(",", $active_teacherids);
$fullname = $shortname = "''";
if ($course = $DB->get_record('course', array('id' => $courseid), '*')) {
$fullname = trim($course->fullname);
$shortname = trim($course->shortname);
}
foreach ($students as $student) {
if ($student["lastaccess"] > $timeopen) {
$active_students++;
$numStudentsActiveCourse++;
}
//evaluation_user_lastaccess($evaluation, $student["id"], $student["lastaccess"], "student", $courseid);
}
$fields =
array("evaluation", "courseid", "fullname", "shortname", "course_of_studies",
"department", "students", "active_students",
"teacherids", "active_teachers", "active_teacherids", "timemodified");
$values = array($evaluation->id, $courseid, $fullname, $shortname, $course_of_studies,
$department, safeCount($students), $numStudentsActiveCourse,
$teacherids, $numTeachersActiveCourse, $active_teacherids, time());
$recObj = new stdClass();
foreach ($fields as $key => $value) {
$recObj->{$value} = $values[$key];
}
$recObj2 =
$DB->get_record_sql("SELECT id from {evaluation_enrolments} WHERE evaluation=$evaluation->id AND courseid=$courseid");
if (isset($recObj2->id) and is_numeric($recObj2->id)) {
$recObj->id = $recObj2->id;
$DB->update_record('evaluation_enrolments', $recObj);
} else {
$DB->insert_record('evaluation_enrolments', $recObj);
}
if (true) //evaluation_debug() )
{
print "<br>\nCourse: $courseid - $fullname\n";
@ob_flush();@ob_end_flush();@flush();@ob_start();
}
if ($students and !empty($teacherids)) {
$possible_evaluations += (safeCount($students) * count(explode(",", $teacherids)));
}
if ($numStudentsActiveCourse and !empty($numTeachersActiveCourse)) {
$possible_active_evaluations += ($numStudentsActiveCourse * $numTeachersActiveCourse);
}
}
if ($possible_evaluations and $possible_active_evaluations) {
if ($evaluationU = $DB->get_record("evaluation", array("id" => $evaluation->id))) {
$evaluationU->possible_evaluations = $possible_evaluations;
$evaluationU->possible_active_evaluations = $possible_active_evaluations;
// $evaluationU->timeclose = $timecloseSaved;
$DB->update_record('evaluation', $evaluationU);
}
}
}
// revert tempoary timeclose
$evaluation->timeclose = $timecloseSaved;
//$DB->update_record('evaluation', $evaluation);
$_SESSION['set_results_' . $evaluation->id] = true;
if (!is_siteadmin()) {
return true;
}
// store evaluation user core data in table evaluation_users
if ($forceUsers or empty($evaluation->possible_evaluations)) /*$evaluation->timeclose > $DB->get_record_sql("SELECT timemodified from {evaluation_users}
ORDER BY timemodified DESC LIMIT 1")->timemodified) */ {
if (true) //evaluation_debug() )
{
print "<br>\nUpdating table evaluation_users<br>\n";
}
$evaluation->timeclose = $timeclose = time() + 86400;
foreach (array("userid", "teacherid") as $participant) {
$cnt = 1;
$completed = $DB->get_records_sql("SELECT $participant AS partid, count(*) AS count
FROM {evaluation_completed}
WHERE evaluation=$evaluation->id
GROUP BY $participant
ORDER BY $participant ASC");
ini_set("output_buffering", 256);
foreach ($completed as $complete) {
$userid = $complete->partid;
$participated = $complete->count;
$user = $DB->get_record_sql("SELECT * FROM {user} WHERE id=$userid ");
if ($user) {
$username = trim($user->username);
$firstname = trim($user->firstname);
$lastname = trim($user->lastname);
$alternatename = trim($user->alternatename);
$email = trim($user->email);
$lastaccess = $user->lastaccess ?: 0;
} else {
continue;
}
$completeU = $DB->get_record_sql("SELECT id, timemodified
FROM {evaluation_completed}
WHERE evaluation=$evaluation->id AND $participant=$userid
ORDER BY timemodified DESC LIMIT 1");
// $lastaccess = evaluation_user_lastaccess($evaluation, $userid, $lastaccess, $participant, $courseid);
$timemodified = $lastaccess;
if ($completeU and $completeU->timemodified > 0) {
$lastaccess = $completeU->timemodified;
}
$fullname = "$firstname $lastname";
$utype = ($participant == "userid" ? "student" : "teacher");
$fields = array("userid", "username", "firstname", "lastname", "alternatename", "email", "$utype", "lastaccess",
"timemodified");
$values = array("$userid", "$username", "$firstname", "$lastname", "$alternatename", "$email", "$participated",
$lastaccess, time());
$recObj = new stdClass();
foreach ($fields as $key => $value) {
$recObj->{$value} = $values[$key];
}
$userID = $DB->get_record_sql("SELECT id,lastaccess from {evaluation_users} WHERE userid=$userid");
// update if existing
if (isset($userID->id) and $userID->id) {
$recObj->id = $userID->id;
//if ( $userID->lastaccess>0 ) { unset($recObj->lastaccess); }
$DB->update_record('evaluation_users', $recObj);
} else {
$DB->insert_record('evaluation_users', $recObj);
}
if (true) //evaluation_debug() )
{
print "<br>\n" . str_pad(number_format($cnt), 6, " ", STR_PAD_LEFT) . ". <b>$participant</b>: " .
str_pad($userid, 6) . " - "
. str_pad($username, 12) . " - " . str_pad($fullname, 42) . " - Replies $utype: $participated\n";
@ob_flush();@ob_end_flush();@flush();@ob_start();
}
$cnt++;
}
}
}
$evaluation->timeclose = $timecloseSaved;
}
return true;
}
function ev_shuffle_completed_userids($evaluation, $force = false) {
global $DB;
if ($evaluation->anonymous < 1) {
print "<br>Funktion ev_shuffle_completed_userids(): Die Evaluation '$evaluation->name' ist nicht anonym.<br>"
. "Daher ist keine Anonymisierung der Abgaben erforderlich!<br>\n";
return;
} else if ($evaluation->anonymized and !$force) {
print "<br>Funktion ev_shuffle_completed_userids(): Die Evaluation '$evaluation->name' wurde bereits anonymisiert.<br>"
. "Daher ist keine Anonymisierung der Abgaben erforderlich!<br>\n";
return;
}
$courses = evaluation_participating_courses($evaluation);
$cntC = 1;
print '<br><hr>ev_shuffle_completed_userids(): Course id = <span id="showCourseRec_' . $evaluation->id . '"> </span><hr>';
ini_set("output_buffering", 350);
@ob_flush();@ob_end_flush();@flush();@ob_start();
foreach ($courses as $courseid) {
print '<script>document.getElementById("showCourseRec_' . $evaluation->id . '").innerHTML = "<b>' . $courseid . '</b> (' .
$cntC . ' courses)";</script>';
$cntC++;
@ob_flush();@ob_end_flush();@flush();@ob_start();
$completed = $DB->get_records_sql("SELECT * FROM {evaluation_completed} WHERE evaluation=$evaluation->id AND courseid=$courseid
ORDER BY id");
$userids = array();
if (safeCount($completed)) {
foreach ($completed as $record) {
$userids[] = $record->userid;
}
//print "<br><hr>userids: " . var_export($userids, true);
shuffle($userids);
$cnt = 0;
foreach ($completed as $record) {
$record->userid = $userids[$cnt];
$cnt++;
$DB->update_record('evaluation_completed', $record);
}
}
}
$recObj = new stdClass();
//$DB->get_record_sql("SELECT * FROM {evaluation} WHERE id=$evaluation->id");
$recObj->id = $evaluation->id;
$recObj->anonymized = 1;
$DB->update_record('evaluation', $recObj);
}
// identify course roles and set permissions
function evaluation_check_Roles_and_Permissions($courseid, $evaluation, $cm, $setD = true, $user = false) {
global $DB, $USER, $CFG;
$evaluationName = $evaluation->name;
$evaluationCourse = $evaluation->course;
$isPermitted = $isTeacher = $isStudent = $SiteEvaluation = false;
$teachers = array();
$courseid = false ? optional_param('courseid', false, PARAM_INT) : $courseid;
$CourseTitle = $CourseName = "";
if (empty($user) or !isset($user->username)) {
$user = $USER;
}
$username = $user->username;
// unset evaluation sessions if required
validate_evaluation_sessions($evaluation);
if ($evaluationCourse == SITEID) {
if ($setD and !defined("SiteEvaluation")) {
define("SiteEvaluation", true);
}
$SiteEvaluation = true;
} else {
$courseid = $evaluationCourse;
}
// if called from inside a course
if ($courseid and $courseid !== SITEID) {
$evaluation_is_open = (evaluation_is_open($evaluation) or $evaluation->timeopen > time());
//$evaluation_is_open = true;
if ($evaluation_is_open) {
$CourseRec = $DB->get_record_sql("SELECT id, fullname, shortname FROM {course} WHERE id = $courseid");
} else {
$CourseRec = $DB->get_record_sql("SELECT DISTINCT ON (courseid) courseid, fullname, shortname, teacherids, id
FROM {evaluation_enrolments} WHERE evaluation = $evaluation->id AND courseid = $courseid");
}
if (isset($CourseRec->fullname)) // || is_object( $CourseRec ))
{
if (empty($_SESSION["LoggedInAs"])) {
$CourseTitle = get_string("course", "evaluation") .
": <span style=\"font-size:12pt;font-weight:bold;\"><a href=\"/course/view.php?id="
. $courseid . "\">" . $CourseRec->fullname . "</a> (" . $CourseRec->shortname . ")</span>";
} else {
$CourseTitle = get_string("course", "evaluation") . ": <span style=\"font-size:12pt;font-weight:bold;\">"
. $CourseRec->fullname . " (" . $CourseRec->shortname . ")</span>";
}
if ($evaluation_is_open) { //(3,4,5,9,12);" ); // get all teachers and students
// get editingteachers, teachers and students
$roleT = $DB->get_records_sql("SELECT * FROM {role} WHERE id IN (3,4,12,5);");
$contextC = context_course::instance($courseid);
if (is_array($contextC) or is_object($contextC)) {
foreach ($roleT as $role) {
$rolesC = get_role_users($role->id, $contextC);
foreach ($rolesC as $roleC) {
if ($username == $roleC->username) { // if student
if ($role->id == 5) {
$isStudent = true;
} else {
$isTeacher = true;
}
}
}
}
}
} else // evaluation is closed
{
if (in_array($user->id, explode(",", $CourseRec->teacherids))) {
$isTeacher = true;
} else {
$isStudent = !empty($DB->get_record_sql("SELECT DISTINCT ON (userid) userid, evaluation, id
FROM {evaluation_users_la}
WHERE evaluation = $evaluation->id AND userid = $user->id AND role='student'"));
}
}
if ($isStudent && !$isTeacher) {
$evaluationcompletion = new mod_evaluation_completion($evaluation, $cm, $courseid);
if ($setD and !defined("isStudent")) {
define("isStudent", true);
}
if ($evaluationcompletion->is_open()) {
if ($setD and !defined("EVALUATION_ALLOWED")) {
define("EVALUATION_ALLOWED", $username);
}
}
}
if ($isTeacher) {
$isPermitted = true;
if ($setD and !defined("isTeacher")) {
define("isTeacher", true);
}
}
$CourseName = $CourseRec->fullname;
}
} // if $courseid
if ($setD AND (evaluation_isPrivilegedUser($evaluation, $user) or is_siteadmin())) //AND $evaluation->course == SITEID) )
{
$isPermitted = true; //( empty($_SESSION['CoS_privileged'][$user->username]) ?true :!$courseid );
if ($setD and !defined("EVALUATION_OWNER")) {
define("EVALUATION_OWNER", $username);
}
}
/*
}
else //if !$SiteEvaluation
{ if ( $setD AND !defined( "EVALUATION_ALLOWED") ) { define( "EVALUATION_ALLOWED", $username ); }
//if ( $setD AND !defined( "isStudent") ) { define( "isStudent", true ); }
}
*/
$showTeachers = "";
if ($setD and $courseid) {
evaluation_showteachers($evaluation, $courseid, $cm->id);
}
return array($isPermitted, $CourseTitle, $CourseName, $SiteEvaluation);
}
function evaluation_isPrivilegedUser($evaluation, $user = false) {
global $USER;
$privileged_users = array();
if (empty($user) or !isset($user->username)) {
$user = $USER;
}
if (isset($_SESSION["EVALUATION_OWNER"]) and $_SESSION["EVALUATION_OWNER"] == $user->username) {
return true;
}
if (!empty($evaluation->privileged_users)) {
$privileged_users = explode("\n", $evaluation->privileged_users);
}
if (in_arrayi($user->username, $privileged_users) or evaluation_cosPrivileged($evaluation)) {
$_SESSION["EVALUATION_OWNER"] = $user->username;
return true;
}
if ( !isset($_SESSION["privileged_global_users"]) OR !is_array($_SESSION["privileged_global_users"])) {
ev_set_privileged_users();
}
if (in_arrayi($user->username, $_SESSION["privileged_global_users"])) {
$_SESSION["EVALUATION_OWNER"] = $user->username;
return true;
}
if (is_siteadmin()) {
$_SESSION["EVALUATION_OWNER"] = $USER->id;
return true;
}
return false;
}
// is user CoS privileged
function evaluation_cosPrivileged($evaluation) {
global $USER;
// get CoS privileged users
if (!isset($_SESSION['CoS_privileged'])) {
$_SESSION['CoS_privileged'] = array();
ev_set_privileged_users();
get_evaluation_filters($evaluation);
}
if (false and evaluation_debug(false)) {
print "<br><hr>:\n_SESSION['CoS_privileged']: " . nl2br(var_export($_SESSION['CoS_privileged'], true)) . "<hr>\n";
}
if (isset($_SESSION['CoS_privileged'][$USER->username]) and !empty($_SESSION['CoS_privileged'][$USER->username])) {
return evaluation_is_cosPrivileged_filter($_SESSION['CoS_privileged'][$USER->username],
$_SESSION['filter_course_of_studies']);
}
return false;
}
function evaluation_is_cosPrivileged_filter($needles, $haystack) {
global $evaluation;
if (empty($needles) OR empty($haystack)){
return false;
}
return array_intersect($needles, $haystack) !== [];
}
// is user CoS privileged users
function evaluation_get_cosPrivileged_filter($evaluation, $tableName = "") {
global $USER, $evaluation;
$filter = "";
$setfilter = false;
// get CoS privileged users
if (!isset($_SESSION['CoS_privileged'])) {
ev_set_privileged_users();
get_evaluation_filters($evaluation);
}
if (false) //evaluation_debug( false ) )
{
print "<br><hr>:\n_SESSION['CoS_privileged']: " . nl2br(var_export($_SESSION['CoS_privileged'], true)) . "<hr>\n";
}
if (!empty($_SESSION['CoS_privileged'][$USER->username]) AND !empty($_SESSION['filter_course_of_studies'])
AND evaluation_is_cosPrivileged_filter($_SESSION['CoS_privileged'][$USER->username],$_SESSION['filter_course_of_studies'])) {
$setfilter = true;
if ($setfilter) {
$filter = " AND " . ($tableName ? $tableName . "." : "") . "course_of_studies ";
$filter .= " IN ('" . implode("','", $_SESSION['CoS_privileged'][$USER->username]) . "')";
}
//else
//{ $filter .= "= '" . $_SESSION['CoS_privileged'][$USER->username][0] . "' "; }
} // exclude WM course_of_studies from list
else if (evaluation_is_WM_disabled($evaluation)) {
$excluded = array();
foreach ($_SESSION["course_of_studies_wm"] as $CoS => $is_WM) {
if ($is_WM) {
$excluded[] = $CoS;
}
}
if (!empty($excluded)) {
$filter = " AND " . ($tableName ? $tableName . "." : "") . "course_of_studies ";
$filter .= " NOT IN ('" . implode("','", $excluded) . "')";
}
}
if (false and !empty($filter) and evaluation_debug(false)) {
print "<br><hr>\n<b>Filter</b>: $filter<hr>\n";
}
return $filter;
}
// privileged_global_users without WM course_of_studies
function evaluation_is_WM_disabled($evaluation) {
global $USER;
if (isset($_SESSION["privileged_global_users"][$USER->username]) and isset($_SESSION["course_of_studies_wm"])
and !isset($_SESSION["privileged_global_users_wm"][$USER->username])
) {
$sg_filter = explode("\n", $evaluation->filter_course_of_studies);
$excluded = array();
foreach ($_SESSION["course_of_studies_wm"] as $CoS => $is_WM) {
if ($is_WM and in_arrayi($CoS, $sg_filter)) {
$excluded[] = $CoS;
}
}
return !empty($excluded);
}
return false;
}
//return all course ids a given mdl_user id is assossiated with
function ev_courses_of_id($evaluation, $userid) {
global $DB;
$evaluation_is_open = (evaluation_is_open($evaluation) or intval(date("Ymd", $evaluation->timeopen)) > intval(date("Ymd")));
$sql = array();
if (true) //$evaluation_is_open )
{
$filter = "";
if ($evaluation->course == SITEID) {
$evaluation_semester = get_evaluation_semester($evaluation);
$filter = " AND RIGHT(c.idnumber,5) = '$evaluation_semester'";
}
$sql = "SELECT DISTINCT ON (e.courseid) e.courseid, e.id, c.shortname, c.fullname, c.idnumber
FROM {enrol} e, {course} c
WHERE e.courseid=c.id AND c.visible=1 $filter AND e.id IN " .
"(SELECT enrolid FROM {user_enrolments} ue WHERE ue.userid=$userid) ORDER BY e.courseid DESC";
}
if (!$evaluation_is_open) {
$filter = evaluation_get_cosPrivileged_filter($evaluation);
$recC = $DB->get_records_sql($sql);
$sql = "SELECT DISTINCT ON (courseid) courseid, id, course_of_studies FROM {evaluation_completed}
WHERE evaluation=$evaluation->id AND (userid=$userid OR teacherid=$userid) $filter ORDER BY courseid DESC";
$recEv = $DB->get_records_sql($sql);
return array_merge_recursive_distinct($recC, $recEv);
}
return $DB->get_records_sql($sql);
}
function ev_CoS_of_id($evaluation, $userid) {
global $DB;
$courses = ev_courses_of_id($evaluation, $userid);
$CoS = array();
//$is_open = evaluation_is_open ($evaluation);
foreach ($courses as $course) {
if (isset($course->course_of_studies)) {
$CoS[$course->course_of_studies] = $course->course_of_studies;
} else {
$cos = evaluation_get_course_of_studies($course->courseid);
if ($cos) {
$CoS[$cos] = $cos;
}
}
}
if (false) //evaluation_debug( false ) )
{
print "<br><hr><b>CoS</b>: " . nl2br(var_export($CoS, true)) . "<hr>Courses:<br>" . nl2br(var_export($courses, true)) .
"<hr>\n";
}
return $CoS;
}
function ev_is_course_in_CoS($evaluation, $courseid) {
global $DB;
$filter = evaluation_get_cosPrivileged_filter($evaluation);
$count =
$DB->count_records_sql("SELECT COUNT(*) FROM {evaluation_completed} WHERE evaluation=$evaluation->id AND courseid=$courseid $filter");
if (false and evaluation_debug(false)) {
print "<br><hr><b>Course $courseid is " . ($count ? "" : "NOT") . " in CoS_privileged courses</b><hr>\n";
}
return $count;
}
function ev_is_user_in_CoS($evaluation, $userid) {
global $DB, $USER;
//$DB->set_debug(true);
$filter = evaluation_get_cosPrivileged_filter($evaluation);
$sql =
"SELECT COUNT(*) FROM {evaluation_completed} WHERE evaluation=$evaluation->id AND ( userid=$userid OR teacherid=$userid ) $filter";
$count = $DB->count_records_sql($sql);
if (false and evaluation_debug(false)) {
print "<br><hr><b>userid $userid is " . ($count ? "" : "NOT") . " in CoS_privileged courses</b><br>sql: $sql<hr>\n";
}
return $count;
}
// get role name of given mdl_user id in course courseid
function ev_roles_in_course($userid, $courseid) {
$context = context_course::instance($courseid);
$roles = get_user_roles($context, $userid, true);
$rolenames = array();
foreach ($roles as $role) {
$rolenames[$role->name] = $role->name;
}
return implode(", ", $rolenames);
}
function get_department_from_cos($cos) {
if ( isset($_SESSION['CoS_department']) AND safeCount($_SESSION['CoS_department']) ) {
$keys = array_keys($_SESSION['CoS_department']);
$dept = array_searchi($cos, $keys);
if ($dept AND isset($_SESSION['CoS_department'][$keys[$dept]]) ) {
$department = $_SESSION['CoS_department'][$keys[$dept]];
return $department;
}
}
return "";
}
// view as teacher, student or standard user
function evaluation_LoginAs() {
global $CFG, $DB, $USER, $PAGE, $OUTPUT, $id, $teacheridSaved,
$courseid, $teacherid, $course_of_studiesID, $evaluation, $downloading;
// !is_siteadmin() AND
if (!(is_siteadmin() OR $USER->username =="harry" OR $USER->username =="bleckerth" OR $USER->username =="gorling" OR $USER->username =="khayat")
AND empty($_SESSION["LoggedInAs"])) {
return false;
}
if ($evaluation->course !== SITEID or !empty($downloading)){
// or (isset($_SESSION["EVALUATION_OWNER"]) and
// $_SESSION["EVALUATION_OWNER"] !== $USER->id and empty($_SESSION["LoggedInAs"]) and empty($USER->realuser))) {
return false;
}
$teachers = $DB->get_records_sql("SELECT DISTINCT ON (teacherid) teacherid, id
from {evaluation_completed}
WHERE evaluation=$evaluation->id ORDER BY teacherid");
$students = $DB->get_records_sql("SELECT DISTINCT ON (userid) userid,
id from {evaluation_completed}
WHERE evaluation=$evaluation->id ORDER BY userid");
$userid = false;
$role = optional_param('LoginAs', "", PARAM_TEXT);
$cmid = $id;
$isActive = "deleted=0 AND suspended=0 AND ";
list($sg_filter, $courses_filter) = get_evaluation_filters($evaluation);
evaluation_cosPrivileged($evaluation);
$CoS_privileged = array();
foreach ($_SESSION['CoS_privileged'] AS $CoSuser=>$CoSA){
foreach ( $CoSA AS $cusername => $CoS){
if ( in_arrayi($CoS, $sg_filter)){
if (!isset($_SESSION['CoS_privileged_sgl'][$CoSuser])) {
$CoS_privileged[$CoSuser] = $CoS;
}
}
}
}
$CoS_privileged_cnt = safeCount($CoS_privileged);
// SGL
$CoS_privileged_sgl = array();
foreach ($_SESSION['CoS_privileged_sgl'] AS $CoSuser=>$CoSA){
foreach ( $CoSA AS $cusername => $CoS){
if ( in_arrayi($CoS, $sg_filter)){
$CoS_privileged_sgl[$CoSuser] = $CoS;
}
}
}
$CoS_privileged_sgl_cnt = safeCount($CoS_privileged_sgl);
if ($role == "logout" and !empty($USER->realuser)) {
$userid = $_SESSION["EVALUATION_OWNER"] = $USER->realuser;
} else if ($role == "privileg") {
// $role = "Privilegierte Person";
if (!empty($evaluation->privileged_users) or !empty($_SESSION["privileged_global_users"])) {
$privileged_users = explode("\n", $evaluation->privileged_users);
if (!empty($_SESSION["privileged_global_users"])) { //array_merge_recursive_distinct( $privileged_users, $_SESSION["privileged_global_users"]);
$privileged_users += $_SESSION["privileged_global_users"];
}
$cnt = 0;
$choice = random_int(0, intval(safeCount($privileged_users)));
if (false) //evaluation_debug( false ) )
{
print "<br><hr>:Selected: $choice\nprivileged_users: " . nl2br(var_export($privileged_users, true)) . "<hr>\n";
}
foreach ($privileged_users as $username) {
if ($cnt == $choice) { //print "<br><hr>Current choice: '$username'<hr>\n";
$user = $DB->get_record_sql("SELECT id,username from {user} WHERE $isActive username='" . trim($username) . "'");
if (isset($user->id)) {
$userid = $user->id;
break;
} else if (safeCount($privileged_users) > $cnt) {
$choice++;
}
}
$cnt++;
}
}
} else if ($role == "priv_sg") { // stristr($role, "priv_sg")) {
// $role = "Privilegierte Person (Studiengang)";
if ($CoS_privileged_cnt) {
$cnt = 0;
$choice = random_int(0, $CoS_privileged_cnt);
if (false) //evaluation_debug( false ) )
{
print "<br><hr>\$CoS_privileged_cnt: $CoS_privileged_cnt:\n_CoS_privileged: "
. nl2br(var_export($CoS_privileged, true)) . "<hr>\n";
}
foreach (array_keys($CoS_privileged) as $uKey) {
if ($cnt == $choice) {
$username = $uKey;
// print "<br><hr>uKey: \n" .nl2br(var_export($uKey,true))."<hr>\n";
if ($user = $DB->get_record_sql("SELECT id from {user} WHERE $isActive username='$username'") and isset($user->id)) {
$userid = $user->id;
break;
} else if ($CoS_privileged_cnt > $cnt) {
$choice++;
}
}
$cnt++;
}
}
} else if ($role == "priv_sg_sgl") {
// $role = "Privilegierte Person (Studiengang, SGL)";
if ($CoS_privileged_sgl_cnt) {
$cnt = 0;
$choice = random_int(0, $CoS_privileged_sgl_cnt);
if (false) //evaluation_debug( false ) )
{
print "<br><hr>\$CoS_privileged_cnt: $CoS_privileged_cnt:\n_CoS_privileged: "
. nl2br(var_export($CoS_privileged, true)) . "<hr>\n";
}
foreach (array_keys($CoS_privileged_sgl) as $uKey) {
if ($cnt == $choice) {
$username = $uKey;
// print "<br><hr>uKey: \n" .nl2br(var_export($uKey,true))."<hr>\n";
if ($user = $DB->get_record_sql("SELECT id from {user} WHERE $isActive username='$username'") and isset($user->id)) {
$userid = $user->id;
break;
} else if ($CoS_privileged_sgl_cnt > $cnt) {
$choice++;
}
}
$cnt++;
}
}
} else if (strstr($role, "teacher")) {
$role = "editingteacher";
if (false AND $teacheridSaved) {
$userid = $teacheridSaved;
} else {
if ( $teachers ) {
$choice = random_int(1, intval(safeCount($teachers)));
$cnt = 1;