forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
3928 lines (3437 loc) · 151 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 internal classes and functions for module workshop
*
* All the workshop specific functions, needed to implement the module
* logic, should go to here. Instead of having bunch of function named
* workshop_something() taking the workshop instance as the first
* parameter, we use a class workshop that provides all methods.
*
* @package mod_workshop
* @copyright 2009 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/lib.php'); // we extend this library here
require_once($CFG->libdir . '/gradelib.php'); // we use some rounding and comparing routines here
require_once($CFG->libdir . '/filelib.php');
/**
* Full-featured workshop API
*
* This wraps the workshop database record with a set of methods that are called
* from the module itself. The class should be initialized right after you get
* $workshop, $cm and $course records at the begining of the script.
*/
class workshop {
/** error status of the {@link self::add_allocation()} */
const ALLOCATION_EXISTS = -9999;
/** the internal code of the workshop phases as are stored in the database */
const PHASE_SETUP = 10;
const PHASE_SUBMISSION = 20;
const PHASE_ASSESSMENT = 30;
const PHASE_EVALUATION = 40;
const PHASE_CLOSED = 50;
/** the internal code of the examples modes as are stored in the database */
const EXAMPLES_VOLUNTARY = 0;
const EXAMPLES_BEFORE_SUBMISSION = 1;
const EXAMPLES_BEFORE_ASSESSMENT = 2;
/** @var cm_info course module record */
public $cm;
/** @var stdclass course record */
public $course;
/** @var stdclass context object */
public $context;
/** @var int workshop instance identifier */
public $id;
/** @var string workshop activity name */
public $name;
/** @var string introduction or description of the activity */
public $intro;
/** @var int format of the {@link $intro} */
public $introformat;
/** @var string instructions for the submission phase */
public $instructauthors;
/** @var int format of the {@link $instructauthors} */
public $instructauthorsformat;
/** @var string instructions for the assessment phase */
public $instructreviewers;
/** @var int format of the {@link $instructreviewers} */
public $instructreviewersformat;
/** @var int timestamp of when the module was modified */
public $timemodified;
/** @var int current phase of workshop, for example {@link workshop::PHASE_SETUP} */
public $phase;
/** @var bool optional feature: students practise evaluating on example submissions from teacher */
public $useexamples;
/** @var bool optional feature: students perform peer assessment of others' work (deprecated, consider always enabled) */
public $usepeerassessment;
/** @var bool optional feature: students perform self assessment of their own work */
public $useselfassessment;
/** @var float number (10, 5) unsigned, the maximum grade for submission */
public $grade;
/** @var float number (10, 5) unsigned, the maximum grade for assessment */
public $gradinggrade;
/** @var string type of the current grading strategy used in this workshop, for example 'accumulative' */
public $strategy;
/** @var string the name of the evaluation plugin to use for grading grades calculation */
public $evaluation;
/** @var int number of digits that should be shown after the decimal point when displaying grades */
public $gradedecimals;
/** @var int number of allowed submission attachments and the files embedded into submission */
public $nattachments;
/** @var bool allow submitting the work after the deadline */
public $latesubmissions;
/** @var int maximum size of the one attached file in bytes */
public $maxbytes;
/** @var int mode of example submissions support, for example {@link workshop::EXAMPLES_VOLUNTARY} */
public $examplesmode;
/** @var int if greater than 0 then the submission is not allowed before this timestamp */
public $submissionstart;
/** @var int if greater than 0 then the submission is not allowed after this timestamp */
public $submissionend;
/** @var int if greater than 0 then the peer assessment is not allowed before this timestamp */
public $assessmentstart;
/** @var int if greater than 0 then the peer assessment is not allowed after this timestamp */
public $assessmentend;
/** @var bool automatically switch to the assessment phase after the submissions deadline */
public $phaseswitchassessment;
/** @var string conclusion text to be displayed at the end of the activity */
public $conclusion;
/** @var int format of the conclusion text */
public $conclusionformat;
/** @var int the mode of the overall feedback */
public $overallfeedbackmode;
/** @var int maximum number of overall feedback attachments */
public $overallfeedbackfiles;
/** @var int maximum size of one file attached to the overall feedback */
public $overallfeedbackmaxbytes;
/**
* @var workshop_strategy grading strategy instance
* Do not use directly, get the instance using {@link workshop::grading_strategy_instance()}
*/
protected $strategyinstance = null;
/**
* @var workshop_evaluation grading evaluation instance
* Do not use directly, get the instance using {@link workshop::grading_evaluation_instance()}
*/
protected $evaluationinstance = null;
/**
* Initializes the workshop API instance using the data from DB
*
* Makes deep copy of all passed records properties.
*
* For unit testing only, $cm and $course may be set to null. This is so that
* you can test without having any real database objects if you like. Not all
* functions will work in this situation.
*
* @param stdClass $dbrecord Workshop instance data from {workshop} table
* @param stdClass|cm_info $cm Course module record
* @param stdClass $course Course record from {course} table
* @param stdClass $context The context of the workshop instance
*/
public function __construct(stdclass $dbrecord, $cm, $course, stdclass $context=null) {
foreach ($dbrecord as $field => $value) {
if (property_exists('workshop', $field)) {
$this->{$field} = $value;
}
}
if (is_null($cm) || is_null($course)) {
throw new coding_exception('Must specify $cm and $course');
}
$this->course = $course;
if ($cm instanceof cm_info) {
$this->cm = $cm;
} else {
$modinfo = get_fast_modinfo($course);
$this->cm = $modinfo->get_cm($cm->id);
}
if (is_null($context)) {
$this->context = context_module::instance($this->cm->id);
} else {
$this->context = $context;
}
}
////////////////////////////////////////////////////////////////////////////////
// Static methods //
////////////////////////////////////////////////////////////////////////////////
/**
* Return list of available allocation methods
*
* @return array Array ['string' => 'string'] of localized allocation method names
*/
public static function installed_allocators() {
$installed = core_component::get_plugin_list('workshopallocation');
$forms = array();
foreach ($installed as $allocation => $allocationpath) {
if (file_exists($allocationpath . '/lib.php')) {
$forms[$allocation] = get_string('pluginname', 'workshopallocation_' . $allocation);
}
}
// usability - make sure that manual allocation appears the first
if (isset($forms['manual'])) {
$m = array('manual' => $forms['manual']);
unset($forms['manual']);
$forms = array_merge($m, $forms);
}
return $forms;
}
/**
* Returns an array of options for the editors that are used for submitting and assessing instructions
*
* @param stdClass $context
* @uses EDITOR_UNLIMITED_FILES hard-coded value for the 'maxfiles' option
* @return array
*/
public static function instruction_editors_options(stdclass $context) {
return array('subdirs' => 1, 'maxbytes' => 0, 'maxfiles' => -1,
'changeformat' => 1, 'context' => $context, 'noclean' => 1, 'trusttext' => 0);
}
/**
* Given the percent and the total, returns the number
*
* @param float $percent from 0 to 100
* @param float $total the 100% value
* @return float
*/
public static function percent_to_value($percent, $total) {
if ($percent < 0 or $percent > 100) {
throw new coding_exception('The percent can not be less than 0 or higher than 100');
}
return $total * $percent / 100;
}
/**
* Returns an array of numeric values that can be used as maximum grades
*
* @return array Array of integers
*/
public static function available_maxgrades_list() {
$grades = array();
for ($i=100; $i>=0; $i--) {
$grades[$i] = $i;
}
return $grades;
}
/**
* Returns the localized list of supported examples modes
*
* @return array
*/
public static function available_example_modes_list() {
$options = array();
$options[self::EXAMPLES_VOLUNTARY] = get_string('examplesvoluntary', 'workshop');
$options[self::EXAMPLES_BEFORE_SUBMISSION] = get_string('examplesbeforesubmission', 'workshop');
$options[self::EXAMPLES_BEFORE_ASSESSMENT] = get_string('examplesbeforeassessment', 'workshop');
return $options;
}
/**
* Returns the list of available grading strategy methods
*
* @return array ['string' => 'string']
*/
public static function available_strategies_list() {
$installed = core_component::get_plugin_list('workshopform');
$forms = array();
foreach ($installed as $strategy => $strategypath) {
if (file_exists($strategypath . '/lib.php')) {
$forms[$strategy] = get_string('pluginname', 'workshopform_' . $strategy);
}
}
return $forms;
}
/**
* Returns the list of available grading evaluation methods
*
* @return array of (string)name => (string)localized title
*/
public static function available_evaluators_list() {
$evals = array();
foreach (core_component::get_plugin_list_with_file('workshopeval', 'lib.php', false) as $eval => $evalpath) {
$evals[$eval] = get_string('pluginname', 'workshopeval_' . $eval);
}
return $evals;
}
/**
* Return an array of possible values of assessment dimension weight
*
* @return array of integers 0, 1, 2, ..., 16
*/
public static function available_dimension_weights_list() {
$weights = array();
for ($i=16; $i>=0; $i--) {
$weights[$i] = $i;
}
return $weights;
}
/**
* Return an array of possible values of assessment weight
*
* Note there is no real reason why the maximum value here is 16. It used to be 10 in
* workshop 1.x and I just decided to use the same number as in the maximum weight of
* a single assessment dimension.
* The value looks reasonable, though. Teachers who would want to assign themselves
* higher weight probably do not want peer assessment really...
*
* @return array of integers 0, 1, 2, ..., 16
*/
public static function available_assessment_weights_list() {
$weights = array();
for ($i=16; $i>=0; $i--) {
$weights[$i] = $i;
}
return $weights;
}
/**
* Helper function returning the greatest common divisor
*
* @param int $a
* @param int $b
* @return int
*/
public static function gcd($a, $b) {
return ($b == 0) ? ($a):(self::gcd($b, $a % $b));
}
/**
* Helper function returning the least common multiple
*
* @param int $a
* @param int $b
* @return int
*/
public static function lcm($a, $b) {
return ($a / self::gcd($a,$b)) * $b;
}
/**
* Returns an object suitable for strings containing dates/times
*
* The returned object contains properties date, datefullshort, datetime, ... containing the given
* timestamp formatted using strftimedate, strftimedatefullshort, strftimedatetime, ... from the
* current lang's langconfig.php
* This allows translators and administrators customize the date/time format.
*
* @param int $timestamp the timestamp in UTC
* @return stdclass
*/
public static function timestamp_formats($timestamp) {
$formats = array('date', 'datefullshort', 'dateshort', 'datetime',
'datetimeshort', 'daydate', 'daydatetime', 'dayshort', 'daytime',
'monthyear', 'recent', 'recentfull', 'time');
$a = new stdclass();
foreach ($formats as $format) {
$a->{$format} = userdate($timestamp, get_string('strftime'.$format, 'langconfig'));
}
$day = userdate($timestamp, '%Y%m%d', 99, false);
$today = userdate(time(), '%Y%m%d', 99, false);
$tomorrow = userdate(time() + DAYSECS, '%Y%m%d', 99, false);
$yesterday = userdate(time() - DAYSECS, '%Y%m%d', 99, false);
$distance = (int)round(abs(time() - $timestamp) / DAYSECS);
if ($day == $today) {
$a->distanceday = get_string('daystoday', 'workshop');
} elseif ($day == $yesterday) {
$a->distanceday = get_string('daysyesterday', 'workshop');
} elseif ($day < $today) {
$a->distanceday = get_string('daysago', 'workshop', $distance);
} elseif ($day == $tomorrow) {
$a->distanceday = get_string('daystomorrow', 'workshop');
} elseif ($day > $today) {
$a->distanceday = get_string('daysleft', 'workshop', $distance);
}
return $a;
}
////////////////////////////////////////////////////////////////////////////////
// Workshop API //
////////////////////////////////////////////////////////////////////////////////
/**
* Fetches all enrolled users with the capability mod/workshop:submit in the current workshop
*
* The returned objects contain properties required by user_picture and are ordered by lastname, firstname.
* Only users with the active enrolment are returned.
*
* @param bool $musthavesubmission if true, return only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set)
* @param int $limitnum return a subset containing this number of records (optional, required if $limitfrom is set)
* @return array array[userid] => stdClass
*/
public function get_potential_authors($musthavesubmission=true, $groupid=0, $limitfrom=0, $limitnum=0) {
global $DB;
list($sql, $params) = $this->get_users_with_capability_sql('mod/workshop:submit', $musthavesubmission, $groupid);
if (empty($sql)) {
return array();
}
list($sort, $sortparams) = users_order_by_sql('tmp');
$sql = "SELECT *
FROM ($sql) tmp
ORDER BY $sort";
return $DB->get_records_sql($sql, array_merge($params, $sortparams), $limitfrom, $limitnum);
}
/**
* Returns the total number of users that would be fetched by {@link self::get_potential_authors()}
*
* @param bool $musthavesubmission if true, count only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @return int
*/
public function count_potential_authors($musthavesubmission=true, $groupid=0) {
global $DB;
list($sql, $params) = $this->get_users_with_capability_sql('mod/workshop:submit', $musthavesubmission, $groupid);
if (empty($sql)) {
return 0;
}
$sql = "SELECT COUNT(*)
FROM ($sql) tmp";
return $DB->count_records_sql($sql, $params);
}
/**
* Fetches all enrolled users with the capability mod/workshop:peerassess in the current workshop
*
* The returned objects contain properties required by user_picture and are ordered by lastname, firstname.
* Only users with the active enrolment are returned.
*
* @param bool $musthavesubmission if true, return only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set)
* @param int $limitnum return a subset containing this number of records (optional, required if $limitfrom is set)
* @return array array[userid] => stdClass
*/
public function get_potential_reviewers($musthavesubmission=false, $groupid=0, $limitfrom=0, $limitnum=0) {
global $DB;
list($sql, $params) = $this->get_users_with_capability_sql('mod/workshop:peerassess', $musthavesubmission, $groupid);
if (empty($sql)) {
return array();
}
list($sort, $sortparams) = users_order_by_sql('tmp');
$sql = "SELECT *
FROM ($sql) tmp
ORDER BY $sort";
return $DB->get_records_sql($sql, array_merge($params, $sortparams), $limitfrom, $limitnum);
}
/**
* Returns the total number of users that would be fetched by {@link self::get_potential_reviewers()}
*
* @param bool $musthavesubmission if true, count only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @return int
*/
public function count_potential_reviewers($musthavesubmission=false, $groupid=0) {
global $DB;
list($sql, $params) = $this->get_users_with_capability_sql('mod/workshop:peerassess', $musthavesubmission, $groupid);
if (empty($sql)) {
return 0;
}
$sql = "SELECT COUNT(*)
FROM ($sql) tmp";
return $DB->count_records_sql($sql, $params);
}
/**
* Fetches all enrolled users that are authors or reviewers (or both) in the current workshop
*
* The returned objects contain properties required by user_picture and are ordered by lastname, firstname.
* Only users with the active enrolment are returned.
*
* @see self::get_potential_authors()
* @see self::get_potential_reviewers()
* @param bool $musthavesubmission if true, return only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set)
* @param int $limitnum return a subset containing this number of records (optional, required if $limitfrom is set)
* @return array array[userid] => stdClass
*/
public function get_participants($musthavesubmission=false, $groupid=0, $limitfrom=0, $limitnum=0) {
global $DB;
list($sql, $params) = $this->get_participants_sql($musthavesubmission, $groupid);
if (empty($sql)) {
return array();
}
list($sort, $sortparams) = users_order_by_sql('tmp');
$sql = "SELECT *
FROM ($sql) tmp
ORDER BY $sort";
return $DB->get_records_sql($sql, array_merge($params, $sortparams), $limitfrom, $limitnum);
}
/**
* Returns the total number of records that would be returned by {@link self::get_participants()}
*
* @param bool $musthavesubmission if true, return only users who have already submitted
* @param int $groupid 0 means ignore groups, any other value limits the result by group id
* @return int
*/
public function count_participants($musthavesubmission=false, $groupid=0) {
global $DB;
list($sql, $params) = $this->get_participants_sql($musthavesubmission, $groupid);
if (empty($sql)) {
return 0;
}
$sql = "SELECT COUNT(*)
FROM ($sql) tmp";
return $DB->count_records_sql($sql, $params);
}
/**
* Checks if the given user is an actively enrolled participant in the workshop
*
* @param int $userid, defaults to the current $USER
* @return boolean
*/
public function is_participant($userid=null) {
global $USER, $DB;
if (is_null($userid)) {
$userid = $USER->id;
}
list($sql, $params) = $this->get_participants_sql();
if (empty($sql)) {
return false;
}
$sql = "SELECT COUNT(*)
FROM {user} uxx
JOIN ({$sql}) pxx ON uxx.id = pxx.id
WHERE uxx.id = :uxxid";
$params['uxxid'] = $userid;
if ($DB->count_records_sql($sql, $params)) {
return true;
}
return false;
}
/**
* Groups the given users by the group membership
*
* This takes the module grouping settings into account. If a grouping is
* set, returns only groups withing the course module grouping. Always
* returns group [0] with all the given users.
*
* @param array $users array[userid] => stdclass{->id ->lastname ->firstname}
* @return array array[groupid][userid] => stdclass{->id ->lastname ->firstname}
*/
public function get_grouped($users) {
global $DB;
global $CFG;
$grouped = array(); // grouped users to be returned
if (empty($users)) {
return $grouped;
}
if ($this->cm->groupingid) {
// Group workshop set to specified grouping - only consider groups
// within this grouping, and leave out users who aren't members of
// this grouping.
$groupingid = $this->cm->groupingid;
// All users that are members of at least one group will be
// added into a virtual group id 0
$grouped[0] = array();
} else {
$groupingid = 0;
// there is no need to be member of a group so $grouped[0] will contain
// all users
$grouped[0] = $users;
}
$gmemberships = groups_get_all_groups($this->cm->course, array_keys($users), $groupingid,
'gm.id,gm.groupid,gm.userid');
foreach ($gmemberships as $gmembership) {
if (!isset($grouped[$gmembership->groupid])) {
$grouped[$gmembership->groupid] = array();
}
$grouped[$gmembership->groupid][$gmembership->userid] = $users[$gmembership->userid];
$grouped[0][$gmembership->userid] = $users[$gmembership->userid];
}
return $grouped;
}
/**
* Returns the list of all allocations (i.e. assigned assessments) in the workshop
*
* Assessments of example submissions are ignored
*
* @return array
*/
public function get_allocations() {
global $DB;
$sql = 'SELECT a.id, a.submissionid, a.reviewerid, s.authorid
FROM {workshop_assessments} a
INNER JOIN {workshop_submissions} s ON (a.submissionid = s.id)
WHERE s.example = 0 AND s.workshopid = :workshopid';
$params = array('workshopid' => $this->id);
return $DB->get_records_sql($sql, $params);
}
/**
* Returns the total number of records that would be returned by {@link self::get_submissions()}
*
* @param mixed $authorid int|array|'all' If set to [array of] integer, return submission[s] of the given user[s] only
* @param int $groupid If non-zero, return only submissions by authors in the specified group
* @return int number of records
*/
public function count_submissions($authorid='all', $groupid=0) {
global $DB;
$params = array('workshopid' => $this->id);
$sql = "SELECT COUNT(s.id)
FROM {workshop_submissions} s
JOIN {user} u ON (s.authorid = u.id)";
if ($groupid) {
$sql .= " JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
$params['groupid'] = $groupid;
}
$sql .= " WHERE s.example = 0 AND s.workshopid = :workshopid";
if ('all' === $authorid) {
// no additional conditions
} elseif (!empty($authorid)) {
list($usql, $uparams) = $DB->get_in_or_equal($authorid, SQL_PARAMS_NAMED);
$sql .= " AND authorid $usql";
$params = array_merge($params, $uparams);
} else {
// $authorid is empty
return 0;
}
return $DB->count_records_sql($sql, $params);
}
/**
* Returns submissions from this workshop
*
* Fetches data from {workshop_submissions} and adds some useful information from other
* tables. Does not return textual fields to prevent possible memory lack issues.
*
* @see self::count_submissions()
* @param mixed $authorid int|array|'all' If set to [array of] integer, return submission[s] of the given user[s] only
* @param int $groupid If non-zero, return only submissions by authors in the specified group
* @param int $limitfrom Return a subset of records, starting at this point (optional)
* @param int $limitnum Return a subset containing this many records in total (optional, required if $limitfrom is set)
* @return array of records or an empty array
*/
public function get_submissions($authorid='all', $groupid=0, $limitfrom=0, $limitnum=0) {
global $DB;
$authorfields = user_picture::fields('u', null, 'authoridx', 'author');
$gradeoverbyfields = user_picture::fields('t', null, 'gradeoverbyx', 'over');
$params = array('workshopid' => $this->id);
$sql = "SELECT s.id, s.workshopid, s.example, s.authorid, s.timecreated, s.timemodified,
s.title, s.grade, s.gradeover, s.gradeoverby, s.published,
$authorfields, $gradeoverbyfields
FROM {workshop_submissions} s
JOIN {user} u ON (s.authorid = u.id)";
if ($groupid) {
$sql .= " JOIN {groups_members} gm ON (gm.userid = u.id AND gm.groupid = :groupid)";
$params['groupid'] = $groupid;
}
$sql .= " LEFT JOIN {user} t ON (s.gradeoverby = t.id)
WHERE s.example = 0 AND s.workshopid = :workshopid";
if ('all' === $authorid) {
// no additional conditions
} elseif (!empty($authorid)) {
list($usql, $uparams) = $DB->get_in_or_equal($authorid, SQL_PARAMS_NAMED);
$sql .= " AND authorid $usql";
$params = array_merge($params, $uparams);
} else {
// $authorid is empty
return array();
}
list($sort, $sortparams) = users_order_by_sql('u');
$sql .= " ORDER BY $sort";
return $DB->get_records_sql($sql, array_merge($params, $sortparams), $limitfrom, $limitnum);
}
/**
* Returns a submission record with the author's data
*
* @param int $id submission id
* @return stdclass
*/
public function get_submission_by_id($id) {
global $DB;
// we intentionally check the workshopid here, too, so the workshop can't touch submissions
// from other instances
$authorfields = user_picture::fields('u', null, 'authoridx', 'author');
$gradeoverbyfields = user_picture::fields('g', null, 'gradeoverbyx', 'gradeoverby');
$sql = "SELECT s.*, $authorfields, $gradeoverbyfields
FROM {workshop_submissions} s
INNER JOIN {user} u ON (s.authorid = u.id)
LEFT JOIN {user} g ON (s.gradeoverby = g.id)
WHERE s.example = 0 AND s.workshopid = :workshopid AND s.id = :id";
$params = array('workshopid' => $this->id, 'id' => $id);
return $DB->get_record_sql($sql, $params, MUST_EXIST);
}
/**
* Returns a submission submitted by the given author
*
* @param int $id author id
* @return stdclass|false
*/
public function get_submission_by_author($authorid) {
global $DB;
if (empty($authorid)) {
return false;
}
$authorfields = user_picture::fields('u', null, 'authoridx', 'author');
$gradeoverbyfields = user_picture::fields('g', null, 'gradeoverbyx', 'gradeoverby');
$sql = "SELECT s.*, $authorfields, $gradeoverbyfields
FROM {workshop_submissions} s
INNER JOIN {user} u ON (s.authorid = u.id)
LEFT JOIN {user} g ON (s.gradeoverby = g.id)
WHERE s.example = 0 AND s.workshopid = :workshopid AND s.authorid = :authorid";
$params = array('workshopid' => $this->id, 'authorid' => $authorid);
return $DB->get_record_sql($sql, $params);
}
/**
* Returns published submissions with their authors data
*
* @return array of stdclass
*/
public function get_published_submissions($orderby='finalgrade DESC') {
global $DB;
$authorfields = user_picture::fields('u', null, 'authoridx', 'author');
$sql = "SELECT s.id, s.authorid, s.timecreated, s.timemodified,
s.title, s.grade, s.gradeover, COALESCE(s.gradeover,s.grade) AS finalgrade,
$authorfields
FROM {workshop_submissions} s
INNER JOIN {user} u ON (s.authorid = u.id)
WHERE s.example = 0 AND s.workshopid = :workshopid AND s.published = 1
ORDER BY $orderby";
$params = array('workshopid' => $this->id);
return $DB->get_records_sql($sql, $params);
}
/**
* Returns full record of the given example submission
*
* @param int $id example submission od
* @return object
*/
public function get_example_by_id($id) {
global $DB;
return $DB->get_record('workshop_submissions',
array('id' => $id, 'workshopid' => $this->id, 'example' => 1), '*', MUST_EXIST);
}
/**
* Returns the list of example submissions in this workshop with reference assessments attached
*
* @return array of objects or an empty array
* @see workshop::prepare_example_summary()
*/
public function get_examples_for_manager() {
global $DB;
$sql = 'SELECT s.id, s.title,
a.id AS assessmentid, a.grade, a.gradinggrade
FROM {workshop_submissions} s
LEFT JOIN {workshop_assessments} a ON (a.submissionid = s.id AND a.weight = 1)
WHERE s.example = 1 AND s.workshopid = :workshopid
ORDER BY s.title';
return $DB->get_records_sql($sql, array('workshopid' => $this->id));
}
/**
* Returns the list of all example submissions in this workshop with the information of assessments done by the given user
*
* @param int $reviewerid user id
* @return array of objects, indexed by example submission id
* @see workshop::prepare_example_summary()
*/
public function get_examples_for_reviewer($reviewerid) {
global $DB;
if (empty($reviewerid)) {
return false;
}
$sql = 'SELECT s.id, s.title,
a.id AS assessmentid, a.grade, a.gradinggrade
FROM {workshop_submissions} s
LEFT JOIN {workshop_assessments} a ON (a.submissionid = s.id AND a.reviewerid = :reviewerid AND a.weight = 0)
WHERE s.example = 1 AND s.workshopid = :workshopid
ORDER BY s.title';
return $DB->get_records_sql($sql, array('workshopid' => $this->id, 'reviewerid' => $reviewerid));
}
/**
* Prepares renderable submission component
*
* @param stdClass $record required by {@see workshop_submission}
* @param bool $showauthor show the author-related information
* @return workshop_submission
*/
public function prepare_submission(stdClass $record, $showauthor = false) {
$submission = new workshop_submission($this, $record, $showauthor);
$submission->url = $this->submission_url($record->id);
return $submission;
}
/**
* Prepares renderable submission summary component
*
* @param stdClass $record required by {@see workshop_submission_summary}
* @param bool $showauthor show the author-related information
* @return workshop_submission_summary
*/
public function prepare_submission_summary(stdClass $record, $showauthor = false) {
$summary = new workshop_submission_summary($this, $record, $showauthor);
$summary->url = $this->submission_url($record->id);
return $summary;
}
/**
* Prepares renderable example submission component
*
* @param stdClass $record required by {@see workshop_example_submission}
* @return workshop_example_submission
*/
public function prepare_example_submission(stdClass $record) {
$example = new workshop_example_submission($this, $record);
return $example;
}
/**
* Prepares renderable example submission summary component
*
* If the example is editable, the caller must set the 'editable' flag explicitly.
*
* @param stdClass $example as returned by {@link workshop::get_examples_for_manager()} or {@link workshop::get_examples_for_reviewer()}
* @return workshop_example_submission_summary to be rendered
*/
public function prepare_example_summary(stdClass $example) {
$summary = new workshop_example_submission_summary($this, $example);
if (is_null($example->grade)) {
$summary->status = 'notgraded';
$summary->assesslabel = get_string('assess', 'workshop');
} else {
$summary->status = 'graded';
$summary->assesslabel = get_string('reassess', 'workshop');
}
$summary->gradeinfo = new stdclass();
$summary->gradeinfo->received = $this->real_grade($example->grade);
$summary->gradeinfo->max = $this->real_grade(100);
$summary->url = new moodle_url($this->exsubmission_url($example->id));
$summary->editurl = new moodle_url($this->exsubmission_url($example->id), array('edit' => 'on'));
$summary->assessurl = new moodle_url($this->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
return $summary;
}
/**
* Prepares renderable assessment component
*
* The $options array supports the following keys:
* showauthor - should the author user info be available for the renderer
* showreviewer - should the reviewer user info be available for the renderer
* showform - show the assessment form if it is available
* showweight - should the assessment weight be available for the renderer
*
* @param stdClass $record as returned by eg {@link self::get_assessment_by_id()}
* @param workshop_assessment_form|null $form as returned by {@link workshop_strategy::get_assessment_form()}
* @param array $options
* @return workshop_assessment
*/
public function prepare_assessment(stdClass $record, $form, array $options = array()) {
$assessment = new workshop_assessment($this, $record, $options);
$assessment->url = $this->assess_url($record->id);
$assessment->maxgrade = $this->real_grade(100);
if (!empty($options['showform']) and !($form instanceof workshop_assessment_form)) {
debugging('Not a valid instance of workshop_assessment_form supplied', DEBUG_DEVELOPER);
}
if (!empty($options['showform']) and ($form instanceof workshop_assessment_form)) {
$assessment->form = $form;
}
if (empty($options['showweight'])) {
$assessment->weight = null;
}
if (!is_null($record->grade)) {
$assessment->realgrade = $this->real_grade($record->grade);
}
return $assessment;
}
/**
* Prepares renderable example submission's assessment component
*
* The $options array supports the following keys:
* showauthor - should the author user info be available for the renderer
* showreviewer - should the reviewer user info be available for the renderer
* showform - show the assessment form if it is available
*
* @param stdClass $record as returned by eg {@link self::get_assessment_by_id()}
* @param workshop_assessment_form|null $form as returned by {@link workshop_strategy::get_assessment_form()}
* @param array $options
* @return workshop_example_assessment
*/
public function prepare_example_assessment(stdClass $record, $form = null, array $options = array()) {
$assessment = new workshop_example_assessment($this, $record, $options);
$assessment->url = $this->exassess_url($record->id);
$assessment->maxgrade = $this->real_grade(100);
if (!empty($options['showform']) and !($form instanceof workshop_assessment_form)) {