Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / workshop / view.php
blob49266aa43380e623128e0313e3c47999d5e3b884
1 <?php // $Id$
3 /*************************************************
4 ACTIONS handled are:
6 displayfinalgrade (for students)
7 notavailable (for students)
8 studentsview
9 submitexample
10 teachersview
11 showdescription
12 showallsubmissions
14 ************************************************/
16 require("../../config.php");
17 require("lib.php");
18 require("locallib.php");
20 $id = required_param('id', PARAM_INT); // Course Module ID
21 $action = optional_param('action', '', PARAM_ALPHA);
22 $sort = optional_param('sort', 'lastname', PARAM_ALPHA);
23 $dir = optional_param('dir', 'ASC', PARAM_ALPHA);
25 $timenow = time();
27 // get some useful stuff...
28 if (! $cm = get_coursemodule_from_id('workshop', $id)) {
29 error("Course Module ID was incorrect");
31 if (! $course = get_record("course", "id", $cm->course)) {
32 error("Course is misconfigured");
34 if (! $workshop = get_record("workshop", "id", $cm->instance)) {
35 error("Course module is incorrect");
38 require_login($course->id, false, $cm);
39 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
41 $strworkshops = get_string("modulenameplural", "workshop");
42 $strworkshop = get_string("modulename", "workshop");
44 // ...and if necessary set default action
45 if (workshop_is_teacher($workshop)) {
46 if (empty($action)) { // no action specified, either go straight to elements page else the admin page
47 // has the assignment any elements
48 if (count_records("workshop_elements", "workshopid", $workshop->id) >= $workshop->nelements) {
49 $action = "teachersview";
51 else {
52 redirect("assessments.php?action=editelements&id=$cm->id");
55 } else { // it's a student then
56 if (!$cm->visible) {
57 notice(get_string("activityiscurrentlyhidden"));
59 if ($timenow < $workshop->submissionstart) {
60 $action = 'notavailable';
61 } else if (!$action) {
62 if ($timenow < $workshop->assessmentend) {
63 $action = 'studentsview';
64 } else {
65 $action = 'displayfinalgrade';
70 // ...display header...
71 $navigation = build_navigation($action, $cm);
72 print_header_simple(format_string($workshop->name), "", $navigation,
73 "", "", true, update_module_button($cm->id, $course->id, $strworkshop), navmenu($course, $cm));
76 // ...log activity...
77 add_to_log($course->id, "workshop", "view", "view.php?id=$cm->id", $workshop->id, $cm->id);
79 if ($action == 'studentsview' and !workshop_is_student($workshop)) {
80 $action = 'showdescription';
83 /****************** display final grade (for students) ************************************/
84 if ($action == 'displayfinalgrade' ) {
85 require_capability('mod/workshop:participate', $context);
87 print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
88 get_string("showdescription", 'workshop')."</a></b>");
89 // show the final grades as stored in the tables...
90 if ($submissions = workshop_get_user_submissions($workshop, $USER)) { // any submissions from user?
91 print_heading(get_string("displayoffinalgrades", "workshop"));
92 echo "<div class=\"boxaligncenter\"><table border=\"1\" width=\"90%\"><tr>";
93 echo "<td><b>".get_string("submissions", "workshop")."</b></td>";
94 if ($workshop->wtype) {
95 echo "<td align=\"center\"><b>".get_string("assessmentsdone", "workshop")."</b></td>";
96 echo "<td align=\"center\"><b>".get_string("gradeforassessments", "workshop")."</b></td>";
98 echo "<td align=\"center\"><b>".get_string("teacherassessments", "workshop",
99 $course->teacher)."</b></td>";
100 if ($workshop->wtype) {
101 echo "<td align=\"center\"><b>".get_string("studentassessments", "workshop",
102 $course->student)."</b></td>";
104 echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "workshop")."</b></td>";
105 echo "<td align=\"center\"><b>".get_string("overallgrade", "workshop")."</b></td></tr>\n";
106 foreach ($submissions as $submission) {
107 $grade = workshop_submission_grade($workshop, $submission);
108 echo "<tr><td>".workshop_print_submission_title($workshop, $submission)."</td>\n";
109 if ($workshop->wtype) {
110 echo "<td align=\"center\">".workshop_print_user_assessments($workshop, $USER, $gradinggrade)."</td>";
111 echo "<td align=\"center\">$gradinggrade</td>";
113 echo "<td align=\"center\">".workshop_print_submission_assessments($workshop,
114 $submission, "teacher")."</td>";
115 if ($workshop->wtype) {
116 echo "<td align=\"center\">".workshop_print_submission_assessments($workshop,
117 $submission, "student")."</td>";
119 echo "<td align=\"center\">$grade</td>";
120 echo "<td align=\"center\">".number_format($gradinggrade + $grade, 1)."</td></tr>\n";
122 echo "</table></div><br clear=\"all\" />\n";
123 workshop_print_key($workshop);
124 } else {
125 print_heading(get_string('nowork', 'workshop'));
127 if ($workshop->showleaguetable) {
128 workshop_print_league_table($workshop);
133 /****************** assignment not available (for students)***********************/
134 elseif ($action == 'notavailable') {
135 print_heading(get_string("notavailable", "workshop"));
138 /****************** student's view could be in 1 of 4 stages ***********************/
139 elseif ($action == 'studentsview') {
140 require_capability('mod/workshop:participate', $context);
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;
150 } elseif (isset($USER->workshoploggedin[$workshop->id])) {
151 $correctpass = true;
154 if (!$correctpass) {
155 print_simple_box_start("center");
156 echo "<form id=\"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>";
163 echo "<tr align=\"center\"><td>".get_string("passwordprotectedworkshop", "workshop", format_string($workshop->name)).
164 "</td></tr>";
165 echo "<tr align=\"center\"><td>".get_string("enterpassword", "workshop").
166 " <input type=\"password\" name=\"userpassword\" /></td></tr>";
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();
178 workshop_print_assignment_info($workshop);
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_box(format_text($workshop->description, $workshop->format), 'generalbox', 'intro');
183 } else {
184 print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
185 get_string("showdescription", 'workshop')."</a></b>");
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);
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);
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"));
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);
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);
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);
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);
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);
239 // list previous submissions
240 print_heading(get_string("yoursubmissions", "workshop"));
241 workshop_list_user_submissions($workshop, $USER);
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;
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++;
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\" />";
273 /****************** submission of example by teacher only***********************/
274 elseif ($action == 'submitexample') {
276 require_capability('mod/workshop:manage', $context);
278 // list previous submissions from teacher
279 workshop_list_user_submissions($workshop, $USER);
281 echo "<hr size=\"1\" noshade=\"noshade\" />";
283 // print upload form
284 print_heading(get_string("submitexampleassignment", "workshop").":");
285 workshop_print_upload_form($workshop);
287 print_heading("<a $CFG->frametarget href=\"view.php?id=$cm->id\">".get_string("cancel")."</a>");
291 /****************** teacher's view - display admin page ************/
292 elseif ($action == 'teachersview') {
294 require_capability('mod/workshop:manage', $context);
296 // automatically grade assessments if workshop has examples and/or peer assessments
297 if ($workshop->gradingstrategy and ($workshop->ntassessments or $workshop->nsassessments)) {
298 workshop_grade_assessments($workshop);
301 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
303 /// find out current groups mode
304 $groupmode = groups_get_activity_groupmode($cm);
305 $currentgroup = groups_get_activity_group($cm, true);
306 groups_print_activity_menu($cm, "view.php?id=$cm->id");
308 /// Print admin links
309 echo "<table width=\"100%\"><tr><td>";
310 echo "<a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
311 get_string("administration")."</a>\n";
313 echo '</td></tr>';
315 echo '<tr><td>';
316 workshop_print_assignment_info($workshop);
317 print_heading("<a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
318 get_string("showdescription", 'workshop')."</a>");
319 echo '</td></tr>';
321 /// Print grade tables /////////////////////////////////////////////////
323 // display the teacher's submissions
324 if ($workshop->ntassessments) {
325 $table->head = array(get_string("examplesubmissions", "workshop"),
326 get_string("assessmentsby", "workshop", $course->teachers),
327 get_string("assessmentsby", "workshop", $course->students));
328 $table->data = array();
329 if ($submissions = workshop_get_teacher_submissions($workshop)) {
330 foreach ($submissions as $submission) {
331 $teacherassessments = workshop_print_submission_assessments($workshop, $submission, "teacher");
332 // If not yet assessed, show assess link
333 if ($teacherassessments == '&nbsp;') {
334 $teacherassessments = '<a href="assess.php?id='.
335 $cm->id.'&amp;sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
337 $title = workshop_print_submission_title($workshop, $submission).
338 " <a href=\"submissions.php?action=editsubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
339 "<img src=\"$CFG->pixpath/t/edit.gif\" ".
340 'class="iconsmall" alt="'.get_string('edit').'" /></a>'.
341 " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
342 "<img src=\"$CFG->pixpath/t/delete.gif\" ".
343 'class="iconsmall" alt="'.get_string('delete', 'workshop').'" /></a>';
344 $table->data[] = array($title, $teacherassessments,
345 workshop_print_submission_assessments($workshop, $submission, "student"));
348 // Put in a submission link
349 $table->data[] = array("<b><a href=\"view.php?id=$cm->id&amp;action=submitexample\">".
350 get_string("submitexampleassignment", "workshop")."</a></b>".
351 helpbutton("submissionofexamples", get_string("submitexampleassignment", "workshop"), "workshop", true, false, '', true),
352 '&nbsp;', '&nbsp;');
353 print_table($table);
354 workshop_print_key($workshop);
357 // Get all the students
358 if (!$users = workshop_get_students($workshop)) {
359 echo ('</table>');
360 print_heading(get_string("nostudentsyet"));
361 print_footer($course);
362 exit;
365 if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) {
366 $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id');
367 foreach($users as $key => $user) {
368 if (!isset($groupingusers[$user->id])) {
369 unset($users[$key]);
374 /// Now prepare table with student assessments and submissions
375 $tablesort->data = array();
376 $tablesort->sortdata = array();
377 foreach ($users as $user) {
378 // skip if student not in group
379 if ($currentgroup) {
380 if (!groups_is_member($currentgroup, $user->id)) {
381 continue;
384 if ($submissions = workshop_get_user_submissions($workshop, $user)) {
385 foreach ($submissions as $submission) {
386 $data = array();
387 $sortdata = array();
389 $data[] = "<a name=\"userid$user->id\" href=\"{$CFG->wwwroot}/user/view.php?id=$user->id&amp;course=$course->id\">".
390 fullname($user).'</a>';
391 $sortdata['firstname'] = $user->firstname;
392 $sortdata['lastname'] = $user->lastname;
394 if ($workshop->wtype) {
395 $data[] = workshop_print_user_assessments($workshop, $user, $gradinggrade);
397 $data[] = $gradinggrade;
398 $sortdata['agrade'] = $gradinggrade;
401 $data[] = workshop_print_submission_title($workshop, $submission).
402 " <a href=\"submissions.php?action=adminamendtitle&amp;id=$cm->id&amp;sid=$submission->id\">".
403 "<img src=\"$CFG->pixpath/t/edit.gif\" ".
404 'class="iconsmall" alt="'.get_string('amendtitle', 'workshop').'" /></a>'.
405 " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
406 "<img src=\"$CFG->pixpath/t/delete.gif\" ".
407 'class="iconsmall" alt="'.get_string('delete', 'workshop').'" /></a>';
408 $sortdata['title'] = $submission->title;
410 $data[] = userdate($submission->timecreated, get_string('datestr', 'workshop'));
411 $sortdata['date'] = $submission->timecreated;
413 if (($tmp = workshop_print_submission_assessments($workshop, $submission, "teacher")) == '&nbsp;') {
414 $data[] = '<a href="assess.php?id='.
415 $cm->id.'&amp;sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
416 $sortdata['tassmnt'] = -1;
417 } else {
418 $data[] = $tmp;
419 $sortdata['tassmnt'] = 1; // GWD still have to fix this
422 if ($workshop->wtype) {
423 $data[] = workshop_print_submission_assessments($workshop, $submission, "student");
426 $grade = workshop_submission_grade($workshop, $submission);
427 $data[] = $grade;
428 $sortdata['sgrade'] = $grade;
430 if ($workshop->wtype) {
431 $data[] = number_format($gradinggrade + $grade, 1);
432 $sortdata['ograde'] = $gradinggrade + $grade;
435 $tablesort->data[] = $data;
436 $tablesort->sortdata[] = $sortdata;
441 function workshop_sortfunction($a, $b) {
442 global $sort, $dir;
443 if ($dir == 'ASC') {
444 return ($a[$sort] > $b[$sort]);
445 } else {
446 return ($a[$sort] < $b[$sort]);
449 uasort($tablesort->sortdata, 'workshop_sortfunction');
450 $table->data = array();
451 foreach($tablesort->sortdata as $key => $row) {
452 $table->data[] = $tablesort->data[$key];
455 if ($workshop->wtype) {
456 $table->align = array ('left', 'center', 'center', 'left', 'center', 'center', 'center', 'center', 'center', 'center');
457 $columns = array('firstname', 'lastname', 'agrade', 'title', 'date', 'tassmnt', 'sgrade', 'ograde');
458 } else {
459 $table->align = array ('left', 'left', 'center', 'center', 'center', 'center');
460 $columns = array('firstname', 'lastname', 'title', 'date', 'tassmnt', 'ograde');
462 $table->width = "95%";
464 foreach ($columns as $column) {
465 $string[$column] = get_string("$column", 'workshop');
466 if ($sort != $column) {
467 $columnicon = '';
468 $columndir = 'ASC';
469 } else {
470 $columndir = $dir == 'ASC' ? 'DESC':'ASC';
471 if ($column == 'lastaccess') {
472 $columnicon = $dir == 'ASC' ? 'up':'down';
473 } else {
474 $columnicon = $dir == 'ASC' ? 'down':'up';
476 $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"$columnicon\" />";
479 $$column = "<a href=\"view.php?id=$id&amp;sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
482 if ($workshop->wtype) {
483 $table->head = array ("$firstname / $lastname", get_string("assmnts", "workshop"), $agrade,
484 $title, $date, $tassmnt, get_string('passmnts', 'workshop'), $sgrade, $ograde);
485 } else {
486 $table->head = array ("$firstname / $lastname", $title, $date, $tassmnt, $ograde);
489 echo '<tr><td>';
490 print_table($table);
491 echo '</td></tr>';
492 echo '<tr><td>';
493 workshop_print_key($workshop);
494 echo '</td></tr>';
496 // grading grade analysis
497 unset($table);
498 $table->head = array (get_string("count", "workshop"), get_string("mean", "workshop"),
499 get_string("standarddeviation", "workshop"), get_string("maximum", "workshop"),
500 get_string("minimum", "workshop"));
501 $table->align = array ("center", "center", "center", "center", "center");
502 $table->size = array ("*", "*", "*", "*", "*");
503 $table->cellpadding = 2;
504 $table->cellspacing = 0;
505 if ($currentgroup) {
506 $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean,
507 STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max
508 FROM {$CFG->prefix}groups_members g, {$CFG->prefix}workshop_assessments a
509 WHERE g.groupid = $currentgroup AND a.userid = g.userid AND a.timegraded > 0
510 AND a.workshopid = $workshop->id");
511 } elseif (!empty($cm->groupingid) && !empty($CFG->enablegroupings)) {
512 $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean,
513 STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max
514 FROM {$CFG->prefix}workshop_assessments a
515 INNER JOIN {$CFG->prefix}groups_members g ON a.userid = g.userid
516 INNER JOIN {$CFG->prefix}groupings_groups gg ON g.groupid = gg.groupid
517 WHERE gg.groupingid = {$cm->groupingid} AND a.timegraded > 0
518 AND a.workshopid = $workshop->id");
519 } else { // no group/all participants
520 $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean,
521 STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max
522 FROM {$CFG->prefix}workshop_assessments a
523 WHERE a.timegraded > 0 AND a.workshopid = $workshop->id");
525 $table->data[] = array($stats->count, number_format($stats->mean * $workshop->gradinggrade / 100, 1),
526 number_format($stats->stddev * $workshop->gradinggrade /100, 1),
527 number_format($stats->max * $workshop->gradinggrade / 100, 1),
528 number_format($stats->min* $workshop->gradinggrade / 100, 1));
529 echo '<tr><td>';
530 print_heading(get_string("gradinggrade", "workshop")." ".get_string("analysis", "workshop"));
531 print_table($table);
532 echo '</td></tr>';
534 if ($workshop->showleaguetable and time() > $workshop->assessmentend) {
535 workshop_print_league_table($workshop);
536 if ($workshop->anonymous) {
537 echo "<p>".get_string("namesnotshowntostudents", "workshop", $course->students)."</p>\n";
540 echo '</table>';
544 /****************** show description ************/
545 elseif ($action == 'showdescription') {
546 workshop_print_assignment_info($workshop);
547 print_box(format_text($workshop->description, $workshop->format), 'generalbox', 'intro');
548 if (isset($_SERVER["HTTP_REFERER"])) {
549 print_continue(htmlentities($_SERVER["HTTP_REFERER"]));
550 } else {
551 print_continue("$CFG->wwwroot/course/view.php?id=$cm->id");
556 /****************** teacher's view - list all submissions ************/
557 elseif ($action == 'allsubmissions') {
558 require_capability('mod/workshop:manage', $context);
560 if ($submissions = get_records('workshop_submissions', 'workshopid', $workshop->id)) {
561 foreach($submissions as $submission) {
562 $user = get_record('user', 'id', $submission->userid);
563 print_heading('"'.$submission->title.'" '.get_string('by', 'workshop').' '.fullname($user));
564 workshop_print_submission($workshop, $submission);
570 /*************** no man's land **************************************/
571 else {
572 error("Fatal Error: Unknown Action: ".$action."\n");
576 print_footer($course);