More changes to the user interface. See http://moodle.org/mod/forum/view.php?id=740.
[moodle.git] / mod / workshop / view.php
1 <?php  // $Id: view.php, v1.1 23 Aug 2003
3 /*************************************************
4     ACTIONS handled are:
6     displayfinalgrade (for students)
7     notavailable (for students)
8     studentsview
9     submitexample 
10     teachersview
11     showdescription
12     showallsubmissions
13     
14 ************************************************/
16     require("../../config.php");
17     require("lib.php");
18     require("locallib.php");
19     
20     require_variable($id);    // Course Module ID
21     optional_variable($action, '');
22     optional_variable($sort, "lastname");
23     optional_variable($dir, "ASC");
25     $timenow = time();
27     // get some useful stuff...
28     if (! $cm = get_record("course_modules", "id", $id)) {
29         error("Course Module ID was incorrect");
30     }
31     if (! $course = get_record("course", "id", $cm->course)) {
32         error("Course is misconfigured");
33     }
34     if (! $workshop = get_record("workshop", "id", $cm->instance)) {
35         error("Course module is incorrect");
36     }
38     require_login($course->id);
40     // ...log activity...
41     add_to_log($course->id, "workshop", "view", "view.php?id=$cm->id", $workshop->id, $cm->id);
43     $strworkshops = get_string("modulenameplural", "workshop");
44     $strworkshop  = get_string("modulename", "workshop");
45     $straction = ($action) ? '-> '.get_string($action, 'workshop') : '';
47     // ...display header...
48     print_header_simple("$workshop->name", "",
49                  "<a href=\"index.php?id=$course->id\">$strworkshops</a> -> 
50                   <a href=\"view.php?id=$cm->id\">$workshop->name</a> $straction", 
51                   "", "", true, update_module_button($cm->id, $course->id, $strworkshop), navmenu($course, $cm));
53     // ...and if necessary set default action 
54     
55     if (isteacher($course->id)) {
56         if (empty($action)) { // no action specified, either go straight to elements page else the admin page
57             // has the assignment any elements
58             if (count_records("workshop_elements", "workshopid", $workshop->id) >= $workshop->nelements) {
59                 $action = "teachersview";
60             }
61             else {
62                 redirect("assessments.php?action=editelements&id=$cm->id");
63             }
64         }
65     }
66     elseif (!isguest()) { // it's a student then
67         if (!$cm->visible) {
68             notice(get_string("activityiscurrentlyhidden"));
69         }
70         if ($timenow < $workshop->submissionstart) { 
71             $action = 'notavailable'; 
72         } else if (!$action) {
73             if ($timenow < $workshop->assessmentend) {
74                 $action = 'studentsview';
75             } else {
76                 $action = 'displayfinalgrade';
77             }
78         }
79     }
80     else { // it's a guest, oh no!
81         $action = 'notavailable';
82     }
85     /****************** display final grade (for students) ************************************/
86     if ($action == 'displayfinalgrade' ) {
88         print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
89                 get_string("showdescription", 'workshop')."</a></b>");
90         // show the final grades as stored in the tables...
91         if ($submissions = workshop_get_user_submissions($workshop, $USER)) { // any submissions from user?
92             print_heading(get_string("displayoffinalgrades", "workshop"));
93             echo "<center><table border=\"1\" width=\"90%\"><tr>";
94             echo "<td><b>".get_string("submissions", "workshop")."</b></td>";
95             if ($workshop->wtype) {
96                 echo "<td align=\"center\"><b>".get_string("assessmentsdone", "workshop")."</b></td>";
97                 echo "<td align=\"center\"><b>".get_string("gradeforassessments", "workshop")."</b></td>";
98             }
99             echo "<td align=\"center\"><b>".get_string("teacherassessments", "workshop", 
100                         $course->teacher)."</b></td>";
101             if ($workshop->wtype) {
102                 echo "<td align=\"center\"><b>".get_string("studentassessments", "workshop", 
103                         $course->student)."</b></td>";
104             }
105             echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "workshop")."</b></td>";
106             echo "<td align=\"center\"><b>".get_string("overallgrade", "workshop")."</b></td></tr>\n";
107             foreach ($submissions as $submission) {
108                 $grade = workshop_submission_grade($workshop, $submission);
109                 echo "<tr><td>".workshop_print_submission_title($workshop, $submission)."</td>\n";
110                 if ($workshop->wtype) {
111                     echo "<td align=\"center\">".workshop_print_user_assessments($workshop, $USER, $gradinggrade)."</td>";
112                     echo "<td align=\"center\">$gradinggrade</td>";
113                 }
114                 echo "<td align=\"center\">".workshop_print_submission_assessments($workshop, 
115                             $submission, "teacher")."</td>";
116                 if ($workshop->wtype) {
117                     echo "<td align=\"center\">".workshop_print_submission_assessments($workshop, 
118                             $submission, "student")."</td>";
119                 }
120                 echo "<td align=\"center\">$grade</td>";
121                 echo "<td align=\"center\">".number_format($gradinggrade + $grade, 1)."</td></tr>\n";
122             }
123             echo "</table><br clear=\"all\" />\n";
124             workshop_print_key($workshop);
125         } else {
126             print_heading(get_string('nowork', 'workshop'));
127         }
128         if ($workshop->showleaguetable) {
129             workshop_print_league_table($workshop);
130         }
131     }   
133     
134     /****************** assignment not available (for students)***********************/
135     elseif ($action == 'notavailable') {
136         print_heading(get_string("notavailable", "workshop"));
137     }
140     /****************** student's view could be in 1 of 4 stages ***********************/
141     elseif ($action == 'studentsview') {
142         // is a password needed?
143         if ($workshop->usepassword) {
144             $correctpass = false;
145             if (isset($_POST['userpassword'])) {
146                 if ($workshop->password == md5(trim($_POST['userpassword']))) {
147                     $USER->workshoploggedin[$workshop->id] = true;
148                     $correctpass = true;
149                 }
150             } elseif (isset($USER->workshoploggedin[$workshop->id])) {
151                 $correctpass = true;
152             }
154             if (!$correctpass) {
155                 print_simple_box_start("center");
156                 echo "<form name=\"password\" method=\"post\" action=\"view.php\">\n";
157                 echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
158                 echo "<table cellpadding=\"7px\">";
159                 if (isset($_POST['userpassword'])) {
160                     echo "<tr align=\"center\" style='color:#DF041E;'><td>".get_string("wrongpassword", "workshop").
161                         "</td></tr>";
162                 }
163                 echo "<tr align=\"center\"><td>".get_string("passwordprotectedworkshop", "workshop", $workshop->name).
164                     "</td></tr>";
165                 echo "<tr align=\"center\"><td>".get_string("enterpassword", "workshop").
166                     " <input type=\"password\" name=\"userpassword\" /></td></tr>";
167                         
168                 echo "<tr align=\"center\"><td>";
169                 echo "<input type=\"button\" value=\"".get_string("cancel").
170                     "\" onclick=\"parent.location='../../course/view.php?id=$course->id';\">  ";
171                 echo "<input type=\"button\" value=\"".get_string("continue").
172                     "\" onclick=\"document.password.submit();\" />";
173                 echo "</td></tr></table>";
174                 print_simple_box_end();
175                 exit();
176             }
177         }
178         workshop_print_assignment_info($workshop);
179         
180         // if the student has not yet submitted show the full description
181         if (!record_exists('workshop_submissions', 'workshopid', $workshop->id, 'userid', $USER->id)) {
182             print_simple_box(format_text($workshop->description, $workshop->format), 'center');
183         } else {
184             print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
185                 get_string("showdescription", 'workshop')."</a></b>");
186         }
187         
188         // in Stage 1? - are there any teacher's submissions? and...
189         // ...has student assessed the required number of the teacher's submissions 
190         if ($workshop->ntassessments and (!workshop_test_user_assessments($workshop, $USER))) {
191             print_heading(get_string("pleaseassesstheseexamplesfromtheteacher", "workshop", 
192                         $course->teacher));
193             workshop_list_teacher_submissions($workshop, $USER);
194         }
195         // in stage 2? - submit own first attempt
196         else {
197             if ($workshop->ntassessments) { 
198                 // show assessment of the teacher's examples, there may be feedback from teacher
199                 print_heading(get_string("yourassessmentsofexamplesfromtheteacher", "workshop", 
200                             $course->teacher));
201                 workshop_list_teacher_submissions($workshop, $USER);
202             }
203             // has user submitted anything yet? 
204             if (!workshop_get_user_submissions($workshop, $USER)) {
205                 if ($timenow < $workshop->submissionend) {
206                     // print upload form
207                     print_heading(get_string("submitassignmentusingform", "workshop").":");
208                     workshop_print_upload_form($workshop);
209                 } else {
210                     print_heading(get_string("submissionsnolongerallowed", "workshop"));
211                 }
212             }   
213             // in stage 3? - grade other student's submissions, resubmit and list all submissions
214             else {
215                 // is self assessment used in this workshop?
216                 if ($workshop->includeself) {
217                     // prints a table if there are any submissions which have not been self assessed yet
218                     workshop_list_self_assessments($workshop, $USER);
219                 }
220                 // if peer assessments are being done then show some  to assess...
221                 if ($workshop->nsassessments and ($workshop->assessmentstart < $timenow and $workshop->assessmentend > $timenow)) {  
222                     workshop_list_student_submissions($workshop, $USER);
223                 }
224                 // ..and any they have already done (and have gone cold)...
225                 if (workshop_count_user_assessments($workshop, $USER, "student")) {
226                     print_heading(get_string("yourassessments", "workshop"));
227                     workshop_list_assessed_submissions($workshop, $USER);
228                 }
229                 // list any assessments by teachers
230                 if (workshop_count_teacher_assessments_by_user($workshop, $USER) and ($timenow > $workshop->releasegrades)) {
231                     print_heading(get_string("assessmentsby", "workshop", $course->teachers));
232                     workshop_list_teacher_assessments_by_user($workshop, $USER);
233                 }
234                 // ... and show peer assessments
235                 if (workshop_count_peer_assessments($workshop, $USER)) {
236                     print_heading(get_string("assessmentsby", "workshop", $course->students));
237                     workshop_list_peer_assessments($workshop, $USER);
238                 }
239                 // list previous submissions
240                 print_heading(get_string("yoursubmissions", "workshop"));
241                 workshop_list_user_submissions($workshop, $USER);
242                 
243                 // are resubmissions allowed and the workshop is in submission/assessment phase?
244                 if ($workshop->resubmit and ($timenow > $workshop->assessmentstart and $timenow < $workshop->submissionend)) {
245                     // see if there are any cold assessments of the last submission
246                     // if there are then print upload form
247                     if ($submissions = workshop_get_user_submissions($workshop, $USER)) {
248                         foreach ($submissions as $submission) {
249                             $lastsubmission = $submission;
250                             break;
251                         }
252                         $n = 0; // number of cold assessments (not include self assessments)
253                         if ($assessments = workshop_get_assessments($lastsubmission)) {
254                             foreach ($assessments as $assessment) {
255                                 if ($assessment->userid <> $USER->id) {
256                                     $n++;
257                                 }
258                             }
259                         }
260                         if ($n) {
261                             echo "<hr size=\"1\" noshade=\"noshade\" />";
262                             print_heading(get_string("submitrevisedassignment", "workshop").":");
263                             workshop_print_upload_form($workshop);
264                             echo "<hr size=\"1\" noshade=\"noshade\" />";
265                         }
266                     }
267                 }
268             }
269         }
270     }
273     /****************** submission of example by teacher only***********************/
274     elseif ($action == 'submitexample') {
275     
276         if (!isteacher($course->id)) {
277             error("Only teachers can look at this page");
278         }
279         
280         // list previous submissions from teacher 
281         workshop_list_user_submissions($workshop, $USER);
282     
283         echo "<hr size=\"1\" noshade=\"noshade\" />";
284     
285         // print upload form
286         print_heading(get_string("submitexampleassignment", "workshop").":");
287         workshop_print_upload_form($workshop);
288         
289         print_heading("<a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id\">".get_string("cancel")."</a>");
290     }
293     /****************** teacher's view - display admin page  ************/
294     elseif ($action == 'teachersview') {
296         if (!isteacher($course->id)) {
297             error("Only teachers can look at this page");
298         }
299         
300         // automatically grade assessments if workshop has examples and/or peer assessments
301         if ($workshop->gradingstrategy and ($workshop->ntassessments or $workshop->nsassessments)) {
302             workshop_grade_assessments($workshop);
303         }
305         /// Check to see if groups are being used in this workshop
306         /// and if so, set $currentgroup to reflect the current group
307         $changegroup = isset($_GET['group']) ? $_GET['group'] : -1;  // Group change requested?
308         $groupmode = groupmode($course, $cm);   // Groups are being used?
309         $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
310         
311         /// Print settings and things in a table across the top
312         echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top">';
313         
314         /// Allow the teacher to change groups (for this session)
315         if ($groupmode and isteacheredit($course->id)) {
316             if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
317                 echo '<td>';
318                 print_group_menu($groups, $groupmode, $currentgroup, "view.php?id=$cm->id");
319                 echo '</td>';
320             }
321         }
322         /// Print admin links
323         echo "<td align=\"right\"><b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
324             get_string("showdescription", 'workshop')."</a></b> \n";
325  
326         echo "<br /><b><a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
327             get_string("administration")."</a></b> \n";
328         
329         echo '</td></tr>';
330         
331         workshop_print_assignment_info($workshop);
332         
333         /// Print grade tables /////////////////////////////////////////////////
334         
335         // display the teacher's submissions
336         if ($workshop->ntassessments) {
337             $table->head = array(get_string("examplesubmissions", "workshop"),
338                 get_string("assessmentsby", "workshop", $course->teachers),
339                 get_string("assessmentsby", "workshop", $course->students));
340             $table->data = array();
341             if ($submissions = workshop_get_teacher_submissions($workshop)) {
342                 foreach ($submissions as $submission) {
343                     $teacherassessments = workshop_print_submission_assessments($workshop, $submission, "teacher");
344                     // If not yet assessed, show assess link
345                     if ($teacherassessments == '&nbsp;') {
346                         $teacherassessments = '<a href="assess.php?id='.
347                             $cm->id.'&sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
348                     }
349                     $title = workshop_print_submission_title($workshop, $submission).
350                         " <a href=\"submissions.php?action=editsubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
351                         "<img src=\"$CFG->pixpath/t/edit.gif\" ".
352                         'height="11" width="11" border="0" alt="'.get_string('edit').'"></a>'.
353                         " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
354                         "<img src=\"$CFG->pixpath/t/delete.gif\" ".
355                         'height="11" width="11" border="0" alt="'.get_string('delete', 'workshop').'"></a>';
356                     $table->data[] = array($title, $teacherassessments,
357                         workshop_print_submission_assessments($workshop, $submission, "student"));
358                 }
359             }
360             // Put in a submission link
361             $table->data[] = array("<b><a href=\"view.php?id=$cm->id&amp;action=submitexample\">".
362                 get_string("submitexampleassignment", "workshop")."</a></b>".
363                 helpbutton("submissionofexamples", get_string("submitexampleassignment", "workshop"), "workshop", true, false, '', true), 
364                 '&nbsp;', '&nbsp;');
365             
366             print_table($table);
367             workshop_print_key($workshop);
368         }
369     
370         // Get all the students
371         if (!$users = get_course_students($course->id, "u.lastname, u.firstname")) {
372             print_heading(get_string("nostudentsyet"));
373             print_footer($course);
374             exit;
375         }
376         
377         /// Now prepare table with student assessments and submissions
378         $tablesort->data = array();
379         $tablesort->sortdata = array();
380         foreach ($users as $user) {
381             // skip if student not in group
382             if ($currentgroup) {
383                 if (!ismember($currentgroup, $user->id)) {
384                     continue;
385                 }
386             }
387             if ($submissions = workshop_get_user_submissions($workshop, $user)) {
388                 foreach ($submissions as $submission) {
389                     $data = array();
390                     $sortdata = array();
391                     
392                     $data[] = "<a name=\"userid$user->id\" href=\"{$CFG->wwwroot}/user/view.php?id=$user->id&amp;course=$course->id\">".
393                         fullname($user).'</a>';
394                     $sortdata['firstname'] = $user->firstname;
395                     $sortdata['lastname'] = $user->lastname;
396                     
397                     if ($workshop->wtype) {
398                         $data[] = workshop_print_user_assessments($workshop, $user, $gradinggrade);
399                         
400                         $data[] = $gradinggrade;
401                         $sortdata['agrade'] = $gradinggrade;
402                     }
403                     
404                     $data[] = workshop_print_submission_title($workshop, $submission).
405                         " <a href=\"submissions.php?action=adminamendtitle&amp;id=$cm->id&amp;sid=$submission->id\">".
406                         "<img src=\"$CFG->pixpath/t/edit.gif\" ".
407                         'height="11" width="11" border="0" alt="'.get_string('amendtitle', 'workshop').'"></a>'.
408                         " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
409                         "<img src=\"$CFG->pixpath/t/delete.gif\" ".
410                         'height="11" width="11" border="0" alt="'.get_string('delete', 'workshop').'"></a>';
411                     $sortdata['title'] = $submission->title;
412                     
413                     $data[] = userdate($submission->timecreated, get_string('datestr', 'workshop'));
414                     $sortdata['date'] = $submission->timecreated;
415                     
416                     if (($tmp = workshop_print_submission_assessments($workshop, $submission, "teacher")) == '&nbsp;') {
417                         $data[] = '<a href="assess.php?id='.
418                             $cm->id.'&sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
419                         $sortdata['tassmnt'] = -1;
420                     } else {
421                         $data[] = $tmp;
422                         $sortdata['tassmnt'] = 1; // GWD still have to fix this
423                     }
424                     
425                     if ($workshop->wtype) {
426                         $data[] = workshop_print_submission_assessments($workshop, $submission, "student");
427                     }
428                     
429                     $grade = workshop_submission_grade($workshop, $submission);
430                     $data[] = $grade;
431                     $sortdata['sgrade'] = $grade;
433                     if ($workshop->wtype) {
434                         $data[] = number_format($gradinggrade + $grade, 1);
435                         $sortdata['ograde'] = $gradinggrade + $grade;
436                     }
438                     $tablesort->data[] = $data;
439                     $tablesort->sortdata[] = $sortdata;
440                 }
441             }
442         }
443         
444         function workshop_sortfunction($a, $b) {
445            global $sort, $dir;
446            if ($dir == 'ASC') {
447                return ($a[$sort] > $b[$sort]);
448            } else {
449                return ($a[$sort] < $b[$sort]);
450            }
451         }
452         uasort($tablesort->sortdata, 'workshop_sortfunction');
453         $table->data = array();
454         foreach($tablesort->sortdata as $key => $row) {
455             $table->data[] = $tablesort->data[$key];
456         }
457         
458         if ($workshop->wtype) {
459             $table->align = array ('left', 'center', 'center', 'left', 'center', 'center', 'center', 'center', 'center', 'center');
460             $columns = array('firstname', 'lastname', 'agrade', 'title', 'date', 'tassmnt', 'sgrade', 'ograde');
461         } else {
462             $table->align = array ('left', 'left', 'center', 'center', 'center', 'center');
463             $columns = array('firstname', 'lastname', 'title', 'date', 'tassmnt', 'ograde');
464         }
465         $table->width = "95%";  
466     
467         foreach ($columns as $column) {
468             $string[$column] = get_string("$column", 'workshop');
469             if ($sort != $column) {
470                 $columnicon = '';
471                 $columndir = 'ASC';
472             } else {
473                 $columndir = $dir == 'ASC' ? 'DESC':'ASC';
474                 if ($column == 'lastaccess') {
475                     $columnicon = $dir == 'ASC' ? 'up':'down';
476                 } else {
477                     $columnicon = $dir == 'ASC' ? 'down':'up';
478                 }
479                 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" />";
480     
481             }
482             $$column = "<a href=\"view.php?id=$id&amp;sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
483         }
484     
485         if ($workshop->wtype) {
486             $table->head = array ("$firstname / $lastname", get_string("assmnts", "workshop"), $agrade,
487                 $title, $date, $tassmnt, get_string('passmnts', 'workshop'), $sgrade, $ograde);
488         } else {
489             $table->head = array ("$firstname / $lastname", $title, $date, $tassmnt, $ograde);
490         }
491         
492         print_table($table);
493         workshop_print_key($workshop);
494         
495         // grading grade analysis
496         unset($table);
497         $table->head = array (get_string("count", "workshop"), get_string("mean", "workshop"),
498             get_string("standarddeviation", "workshop"), get_string("maximum", "workshop"), 
499             get_string("minimum", "workshop"));
500         $table->align = array ("center", "center", "center", "center", "center");
501         $table->size = array ("*", "*", "*", "*", "*");
502         $table->cellpadding = 2;
503         $table->cellspacing = 0;
504         if ($currentgroup) {
505             $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean, 
506                     STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max 
507                     FROM {$CFG->prefix}groups_members g, {$CFG->prefix}workshop_assessments a 
508                     WHERE g.groupid = $currentgroup AND a.userid = g.userid AND a.timegraded > 0 
509                     AND a.workshopid = $workshop->id");
510         } else { // no group/all participants
511             $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean, 
512                     STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max 
513                     FROM {$CFG->prefix}workshop_assessments a 
514                     WHERE a.timegraded > 0 AND a.workshopid = $workshop->id");
515         }   
516         $table->data[] = array($stats->count, number_format($stats->mean * $workshop->gradinggrade / 100, 1), 
517                 number_format($stats->stddev * $workshop->gradinggrade /100, 1), 
518                 number_format($stats->max * $workshop->gradinggrade / 100, 1), 
519                 number_format($stats->min* $workshop->gradinggrade / 100, 1));
520         print_heading(get_string("gradinggrade", "workshop")." ".get_string("analysis", "workshop"));
521         print_table($table);
522         
523         if ($workshop->showleaguetable and time() > $workshop->assessmentend) {
524             workshop_print_league_table($workshop);
525             if ($workshop->anonymous) {
526                 echo "<p>".get_string("namesnotshowntostudents", "workshop", $course->students)."</p>\n";
527             }
528         } 
529     }
532     /****************** show description  ************/
533     elseif ($action == 'showdescription') {
535         workshop_print_assignment_info($workshop);
536         print_simple_box(format_text($workshop->description, $workshop->format));
537         print_continue($_SERVER["HTTP_REFERER"]);
538     }
541     /****************** teacher's view - list all submissions  ************/
542     elseif ($action == 'allsubmissions') {
544         if ($submissions = get_records('workshop_submissions', 'workshopid', $workshop->id)) {
545             foreach($submissions as $submission) {
546                 $user = get_record('user', 'id', $submission->userid);
547                 print_heading('"'.$submission->title.'" '.get_string('by', 'workshop').' '.fullname($user));
548                 workshop_print_submission($workshop, $submission);
549             }
550         }
551     }
554     /*************** no man's land **************************************/
555     else {
556         error("Fatal Error: Unknown Action: ".$action."\n");
557     }
559     print_footer($course);
560     
561 ?>