Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / mod / workshop / lib.php
blob877b41a9078ca85ad843904e8ecd7d6facdd43f9
1 <?php // $Id$
3 // workshop constants and standard Moodle functions plus the workshop functions
4 // called by the standard functions
6 // see also locallib.php for other non-standard workshop functions
8 require_once($CFG->libdir.'/filelib.php');
10 /*** Constants **********************************/
13 $WORKSHOP_EWEIGHTS = array( 0 => -4.0, 1 => -2.0, 2 => -1.5, 3 => -1.0, 4 => -0.75, 5 => -0.5, 6 => -0.25,
14 7 => 0.0, 8 => 0.25, 9 => 0.5, 10 => 0.75, 11=> 1.0, 12 => 1.5, 13=> 2.0,
15 14 => 4.0);
17 $WORKSHOP_FWEIGHTS = array( 0 => 0, 1 => 0.1, 2 => 0.25, 3 => 0.5, 4 => 0.75, 5 => 1.0, 6 => 1.5,
18 7 => 2.0, 8 => 3.0, 9 => 5.0, 10 => 7.5, 11=> 10.0, 12=>50.0);
21 $WORKSHOP_ASSESSMENT_COMPS = array (
22 0 => array('name' => get_string('verylax', 'workshop'), 'value' => 1),
23 1 => array('name' => get_string('lax', 'workshop'), 'value' => 0.6),
24 2 => array('name' => get_string('fair', 'workshop'), 'value' => 0.4),
25 3 => array('name' => get_string('strict', 'workshop'), 'value' => 0.33),
26 4 => array('name' => get_string('verystrict', 'workshop'), 'value' => 0.2) );
29 /*** Moodle 1.7 compatibility functions *****
31 ********************************************/
32 function workshop_context($workshop) {
33 //TODO: add some $cm caching if needed
34 if (is_object($workshop)) {
35 $workshop = $workshop->id;
37 if (! $cm = get_coursemodule_from_instance('workshop', $workshop)) {
38 error('Course Module ID was incorrect');
41 return get_context_instance(CONTEXT_MODULE, $cm->id);
44 function workshop_is_teacher($workshop, $userid=NULL) {
45 return has_capability('mod/workshop:manage', workshop_context($workshop), $userid);
48 function workshop_is_teacheredit($workshop, $userid=NULL) {
49 return has_capability('mod/workshop:manage', workshop_context($workshop), $userid)
50 and has_capability('moodle/site:accessallgroups', workshop_context($workshop), $userid);
53 function workshop_is_student($workshop, $userid=NULL) {
54 return has_capability('mod/workshop:participate', workshop_context($workshop), $userid);
57 function workshop_get_students($workshop, $sort='u.lastaccess', $fields='u.*') {
58 return $users = get_users_by_capability(workshop_context($workshop), 'mod/workshop:participate', $fields, $sort);
61 function workshop_get_teachers($workshop, $sort='u.lastaccess', $fields='u.*') {
62 return $users = get_users_by_capability(workshop_context($workshop), 'mod/workshop:manage', $fields, $sort);
66 /*** Standard Moodle functions ******************
67 workshop_add_instance($workshop)
68 workshop_check_dates($workshop)
69 workshop_cron ()
70 workshop_delete_instance($id)
71 workshop_grades($workshopid)
72 workshop_print_recent_activity(&$logs, $isteacher=false)
73 workshop_refresh_events($workshop)
74 workshop_update_instance($workshop)
75 workshop_user_complete($course, $user, $mod, $workshop)
76 workshop_user_outline($course, $user, $mod, $workshop)
77 **********************************************/
79 ///////////////////////////////////////////////////////////////////////////////
80 function workshop_add_instance($workshop) {
81 // Given an object containing all the necessary data,
82 // (defined by the form in mod.html) this function
83 // will create a new instance and return the id number
84 // of the new instance.
86 $workshop->timemodified = time();
88 $workshop->submissionstart = make_timestamp($workshop->submissionstartyear,
89 $workshop->submissionstartmonth, $workshop->submissionstartday, $workshop->submissionstarthour,
90 $workshop->submissionstartminute);
92 $workshop->assessmentstart = make_timestamp($workshop->assessmentstartyear,
93 $workshop->assessmentstartmonth, $workshop->assessmentstartday, $workshop->assessmentstarthour,
94 $workshop->assessmentstartminute);
96 $workshop->submissionend = make_timestamp($workshop->submissionendyear,
97 $workshop->submissionendmonth, $workshop->submissionendday, $workshop->submissionendhour,
98 $workshop->submissionendminute);
100 $workshop->assessmentend = make_timestamp($workshop->assessmentendyear,
101 $workshop->assessmentendmonth, $workshop->assessmentendday, $workshop->assessmentendhour,
102 $workshop->assessmentendminute);
104 $workshop->releasegrades = make_timestamp($workshop->releaseyear,
105 $workshop->releasemonth, $workshop->releaseday, $workshop->releasehour,
106 $workshop->releaseminute);
108 if (!workshop_check_dates($workshop)) {
109 return get_string('invaliddates', 'workshop');
112 if ($returnid = insert_record("workshop", $workshop)) {
114 $event = NULL;
115 $event->name = get_string('submissionstartevent','workshop', $workshop->name);
116 $event->description = $workshop->description;
117 $event->courseid = $workshop->course;
118 $event->groupid = 0;
119 $event->userid = 0;
120 $event->modulename = 'workshop';
121 $event->instance = $returnid;
122 $event->eventtype = 'submissionstart';
123 $event->timestart = $workshop->submissionstart;
124 $event->timeduration = 0;
125 add_event($event);
127 $event->name = get_string('submissionendevent','workshop', $workshop->name);
128 $event->eventtype = 'submissionend';
129 $event->timestart = $workshop->submissionend;
130 add_event($event);
132 $event->name = get_string('assessmentstartevent','workshop', $workshop->name);
133 $event->eventtype = 'assessmentstart';
134 $event->timestart = $workshop->assessmentstart;
135 add_event($event);
137 $event->name = get_string('assessmentendevent','workshop', $workshop->name);
138 $event->eventtype = 'assessmentend';
139 $event->timestart = $workshop->assessmentend;
140 add_event($event);
143 return $returnid;
146 ///////////////////////////////////////////////////////////////////////////////
147 // returns true if the dates are valid, false otherwise
148 function workshop_check_dates($workshop) {
149 // allow submission and assessment to start on the same date and to end on the same date
150 // but enforce non-empty submission period and non-empty assessment period.
151 return ($workshop->submissionstart < $workshop->submissionend and
152 $workshop->submissionstart <= $workshop->assessmentstart and
153 $workshop->assessmentstart < $workshop->assessmentend and
154 $workshop->submissionend <= $workshop->assessmentend);
158 ///////////////////////////////////////////////////////////////////////////////
159 function workshop_cron () {
160 // Function to be run periodically according to the moodle cron
162 global $CFG, $USER;
164 // if there any ungraded assessments run the grading routine
165 if ($workshops = get_records("workshop")) {
166 foreach ($workshops as $workshop) {
167 // automatically grade assessments if workshop has examples and/or peer assessments
168 if ($workshop->gradingstrategy and ($workshop->ntassessments or $workshop->nsassessments)) {
169 workshop_grade_assessments($workshop);
173 $timenow = time();
175 $CFG->enablerecordcache = true; // We want all the caching we can get
177 // Find all workshop notifications that have yet to be mailed out, and mails them
178 $cutofftime = $timenow - $CFG->maxeditingtime;
180 // look for new assessments
181 if ($assessments = workshop_get_unmailed_assessments($cutofftime)) {
182 foreach ($assessments as $assessment) {
184 echo "Processing workshop assessment $assessment->id\n";
186 // only process the entry once
187 if (! set_field("workshop_assessments", "mailed", "1", "id", "$assessment->id")) {
188 echo "Could not update the mailed field for id $assessment->id\n";
191 if (! $submission = get_record("workshop_submissions", "id", "$assessment->submissionid")) {
192 echo "Could not find submission $assessment->submissionid\n";
193 continue;
195 if (! $workshop = get_record("workshop", "id", $submission->workshopid)) {
196 echo "Could not find workshop id $submission->workshopid\n";
197 continue;
199 if (! $course = get_record("course", "id", $workshop->course)) {
200 error("Could not find course id $workshop->course");
201 continue;
203 if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
204 error("Course Module ID was incorrect");
205 continue;
207 if (! $submissionowner = get_record("user", "id", "$submission->userid")) {
208 echo "Could not find user $submission->userid\n";
209 continue;
211 if (! $assessmentowner = get_record("user", "id", "$assessment->userid")) {
212 echo "Could not find user $assessment->userid\n";
213 continue;
215 if (! workshop_is_student($workshop, $submissionowner->id) and !workshop_is_teacher($workshop,
216 $submissionowner->id)) {
217 continue; // Not an active participant
219 if (! workshop_is_student($workshop, $assessmentowner->id) and !workshop_is_teacher($workshop,
220 $assessmentowner->id)) {
221 continue; // Not an active participant
223 // don't sent self assessment
224 if ($submissionowner->id == $assessmentowner->id) {
225 continue;
227 $strworkshops = get_string("modulenameplural", "workshop");
228 $strworkshop = get_string("modulename", "workshop");
230 // it's an assessment, tell the submission owner
231 $USER->lang = $submissionowner->lang;
232 $sendto = $submissionowner;
233 // "Your assignment \"$submission->title\" has been assessed by"
234 if (workshop_is_student($workshop, $assessmentowner->id)) {
235 $msg = get_string("mail1", "workshop", $submission->title)." a $course->student.\n";
237 else {
238 $msg = get_string("mail1", "workshop", $submission->title).
239 " ".fullname($assessmentowner)."\n";
241 // "The comments and grade can be seen in the workshop assignment '$workshop->name'
242 // I have taken the following line out because the info is repeated below.
243 // $msg .= get_string("mail2", "workshop", $workshop->name)."\n\n";
245 $postsubject = "$course->shortname: $strworkshops: ".format_string($workshop->name,true);
246 $posttext = "$course->shortname -> $strworkshops -> ".format_string($workshop->name,true)."\n";
247 $posttext .= "---------------------------------------------------------------------\n";
248 $posttext .= $msg;
249 // "The comments and grade can be seen in ..."
250 $posttext .= get_string("mail2", "workshop",
251 format_string($workshop->name,true).", $CFG->wwwroot/mod/workshop/view.php?id=$cm->id")."\n";
252 $posttext .= "---------------------------------------------------------------------\n";
253 if ($sendto->mailformat == 1) { // HTML
254 $posthtml = "<p><font face=\"sans-serif\">".
255 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
256 "<a href=\"$CFG->wwwroot/mod/workshop/index.php?id=$course->id\">$strworkshops</a> ->".
257 "<a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a></font></p>";
258 $posthtml .= "<hr><font face=\"sans-serif\">";
259 $posthtml .= "<p>$msg</p>";
260 $posthtml .= "<p>".get_string("mail2", "workshop",
261 " <a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a>")."</p></font><hr>";
262 } else {
263 $posthtml = "";
266 if (!$teacher = get_teacher($course->id)) {
267 echo "Error: can not find teacher for course $course->id!\n";
270 if (! email_to_user($sendto, $teacher, $postsubject, $posttext, $posthtml)) {
271 echo "Error: workshop cron: Could not send out mail for id $submission->id to
272 user $sendto->id ($sendto->email)\n";
277 // look for new assessments of resubmissions
278 if ($assessments = workshop_get_unmailed_resubmissions($cutofftime)) {
279 $timenow = time();
281 foreach ($assessments as $assessment) {
283 echo "Processing workshop assessment $assessment->id\n";
285 // only process the entry once
286 if (! set_field("workshop_assessments", "mailed", "1", "id", "$assessment->id")) {
287 echo "Could not update the mailed field for id $assessment->id\n";
290 if (! $submission = get_record("workshop_submissions", "id", "$assessment->submissionid")) {
291 echo "Could not find submission $assessment->submissionid\n";
292 continue;
294 if (! $workshop = get_record("workshop", "id", $submission->workshopid)) {
295 echo "Could not find workshop id $submission->workshopid\n";
296 continue;
298 if (! $course = get_record("course", "id", $workshop->course)) {
299 error("Could not find course id $workshop->course");
300 continue;
302 if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
303 error("Course Module ID was incorrect");
304 continue;
306 if (! $submissionowner = get_record("user", "id", "$submission->userid")) {
307 echo "Could not find user $submission->userid\n";
308 continue;
310 if (! $assessmentowner = get_record("user", "id", "$assessment->userid")) {
311 echo "Could not find user $assessment->userid\n";
312 continue;
314 if (! workshop_is_student($workshop, $submissionowner->id) and !workshop_is_teacher($workshop,
315 $submissionowner->id)) {
316 continue; // Not an active participant
318 if (! workshop_is_student($workshop, $assessmentowner->id) and !workshop_is_teacher($workshop,
319 $assessmentowner->id)) {
320 continue; // Not an active participant
323 $strworkshops = get_string("modulenameplural", "workshop");
324 $strworkshop = get_string("modulename", "workshop");
326 // it's a resubission assessment, tell the assessment owner to (re)assess
327 $USER->lang = $assessmentowner->lang;
328 $sendto = $assessmentowner;
329 // "The assignment \"$submission->title\" is a revised piece of work. "
330 $msg = get_string("mail8", "workshop", $submission->title)."\n";
331 // "Please assess it in the workshop assignment '$workshop->name'
332 // $msg .= get_string("mail9", "workshop", $workshop->name)."\n\n";
334 $postsubject = "$course->shortname: $strworkshops: ".format_string($workshop->name,true);
335 $posttext = "$course->shortname -> $strworkshops -> ".format_string($workshop->name,true)."\n";
336 $posttext .= "---------------------------------------------------------------------\n";
337 $posttext .= $msg;
338 // "Please assess it in ..."
339 $posttext .= get_string("mail9", "workshop",
340 format_string($workshop->name,true).", $CFG->wwwroot/mod/workshop/view.php?id=$cm->id")."\n";
341 $posttext .= "---------------------------------------------------------------------\n";
342 if ($sendto->mailformat == 1) { // HTML
343 $posthtml = "<p><font face=\"sans-serif\">".
344 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
345 "<a href=\"$CFG->wwwroot/mod/workshop/index.php?id=$course->id\">$strworkshops</a> ->".
346 "<a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a></font></p>";
347 $posthtml .= "<hr><font face=\"sans-serif\">";
348 $posthtml .= "<p>$msg</p>";
349 $posthtml .= "<p>".get_string("mail9", "workshop",
350 " <a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a>").'</p></font><hr>';
352 else {
353 $posthtml = "";
356 if (!$teacher = get_teacher($course->id)) {
357 echo "Error: can not find teacher for course $course->id!\n";
360 if (! email_to_user($sendto, $teacher, $postsubject, $posttext, $posthtml)) {
361 echo "Error: workshop cron: Could not send out mail for id $submission->id to
362 user $sendto->id ($sendto->email)\n";
367 // look for new comments
368 if ($comments = workshop_get_unmailed_comments($cutofftime)) {
369 $timenow = time();
371 foreach ($comments as $comment) {
373 echo "Processing workshop comment $comment->id\n";
375 // only process the entry once
376 if (! set_field("workshop_comments", "mailed", "1", "id", "$comment->id")) {
377 echo "Could not update the mailed field for comment id $comment->id\n";
380 if (! $assessment = get_record("workshop_assessments", "id", "$comment->assessmentid")) {
381 echo "Could not find assessment $comment->assessmentid\n";
382 continue;
384 if (! $submission = get_record("workshop_submissions", "id", "$assessment->submissionid")) {
385 echo "Could not find submission $assessment->submissionid\n";
386 continue;
388 if (! $workshop = get_record("workshop", "id", $submission->workshopid)) {
389 echo "Could not find workshop id $submission->workshopid\n";
390 continue;
392 if (! $course = get_record("course", "id", $workshop->course)) {
393 error("Could not find course id $workshop->course");
394 continue;
396 if (! $cm = get_coursemodule_from_instance("workshop", $workshop->id, $course->id)) {
397 error("Course Module ID was incorrect");
398 continue;
400 if (! $submissionowner = get_record("user", "id", "$submission->userid")) {
401 echo "Could not find user $submission->userid\n";
402 continue;
404 if (! $assessmentowner = get_record("user", "id", "$assessment->userid")) {
405 echo "Could not find user $assessment->userid\n";
406 continue;
408 if (! workshop_is_student($workshop, $submissionowner->id) and !workshop_is_teacher($workshop,
409 $submissionowner->id)) {
410 continue; // Not an active participant
412 if (! workshop_is_student($workshop, $assessmentowner->id) and !workshop_is_teacher($workshop,
413 $assessmentowner->id)) {
414 continue; // Not an active participant
417 $strworkshops = get_string("modulenameplural", "workshop");
418 $strworkshop = get_string("modulename", "workshop");
420 // see if the submission owner needs to be told
421 if ($comment->userid != $submission->userid) {
422 $USER->lang = $submissionowner->lang;
423 $sendto = $submissionowner;
424 // "A comment has been added to the assignment \"$submission->title\" by
425 if (workshop_is_student($workshop, $assessmentowner->id)) {
426 $msg = get_string("mail4", "workshop", $submission->title)." a $course->student.\n";
428 else {
429 $msg = get_string("mail4", "workshop", $submission->title)." ".fullname($assessmentowner)."\n";
431 // "The new comment can be seen in the workshop assignment '$workshop->name'
432 // $msg .= get_string("mail5", "workshop", $workshop->name)."\n\n";
434 $postsubject = "$course->shortname: $strworkshops: ".format_string($workshop->name,true);
435 $posttext = "$course->shortname -> $strworkshops -> ".format_string($workshop->name,true)."\n";
436 $posttext .= "---------------------------------------------------------------------\n";
437 $posttext .= $msg;
438 // "The new comment can be seen in ..."
439 $posttext .= get_string("mail5", "workshop",
440 format_string($workshop->name,true).", $CFG->wwwroot/mod/workshop/view.php?id=$cm->id")."\n";
441 $posttext .= "---------------------------------------------------------------------\n";
442 if ($sendto->mailformat == 1) { // HTML
443 $posthtml = "<p><font face=\"sans-serif\">".
444 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
445 "<a href=\"$CFG->wwwroot/mod/workshop/index.php?id=$course->id\">$strworkshops</a> ->".
446 "<a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a></font></p>";
447 $posthtml .= "<hr><font face=\"sans-serif\">";
448 $posthtml .= "<p>$msg</p>";
449 $posthtml .= "<p>".get_string("mail5", "workshop",
450 " <a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a>")
451 ."</p></font><hr>";
453 else {
454 $posthtml = "";
457 if (!$teacher = get_teacher($course->id)) {
458 echo "Error: can not find teacher for course $course->id!\n";
461 if (! email_to_user($sendto, $teacher, $postsubject, $posttext, $posthtml)) {
462 echo "Error: workshop cron: Could not send out mail for id $submission->id to user
463 $sendto->id ($sendto->email)\n";
466 // see if the assessor needs to to told
467 if ($comment->userid != $assessment->userid) {
468 $USER->lang = $assessmentowner->lang;
469 $sendto = $assessmentowner;
470 // "A comment has been added to the assignment \"$submission->title\" by
471 if (workshop_is_student($workshop, $submissionowner->id)) {
472 $msg = get_string("mail4", "workshop", $submission->title)." a $course->student.\n";
474 else {
475 $msg = get_string("mail4", "workshop", $submission->title).
476 " ".fullname($submissionowner)."\n";
478 // "The new comment can be seen in the workshop assignment '$workshop->name'
479 // $msg .= get_string("mail5", "workshop", $workshop->name)."\n\n";
481 $postsubject = "$course->shortname: $strworkshops: ".format_string($workshop->name,true);
482 $posttext = "$course->shortname -> $strworkshops -> ".format_string($workshop->name,true)."\n";
483 $posttext .= "---------------------------------------------------------------------\n";
484 $posttext .= $msg;
485 // "The new comment can be seen in ..."
486 $posttext .= get_string("mail5", "workshop",
487 format_string($workshop->name,true).", $CFG->wwwroot/mod/workshop/view.php?id=$cm->id")."\n";
488 $posttext .= "---------------------------------------------------------------------\n";
489 if ($sendto->mailformat == 1) { // HTML
490 $posthtml = "<p><font face=\"sans-serif\">".
491 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
492 "<a href=\"$CFG->wwwroot/mod/workshop/index.php?id=$course->id\">$strworkshops</a> ->".
493 "<a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a></font></p>";
494 $posthtml .= "<hr><font face=\"sans-serif\">";
495 $posthtml .= "<p>$msg</p>";
496 $posthtml .= "<p>".get_string("mail5", "workshop",
497 " <a href=\"$CFG->wwwroot/mod/workshop/view.php?id=$cm->id\">".format_string($workshop->name,true)."</a>")
498 ."</p></font><hr>";
500 else {
501 $posthtml = "";
504 if (!$teacher = get_teacher($course->id)) {
505 echo "Error: can not find teacher for course $course->id!\n";
508 if (! email_to_user($sendto, $teacher, $postsubject, $posttext, $posthtml)) {
509 echo "Error: workshop cron: Could not send out mail for id $submission->id to user
510 $sendto->id ($sendto->email)\n";
512 if (! set_field("workshop_comments", "mailed", "1", "id", "$comment->id")) {
513 echo "Could not update the mailed field for comment id $comment->id\n";
518 return true;
522 ///////////////////////////////////////////////////////////////////////////////
523 function workshop_delete_instance($id) {
524 // Given an ID of an instance of this module,
525 // this function will permanently delete the instance
526 // and any data that depends on it.
528 if (! $workshop = get_record("workshop", "id", "$id")) {
529 return false;
532 // delete all the associated records in the workshop tables, start positive...
533 $result = true;
535 if (! delete_records("workshop_comments", "workshopid", "$workshop->id")) {
536 $result = false;
539 if (! delete_records("workshop_stockcomments", "workshopid", "$workshop->id")) {
540 $result = false;
543 if (! delete_records("workshop_grades", "workshopid", "$workshop->id")) {
544 $result = false;
547 if (! delete_records("workshop_elements", "workshopid", "$workshop->id")) {
548 $result = false;
551 if (! delete_records("workshop_assessments", "workshopid", "$workshop->id")) {
552 $result = false;
555 if (! delete_records("workshop_submissions", "workshopid", "$workshop->id")) {
556 $result = false;
559 if (! delete_records("workshop", "id", "$workshop->id")) {
560 $result = false;
563 if (! delete_records('event', 'modulename', 'workshop', 'instance', $workshop->id)) {
564 $result = false;
567 return $result;
571 ///////////////////////////////////////////////////////////////////////////////
572 function workshop_grades($workshopid) {
573 /// Must return an array of grades, indexed by user, and a max grade.
574 /// only returns grades once assessment has started
575 /// returns nothing if workshop is not graded
576 global $CFG;
578 $return = null;
579 if ($workshop = get_record("workshop", "id", $workshopid)) {
580 if (($workshop->assessmentstart < time()) and $workshop->gradingstrategy) {
581 if ($students = workshop_get_students($workshop)) {
582 foreach ($students as $student) {
583 if ($workshop->wtype) {
584 $gradinggrade = workshop_gradinggrade($workshop, $student);
585 } else { // ignore grading grades for simple assignments
586 $gradinggrade = 0;
588 $bestgrade = 0;
589 if ($submissions = workshop_get_user_submissions($workshop, $student)) {
590 foreach ($submissions as $submission) {
591 if (!$submission->late) {
592 $grade = workshop_submission_grade($workshop, $submission);
593 } else {
594 $grade = 0.01;
596 if ($grade > $bestgrade) {
597 $bestgrade = $grade;
601 $return->grades[$student->id] = $gradinggrade + $bestgrade;
605 // set maximum grade if graded
606 if ($workshop->gradingstrategy) {
607 if ($workshop->wtype) {
608 $return->maxgrade = $workshop->grade + $workshop->gradinggrade;
609 } else { // ignore grading grades for simple assignemnts
610 $return->maxgrade = $workshop->grade;
614 return $return;
617 //////////////////////////////////////////////////////////////////////////////////////
618 function workshop_is_recent_activity($course, $isteacher, $timestart) {//jlw1 added for adding mark to courses with activity in My Moodle
619 global $CFG;
621 // have a look for agreed assessments for this user (agree)
622 $agreecontent = false;
623 if (!$isteacher) { // teachers only need to see submissions
624 if ($logs = workshop_get_agree_logs($course, $timestart)) {
625 // got some, see if any belong to a visible module
626 foreach ($logs as $log) {
627 // Create a temp valid module structure (only need courseid, moduleid)
628 $tempmod->course = $course->id;
629 $tempmod->id = $log->workshopid;
630 //Obtain the visible property from the instance
631 if (instance_is_visible("workshop",$tempmod)) {
632 $agreecontent = true;
633 break;
638 return false;
642 ///////////////////////////////////////////////////////////////////////////////
644 // NOTE: $isteacher usage should be converted to use roles.
645 // TODO: Fix this function.
647 function workshop_print_recent_activity($course, $isteacher, $timestart) {
648 global $CFG;
650 // have a look for agreed assessments for this user (agree)
651 $agreecontent = false;
652 if (!$isteacher) { // teachers only need to see submissions
653 if ($logs = workshop_get_agree_logs($course, $timestart)) {
654 // got some, see if any belong to a visible module
655 foreach ($logs as $log) {
656 // Create a temp valid module structure (only need courseid, moduleid)
657 $tempmod->course = $course->id;
658 $tempmod->id = $log->workshopid;
659 //Obtain the visible property from the instance
660 if (instance_is_visible("workshop",$tempmod)) {
661 $agreecontent = true;
662 break;
665 // if we got some "live" ones then output them
666 if ($agreecontent) {
667 print_headline(get_string("workshopagreedassessments", "workshop").":");
668 foreach ($logs as $log) {
669 //Create a temp valid module structure (only need courseid, moduleid)
670 $tempmod->course = $course->id;
671 $tempmod->id = $log->workshopid;
672 //Obtain the visible property from the instance
673 if (instance_is_visible("workshop",$tempmod)) {
674 if (!workshop_is_teacher($workshop, $log->userid)) { // don't break anonymous rule
675 $log->firstname = $course->student;
676 $log->lastname = '';
678 print_recent_activity_note($log->time, $log, $log->name,
679 $CFG->wwwroot.'/mod/workshop/'.$log->url);
686 // have a look for new assessments for this user (assess)
687 $assesscontent = false;
688 if (!$isteacher) { // teachers only need to see submissions
689 if ($logs = workshop_get_assess_logs($course, $timestart)) {
690 // got some, see if any belong to a visible module
691 foreach ($logs as $log) {
692 // Create a temp valid module structure (only need courseid, moduleid)
693 $tempmod->course = $course->id;
694 $tempmod->id = $log->workshopid;
695 //Obtain the visible property from the instance
696 if (instance_is_visible("workshop",$tempmod)) {
697 $assesscontent = true;
698 break;
701 // if we got some "live" ones then output them
702 if ($assesscontent) {
703 print_headline(get_string("workshopassessments", "workshop").":");
704 foreach ($logs as $log) {
705 //Create a temp valid module structure (only need courseid, moduleid)
706 $tempmod->course = $course->id;
707 $tempmod->id = $log->workshopid;
708 //Obtain the visible property from the instance
709 if (instance_is_visible("workshop",$tempmod)) {
710 if (!workshop_is_teacher($tempmod->id, $log->userid)) { // don't break anonymous rule
711 $log->firstname = $course->student;
712 $log->lastname = '';
714 print_recent_activity_note($log->time, $log, $log->name,
715 $CFG->wwwroot.'/mod/workshop/'.$log->url);
721 // have a look for new comments for this user (comment)
722 $commentcontent = false;
723 if (!$isteacher) { // teachers only need to see submissions
724 if ($logs = workshop_get_comment_logs($course, $timestart)) {
725 // got some, see if any belong to a visible module
726 foreach ($logs as $log) {
727 // Create a temp valid module structure (only need courseid, moduleid)
728 $tempmod->course = $course->id;
729 $tempmod->id = $log->workshopid;
730 //Obtain the visible property from the instance
731 if (instance_is_visible("workshop",$tempmod)) {
732 $commentcontent = true;
733 break;
736 // if we got some "live" ones then output them
737 if ($commentcontent) {
738 print_headline(get_string("workshopcomments", "workshop").":");
739 foreach ($logs as $log) {
740 //Create a temp valid module structure (only need courseid, moduleid)
741 $tempmod->course = $course->id;
742 $tempmod->id = $log->workshopid;
743 //Obtain the visible property from the instance
744 if (instance_is_visible("workshop",$tempmod)) {
745 $log->firstname = $course->student; // Keep anonymous
746 $log->lastname = '';
747 print_recent_activity_note($log->time, $log, $log->name,
748 $CFG->wwwroot.'/mod/workshop/'.$log->url);
755 // have a look for new assessment gradings for this user (grade)
756 $gradecontent = false;
757 if ($logs = workshop_get_grade_logs($course, $timestart)) {
758 // got some, see if any belong to a visible module
759 foreach ($logs as $log) {
760 // Create a temp valid module structure (only need courseid, moduleid)
761 $tempmod->course = $course->id;
762 $tempmod->id = $log->workshopid;
763 //Obtain the visible property from the instance
764 if (instance_is_visible("workshop",$tempmod)) {
765 $gradecontent = true;
766 break;
769 // if we got some "live" ones then output them
770 if ($gradecontent) {
771 print_headline(get_string("workshopfeedback", "workshop").":");
772 foreach ($logs as $log) {
773 //Create a temp valid module structure (only need courseid, moduleid)
774 $tempmod->course = $course->id;
775 $tempmod->id = $log->workshopid;
776 //Obtain the visible property from the instance
777 if (instance_is_visible("workshop",$tempmod)) {
778 $log->firstname = $course->teacher; // Keep anonymous
779 $log->lastname = '';
780 print_recent_activity_note($log->time, $log, $log->name,
781 $CFG->wwwroot.'/mod/workshop/'.$log->url);
787 // have a look for new submissions (only show to teachers) (submit)
788 $submitcontent = false;
789 if ($isteacher) {
790 if ($logs = workshop_get_submit_logs($course, $timestart)) {
791 // got some, see if any belong to a visible module
792 foreach ($logs as $log) {
793 // Create a temp valid module structure (only need courseid, moduleid)
794 $tempmod->course = $course->id;
795 $tempmod->id = $log->workshopid;
796 //Obtain the visible property from the instance
797 if (instance_is_visible("workshop",$tempmod)) {
798 $submitcontent = true;
799 break;
802 // if we got some "live" ones then output them
803 if ($submitcontent) {
804 print_headline(get_string("workshopsubmissions", "workshop").":");
805 foreach ($logs as $log) {
806 //Create a temp valid module structure (only need courseid, moduleid)
807 $tempmod->course = $course->id;
808 $tempmod->id = $log->workshopid;
809 //Obtain the visible property from the instance
810 if (instance_is_visible("workshop",$tempmod)) {
811 print_recent_activity_note($log->time, $log, $log->name,
812 $CFG->wwwroot.'/mod/workshop/'.$log->url);
819 return $agreecontent or $assesscontent or $commentcontent or $gradecontent or $submitcontent;
823 ///////////////////////////////////////////////////////////////////////////////
824 function workshop_refresh_events($courseid = 0) {
825 // This standard function will check all instances of this module
826 // and make sure there are up-to-date events created for each of them.
827 // If courseid = 0, then every workshop event in the site is checked, else
828 // only workshop events belonging to the course specified are checked.
829 // This function is used, in its new format, by restore_refresh_events()
831 if ($courseid == 0) {
832 if (! $workshops = get_records("workshop")) {
833 return true;
835 } else {
836 if (! $workshops = get_records("workshop", "course", $courseid)) {
837 return true;
840 $moduleid = get_field('modules', 'id', 'name', 'workshop');
842 foreach ($workshops as $workshop) {
844 $dates = array(
845 'submissionstart' => $workshop->submissionstart,
846 'submissionend' => $workshop->submissionend,
847 'assessmentstart' => $workshop->assessmentstart,
848 'assessmentend' => $workshop->assessmentend
851 foreach ($dates as $type => $date) {
853 if ($date) {
854 if ($event = get_record('event', 'modulename', 'workshop', 'instance', $workshop->id, 'eventtype', $type)) {
855 $event->name = addslashes(get_string($type.'event','workshop', $workshop->name));
856 $event->description = addslashes($workshop->description);
857 $event->eventtype = $type;
858 $event->timestart = $date;
859 update_event($event);
860 } else {
861 $event->courseid = $workshop->course;
862 $event->modulename = 'workshop';
863 $event->instance = $workshop->id;
864 $event->name = addslashes(get_string($type.'event','workshop', $workshop->name));
865 $event->description = addslashes($workshop->description);
866 $event->eventtype = $type;
867 $event->timestart = $date;
868 $event->timeduration = 0;
869 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $workshop->id);
870 add_event($event);
875 return true;
879 ///////////////////////////////////////////////////////////////////////////////
880 function workshop_update_instance($workshop) {
881 // Given an object containing all the necessary data,
882 // (defined by the form in mod.html) this function
883 // will update an existing instance with new data.
884 global $CFG;
886 $workshop->timemodified = time();
888 $workshop->submissionstart = make_timestamp($workshop->submissionstartyear,
889 $workshop->submissionstartmonth, $workshop->submissionstartday, $workshop->submissionstarthour,
890 $workshop->submissionstartminute);
892 $workshop->assessmentstart = make_timestamp($workshop->assessmentstartyear,
893 $workshop->assessmentstartmonth, $workshop->assessmentstartday, $workshop->assessmentstarthour,
894 $workshop->assessmentstartminute);
896 $workshop->submissionend = make_timestamp($workshop->submissionendyear,
897 $workshop->submissionendmonth, $workshop->submissionendday, $workshop->submissionendhour,
898 $workshop->submissionendminute);
900 $workshop->assessmentend = make_timestamp($workshop->assessmentendyear,
901 $workshop->assessmentendmonth, $workshop->assessmentendday, $workshop->assessmentendhour,
902 $workshop->assessmentendminute);
904 $workshop->releasegrades = make_timestamp($workshop->releaseyear,
905 $workshop->releasemonth, $workshop->releaseday, $workshop->releasehour,
906 $workshop->releaseminute);
908 if (!workshop_check_dates($workshop)) {
909 return get_string('invaliddates', 'workshop');
912 // set the workshop's type
913 $wtype = 0; // 3 phases, no grading grades
914 if ($workshop->includeself or $workshop->ntassessments) $wtype = 1; // 3 phases with grading grades
915 if ($workshop->nsassessments) $wtype = 2; // 5 phases with grading grades
916 $workshop->wtype = $wtype;
918 // encode password if necessary
919 if (!empty($workshop->password)) {
920 $workshop->password = md5($workshop->password);
921 } else {
922 unset($workshop->password);
925 $workshop->id = $workshop->instance;
927 if ($returnid = update_record("workshop", $workshop)) {
929 $dates = array(
930 'submissionstart' => $workshop->submissionstart,
931 'submissionend' => $workshop->submissionend,
932 'assessmentstart' => $workshop->assessmentstart,
933 'assessmentend' => $workshop->assessmentend
935 $moduleid = get_field('modules', 'id', 'name', 'workshop');
937 foreach ($dates as $type => $date) {
938 if ($event = get_record('event', 'modulename', 'workshop', 'instance', $workshop->id, 'eventtype', $type)) {
939 $event->name = get_string($type.'event','workshop', $workshop->name);
940 $event->description = $workshop->description;
941 $event->eventtype = $type;
942 $event->timestart = $date;
943 update_event($event);
944 } else if ($date) {
945 $event = NULL;
946 $event->name = get_string($type.'event','workshop', $workshop->name);
947 $event->description = $workshop->description;
948 $event->courseid = $workshop->course;
949 $event->groupid = 0;
950 $event->userid = 0;
951 $event->modulename = 'workshop';
952 $event->instance = $workshop->instance;
953 $event->eventtype = $type;
954 $event->timestart = $date;
955 $event->timeduration = 0;
956 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $workshop->id);
957 add_event($event);
962 if (time() > $workshop->assessmentstart) {
963 // regrade all the submissions...
964 set_field("workshop_submissions", "nassessments", 0, "workshopid", $workshop->id);
965 workshop_grade_assessments($workshop);
968 return $returnid;
971 ///////////////////////////////////////////////////////////////////////////////
972 function workshop_user_complete($course, $user, $mod, $workshop) {
973 if ($submission = workshop_get_student_submission($workshop, $user)) {
974 if ($basedir = workshop_file_area($workshop, $user)) {
975 if ($files = get_directory_list($basedir)) {
976 $countfiles = count($files).' '.get_string('submissions', 'workshop');
977 foreach ($files as $file) {
978 $countfiles .= "; $file";
983 print_simple_box_start();
985 echo $submission->description.'<br />';
987 if (!empty($countfiles)) {
988 echo $countfiles,'<br />';
991 workshop_print_feedback($course, $submission);
993 print_simple_box_end();
995 } else {
996 print_string('notsubmittedyet', 'workshop');
1000 //////////////////////////////////////////////////////////////////////////////////////
1001 function workshop_print_feedback($course, $submission) {
1002 global $CFG, $RATING;
1004 if (! $feedbacks = get_records('workshop_assessments', 'submissionid', $submission->id)) {
1005 return;
1008 $strgrade = get_string('grade');
1009 $strnograde = get_string('nograde');
1011 foreach ($feedbacks as $feedback) {
1012 if (! $user = get_record('user', 'id', $feedback->userid)) {
1013 /// Weird error but we'll just ignore it and continue with other feedback
1014 continue;
1017 echo '<table cellspacing="0" class="workshop_feedbackbox">';
1019 echo '<tr>';
1020 echo '<td class="picture left">';
1021 print_user_picture($user->id, $course->id, $user->picture);
1022 echo '</td>';
1023 echo '<td><span class="author">'.fullname($user).'</span>';
1024 echo '<span class="time">'.userdate($feedback->timegraded).'</span>';
1025 echo '</tr>';
1027 echo '<tr><td class="left side">&nbsp;</td>';
1028 echo '<td class="content">';
1030 if ($feedback->grade) {
1031 echo $strgrade.': '.$feedback->grade;
1032 } else {
1033 echo $strnograde;
1036 echo '<span class="comment">'.format_text($feedback->generalcomment).'</span>';
1037 echo '<span class="teachercomment">'.format_text($feedback->teachercomment).'</span>';
1038 echo '</td></tr></table>';
1046 ///////////////////////////////////////////////////////////////////////////////
1047 function workshop_user_outline($course, $user, $mod, $workshop) {
1048 if ($submissions = workshop_get_user_submissions($workshop, $user)) {
1049 $result->info = count($submissions)." ".get_string("submissions", "workshop");
1050 // workshop_get_user_submissions returns the newest one first
1051 foreach ($submissions as $submission) {
1052 $result->time = $submission->timecreated;
1053 break;
1055 return $result;
1057 return NULL;
1060 //////////////////////////////////////////////////////////////////////////////////////
1061 function workshop_get_participants($workshopid) {
1062 //Returns the users with data in one workshop
1063 //(users with records in workshop_submissions, workshop_assessments and workshop_comments, students)
1065 global $CFG;
1067 //Get students from workshop_submissions
1068 $st_submissions = get_records_sql("SELECT DISTINCT u.id, u.id
1069 FROM {$CFG->prefix}user u,
1070 {$CFG->prefix}workshop_submissions s
1071 WHERE s.workshopid = '$workshopid' and
1072 u.id = s.userid");
1073 //Get students from workshop_assessments
1074 $st_assessments = get_records_sql("SELECT DISTINCT u.id, u.id
1075 FROM {$CFG->prefix}user u,
1076 {$CFG->prefix}workshop_assessments a
1077 WHERE a.workshopid = '$workshopid' and
1078 u.id = a.userid");
1080 //Get students from workshop_comments
1081 $st_comments = get_records_sql("SELECT DISTINCT u.id, u.id
1082 FROM {$CFG->prefix}user u,
1083 {$CFG->prefix}workshop_comments c
1084 WHERE c.workshopid = '$workshopid' and
1085 u.id = c.userid");
1087 //Add st_assessments to st_submissions
1088 if ($st_assessments) {
1089 foreach ($st_assessments as $st_assessment) {
1090 $st_submissions[$st_assessment->id] = $st_assessment;
1093 //Add st_comments to st_submissions
1094 if ($st_comments) {
1095 foreach ($st_comments as $st_comment) {
1096 $st_submissions[$st_comment->id] = $st_comment;
1099 //Return st_submissions array (it contains an array of unique users)
1100 return ($st_submissions);
1103 //////////////////////////////////////////////////////////////////////////////////////
1104 function workshop_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid,
1105 $workshop="0", $user="", $groupid="") {
1106 // Returns all workshop posts since a given time. If workshop is specified then
1107 // this restricts the results
1109 global $CFG;
1111 if ($workshop) {
1112 $workshopselect = " AND cm.id = '$workshop'";
1113 } else {
1114 $workshopselect = "";
1117 if ($user) {
1118 $userselect = " AND u.id = '$user'";
1119 } else {
1120 $userselect = "";
1123 $posts = get_records_sql("SELECT s.*, u.firstname, u.lastname,
1124 u.picture, cm.instance, w.name, cm.section
1125 FROM {$CFG->prefix}workshop_submissions s,
1126 {$CFG->prefix}user u,
1127 {$CFG->prefix}course_modules cm,
1128 {$CFG->prefix}workshop w
1129 WHERE s.timecreated > '$sincetime' $workshopselect
1130 AND s.userid = u.id $userselect
1131 AND w.course = '$courseid'
1132 AND cm.instance = w.id
1133 AND cm.course = w.course
1134 AND s.workshopid = w.id
1135 ORDER BY s.id");
1138 if (empty($posts)) {
1139 return;
1142 foreach ($posts as $post) {
1144 if (empty($groupid) || ismember($groupid, $post->userid)) {
1146 $tmpactivity = new Object;
1148 $tmpactivity->type = "workshop";
1149 $tmpactivity->defaultindex = $index;
1150 $tmpactivity->instance = $post->instance;
1151 $tmpactivity->name = $post->name;
1152 $tmpactivity->section = $post->section;
1154 $tmpactivity->content->id = $post->id;
1155 $tmpactivity->content->title = $post->title;
1157 $tmpactivity->user->userid = $post->userid;
1158 $tmpactivity->user->fullname = fullname($post);
1159 $tmpactivity->user->picture = $post->picture;
1161 $tmpactivity->timestamp = $post->timecreated;
1162 $activities[] = $tmpactivity;
1164 $index++;
1168 return;
1171 //////////////////////////////////////////////////////////////////////////////////////
1172 function workshop_print_recent_mod_activity($activity, $course, $detail=false) {
1174 global $CFG;
1176 echo '<table border="0" cellpadding="3" cellspacing="0">';
1178 if (!empty($activity->content->parent)) {
1179 $openformat = "<font size=\"2\"><i>";
1180 $closeformat = "</i></font>";
1181 } else {
1182 $openformat = "<b>";
1183 $closeformat = "</b>";
1186 echo "<tr><td class=\"workshoppostpicture\" width=\"35\" valign=\"top\">";
1187 print_user_picture($activity->user->userid, $course, $activity->user->picture);
1188 echo "</td><td>$openformat";
1190 if ($detail) {
1191 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
1192 "class=\"icon\" alt=\"".strip_tags(format_string($activity->name,true))."\" /> ";
1194 echo "<a href=\"$CFG->wwwroot/mod/workshop/view.php?"
1195 . "#" . $activity->content->id . "\">".$activity->content->title;
1197 echo "</a>$closeformat";
1199 echo "<br /><font size=\"2\">";
1200 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&amp;course=" . "$course\">"
1201 . $activity->user->fullname . "</a>";
1202 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
1203 echo "</table>";
1205 return;
1210 //////////////////////////////////////////////////////////////////////////////////////
1211 // Non-standard workshop functions
1212 ///////////////////////////////////////////////////////////////////////////////////////////////
1213 function workshop_compare_assessments($workshop, $assessment1, $assessment2) {
1214 global $WORKSHOP_ASSESSMENT_COMPS, $WORKSHOP_EWEIGHTS;
1215 // first get the assignment elements for maxscores...
1216 $elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC");
1217 foreach ($elementsraw as $element) {
1218 $maxscore[] = $element->maxscore; // to renumber index 0,1,2...
1219 $weight[] = $WORKSHOP_EWEIGHTS[$element->weight]; // get real value and renumber index 0,1,2...
1221 for ($i = 0; $i < 2; $i++) {
1222 if ($i) {
1223 $rawgrades = get_records("workshop_grades", "assessmentid", $assessment1->id, "elementno ASC");
1224 } else {
1225 $rawgrades = get_records("workshop_grades", "assessmentid", $assessment2->id, "elementno ASC");
1227 foreach ($rawgrades as $grade) {
1228 $grades[$i][] = $grade->grade;
1231 $sumdiffs = 0;
1232 $sumweights = 0;
1233 switch ($workshop->gradingstrategy) {
1234 case 1 : // accumulative grading and...
1235 case 4 : // ...rubic grading
1236 for ($i=0; $i < $workshop->nelements; $i++) {
1237 $diff = ($grades[0][$i] - $grades[1][$i]) * $weight[$i] / $maxscore[$i];
1238 $sumdiffs += $diff * $diff; // use squared distances
1239 $sumweights += $weight[$i];
1241 break;
1242 case 2 : // error banded grading
1243 // ignore maxscores here, the grades are either 0 or 1,
1244 for ($i=0; $i < $workshop->nelements; $i++) {
1245 $diff = ($grades[0][$i] - $grades[1][$i]) * $weight[$i];
1246 $sumdiffs += $diff * $diff; // use squared distances
1247 $sumweights += $weight[$i];
1249 break;
1250 case 3 : // criterion grading
1251 // here we only need to look at the difference between the "zero" grade elements
1252 $diff = ($grades[0][0] - $grades[1][0]) / (count($elementsraw) - 1);
1253 $sumdiffs = $diff * $diff;
1254 $sumweights = 1;
1255 break;
1257 // convert to a sensible grade (always out of 100)
1258 $COMP = (object)$WORKSHOP_ASSESSMENT_COMPS[$workshop->assessmentcomps];
1259 $factor = $COMP->value;
1260 $gradinggrade = (($factor - ($sumdiffs / $sumweights)) / $factor) * 100;
1261 if ($gradinggrade < 0) {
1262 $gradinggrade = 0;
1264 return $gradinggrade;
1268 //////////////////////////////////////////////////////////////////////////////////////
1269 function workshop_count_assessments($submission) {
1270 // Return the (real) assessments for this submission,
1271 $timenow = time();
1272 return count_records_select("workshop_assessments",
1273 "submissionid = $submission->id AND timecreated < $timenow");
1277 //////////////////////////////////////////////////////////////////////////////////////
1278 function workshop_count_ungraded_assessments($workshop) {
1279 // function returns the number of ungraded assessments by students
1280 global $CFG;
1282 $timenow = time();
1283 $n = 0;
1284 // get all the cold assessments that have not been graded
1285 if ($assessments = get_records_select("workshop_assessments", "workshopid = $workshop->id AND
1286 (timecreated + $CFG->maxeditingtime) < $timenow AND timegraded = 0")) {
1287 foreach ($assessments as $assessment) {
1288 if (workshop_is_student($workshop, $assessment->userid)) {
1289 $n++;
1293 return $n;
1297 //////////////////////////////////////////////////////////////////////////////////////
1298 function workshop_file_area($workshop, $submission) {
1299 return make_upload_directory( workshop_file_area_name($workshop, $submission) );
1303 //////////////////////////////////////////////////////////////////////////////////////
1304 function workshop_file_area_name($workshop, $submission) {
1305 // Creates a directory file name, suitable for make_upload_directory()
1306 global $CFG;
1308 return "$workshop->course/$CFG->moddata/workshop/$submission->id";
1312 ///////////////////////////////////////////////////////////////////////////////////////////////
1313 function workshop_get_agree_logs($course, $timestart) {
1314 // get the "agree" entries for this user (the assessment owner) and add the first and last names
1315 // the last two probably wont be used...
1316 global $CFG, $USER;
1317 if (empty($USER->id)) {
1318 return false;
1321 $timethen = time() - $CFG->maxeditingtime;
1322 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.workshopid, a.userid, e.name
1323 FROM {$CFG->prefix}log l,
1324 {$CFG->prefix}workshop e,
1325 {$CFG->prefix}workshop_submissions s,
1326 {$CFG->prefix}workshop_assessments a,
1327 {$CFG->prefix}user u
1328 WHERE l.time > $timestart AND l.time < $timethen
1329 AND l.course = $course->id AND l.module = 'workshop' AND l.action = 'agree'
1330 AND a.id = l.info AND s.id = a.submissionid AND a.userid = $USER->id
1331 AND u.id = s.userid AND e.id = a.workshopid");
1335 ///////////////////////////////////////////////////////////////////////////////////////////////
1336 function workshop_get_assess_logs($course, $timestart) {
1337 // get the "assess" entries for this user and add the first and last names...
1338 global $CFG, $USER;
1339 if (empty($USER->id)) {
1340 return false;
1343 $timethen = time() - $CFG->maxeditingtime;
1344 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.workshopid, a.userid, e.name
1345 FROM {$CFG->prefix}log l,
1346 {$CFG->prefix}workshop e,
1347 {$CFG->prefix}workshop_submissions s,
1348 {$CFG->prefix}workshop_assessments a,
1349 {$CFG->prefix}user u
1350 WHERE l.time > $timestart AND l.time < $timethen
1351 AND l.course = $course->id AND l.module = 'workshop' AND l.action = 'assess'
1352 AND a.id = l.info AND s.id = a.submissionid AND s.userid = $USER->id
1353 AND u.id = a.userid AND e.id = a.workshopid");
1357 //////////////////////////////////////////////////////////////////////////////////////
1358 function workshop_get_assessments($submission, $all = '', $order = '') {
1359 // Return assessments for this submission ordered oldest first, newest last
1360 // new assessments made within the editing time are NOT returned unless they
1361 // belong to the user or the second argument is set to ALL
1362 global $CFG, $USER;
1364 $timenow = time();
1365 if (!$order) {
1366 $order = "timecreated DESC";
1368 if ($all != 'ALL') {
1369 return get_records_select("workshop_assessments", "(submissionid = $submission->id) AND
1370 ((timecreated < $timenow - $CFG->maxeditingtime) or
1371 ((timecreated < $timenow) AND (userid = $USER->id)))", $order);
1372 } else {
1373 return get_records_select("workshop_assessments", "submissionid = $submission->id AND
1374 (timecreated < $timenow)", $order);
1379 ///////////////////////////////////////////////////////////////////////////////////////////////
1380 function workshop_get_comment_logs($course, $timestart) {
1381 // get the "comment" entries for this user and add the first and last names (which may not be used)...
1382 global $CFG, $USER;
1383 if (empty($USER->id)) {
1384 return false;
1387 $timethen = time() - $CFG->maxeditingtime;
1388 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.workshopid, e.name
1389 FROM {$CFG->prefix}log l,
1390 {$CFG->prefix}workshop e,
1391 {$CFG->prefix}workshop_submissions s,
1392 {$CFG->prefix}workshop_assessments a,
1393 {$CFG->prefix}workshop_comments c,
1394 {$CFG->prefix}user u
1395 WHERE l.time > $timestart AND l.time < $timethen
1396 AND l.course = $course->id AND l.module = 'workshop' AND l.action = 'comment'
1397 AND c.id = l.info AND c.userid != $USER->id AND a.id = c.assessmentid
1398 AND s.id = a.submissionid AND (s.userid = $USER->id OR a.userid = $USER->id)
1399 AND u.id = a.userid AND e.id = a.workshopid");
1403 ///////////////////////////////////////////////////////////////////////////////////////////////
1404 function workshop_get_grade_logs($course, $timestart) {
1405 // get the "grade" entries for this user and add the first and last names (of submission owner,
1406 // better to get name of teacher...
1407 // ...but not available in assessment record...)
1408 global $CFG, $USER;
1409 if (empty($USER->id)) {
1410 return false;
1413 $timethen = time() - $CFG->maxeditingtime;
1414 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.workshopid, e.name
1415 FROM {$CFG->prefix}log l,
1416 {$CFG->prefix}workshop e,
1417 {$CFG->prefix}workshop_submissions s,
1418 {$CFG->prefix}workshop_assessments a,
1419 {$CFG->prefix}user u
1420 WHERE l.time > $timestart AND l.time < $timethen
1421 AND l.course = $course->id AND l.module = 'workshop' AND l.action = 'grade'
1422 AND a.id = l.info AND s.id = a.submissionid AND a.userid = $USER->id
1423 AND u.id = s.userid AND e.id = a.workshopid");
1427 //////////////////////////////////////////////////////////////////////////////////////
1428 function workshop_get_student_submission($workshop, $user) {
1429 // Return a submission for a particular user
1430 global $CFG;
1432 $submission = get_record("workshop_submissions", "workshopid", $workshop->id, "userid", $user->id);
1433 if (!empty($submission->timecreated)) {
1434 return $submission;
1436 return NULL;
1440 //////////////////////////////////////////////////////////////////////////////////////
1441 function workshop_get_student_submissions($workshop, $order = "title") {
1442 // Return all ENROLLED student submissions
1443 global $CFG;
1445 if ($order == "title") {
1446 $order = "s.title";
1448 if ($order == "name") {
1449 $order = "a.lastname, a.firstname";
1451 if ($order == "time") {
1452 $order = "s.timecreated ASC";
1455 if (!$students = workshop_get_students($workshop)) {
1456 return false;
1458 $list = "(";
1459 foreach ($students as $student) {
1460 $list .= "$student->id,";
1462 $list = rtrim($list, ',').")";
1464 return get_records_sql("SELECT s.* FROM {$CFG->prefix}workshop_submissions s, {$CFG->prefix}user a
1465 WHERE s.userid IN $list
1466 AND s.workshopid = $workshop->id
1467 AND s.timecreated > 0
1468 AND s.userid = a.id
1469 ORDER BY $order");
1473 ///////////////////////////////////////////////////////////////////////////////////////////////
1474 function workshop_get_submit_logs($course, $timestart) {
1475 // get the "submit" entries and add the first and last names...
1476 global $CFG, $USER;
1478 $timethen = time() - $CFG->maxeditingtime;
1479 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, l.info as workshopid, e.name
1480 FROM {$CFG->prefix}log l,
1481 {$CFG->prefix}workshop e,
1482 {$CFG->prefix}user u
1483 WHERE l.time > $timestart AND l.time < $timethen
1484 AND l.course = $course->id AND l.module = 'workshop'
1485 AND l.action = 'submit'
1486 AND e.id = l.info
1487 AND u.id = l.userid");
1491 //////////////////////////////////////////////////////////////////////////////////////
1492 function workshop_get_unmailed_assessments($cutofftime) {
1493 /// Return list of assessments that have not been mailed out
1494 global $CFG;
1495 return get_records_sql("SELECT a.*, g.course, g.name
1496 FROM {$CFG->prefix}workshop_assessments a, {$CFG->prefix}workshop g
1497 WHERE a.mailed = 0
1498 AND a.timecreated < $cutofftime
1499 AND g.id = a.workshopid
1500 AND g.releasegrades < $cutofftime");
1504 //////////////////////////////////////////////////////////////////////////////////////
1505 function workshop_get_unmailed_comments($cutofftime) {
1506 /// Return list of comments that have not been mailed out
1507 global $CFG;
1508 return get_records_sql("SELECT c.*, g.course, g.name
1509 FROM {$CFG->prefix}workshop_comments c, {$CFG->prefix}workshop g
1510 WHERE c.mailed = 0
1511 AND c.timecreated < $cutofftime
1512 AND g.id = c.workshopid");
1516 //////////////////////////////////////////////////////////////////////////////////////
1517 function workshop_get_unmailed_graded_assessments($cutofftime) {
1518 /// Return list of graded assessments that have not been mailed out
1519 global $CFG;
1520 return get_records_sql("SELECT a.*, g.course, g.name
1521 FROM {$CFG->prefix}workshop_assessments a, {$CFG->prefix}workshop g
1522 WHERE a.mailed = 0
1523 AND a.timegraded < $cutofftime
1524 AND a.timegraded > 0
1525 AND g.id = a.workshopid");
1529 //////////////////////////////////////////////////////////////////////////////////////
1530 function workshop_get_unmailed_resubmissions($cutofftime) {
1531 /// Return list of assessments of resubmissions that have not been mailed out
1532 global $CFG;
1533 return get_records_sql("SELECT a.*, w.course, w.name
1534 FROM {$CFG->prefix}workshop_assessments a, {$CFG->prefix}workshop w
1535 WHERE a.mailed = 0
1536 AND a.resubmission = 1
1537 AND w.id = a.workshopid");
1541 //////////////////////////////////////////////////////////////////////////////////////
1542 function workshop_get_user_assessments($workshop, $user) {
1543 // Return all the user's assessments, newest first, oldest last (hot, warm and cold ones)
1544 return get_records_select("workshop_assessments", "workshopid = $workshop->id AND userid = $user->id",
1545 "timecreated DESC");
1549 //////////////////////////////////////////////////////////////////////////////////////
1550 function workshop_get_user_submissions($workshop, $user) {
1551 // return real submissions of user newest first, oldest last. Ignores the dummy submissions
1552 // which get created to hold the final grades for users that make no submissions
1553 return get_records_select("workshop_submissions", "workshopid = $workshop->id AND
1554 userid = $user->id AND timecreated > 0", "timecreated DESC" );
1558 //////////////////////////////////////////////////////////////////////////////////////
1559 function workshop_grade_assessments($workshop, $verbose=false) {
1560 global $WORKSHOP_EWEIGHTS;
1562 // timeout after 10 minutes
1563 @set_time_limit(600);
1565 $timenow = time();
1567 // set minumim value for the variance (of the elements)
1568 $minvar = 0.05;
1570 // check when the standard deviations were calculated
1571 $oldtotalassessments = get_field("workshop_elements", "totalassessments", "workshopid", $workshop->id,
1572 "elementno", 0);
1573 $totalassessments = count_records("workshop_assessments", "workshopid", $workshop->id);
1574 // calculate the std. devs every 10 assessments for low numbers of assessments, thereafter every 100 new assessments
1575 if ((($totalassessments < 100) and (($totalassessments - $oldtotalassessments) > 10)) or
1576 (($totalassessments - $oldtotalassessments) > 100)) {
1577 // calculate the means for each submission using just the "good" assessments
1578 if ($submissions = get_records("workshop_submissions", "workshopid", $workshop->id)) {
1579 foreach ($submissions as $submission) {
1580 $nassessments[$submission->id] = 0;
1581 if ($assessments = workshop_get_assessments($submission)) {
1582 foreach ($assessments as $assessment) {
1583 // test if assessment is "good", a teacher assessment always "good", but may be weighted out
1584 if (workshop_is_teacher($workshop, $assessment->userid)) {
1585 if (!$workshop->teacherweight) {
1586 // drop teacher's assessment as weight is zero
1587 continue;
1589 } elseif ((!$assessment->gradinggrade and $assessment->timegraded) or
1590 ($workshop->agreeassessments and !$assessment->timeagreed)) {
1591 // it's a duff assessment, or it's not been agreed
1592 continue;
1594 if (isset($num[$submission->id])) {
1595 if (workshop_is_teacher($workshop, $assessment->userid)) {
1596 $num[$submission->id] += $workshop->teacherweight; // weight teacher's assessment
1597 } else {
1598 $num[$submission->id]++; // number of assessments
1600 $nassessments[$submission->id]++;
1601 } else {
1602 if (workshop_is_teacher($workshop, $assessment->userid)) {
1603 $num[$submission->id] = $workshop->teacherweight;
1604 } else {
1605 $num[$submission->id] = 1;
1607 $nassessments[$submission->id] = 1;
1609 for ($i = 0; $i < $workshop->nelements; $i++) {
1610 $grade = get_field("workshop_grades", "grade",
1611 "assessmentid", $assessment->id, "elementno", $i);
1612 if (isset($sum[$submission->id][$i])) {
1613 if (workshop_is_teacher($workshop, $assessment->userid)) {
1614 $sum[$submission->id][$i] += $workshop->teacherweight * $grade; // teacher's grade
1615 } else {
1616 $sum[$submission->id][$i] += $grade; // student's grade
1618 } else {
1619 if (workshop_is_teacher($workshop, $assessment->userid)) {
1620 $sum[$submission->id][$i] = $workshop->teacherweight * $grade; // teacher's grade
1621 } else {
1622 $sum[$submission->id][$i] = $grade; // students's grade
1630 if (!isset($num)) {
1631 // no assessments yet
1632 return;
1634 reset($num);
1635 // calculate the means for each submission
1636 $total = 0;
1637 foreach ($num as $submissionid => $n) {
1638 if ($n) { // stop division by zero
1639 for ($i = 0; $i < $workshop->nelements; $i++) {
1640 $mean[$submissionid][$i] = $sum[$submissionid][$i] / $n;
1641 // echo "Submission: $submissionid; Element: $i; Mean: {$mean[$submissionid][$i]}<br />\n";
1643 $total += $n; // weighted total
1646 if ($verbose) {
1647 echo "<p style=\"text-align:center\">".get_string("numberofsubmissions", "workshop", count($num))."<br />\n";
1648 echo get_string("numberofassessmentsweighted", "workshop", $total)."</p>\n";
1651 // now get an estimate of the standard deviation of each element in the assessment
1652 // this is just a rough measure, all assessments are included and teacher's assesments are not weighted
1653 $n = 0;
1654 for ($i = 0; $i < $workshop->nelements; $i++) {
1655 $var[$i] = 0;
1657 foreach ($submissions as $submission) {
1658 if ($assessments = workshop_get_assessments($submission)) {
1659 foreach ($assessments as $assessment) {
1660 $n++;
1661 for ($i = 0; $i < $workshop->nelements; $i++) {
1662 $grade = get_field("workshop_grades", "grade",
1663 "assessmentid", $assessment->id, "elementno", $i);
1664 $temp = $mean[$submission->id][$i] - $grade;
1665 $var[$i] += $temp * $temp;
1670 for ($i = 0; $i < $workshop->nelements; $i++) {
1671 if ($n > 1) {
1672 $sd[$i] = sqrt($var[$i] / ($n - 1));
1673 } else {
1674 $sd[$i] = 0;
1676 set_field("workshop_elements", "stddev", $sd[$i], "workshopid", $workshop->id, "elementno", $i);
1677 set_field("workshop_elements", "totalassessments", $totalassessments, "workshopid", $workshop->id,
1678 "elementno", $i);
1679 if ($verbose) {
1680 echo get_string("standarddeviationofelement", "workshop", $i+1)." $sd[$i]<br />";
1681 if ($sd[$i] <= $minvar) {
1682 print_string("standarddeviationnote", "workshop")."<br />\n";
1689 // this section looks at each submission if the number of assessments made has increased it recalculates the
1690 // grading grades for those assessments
1691 // first get the assignment elements for the weights and the stddevs...
1692 if ($elementsraw = get_records("workshop_elements", "workshopid", $workshop->id, "elementno ASC")) {
1693 foreach ($elementsraw as $element) {
1694 $weight[] = $element->weight; // to renumber index 0,1,2...
1695 $sd[] = $element->stddev; // to renumber index 0,1,2...
1699 unset($num); // may have been used in calculating stddevs
1700 unset($sum); // ditto
1701 if ($submissions = get_records("workshop_submissions", "workshopid", $workshop->id)) {
1702 foreach ($submissions as $submission) {
1703 // see if the number of assessments has changed
1704 $nassessments = workshop_count_assessments($submission);
1705 if ($submission->nassessments <> $nassessments) {
1706 // ...if there are three or more assessments calculate the variance of each assessment.
1707 // Use the variance to find the "best" assessment. (When there is only one or two assessments they
1708 // are not altered by this routine.)
1709 if ($verbose) {
1710 echo "Processing submission $submission->id ($nassessments asessments)...\n";
1712 if ($nassessments > 2) {
1713 $num = 0; // weighted number of assessments
1714 for ($i = 0; $i < $workshop->nelements; $i++) {
1715 $sum[$i] = 0; // weighted sum of grades
1717 if ($assessments = workshop_get_assessments($submission)) {
1718 // first calculate the mean grades for each element
1719 foreach ($assessments as $assessment) {
1720 // test if assessment is "good", a teacher assessment always "good", but may be weighted out
1721 if (workshop_is_teacher($workshop, $assessment->userid)) {
1722 if (!$workshop->teacherweight) {
1723 // drop teacher's assessment as weight is zero
1724 continue;
1726 } else if ((!$assessment->gradinggrade and $assessment->timegraded) or
1727 ($workshop->agreeassessments and !$assessment->timeagreed)) {
1728 // it's a duff assessment, or it's not been agreed
1729 continue;
1731 if (workshop_is_teacher($workshop, $assessment->userid)) {
1732 $num += $workshop->teacherweight; // weight teacher's assessment
1733 } else {
1734 $num++; // student assessment just add one
1736 for ($i = 0; $i < $workshop->nelements; $i++) {
1737 $grade = get_field("workshop_grades", "grade",
1738 "assessmentid", $assessment->id, "elementno", $i);
1739 if (workshop_is_teacher($workshop, $assessment->userid)) {
1740 $sum[$i] += $workshop->teacherweight * $grade; // teacher's grade
1741 } else {
1742 $sum[$i] += $grade; // student's grade
1746 if ($num) { // could all the assessments be duff?
1747 for ($i = 0; $i < $workshop->nelements; $i++) {
1748 $mean[$i] = $sum[$i] / $num;
1749 if ($verbose) echo "Submission: $submission->id; Element: $i; Mean: {$mean[$i]}\n";
1751 } else {
1752 continue; // move to the next submission
1754 // run through the assessments again to see which is the "best" one (the one
1755 // closest to the mean)
1756 $lowest = 10e9;
1757 foreach ($assessments as $assessment) {
1758 if ($workshop->agreeassessments and !$assessment->timeagreed) {
1759 // ignore assessments that have not been agreed
1760 continue;
1762 $var = 0;
1763 for ($i = 0; $i < $workshop->nelements; $i++) {
1764 $grade = get_field("workshop_grades", "grade",
1765 "assessmentid", $assessment->id, "elementno", $i);
1766 if ($sd[$i] > $minvar) {
1767 $temp = ($mean[$i] - $grade) *
1768 $WORKSHOP_EWEIGHTS[$weight[$i]] / $sd[$i];
1769 } else {
1770 $temp = 0;
1772 $var += $temp * $temp;
1774 // find the "best" assessment of this submission
1775 if ($lowest > $var) {
1776 $lowest = $var;
1777 $bestassessmentid = $assessment->id;
1781 if (!$best = get_record("workshop_assessments", "id", $bestassessmentid)) {
1782 notify("Workshop grade assessments: cannot find best assessment");
1783 continue;
1785 if ($verbose) {
1786 echo "Best assessment is $bestassessmentid;\n";
1788 foreach ($assessments as $assessment) {
1789 // don't overwrite teacher's grade
1790 if ($assessment->teachergraded) {
1791 continue;
1793 if ($assessment->id == $bestassessmentid) {
1794 // it's the best one, set the grading grade to the maximum
1795 set_field("workshop_assessments", "gradinggrade", 100, "id", $assessment->id);
1796 set_field("workshop_assessments", "timegraded", $timenow, "id", $assessment->id);
1797 } else {
1798 // it's one of the pack, compare with the best...
1799 $gradinggrade = workshop_compare_assessments($workshop, $best, $assessment);
1800 // ...and save the grade for the assessment
1801 set_field("workshop_assessments", "gradinggrade", $gradinggrade, "id", $assessment->id);
1802 set_field("workshop_assessments", "timegraded", $timenow, "id", $assessment->id);
1806 } else {
1807 // there are less than 3 assessments for this submission
1808 if ($assessments = workshop_get_assessments($submission)) {
1809 foreach ($assessments as $assessment) {
1810 if (!$assessment->timegraded and !$assessment->teachergraded) {
1811 // set the grading grade to the maximum and say it's been graded
1812 set_field("workshop_assessments", "gradinggrade", 100, "id", $assessment->id);
1813 set_field("workshop_assessments", "timegraded", $timenow, "id", $assessment->id);
1818 // set the number of assessments for this submission
1819 set_field("workshop_submissions", "nassessments", $nassessments, "id", $submission->id);
1823 return;
1827 //////////////////////////////////////////////////////////////////////////////////////
1828 function workshop_gradinggrade($workshop, $student) {
1829 // returns the current (external) grading grade of the based on their (cold) assessments
1830 // (needed as it's called by grade)
1832 $gradinggrade = 0;
1833 if ($assessments = workshop_get_user_assessments($workshop, $student)) {
1834 $n = 0;
1835 foreach ($assessments as $assessment) {
1836 $gradinggrade += $assessment->gradinggrade;
1837 $n++;
1839 if ($n < ($workshop->ntassessments + $workshop->nsassessments)) { // the minimum students should do
1840 $n = $workshop->ntassessments + $workshop->nsassessments;
1842 $gradinggrade = $gradinggrade / $n;
1844 return number_format($gradinggrade * $workshop->gradinggrade / 100, 1);
1848 //////////////////////////////////////////////////////////////////////////////////////
1849 function workshop_submission_grade($workshop, $submission) {
1850 // returns the current (external) grade of the submission based on the "good" (cold) assessments
1851 // (needed as it's called by grade)
1853 $grade = 0;
1854 if ($assessments = workshop_get_assessments($submission)) {
1855 $n = 0;
1856 foreach ($assessments as $assessment) {
1857 if ($workshop->agreeassessments and !$assessment->timeagreed) {
1858 // ignore assessments which have not been agreed
1859 continue;
1861 if ($assessment->gradinggrade or !$assessment->timegraded) {
1862 // a good assessment (or one that has not been graded yet)
1863 if (workshop_is_teacher($workshop, $assessment->userid)) {
1864 $timenow = time();
1865 if ($timenow > $workshop->releasegrades) {
1866 // teacher's grade is available
1867 $grade += $workshop->teacherweight * $assessment->grade;
1868 $n += $workshop->teacherweight;
1870 } else {
1871 $grade += $assessment->grade;
1872 $n++;
1876 if ($n) { // stop division by zero
1877 $grade = $grade / $n;
1880 return number_format($grade * $workshop->grade / 100, 1);
1884 /////////////////////////////////////////////////////////////////////////////
1885 function workshop_fullname($userid, $courseid) {
1886 global $CFG;
1887 if (!$user = get_record('user', 'id', $userid)) {
1888 return '';
1890 return '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$courseid.'">'.
1891 fullname($user).'</a>';
1894 function workshop_get_view_actions() {
1895 return array('view','view all');
1898 function workshop_get_post_actions() {
1899 return array('agree','assess','comment','grade','newattachment','removeattachments','resubmit','submit');