This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
forked from villalon/emarking
-
Notifications
You must be signed in to change notification settings - Fork 8
/
locallib.php
2216 lines (2151 loc) · 87.2 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
* @subpackage emarking
* @copyright 2012-2016 Jorge Villalon <[email protected]>
* @copyright 2014 Nicolas Perez <[email protected]>
* @copyright 2014 Carlos Villarroel <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once ($CFG->dirroot . '/mod/emarking/lib.php');
/**
*
* @param unknown $text
* @param unknown $maxlength
* @param unknown $id
* @return unknown|string
*/
function emarking_get_text_view_more($text, $maxlength, $id) {
global $OUTPUT;
if (strlen($text) <= $maxlength) {
return $text;
}
$short = substr($text, 0, $maxlength);
return $short . "... " . emarking_view_more(core_text::strtolower(get_string("viewmore", "mod_emarking")), $text, "regrade", $id);
}
/**
*
* @return multitype:Ambigous <NULL, string> unknown string
*/
function emarking_get_user_lang() {
global $USER;
$lang = $USER->lang;
$parts = explode("_", $lang);
$specific = null;
if (count($parts) > 1) {
$specific = strtoupper($parts[1]);
$lang = $parts[0] . '_' . $specific;
}
return array(
$lang,
$parts[0],
$specific
);
}
/**
* Obtains course module ($cm), course, emarking and context
* objects from cm id in the URL
*
* @return multitype:stdClass context_module unknown mixed
*/
function emarking_get_cm_course_instance() {
global $DB;
// Course module id.
$cmid = required_param('id', PARAM_INT);
return emarking_get_cm_course_instance_by_id($cmid);
}
/**
* Obtains course module ($cm), course, emarking and context
* objects from cm id in the URL
*
* @return multitype:stdClass context_module unknown mixed
*/
function emarking_get_cm_course_instance_by_id($cmid) {
global $DB;
// Validate course module.
if (!$cm = get_coursemodule_from_id('emarking', $cmid)) {
print_error(get_string('invalidcoursemodule', 'mod_emarking') . " id: $cmid");
}
// Validate eMarking activity //TODO: validar draft si está selccionado.
if (!$emarking = $DB->get_record('emarking', array(
'id' => $cm->instance
))) {
print_error(get_string('invalidid', 'mod_emarking') . " id: $cm->id");
}
// Validate course.
if (!$course = $DB->get_record('course', array(
'id' => $emarking->course
))) {
print_error(get_string('invalidcourseid', 'mod_emarking'));
}
$context = context_module::instance($cm->id);
return array(
$cm,
$emarking,
$course,
$context
);
}
/**
*
* @return multitype:string
*/
function emarking_get_regrade_motives_array() {
$output = array(
EMARKING_REGRADE_MISASSIGNED_SCORE,
EMARKING_REGRADE_CORRECT_ALTERNATIVE_ANSWER,
EMARKING_REGRADE_ERROR_CARRIED_FORWARD,
EMARKING_REGRADE_UNCLEAR_FEEDBACK,
EMARKING_REGRADE_STATEMENT_PROBLEM,
EMARKING_REGRADE_OTHER
);
return $output;
}
/**
* Get regrade type in human readable string
*
* @param unknown $type
* @return Ambigous <string, lang_string>|string
*/
function emarking_get_regrade_type_string($type) {
switch ($type) {
case EMARKING_REGRADE_MISASSIGNED_SCORE:
return get_string('missasignedscore', 'mod_emarking');
case EMARKING_REGRADE_UNCLEAR_FEEDBACK:
return get_string('unclearfeedback', 'mod_emarking');
case EMARKING_REGRADE_STATEMENT_PROBLEM:
return get_string('statementproblem', 'mod_emarking');
case EMARKING_REGRADE_ERROR_CARRIED_FORWARD:
return get_string('errorcarriedforward', 'mod_emarking');
case EMARKING_REGRADE_CORRECT_ALTERNATIVE_ANSWER:
return get_string('correctalternativeanswer', 'mod_emarking');
case EMARKING_REGRADE_OTHER:
return get_string('other', 'mod_emarking');
default:
return 'INVALID STATUS';
}
}
/**
* Returns a string indicating how long ago something happened
*
* @param int $time
* in unixtime
* @param string $small
* indicates if the time should be enclosed in a div with a small font
* @return string
*/
function emarking_time_ago($time, $small = false) {
return emarking_time_difference($time, time(), $small);
}
/**
* Returns a string indicating how long ago something happened
*
* @param int $time
* in unixtime
* @param string $small
* indicates if the time should be enclosed in a div with a small font
* @return string
*/
function emarking_time_difference($time1, $time2, $small = false) {
$time = abs($time1 - $time2); // To get the time since that moment.
$ispast = $time1 <= $time2;
$tokens = array(
31536000 => get_string('year'),
2592000 => get_string('month'),
604800 => get_string('week'),
86400 => get_string('day'),
3600 => get_string('hour'),
60 => get_string('minute'),
1 => get_string('second', 'mod_emarking')
);
$tokensplural = array(
31536000 => get_string('years'),
2592000 => get_string('months'),
604800 => get_string('weeks'),
86400 => get_string('days'),
3600 => get_string('hours'),
60 => get_string('minutes'),
1 => get_string('seconds', 'mod_emarking')
);
foreach($tokens as $unit => $text) {
if ($time < $unit) {
continue;
}
$numberofunits = floor($time / $unit);
$message = $numberofunits . ' ' . (($numberofunits > 1) ? $tokensplural[$unit] : $text);
if ($ispast) {
$message = core_text::strtolower(get_string('ago', 'core_message', $message));
} else {
$message = core_text::strtolower($message);
}
if ($small) {
$message = html_writer::div($message, "timeago");
}
return $message;
}
}
/**
* Copies the settings from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @param unknown $rubricoverride
* @param unknown $markersoverride
*/
function emarking_copy_settings($emarkingsrc, $emarkingdst, $rubricoverride, $markersoverride) {
global $DB, $OUTPUT;
$transaction = $DB->start_delegated_transaction();
$criteriaitems = emarking_match_rubrics($emarkingsrc, $emarkingdst, $rubricoverride);
try {
emarking_copy_predefined_comments($emarkingsrc, $emarkingdst);
emarking_copy_pages($emarkingsrc, $emarkingdst, $criteriaitems);
if ($markersoverride) {
emarking_copy_markers($emarkingsrc, $emarkingdst, $criteriaitems);
}
emarking_copy_outcomes($emarkingsrc, $emarkingdst, $criteriaitems, $rubricoverride);
} catch (moodle_exception $exception) {
$DB->rollback_delegated_transaction($transaction, $exception);
return false;
}
// This goes at the end as the emarking object is left unusable.
echo $OUTPUT->box("Copying emarking settings");
$emarkingsrc->id = $emarkingdst->id;
$emarkingsrc->name = $emarkingdst->name;
$emarkingsrc->intro = $emarkingdst->intro;
$emarkingsrc->introformat = $emarkingdst->introformat;
$emarkingsrc->timecreated = $emarkingdst->timecreated;
$emarkingsrc->course = $emarkingdst->course;
if (!$DB->update_record('emarking', $emarkingsrc)) {
$DB->rollback_delegated_transaction($transaction, new moodle_exception("Could not update emarking destination"));
return false;
}
$DB->commit_delegated_transaction($transaction);
echo $OUTPUT->box("Success!");
return true;
}
/**
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @return boolean
*/
function emarking_copy_peer_review($emarkingsrc, $emarkingdst) {
global $DB, $OUTPUT;
// This goes at the end as the emarking object is left unusable.
echo $OUTPUT->box("Copying emarking settings, submission, pages and rubric");
$transaction = $DB->start_delegated_transaction();
try {
emarking_copy_exam($emarkingsrc, $emarkingdst);
emarking_match_rubrics($emarkingsrc, $emarkingdst, true);
emarking_copy_predefined_comments($emarkingsrc, $emarkingdst);
emarking_copy_submissions_drafts($emarkingsrc, $emarkingdst);
emarking_assign_peers($emarkingdst);
} catch (moodle_exception $exception) {
$DB->rollback_delegated_transaction($transaction, $exception);
return false;
}
$emarkingdst->copiedfromparent = 1;
if (!$DB->update_record('emarking', $emarkingdst)) {
$DB->rollback_delegated_transaction($transaction, new moodle_exception("Could not update emarking destination"));
return false;
}
$DB->commit_delegated_transaction($transaction);
echo $OUTPUT->box("Success!");
return true;
}
function emarking_copy_exam($emarkingsrc, $emarkingdst) {
global $DB;
$cmdst = get_coursemodule_from_instance('emarking', $emarkingdst->id);
$contextdst = context_module::instance($cmdst->id);
$fs = get_file_storage();
if (!$examsrc = $DB->get_record('emarking_exams', array(
'emarking' => $emarkingsrc->id
))) {
throw new Exception('Invalid exam from source');
}
unset($examsrc->id);
$examsrc->emarking = $emarkingdst->id;
$examdstid = $DB->insert_record('emarking_exams', $examsrc);
$examfile = $fs->get_file_by_id($examsrc->file);
$filerecord = array(
'component' => 'mod_emarking',
'filearea' => 'exams',
'contextid' => $contextdst->id,
'itemid' => $emarkingdst->id,
'filepath' => '/',
'filename' => $examfile->get_filename()
);
$newexam = $fs->create_file_from_storedfile($filerecord, $examfile);
$examsrc->file = $newexam->get_id();
$examsrc->id = $examdstid;
$DB->update_record('emarking_exams', $examsrc);
}
/**
* Copies the submissions and drafts from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
*/
function emarking_copy_submissions_drafts($emarkingsrc, $emarkingdst) {
global $DB, $OUTPUT;
$cmsrc = get_coursemodule_from_instance('emarking', $emarkingsrc->id);
$cmdst = get_coursemodule_from_instance('emarking', $emarkingdst->id);
$contextsrc = context_module::instance($cmsrc->id);
$contextdst = context_module::instance($cmdst->id);
$fs = get_file_storage();
$submissions = $DB->get_records('emarking_submission', array(
'emarking' => $emarkingsrc->id
));
foreach($submissions as $submission) {
$pages = $DB->get_records('emarking_page', array(
'submission' => $submission->id
));
$drafts = $DB->get_records('emarking_draft', array(
'submissionid' => $submission->id
));
unset($submission->id);
$submission->emarking = $emarkingdst->id;
$submission->status = min(EMARKING_STATUS_SUBMITTED, $submission->status);
$submission->grade = $emarkingsrc->grademin;
$submission->seenbystudent = 0;
$submission->answerkey = 0;
$submission->id = $DB->insert_record('emarking_submission', $submission);
foreach($pages as $page) {
unset($page->id);
$page->submission = $submission->id;
$page->id = $DB->insert_record('emarking_page', $page);
$pagefile = $fs->get_file_by_id($page->file);
$filerecord = array(
'component' => 'mod_emarking',
'filearea' => 'pages',
'contextid' => $contextdst->id,
'itemid' => $emarkingdst->id,
'filepath' => '/',
'filename' => $pagefile->get_filename()
);
$newpage = $fs->create_file_from_storedfile($filerecord, $pagefile);
$page->file = $newpage->get_id();
$pagefileanonymous = $fs->get_file_by_id($page->fileanonymous);
$filerecord = array(
'component' => 'mod_emarking',
'filearea' => 'pages',
'contextid' => $contextdst->id,
'itemid' => $emarkingdst->id,
'filepath' => '/',
'filename' => $pagefileanonymous->get_filename()
);
$newpageanonymous = $fs->create_file_from_storedfile($filerecord, $pagefileanonymous);
$page->fileanonymous = $newpageanonymous->get_id();
$DB->update_record('emarking_page', $page);
}
foreach($drafts as $draft) {
unset($draft->id);
$draft->submissionid = $submission->id;
$draft->status = min(EMARKING_STATUS_SUBMITTED, $draft->status);
$draft->grade = $emarkingsrc->grademin;
$draft->seenbystudent = 0;
$draft->emarkingid = $emarkingdst->id;
$draft->id = $DB->insert_record('emarking_draft', $draft);
}
}
}
/**
* Matches the rubrics from two emarking activities making sure it has the same
* criteria (two criteria are equal if their description are equal).
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @param unknown $context
* @throws moodle_exception
* @return multitype:unknown
*/
function emarking_match_rubrics($emarkingsrc, $emarkingdst, $copyrubric = false) {
global $DB;
$cmsrc = get_coursemodule_from_instance('emarking', $emarkingsrc->id);
$cmdst = get_coursemodule_from_instance('emarking', $emarkingdst->id);
$contextsrc = context_module::instance($cmsrc->id);
$contextdst = context_module::instance($cmdst->id);
list ($gradingmanagersrc, $gradingmethodsrc, $definitionsrc, $rubriccontrollersrc) = emarking_validate_rubric($contextsrc, false, false);
list ($gradingmanagerdst, $gradingmethoddst, $definitiondst, $rubriccontrollerdst) = emarking_validate_rubric($contextdst, false, false);
if ($copyrubric) {
if (!$rubriccontrollerdst) {
$method = $gradingmanagerdst->get_active_method();
$rubriccontrollerdst = $gradingmanagerdst->get_controller($method);
}
if ($rubriccontrollerdst->get_definition()) {
$rubriccontrollerdst->delete_definition();
}
$rubriccontrollerdst->update_definition($rubriccontrollersrc->get_definition_copy($rubriccontrollerdst));
$DB->set_field('grading_definitions', 'timecopied', time(), array(
'id' => $definitionsrc->id
));
list ($gradingmanagerdst, $gradingmethoddst, $definitiondst, $rubriccontrollerdst) = emarking_validate_rubric($contextdst, false, false);
}
if (!$definitionsrc || !$definitiondst) {
throw new moodle_exception("Invalid rubrics for copying, destination emarking doesn't have a rubric and you didn't selected to copy it.");
}
$criteriasrc = $definitionsrc->rubric_criteria;
$criteriadst = $definitiondst->rubric_criteria;
if (count($criteriasrc) != count($criteriadst)) {
throw new moodle_exception("Invalid rubric for copying, they don't have the same number of criteria");
}
$criteriaitems = array();
foreach($criteriasrc as $criterionsrc) {
foreach($criteriadst as $criteriondst) {
if ($criterionsrc['description'] === $criteriondst['description']) {
$criteriaitems[$criterionsrc['id']] = $criteriondst['id'];
}
}
}
if (count($criteriaitems) != count($criteriadst)) {
throw new moodle_exception("Not every criterion name matches in both source and destination emarking activities.");
}
return $criteriaitems;
}
/**
* Copies the pages settings from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
*/
function emarking_copy_pages($emarkingsrc, $emarkingdst, $criteriaitems) {
global $DB, $OUTPUT;
echo $OUTPUT->box("Copying pages settings");
$DB->delete_records('emarking_page_criterion', array(
'emarking' => $emarkingdst->id
));
$pagescriteria = $DB->get_records('emarking_page_criterion', array(
'emarking' => $emarkingsrc->id
));
foreach($pagescriteria as $pagecriterion) {
$newpagecriterion = new stdClass();
$newpagecriterion->emarking = $emarkingdst->id;
$newpagecriterion->page = $pagecriterion->page;
$newpagecriterion->criterion = $criteriaitems[$pagecriterion->criterion];
$newpagecriterion->block = $pagecriterion->block;
$newpagecriterion->timecreated = time();
$newpagecriterion->timemodified = time();
$DB->insert_record('emarking_page_criterion', $newpagecriterion);
}
echo $OUTPUT->box("Success!");
}
/**
* Copies the markers settings from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
*/
function emarking_copy_markers($emarkingsrc, $emarkingdst, $criteriaitems) {
global $DB, $OUTPUT;
echo $OUTPUT->box("Copying markers settings");
$DB->delete_records('emarking_marker_criterion', array(
'emarking' => $emarkingdst->id
));
$markerscriteria = $DB->get_records('emarking_marker_criterion', array(
'emarking' => $emarkingsrc->id
));
foreach($markerscriteria as $markercriterion) {
$context = context_course::instance($emarkingdst->course);
if (!has_capability('mod/emarking:grade', $context, $markercriterion->marker)) {
$manual = enrol_get_plugin('manual');
$instance = $DB->get_record('enrol', array(
'courseid' => $emarkingdst->course,
'enrol' => 'manual'
));
$role = $DB->get_record('role', array(
'archetype' => 'teacher'
));
$manual->enrol_user($instance, $markercriterion->marker, $role->id);
}
$newmarkercriterion = new stdClass();
$newmarkercriterion->emarking = $emarkingdst->id;
$newmarkercriterion->marker = $markercriterion->marker;
$newmarkercriterion->criterion = $criteriaitems[$markercriterion->criterion];
$newmarkercriterion->block = $markercriterion->block;
$newmarkercriterion->timecreated = time();
$newmarkercriterion->timemodified = time();
$DB->insert_record('emarking_marker_criterion', $newmarkercriterion);
}
echo $OUTPUT->box("Success!");
}
/**
* Copies the outcomes settings from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @param unknown $criteriaitems
* @param unknown $override
* @throws moodle_exception
*/
function emarking_copy_outcomes($emarkingsrc, $emarkingdst, $criteriaitems, $override) {
global $DB, $OUTPUT;
if (!emarking_validate_outcomes($emarkingsrc, $emarkingdst)) {
if (!$override) {
throw new moodle_exception("Outcomes in destination emarking do not fit the used in the source and no override was indicated");
} else {
if (!emarking_copy_course_outcomes($emarkingsrc, $emarkingdst)) {
throw new moodle_exception("Overriding outcomes failed, probably due to outcomes being used");
}
}
}
echo $OUTPUT->box("Copying outcomes");
$DB->delete_records('emarking_outcomes_criteria', array(
'emarking' => $emarkingdst->id
));
$outcomescriteria = $DB->get_records('emarking_outcomes_criteria', array(
'emarking' => $emarkingsrc->id
));
foreach($outcomescriteria as $outcomecriterionsrc) {
$outcomecriterion = new stdClass();
$outcomecriterion->emarking = $emarkingdst->id;
$outcomecriterion->outcome = $outcomecriterionsrc->outcome;
$outcomecriterion->criterion = $criteriaitems[$outcomecriterionsrc->criterion];
$outcomecriterion->timecreated = time();
$DB->insert_record('emarking_outcomes_criteria', $outcomecriterion);
}
echo $OUTPUT->box("Success!");
}
/**
* Validates that all outcomes required from the emarking source
* activity are available in the destination course
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @return boolean
*/
function emarking_validate_outcomes($emarkingsrc, $emarkingdst) {
global $DB, $CFG;
require_once ($CFG->libdir . "/grade/grade_outcome.php");
if (!$coursesrc = $DB->get_record('course', array(
'id' => $emarkingsrc->course
))) {
return false;
}
if (!$coursedst = $DB->get_record('course', array(
'id' => $emarkingdst->course
))) {
return false;
}
$outcomessrc = grade_outcome::fetch_all_available($emarkingsrc->course);
$outcomesdst = grade_outcome::fetch_all_available($emarkingdst->course);
$destinationoutcomes = array();
foreach($outcomesdst as $outcomedst) {
$destinationoutcomes[] = $outcomedst->id;
}
foreach($outcomessrc as $outcomesrc) {
if (!array_search($outcomesrc->id, $destinationoutcomes)) {
return false;
}
}
return true;
}
/**
* Copies the outcomes (and associates to course if necessary) from
* a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
* @return boolean
*/
function emarking_copy_course_outcomes($emarkingsrc, $emarkingdst) {
global $DB, $CFG;
require_once ($CFG->libdir . "/gradelib.php");
require_once ($CFG->libdir . "/grade/grade_outcome.php");
if (!$coursesrc = $DB->get_record('course', array(
'id' => $emarkingsrc->course
))) {
return false;
}
if (!$coursedst = $DB->get_record('course', array(
'id' => $emarkingdst->course
))) {
return false;
}
$params = array(
$coursesrc->id,
$emarkingsrc->id
);
$sql = "SELECT outcomeid, itemnumber
FROM {grade_items}
WHERE courseid=? AND outcomeid IS NOT NULL
AND itemmodule = 'emarking' AND iteminstance = ?
AND itemtype = 'mod'";
$tocopy = $DB->get_records_sql($sql, $params);
$outcomesready = array();
foreach($tocopy as $outcomeused) {
$outcomesready[] = $outcomeused->outcomeid;
}
$params = array(
$coursedst->id,
$emarkingdst->id
);
$sql = "SELECT outcomeid, itemnumber
FROM {grade_items}
WHERE courseid=? AND outcomeid IS NOT NULL
AND itemmodule = 'emarking' AND iteminstance = ?
AND itemtype = 'mod'";
$realused = $DB->get_records_sql($sql, $params);
$maxitemnumber = 999;
foreach($realused as $outcomeused) {
if (array_search($outcomeused->outcomeid, $outcomesready)) {
array_remove_by_value($outcomesready, $outcomeused->outcomeid);
}
if ($outcomeused->itemnumber > $maxitemnumber) {
$maxitemnumber = $outcomeused->itemnumber;
}
}
$outcomesdst = grade_outcome::fetch_all_available($emarkingdst->course);
$outcomesavailable = array();
foreach($outcomesdst as $outcomedst) {
$outcomesavailable[] = $outcomedst->id;
}
if ($maxitemnumber < 1000) {
$maxitemnumber = 1000;
}
foreach($outcomesready as $outcometocopy) {
$outcome = grade_outcome::fetch(array(
'id' => $outcometocopy
));
if (!array_search($outcometocopy, $outcomesavailable)) {
$outcome->use_in($emarkingdst->course);
}
$outcomeitem = new grade_item();
$outcomeitem->courseid = $emarkingdst->course;
$outcomeitem->itemtype = 'mod';
$outcomeitem->itemmodule = 'emarking';
$outcomeitem->iteminstance = $emarkingdst->id;
$outcomeitem->itemnumber = $maxitemnumber;
$outcomeitem->itemname = $outcome->fullname;
$outcomeitem->outcomeid = $outcome->id;
$outcomeitem->gradetype = GRADE_TYPE_SCALE;
$outcomeitem->scaleid = $outcome->scaleid;
$outcomeitem->insert();
$maxitemnumber++;
}
return true;
}
/**
* Returns a recordset of emarking objects which are in courses parallel
* to a course parameter
*
* @param unknown $course
* @param unknown $includeown
* @return multitype:
*/
function emarking_get_parallel_emarkings($course, $includeown) {
global $DB;
$parallelcourses = emarking_get_parallel_courses($course, $includeown);
if (!$parallelcourses) {
return false;
}
$parallelsids = array();
foreach($parallelcourses as $parallelcourse) {
if ($parallelcourse->id == $course->id) {
continue;
}
foreach(get_coursemodules_in_course('emarking', $parallelcourse->id) as $cmdst) {
$parallelsids[] = $cmdst->instance;
}
}
$parallelsids = implode(",", $parallelsids);
$parallelemarkings = $DB->get_records_sql("
SELECT e.*, c.id as courseid, c.shortname, c.fullname
FROM {emarking} AS e
INNER JOIN {course} AS c ON (e.course = c.id AND c.id <> ?)
WHERE e.id IN ($parallelsids)
ORDER BY c.fullname, e.name", array(
$course->id
));
return $parallelemarkings;
}
/**
* Copies predefined comments from a source emarking activity to a destination one
*
* @param unknown $emarkingsrc
* @param unknown $emarkingdst
*/
function emarking_copy_predefined_comments($emarkingsrc, $emarkingdst) {
global $DB, $OUTPUT;
echo $OUTPUT->box("Copying predefined comments");
$predefinedsrc = $DB->get_records('emarking_predefined_comment', array(
'emarkingid' => $emarkingsrc->id
));
if (!$predefinedsrc || count($predefinedsrc) == 0) {
echo $OUTPUT->box("No comments to copy");
return true;
}
$DB->delete_records('emarking_predefined_comment', array(
'emarkingid' => $emarkingdst->id
));
foreach($predefinedsrc as $comment) {
unset($comment->id);
$comment->emarkingid = $emarkingdst->id;
$DB->insert_record('emarking_predefined_comment', $comment);
}
echo $OUTPUT->box("Success!");
}
/**
* Returns the HTML for a jquery dialog which will show the content
*
* @param string $title
* @param string $content
* @param string $prefix
* @param int $id
* @return string
*/
function emarking_view_more($title, $content, $prefix, $id) {
$output = "<div id='$prefix" . "-$id' style='display:none;'>";
$output .= $content;
$output .= "</div>";
$output .= "<a class='viewmore' style='' onclick='$(\"#$prefix" . "-$id\").dialog({title:\"$title\",show: { effect: \"scale\", duration: 200 },modal:true,buttons:{" . get_string("close", "mod_emarking") . ": function(){\$(this).dialog(\"close\");}}});'>" . $title . "</a>";
return $output;
}
/**
* Gets the icon used the a submission status
*
* @param int $status
* the submission status
* @param boolean $prefix
* if the icon should include the status description as prefix
* @param int $pctmarked
* if a percentage of marking for the draft is given, when 100% replaces grading for ready for publishing
* @return string the icon HTML
*/
function emarking_get_draft_status_icon($status, $prefix = false, $pctmarked = 0) {
global $OUTPUT;
$html = '';
switch ($status) {
case EMARKING_STATUS_MISSING:
$html = $OUTPUT->pix_icon('i/warning', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_ABSENT:
$html = $OUTPUT->pix_icon('t/block', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_SUBMITTED:
$html = $OUTPUT->pix_icon('i/user', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_GRADING:
if ($pctmarked > 0) {
$html = $OUTPUT->pix_icon('i/grade_partiallycorrect', emarking_get_string_for_status($status, $pctmarked));
} else {
$html = $OUTPUT->pix_icon('i/grade_partiallycorrect', emarking_get_string_for_status($status));
}
break;
case EMARKING_STATUS_PUBLISHED:
$html = $OUTPUT->pix_icon('i/grade_correct', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_REGRADING:
$html = $OUTPUT->pix_icon('i/flagged', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_REGRADING_RESPONDED:
$html = $OUTPUT->pix_icon('i/grade_correct', emarking_get_string_for_status($status));
break;
case EMARKING_STATUS_ACCEPTED:
$html = $OUTPUT->pix_icon('t/locked', emarking_get_string_for_status($status));
break;
default:
$html = $OUTPUT->pix_icon('t/block', emarking_get_string_for_status(EMARKING_STATUS_ABSENT));
break;
}
if ($prefix) {
$html = emarking_get_string_for_status($status, $pctmarked) . " " . $html;
}
return $html;
}
/**
* All possible statuses for an eMarking submission
*
* @return multitype:string array of strings
*/
function emarking_get_statuses_as_array() {
$statuses = array();
$statuses[] = EMARKING_STATUS_MISSING;
$statuses[] = EMARKING_STATUS_ABSENT;
$statuses[] = EMARKING_STATUS_SUBMITTED;
$statuses[] = EMARKING_STATUS_GRADING;
$statuses[] = EMARKING_STATUS_PUBLISHED;
$statuses[] = EMARKING_STATUS_REGRADING;
$statuses[] = EMARKING_STATUS_ACCEPTED;
return $statuses;
}
/**
* Returns an associative array with regrade motives IDs and their
* descriptions
*
* @return multitype:associative array with keys and descriptions
*/
function emarking_get_regrade_motives() {
$motives = array();
foreach(emarking_get_regrade_motives_array() as $m) {
$motive = new stdClass();
$motive->id = $m;
$motive->description = emarking_get_regrade_type_string($m);
$motives[] = $motive;
}
return $motives;
}
/**
* Creates an array with the navigation tabs for emarking
*
* @param unknown $context
* The course context to validate capabilit
* @param unknown $cm
* The course module (emarking activity)
* @return multitype:tabobject
*/
function emarking_tabs($context, $cm, $emarking, $draft=null) {
global $CFG;
$usercangrade = has_capability("mod/emarking:grade", $context);
$issupervisor = has_capability("mod/emarking:supervisegrading", $context);
$enabledreports = isset($CFG->emarking_reportsenabled) ? explode(',',$CFG->emarking_reportsenabled) : Array();
$enabledregrading = isset($CFG->emarking_enableregrading) && $CFG->emarking_enableregrading == 1;
$enabledconfigtab = isset($CFG->emarking_enableconfigtab) && $CFG->emarking_enableconfigtab == 1;
$tabs = array();
// Print tab.
$printtab = new tabobject('printscan', $CFG->wwwroot . "/mod/emarking/print/exam.php?id={$cm->id}", $emarking->type == EMARKING_TYPE_PRINT_ONLY ? get_string('print', 'mod_emarking') : get_string('type_print_scan', 'mod_emarking'));
// Print summary tab.
$printtab->subtree[] = new tabobject('myexams', $CFG->wwwroot . "/mod/emarking/print/exam.php?id={$cm->id}", get_string('exam', 'mod_emarking'));
// Scan tab.
$scantab = new tabobject("scan", $CFG->wwwroot . "/mod/emarking/view.php?id={$cm->id}&scan=1", get_string('exams', 'mod_emarking'));
$scanlist = new tabobject("scanlist", $CFG->wwwroot . "/mod/emarking/view.php?id={$cm->id}&scan=1", get_string('view'));
$uploadanswers = new tabobject("uploadanswers", $CFG->wwwroot . "/mod/emarking/print/uploadanswers.php?id={$cm->id}", get_string('uploadanswers', 'mod_emarking'));
$orphanpages = new tabobject('orphanpages', $CFG->wwwroot . "/mod/emarking/print/orphanpages.php?id={$cm->id}", get_string('orphanpages', 'mod_emarking'));
$scantab->subtree[] = $scanlist;
if ($usercangrade
&& $issupervisor
&& $emarking->type != EMARKING_TYPE_PRINT_ONLY
&& $emarking->uploadtype == EMARKING_UPLOAD_QR) {
$printtab->subtree[] = $uploadanswers;
$printtab->subtree[] = $orphanpages;
}
// Grade tab.
$markingtab = new tabobject("grade", $CFG->wwwroot . "/mod/emarking/view.php?id={$cm->id}", get_string('onscreenmarking', 'mod_emarking'));
$markingtab->subtree[] = new tabobject("mark", $CFG->wwwroot . "/mod/emarking/view.php?id={$cm->id}", get_string("marking", 'mod_emarking'));
$regradestab = new tabobject("regrades", $CFG->wwwroot . "/mod/emarking/marking/regraderequests.php?id={$cm->id}", get_string("regrades", 'mod_emarking'));
if (!$usercangrade) {
if ($emarking->peervisibility) {
$markingtab->subtree[] = new tabobject("ranking", $CFG->wwwroot . "/mod/emarking/reports/ranking.php?id={$cm->id}", get_string("ranking", 'mod_emarking'));
$markingtab->subtree[] = new tabobject("viewpeers", $CFG->wwwroot . "/mod/emarking/reports/viewpeers.php?id={$cm->id}", get_string("reviewpeersfeedback", 'mod_emarking'));
}
if ($enabledregrading && $emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING && $draft != null && $draft->status >= EMARKING_STATUS_PUBLISHED) {
$markingtab->subtree[] = $regradestab;
}
} else {
if ($enabledregrading && has_capability('mod/emarking:regrade', $context) && $emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING) {
$markingtab->subtree[] = $regradestab;
}
if($emarking->evaluatefeedback && has_capability('mod/emarking:grade', $context)){
$markingtab->subtree[] = new tabobject("evalfeedback", $CFG->wwwroot . "/mod/emarking/marking/summaryevalfeedback.php?id={$cm->id}", 'Evaluación del Feedback');
}
}
// Settings tab.
$settingstab = new tabobject("settings", $CFG->wwwroot . "/mod/emarking/marking/settings.php?id={$cm->id}", get_string('settings'), "title", true);
// Settings for marking.
if ($emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING) {
$settingstab->subtree[] = new tabobject("osmsettings", $CFG->wwwroot . "/mod/emarking/marking/settings.php?id={$cm->id}", get_string("marking", 'mod_emarking'));
$settingstab->subtree[] = new tabobject("comment", $CFG->wwwroot . "/mod/emarking/marking/predefinedcomments.php?id={$cm->id}&action=list", get_string("predefinedcomments", 'mod_emarking'));
if (has_capability('mod/emarking:assignmarkers', $context)) {
if($emarking->uploadtype != EMARKING_UPLOAD_FILE)
{
$settingstab->subtree[] = new tabobject("markers", $CFG->wwwroot . "/mod/emarking/marking/markers.php?id={$cm->id}", get_string("markerspercriteria", 'mod_emarking'));
$settingstab->subtree[] = new tabobject("pages", $CFG->wwwroot . "/mod/emarking/marking/pages.php?id={$cm->id}", core_text::strtotitle(get_string("pagespercriteria", 'mod_emarking')));
}
$settingstab->subtree[] = new tabobject("outcomes", $CFG->wwwroot . "/mod/emarking/marking/outcomes.php?id={$cm->id}", core_text::strtotitle(get_string("outcomes", "grades")));
$settingstab->subtree[] = new tabobject("importrubric", $CFG->wwwroot . "/mod/emarking/marking/importrubric.php?id={$cm->id}&action=list", get_string("importrubric", 'mod_emarking'));
$settingstab->subtree[] = new tabobject("export", $CFG->wwwroot . "/mod/emarking/marking/export.php?id={$cm->id}", core_text::strtotitle(get_string("export", "mod_data")));
}
}
// Grade report tab.
$gradereporttab = new tabobject("gradereport", $CFG->wwwroot . "/mod/emarking/reports/feedback.php?id={$cm->id}", get_string("reports", "mod_emarking"));
if(in_array(EMARKING_REPORT_FEEDBACK, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("feedback", $CFG->wwwroot . "/mod/emarking/reports/feedback.php?id={$cm->id}", get_string("feedback", "mod_emarking"));
}
if(in_array(EMARKING_REPORT_GRADES, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("report", $CFG->wwwroot . "/mod/emarking/reports/grades.php?id={$cm->id}", get_string("grades", "grades"));
}
if(in_array(EMARKING_REPORT_MARKING, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("markingreport", $CFG->wwwroot . "/mod/emarking/reports/marking.php?id={$cm->id}", get_string("marking", 'mod_emarking'));
}
if(in_array(EMARKING_REPORT_RANKING, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("ranking", $CFG->wwwroot . "/mod/emarking/reports/ranking.php?id={$cm->id}", get_string("ranking", 'mod_emarking'));
}
if ($emarking->justiceperception > EMARKING_JUSTICE_DISABLED && in_array(EMARKING_REPORT_JUSTICE, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("justicereport", $CFG->wwwroot . "/mod/emarking/reports/justice.php?id={$cm->id}", get_string("justice", 'mod_emarking'));
}
if(in_array(EMARKING_REPORT_OUTCOMES, $enabledreports)) {
$gradereporttab->subtree[] = new tabobject("outcomesreport", $CFG->wwwroot . "/mod/emarking/reports/outcomes.php?id={$cm->id}", get_string("outcomes", "grades"));
}
// Tabs sequence.
if ($usercangrade) {
// Print tab goes always except for markers training.
if ($emarking->type == EMARKING_TYPE_PRINT_ONLY || $emarking->type == EMARKING_TYPE_PRINT_SCAN || $emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING || $emarking->type == EMARKING_TYPE_PEER_REVIEW) {
if (has_capability('mod/emarking:addinstance', $context)) {
if (count($printtab->subtree) == 1) {
$tabs[] = $printtab->subtree[0];
} else {
$tabs[] = $printtab;
}
}
}
// Scan or enablescan tab.
if ($emarking->type == EMARKING_TYPE_PRINT_SCAN) {
$tabs[] = $scantab;
}
// OSM tabs, either marking, reports and settings or enable osm.
if ($emarking->type == EMARKING_TYPE_ON_SCREEN_MARKING || $emarking->type == EMARKING_TYPE_PEER_REVIEW) {
if(count($markingtab->subtree) == 1) {
$tabs[] = $markingtab->subtree[0];
} else {
$tabs[] = $markingtab;
}
if (count($gradereporttab->subtree) == 1) {
$tabs[] = $gradereporttab->subtree[0];
} else {
$tabs[] = $gradereporttab;
}
if ($issupervisor && $enabledconfigtab) {
$tabs[] = $settingstab;
}
}
} else
if ($emarking->type == EMARKING_TYPE_PRINT_SCAN) {
// This case is for students (user can not grade).
$tabs = $scantab->subtree;
} else
if ($emarking->type == EMARKING_TYPE_PRINT_ONLY) {
// This case is for students (user can not grade).
$tabs = array();
} else {
// This case is for students (user can not grade).
$tabs = $markingtab->subtree;
}
return $tabs;
}
/**
* Navigation tabs for printing orders
*
* @param unknown $category
* The category object
* @return multitype:tabobject array of tabobjects
*/
function emarking_printoders_tabs($categoryid) {
$tabs = array();
// Print orders.
$tabs[] = new tabobject("printorders", new moodle_url("/mod/emarking/print/printorders.php", array(
"category" => $categoryid,
"status" => 1
)), get_string("printorders", 'mod_emarking'));
// Print orders history.
$tabs[] = new tabobject("printordershistory", new moodle_url("/mod/emarking/print/printorders.php", array(
"category" => $categoryid,
"status" => 2
)), get_string("records", 'mod_emarking'));
// Statistics.
$statstab = new tabobject("statistics", new moodle_url("/mod/emarking/reports/print.php", array(
"category" => $categoryid
)), get_string("reports", 'mod_emarking'));
// Print statistics.
$statstab->subtree[] = new tabobject("print", new moodle_url("/mod/emarking/reports/print.php", array(
"category" => $categoryid
)), get_string("statistics", 'mod_emarking'));
// Print statistics details.
$statstab->subtree[] = new tabobject("printdetails", new moodle_url("/mod/emarking/reports/printdetails.php", array(
"category" => $categoryid
)), get_string("printdetails", 'mod_emarking'));
$tabs[] = $statstab;
return $tabs;
}