Updated the 19 build version to 20101023
[moodle.git] / mod / exercise / lib.php
blob21516728bd7cb4f8908369a24ae1178f78e4e6e4
1 <?php // $Id$
3 // exercise constants and standard moodle functions plus those functions called directly
4 // see locallib.php for other non-standard exercise functions
6 require_once($CFG->libdir.'/filelib.php');
7 require_once('locallib.php');
9 /*** Constants **********************************/
11 $EXERCISE_TYPE = array (0 => get_string("notgraded", "exercise"),
12 1 => get_string("accumulative", "exercise"),
13 2 => get_string("errorbanded", "exercise"),
14 3 => get_string("criterion", "exercise"),
15 4 => get_string("rubric", "exercise") );
17 $EXERCISE_SCALES = array(
18 0 => array( 'name' => get_string("scaleyes", "exercise"), 'type' => 'radio', 'size' => 2, 'start' => get_string("yes"), 'end' => get_string("no")),
19 1 => array( 'name' => get_string("scalepresent", "exercise"), 'type' => 'radio', 'size' => 2, 'start' => get_string("present", "exercise"), 'end' => get_string("absent", "exercise")),
20 2 => array( 'name' => get_string("scalecorrect", "exercise"), 'type' => 'radio', 'size' => 2, 'start' => get_string("correct", "exercise"), 'end' => get_string("incorrect", "exercise")),
21 3 => array( 'name' => get_string("scalegood3", "exercise"), 'type' => 'radio', 'size' => 3, 'start' => get_string("good", "exercise"), 'end' => get_string("poor", "exercise")),
22 4 => array( 'name' => get_string("scaleexcellent4", "exercise"), 'type' => 'radio', 'size' => 4, 'start' => get_string("excellent", "exercise"), 'end' => get_string("verypoor", "exercise")),
23 5 => array( 'name' => get_string("scaleexcellent5", "exercise"), 'type' => 'radio', 'size' => 5, 'start' => get_string("excellent", "exercise"), 'end' => get_string("verypoor", "exercise")),
24 6 => array( 'name' => get_string("scaleexcellent7", "exercise"), 'type' => 'radio', 'size' => 7, 'start' => get_string("excellent", "exercise"), 'end' => get_string("verypoor", "exercise")),
25 7 => array( 'name' => get_string("scale10", "exercise"), 'type' => 'selection', 'size' => 10),
26 8 => array( 'name' => get_string("scale20", "exercise"), 'type' => 'selection', 'size' => 20),
27 9 => array( 'name' => get_string("scale100", "exercise"), 'type' => 'selection', 'size' => 100));
29 $EXERCISE_EWEIGHTS = array( 0 => -4.0, 1 => -2.0, 2 => -1.5, 3 => -1.0, 4 => -0.75, 5 => -0.5, 6 => -0.25,
30 7 => 0.0, 8 => 0.25, 9 => 0.5, 10 => 0.75, 11=> 1.0, 12 => 1.5, 13=> 2.0, 14 => 4.0);
32 $EXERCISE_ASSESSMENT_COMPS = array (
33 0 => array('name' => get_string("verylax", "exercise"), 'value' => 1),
34 1 => array('name' => get_string("lax", "exercise"), 'value' => 0.6),
35 2 => array('name' => get_string("fair", "exercise"), 'value' => 0.4),
36 3 => array('name' => get_string("strict", "exercise"), 'value' => 0.33),
37 4 => array('name' => get_string("verystrict", "exercise"), 'value' => 0.2) );
39 /*** Standard Moodle functions ******************
40 function exercise_add_instance($exercise)
41 function exercise_choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
42 function exercise_cron ()
43 function exercise_delete_instance($id)
44 function exercise_grades($exerciseid)
45 function exercise_print_recent_activity(&$logs, $isteacher=false)
46 function exercise_update_instance($exercise)
47 function exercise_user_outline($course, $user, $mod, $exercise)
48 function exercise_user_complete($course, $user, $mod, $exercise)
49 **********************************************/
51 /*******************************************************************/
52 function exercise_add_instance($exercise) {
53 // Given an object containing all the necessary data,
54 // (defined by the form in mod.html) this function
55 // will create a new instance and return the id number
56 // of the new instance.
58 $exercise->timemodified = time();
60 // encode password if necessary
61 if (!empty($exercise->password)) {
62 $exercise->password = md5($exercise->password);
63 } else {
64 unset($exercise->password);
67 if ($returnid = insert_record("exercise", $exercise)) {
69 $event = NULL;
70 $event->name = $exercise->name;
71 $event->description = $exercise->description;
72 $event->courseid = $exercise->course;
73 $event->groupid = 0;
74 $event->userid = 0;
75 $event->modulename = 'exercise';
76 $event->instance = $returnid;
77 $event->eventtype = 'deadline';
78 $event->timestart = $exercise->deadline;
79 $event->timeduration = 0;
81 add_event($event);
85 return $returnid;
89 /*******************************************************************/
90 function exercise_choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
91 /// Given an array of value, creates a popup menu to be part of a form
92 /// $options["value"]["label"]
94 if ($nothing == "choose") {
95 $nothing = get_string("choose")."...";
98 if ($script) {
99 $javascript = "onChange=\"$script\"";
100 } else {
101 $javascript = "";
104 $output = "<select name=\"$name\" $javascript>\n";
105 if ($nothing) {
106 $output .= " <option value=\"$nothingvalue\"\n";
107 if ($nothingvalue == $selected) {
108 $output .= " selected=\"selected\"";
110 $output .= ">$nothing</option>\n";
112 if (!empty($options)) {
113 foreach ($options as $value => $label) {
114 $output .= " <option value=\"$value\"";
115 if ($value == $selected) {
116 $output .= " selected=\"selected\"";
118 // stop zero label being replaced by array index value
119 // if ($label) {
120 // $output .= ">$label</option>\n";
121 // } else {
122 // $output .= ">$value</option>\n";
123 // }
124 $output .= ">$label</option>\n";
128 $output .= "</select>\n";
130 if ($return) {
131 return $output;
132 } else {
133 echo $output;
138 /*******************************************************************/
139 function exercise_cron() {
140 // Function to be run periodically according to the moodle cron
141 // Finds all exercise notifications that have yet to be mailed out, and mails them
143 global $CFG, $USER;
145 $cutofftime = time() - $CFG->maxeditingtime;
147 // look for new assessments
148 if ($assessments = exercise_get_unmailed_assessments($cutofftime)) {
149 $timenow = time();
151 foreach ($assessments as $assessment) {
152 echo "Processing exercise assessment $assessment->id\n";
153 // switch on mailed
154 if (! set_field("exercise_assessments", "mailed", "1", "id", "$assessment->id")) {
155 echo "Could not update the mailed field for id $assessment->id\n";
157 if (! $submission = get_record("exercise_submissions", "id", "$assessment->submissionid")) {
158 echo "Could not find submission $assessment->submissionid\n";
159 continue;
161 if (! $exercise = get_record("exercise", "id", $submission->exerciseid)) {
162 echo "Could not find exercise record for id $submission->exerciseid\n";
163 continue;
165 if (! $course = get_record("course", "id", "$exercise->course")) {
166 echo "Could not find course $exercise->course\n";
167 continue;
169 if (! $cm = get_coursemodule_from_instance("exercise", $exercise->id, $course->id)) {
170 error("Course Module ID was incorrect");
171 continue;
173 if (! $submissionowner = get_record("user", "id", "$submission->userid")) {
174 echo "Could not find user $submission->userid\n";
175 continue;
177 if (! $assessmentowner = get_record("user", "id", "$assessment->userid")) {
178 echo "Could not find user $assessment->userid\n";
179 continue;
181 if (! isstudent($course->id, $submissionowner->id) and !isteacher($course->id, $submissionowner->id)) {
182 continue; // Not an active participant
184 if (! isstudent($course->id, $assessmentowner->id) and !isteacher($course->id, $assessmentowner->id)) {
185 continue; // Not an active participant
188 // if the submission belongs to a teacher it's a student assessment. No need to email anything.
189 if (isteacher($course->id, $submissionowner->id)) {
190 continue;
193 $strexercises = get_string("modulenameplural", "exercise");
194 $strexercise = get_string("modulename", "exercise");
196 // it's an assessment, tell the submission owner
197 $USER->lang = $submissionowner->lang;
198 $sendto = $submissionowner;
199 // "Your assignment \"$submission->title\" has been assessed by"
200 $msg = get_string("mail1", "exercise", $submission->title).' '.fullname($assessmentowner).".\n";
201 // "The comments and grade can be seen in the exercise assignment '$exercise->name'
202 $msg .= get_string("mail2", "exercise", format_string($exercise->name,true))."\n\n";
204 $postsubject = "$course->shortname: $strexercises: ".format_string($exercise->name,true);
205 $posttext = "$course->shortname -> $strexercises -> ".format_string($exercise->name,true)."\n";
206 $posttext .= "---------------------------------------------------------------------\n";
207 $posttext .= $msg;
208 // "You can see it in your exercise assignment"
209 $posttext .= get_string("mail3", "exercise").":\n";
210 $posttext .= " $CFG->wwwroot/mod/exercise/view.php?id=$cm->id\n";
211 $posttext .= "---------------------------------------------------------------------\n";
212 if ($sendto->mailformat == 1) { // HTML
213 $posthtml = "<p><font face=\"sans-serif\">".
214 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
215 "<a href=\"$CFG->wwwroot/mod/exercise/index.php?id=$course->id\">$strexercises</a> ->".
216 "<a href=\"$CFG->wwwroot/mod/exercise/view.php?id=$cm->id\">".format_string($exercise->name,true)."</a></font></p>";
217 $posthtml .= "<hr /><font face=\"sans-serif\">";
218 $posthtml .= "<p>$msg</p>";
219 $posthtml .= "<p>".get_string("mail3", "exercise").
220 " <a href=\"$CFG->wwwroot/mod/exercise/view.php?id=$cm->id\">".format_string($exercise->name,true)."</a>.</p></font><hr />";
221 } else {
222 $posthtml = "";
225 if (!$teacher = get_teacher($course->id)) {
226 echo "Error: can not find teacher for course $course->id!\n";
229 if (! email_to_user($sendto, $teacher, $postsubject, $posttext, $posthtml)) {
230 echo "Error: exercise cron: Could not send out mail for id $submission->id to user $sendto->id ($sendto->email)\n";
234 return true;
238 /*******************************************************************/
239 function exercise_delete_instance($id) {
240 // Given an ID of an instance of this module,
241 // this function will permanently delete the instance
242 // and any data that depends on it.
244 if (! $exercise = get_record("exercise", "id", "$id")) {
245 return false;
248 // delete all the associated records in the exercise tables, start positive...
249 $result = true;
251 if (! delete_records("exercise_grades", "exerciseid", "$exercise->id")) {
252 $result = false;
255 if (! delete_records("exercise_rubrics", "exerciseid", "$exercise->id")) {
256 $result = false;
259 if (! delete_records("exercise_elements", "exerciseid", "$exercise->id")) {
260 $result = false;
263 if (! delete_records("exercise_assessments", "exerciseid", "$exercise->id")) {
264 $result = false;
267 if (! delete_records("exercise_submissions", "exerciseid", "$exercise->id")) {
268 $result = false;
271 if (! delete_records("exercise", "id", "$exercise->id")) {
272 $result = false;
275 return $result;
279 /*******************************************************************/
280 function exercise_grades($exerciseid) {
281 /// Must return an array of grades, indexed by user, and a max grade.
282 if (!$exercise = get_record("exercise", "id", $exerciseid)) {
283 error("Exercise record not found");
286 if (! $course = get_record("course", "id", $exercise->course)) {
287 error("Course is misconfigured");
290 if (!$return->maxgrade = ($exercise->grade + $exercise->gradinggrade)) {
291 return NULL;
293 // how to handle multiple submissions?
294 if ($exercise->usemaximum) {
295 if ($bestgrades = exercise_get_best_submission_grades($exercise)) {
296 foreach ($bestgrades as $grade) {
297 $usergrade[$grade->userid] = $grade->grade * $exercise->grade / 100.0;
302 else { // use mean values
303 if ($meangrades = exercise_get_mean_submission_grades($exercise)) {
304 foreach ($meangrades as $grade) {
305 $usergrade[$grade->userid] = $grade->grade * $exercise->grade / 100.0;
309 // now get the users grading grades
310 if ($assessments = exercise_get_teacher_submission_assessments($exercise)) {
311 foreach ($assessments as $assessment) {
312 // add the grading grade if the student's work has been assessed
313 if (isset($usergrade[$assessment->userid])) {
314 $usergrade[$assessment->userid] += $assessment->gradinggrade * $exercise->gradinggrade / 100.0;
318 // tidy the numbers and set up the return array
319 if (isset($usergrade)) {
320 foreach ($usergrade as $userid => $g) {
321 $return->grades[$userid] = number_format($g, 1);
324 return $return;
328 /*******************************************************************/
329 function exercise_print_recent_activity($course, $viewfullanmes, $timestart) {
330 global $CFG;
332 $isteacher = has_capability('mod/exercise:assess', get_context_instance(CONTEXT_COURSE, $course->id));
334 $modinfo = get_fast_modinfo($course);
336 // have a look for new submissions (only show to teachers)
337 $submitcontent = false;
338 if ($isteacher) {
339 if ($logs = exercise_get_submit_logs($course, $timestart)) {
340 // if we got some 'live' ones then output them
341 $submitcontent = true;
342 print_headline(get_string('exercisesubmissions', 'exercise').':');
343 foreach ($logs as $log) {
344 print_recent_activity_note($log->time, $log, $log->name,
345 $CFG->wwwroot.'/mod/exercise/'.str_replace('&', '&amp;', $log->url));
350 // have a look for new assessment gradings for this user
351 $gradecontent = false;
352 if ($logs = exercise_get_grade_logs($course, $timestart)) {
353 // got some, see if any belong to a visible module
354 foreach ($logs as $id=>$log) {
355 $cm = $modinfo->instances['exercise'][$log->exerciseid];
356 if (!$cm->uservisible) {
357 unset($logs[$id]);
358 continue;
361 // if we got some "live" ones then output them
362 if ($logs) {
363 $gradecontent = true;
364 print_headline(get_string('exercisefeedback', 'exercise').':');
365 foreach ($logs as $log) {
366 print_recent_activity_note($log->time, $log, $log->name,
367 $CFG->wwwroot.'/mod/exercise/'.str_replace('&', '&amp;', $log->url));
372 // have a look for new assessments for this user
373 $assesscontent = false;
374 if (!$isteacher) { // teachers only need to see submissions
375 if ($logs = exercise_get_assess_logs($course, $timestart)) {
376 // got some, see if any belong to a visible module
377 foreach ($logs as $id=>$log) {
378 $cm = $modinfo->instances['exercise'][$log->exerciseid];
379 if (!$cm->uservisible) {
380 unset($logs[$id]);
381 continue;
384 // if we got some "live" ones then output them
385 if ($logs) {
386 $assesscontent = true;
387 print_headline(get_string('exerciseassessments', 'exercise').':');
388 foreach ($logs as $log) {
389 print_recent_activity_note($log->time, $log, $log->name,
390 $CFG->wwwroot.'/mod/exercise/'.str_replace('&', '&amp;', $log->url));
395 return $submitcontent or $gradecontent or $assesscontent;
399 ///////////////////////////////////////////////////////////////////////////////
400 function exercise_refresh_events($courseid = 0) {
401 // This standard function will check all instances of this module
402 // and make sure there are up-to-date events created for each of them.
403 // If courseid = 0, then every exercise event in the site is checked, else
404 // only exercise events belonging to the course specified are checked.
405 // This function is used, in its new format, by restore_refresh_events()
407 if ($courseid == 0) {
408 if (! $exercises = get_records("exercise")) {
409 return true;
411 } else {
412 if (! $exercises = get_records("exercise", "course", $courseid)) {
413 return true;
416 $moduleid = get_field('modules', 'id', 'name', 'exercise');
418 foreach ($exercises as $exercise) {
419 $event = NULL;
420 $event->name = addslashes($exercise->name);
421 $event->description = addslashes($exercise->description);
422 $event->timestart = $exercise->deadline;
424 if ($event->id = get_field('event', 'id', 'modulename', 'exercise', 'instance', $exercise->id)) {
425 update_event($event);
427 } else {
428 $event->courseid = $exercise->course;
429 $event->groupid = 0;
430 $event->userid = 0;
431 $event->modulename = 'exercise';
432 $event->instance = $exercise->id;
433 $event->eventtype = 'deadline';
434 $event->timeduration = 0;
435 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $exercise->id);
436 add_event($event);
440 return true;
444 /*******************************************************************/
445 function exercise_update_instance($exercise) {
446 // Given an object containing all the necessary data,
447 // (defined by the form in mod.html) this function
448 // will update an existing instance with new data.
450 $exercise->timemodified = time();
452 // encode password if necessary
453 if (!empty($exercise->password)) {
454 $exercise->password = md5($exercise->password);
455 } else {
456 unset($exercise->password);
459 $exercise->id = $exercise->instance;
461 if ($returnid = update_record("exercise", $exercise)) {
463 $event = NULL;
465 if ($event->id = get_field('event', 'id', 'modulename', 'exercise', 'instance', $exercise->id)) {
467 $event->name = $exercise->name;
468 $event->description = $exercise->description;
469 $event->timestart = $exercise->deadline;
471 update_event($event);
475 return $returnid;
479 /*******************************************************************/
480 function exercise_user_complete($course, $user, $mod, $exercise) {
481 if ($submissions = exercise_get_user_submissions($exercise, $user)) {
482 print_simple_box_start();
483 $table->head = array (get_string("submission", "exercise"), get_string("submitted", "exercise"),
484 get_string("assessed", "exercise"), get_string("grade"));
485 $table->width = "100%";
486 $table->align = array ("left", "left", "left", "center");
487 $table->size = array ("*", "*", "*", "*");
488 $table->cellpadding = 2;
489 $table->cellspacing = 0;
491 foreach ($submissions as $submission) {
492 if ($assessments = exercise_get_assessments($submission)) {
493 // should only be one but we'll loop anyway
494 foreach ($assessments as $assessment) {
495 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
496 userdate($submission->timecreated), userdate($assessment->timecreated),
497 $assessment->grade * $exercise->grade / 100.0);
499 } else {
500 // submission not yet assessed (by teacher)
501 $table->data[] = array(exercise_print_submission_title($exercise, $submission),
502 userdate($submission->timecreated), get_string("notassessedyet", "exercise"), 0);
505 print_table($table);
506 print_simple_box_end();
507 } else {
508 print_string("nosubmissions", "exercise");
513 /*******************************************************************/
514 function exercise_user_outline($course, $user, $mod, $exercise) {
515 if ($submissions = exercise_get_user_submissions($exercise, $user)) {
516 $result->info = count($submissions)." ".get_string("submissions", "exercise");
517 foreach ($submissions as $submission) { // the first one is the most recent one
518 $result->time = $submission->timecreated;
519 break;
521 return $result;
523 return NULL;
527 /*******************************************************************/
528 function exercise_get_participants($exerciseid) {
529 //Returns the users with data in one exercise
530 //(users with records in exercise_submissions and exercise_assessments, students)
532 global $CFG;
534 //Get students from exercise_submissions
535 $st_submissions = get_records_sql("SELECT DISTINCT u.id, u.id
536 FROM {$CFG->prefix}user u,
537 {$CFG->prefix}exercise_submissions s
538 WHERE s.exerciseid = '$exerciseid' and
539 u.id = s.userid");
540 //Get students from exercise_assessments
541 $st_assessments = get_records_sql("SELECT DISTINCT u.id, u.id
542 FROM {$CFG->prefix}user u,
543 {$CFG->prefix}exercise_assessments a
544 WHERE a.exerciseid = '$exerciseid' and
545 u.id = a.userid");
547 //Add st_assessments to st_submissions
548 if ($st_assessments) {
549 foreach ($st_assessments as $st_assessment) {
550 $st_submissions[$st_assessment->id] = $st_assessment;
553 //Return st_submissions array (it contains an array of unique users)
554 return ($st_submissions);
558 ///////////////////////////////////////////////////////////////////////////////////////////////
559 // Non-standard Exercise functions
560 ///////////////////////////////////////////////////////////////////////////////////////////////
561 function exercise_file_area($exercise, $submission) {
562 return make_upload_directory( exercise_file_area_name($exercise, $submission) );
566 ///////////////////////////////////////////////////////////////////////////////////////////////
567 function exercise_file_area_name($exercise, $submission) {
568 // Creates a directory file name, suitable for make_upload_directory()
569 global $CFG;
571 return "$exercise->course/$CFG->moddata/exercise/$submission->id";
575 ///////////////////////////////////////////////////////////////////////////////////////////////
576 function exercise_get_assess_logs($course, $timestart) {
577 // get the "assess" entries for this user and add the first and last names...
578 global $CFG, $USER;
579 if (empty($USER->id)) {
580 return false;
582 $timethen = time() - $CFG->maxeditingtime;
583 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.exerciseid, e.name
584 FROM {$CFG->prefix}log l,
585 {$CFG->prefix}exercise e,
586 {$CFG->prefix}exercise_submissions s,
587 {$CFG->prefix}exercise_assessments a,
588 {$CFG->prefix}user u
589 WHERE l.time > $timestart AND l.time < $timethen
590 AND l.course = $course->id AND l.module = 'exercise' AND l.action = 'assess'
591 AND a.id = l.info AND s.id = a.submissionid AND s.userid = $USER->id
592 AND u.id = a.userid AND e.id = a.exerciseid");
596 ///////////////////////////////////////////////////////////////////////////////////////////////
597 function exercise_get_assessments($submission) {
598 // Return all assessments for this submission provided they are after the editing time,
599 // ordered oldest first, newest last
600 global $CFG;
602 $timenow = time();
603 return get_records_select("exercise_assessments", "(submissionid = $submission->id) AND
604 (timecreated < $timenow - $CFG->maxeditingtime)", "timecreated ASC");
608 ///////////////////////////////////////////////////////////////////////////////////////////////
609 function exercise_get_best_grade($submission) {
610 // Returns the best grade of students' submission (there may, occassionally be more than one assessment)
611 global $CFG;
613 return get_record_sql("SELECT MAX(a.grade) AS grade FROM
614 {$CFG->prefix}exercise_assessments a
615 WHERE a.submissionid = $submission->id
616 GROUP BY a.submissionid");
620 ///////////////////////////////////////////////////////////////////////////////////////////////
621 function exercise_get_grade_logs($course, $timestart) {
622 // get the "grade" entries for this user and add the first and last names...
623 global $CFG, $USER;
624 if (empty($USER->id)) {
625 return false;
628 $timethen = time() - $CFG->maxeditingtime;
629 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, a.exerciseid, e.name
630 FROM {$CFG->prefix}log l,
631 {$CFG->prefix}exercise e,
632 {$CFG->prefix}exercise_submissions s,
633 {$CFG->prefix}exercise_assessments a,
634 {$CFG->prefix}user u
635 WHERE l.time > $timestart AND l.time < $timethen
636 AND l.course = $course->id AND l.module = 'exercise' AND l.action = 'grade'
637 AND a.id = l.info AND s.id = a.submissionid AND a.userid = $USER->id
638 AND u.id = s.userid AND e.id = a.exerciseid");
642 ///////////////////////////////////////////////////////////////////////////////////////////////
643 function exercise_get_mean_submission_grades($exercise) {
644 // Returns the mean grades of students' submissions
645 // ignores hot assessments
646 global $CFG;
648 $timenow = time();
649 $grades = get_records_sql("SELECT DISTINCT u.userid, AVG(a.grade) AS grade FROM
650 {$CFG->prefix}exercise_submissions s,
651 {$CFG->prefix}exercise_assessments a
652 WHERE
653 s.exerciseid = $exercise->id
654 AND s.late = 0
655 AND a.submissionid = s.id
656 AND a.timecreated < $timenow
657 GROUP BY s.userid");
658 return $grades;
662 ///////////////////////////////////////////////////////////////////////////////////////////////
663 function exercise_get_submit_logs($course, $timestart) {
664 // get the "submit" entries and add the first and last names...
665 global $CFG, $USER;
667 $timethen = time() - $CFG->maxeditingtime;
668 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, l.info as exerciseid, e.name
669 FROM {$CFG->prefix}log l,
670 {$CFG->prefix}exercise e,
671 {$CFG->prefix}user u
672 WHERE l.time > $timestart AND l.time < $timethen
673 AND l.course = $course->id AND l.module = 'exercise'
674 AND l.action = 'submit'
675 AND e.id = l.info
676 AND u.id = l.userid");
680 ///////////////////////////////////////////////////////////////////////////////////////////////
681 function exercise_get_teacher_submission_assessments($exercise) {
682 // Return all assessments on the teacher submissions, order by youngest first, oldest last
683 global $CFG;
685 return get_records_sql("SELECT a.* FROM {$CFG->prefix}exercise_submissions s, {$CFG->prefix}exercise_assessments a
686 WHERE s.isexercise = 1
687 AND s.exerciseid = $exercise->id
688 AND a.submissionid = s.id
689 ORDER BY a.timecreated DESC");
693 ///////////////////////////////////////////////////////////////////////////////////////////////
694 function exercise_get_unmailed_assessments($cutofftime) {
695 /// Return list of (ungraded) assessments that have not been mailed out
696 global $CFG;
697 return get_records_sql("SELECT a.*, e.course, e.name
698 FROM {$CFG->prefix}exercise_assessments a, {$CFG->prefix}exercise e
699 WHERE a.mailed = 0
700 AND a.timecreated < $cutofftime
701 AND e.id = a.exerciseid");
705 ///////////////////////////////////////////////////////////////////////////////////////////////
706 function exercise_get_user_submissions($exercise, $user) {
707 // return submission of user oldest first, newest last
708 // teachers submit "exercises"
709 if (! $course = get_record("course", "id", $exercise->course)) {
710 error("Course is misconfigured");
712 if (isteacher($course->id, $user->id)) {
713 return get_records_select("exercise_submissions",
714 "exerciseid = $exercise->id AND isexercise = 1", "timecreated" );
716 return get_records_select("exercise_submissions",
717 "exerciseid = $exercise->id AND userid = $user->id", "timecreated" );
721 ///////////////////////////////////////////////////////////////////////////////////////////////
722 function exercise_print_submission_title($exercise, $submission) {
723 global $CFG;
725 if (!$submission->timecreated) { // a "no submission"
726 return $submission->title;
729 $filearea = exercise_file_area_name($exercise, $submission);
730 if ($basedir = exercise_file_area($exercise, $submission)) {
731 if (list($file) = get_directory_list($basedir)) {
732 $icon = mimeinfo("icon", $file);
733 require_once($CFG->libdir .'/filelib.php');
734 $ffurl = get_file_url("$filearea/$file");
735 return "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"".get_string('file')."\" />".
736 "&nbsp;<a target=\"uploadedfile\" href=\"$ffurl\">$submission->title</a>";
741 function exercise_get_view_actions() {
742 return array('view','view all','display');
745 function exercise_get_post_actions() {
746 return array('assess','delete','grade','open','set up','submit');
750 * Returns all other caps used in module
752 function exercise_get_extra_capabilities() {
753 return array('moodle/site:accessallgroups');