<?php  // $Id: view.php,v 1.48 2006/04/19 19:48:47 skodak Exp $

/*************************************************
    ACTIONS handled are:

    displayfinalgrade (for students)
    notavailable (for students)
    studentsview
    submitexample
    teachersview
    showdescription
    showallsubmissions

************************************************/

    require("../../config.php");
    require("lib.php");
    require("locallib.php");

    $id     = required_param('id', PARAM_INT);    // Course Module ID
    $action = optional_param('action', '', PARAM_ALPHA);
    $sort   = optional_param('sort', 'lastname', PARAM_ALPHA);
    $dir    = optional_param('dir', 'ASC', PARAM_ALPHA);
    $group  = optional_param('group', -1, PARAM_INT);

    $timenow = time();

    // get some useful stuff...
    if (! $cm = get_record("course_modules", "id", $id)) {
        error("Course Module ID was incorrect");
    }
    if (! $course = get_record("course", "id", $cm->course)) {
        error("Course is misconfigured");
    }
    if (! $workshop = get_record("workshop", "id", $cm->instance)) {
        error("Course module is incorrect");
    }

    require_login($course->id, false, $cm);

    $strworkshops = get_string("modulenameplural", "workshop");
    $strworkshop  = get_string("modulename", "workshop");
    $straction = ($action) ? '-> '.get_string($action, 'workshop') : '';

    // ...display header...
    print_header_simple(format_string($workshop->name), "",
                 "<a href=\"index.php?id=$course->id\">$strworkshops</a> ->
                  <a href=\"view.php?id=$cm->id\">".format_string($workshop->name,true)."</a> $straction",
                  "", "", true, update_module_button($cm->id, $course->id, $strworkshop), navmenu($course, $cm));

    // ...and if necessary set default action

    if (isteacher($course->id)) {
        if (empty($action)) { // no action specified, either go straight to elements page else the admin page
            // has the assignment any elements
            if (count_records("workshop_elements", "workshopid", $workshop->id) >= $workshop->nelements) {
                $action = "teachersview";
            }
            else {
                redirect("assessments.php?action=editelements&id=$cm->id");
            }
        }
    }
    elseif (!isguest()) { // it's a student then
        if (!$cm->visible) {
            notice(get_string("activityiscurrentlyhidden"));
        }
        if ($timenow < $workshop->submissionstart) {
            $action = 'notavailable';
        } else if (!$action) {
            if ($timenow < $workshop->assessmentend) {
                $action = 'studentsview';
            } else {
                $action = 'displayfinalgrade';
            }
        }
    }
    else { // it's a guest, oh no!
        $action = 'notavailable';
    }

    // ...log activity...
    add_to_log($course->id, "workshop", "view", "view.php?id=$cm->id", $workshop->id, $cm->id);

    /****************** display final grade (for students) ************************************/
    if ($action == 'displayfinalgrade' ) {

        print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
                get_string("showdescription", 'workshop')."</a></b>");
        // show the final grades as stored in the tables...
        if ($submissions = workshop_get_user_submissions($workshop, $USER)) { // any submissions from user?
            print_heading(get_string("displayoffinalgrades", "workshop"));
            echo "<center><table border=\"1\" width=\"90%\"><tr>";
            echo "<td><b>".get_string("submissions", "workshop")."</b></td>";
            if ($workshop->wtype) {
                echo "<td align=\"center\"><b>".get_string("assessmentsdone", "workshop")."</b></td>";
                echo "<td align=\"center\"><b>".get_string("gradeforassessments", "workshop")."</b></td>";
            }
            echo "<td align=\"center\"><b>".get_string("teacherassessments", "workshop",
                        $course->teacher)."</b></td>";
            if ($workshop->wtype) {
                echo "<td align=\"center\"><b>".get_string("studentassessments", "workshop",
                        $course->student)."</b></td>";
            }
            echo "<td align=\"center\"><b>".get_string("gradeforsubmission", "workshop")."</b></td>";
            echo "<td align=\"center\"><b>".get_string("overallgrade", "workshop")."</b></td></tr>\n";
            foreach ($submissions as $submission) {
                $grade = workshop_submission_grade($workshop, $submission);
                echo "<tr><td>".workshop_print_submission_title($workshop, $submission)."</td>\n";
                if ($workshop->wtype) {
                    echo "<td align=\"center\">".workshop_print_user_assessments($workshop, $USER, $gradinggrade)."</td>";
                    echo "<td align=\"center\">$gradinggrade</td>";
                }
                echo "<td align=\"center\">".workshop_print_submission_assessments($workshop,
                            $submission, "teacher")."</td>";
                if ($workshop->wtype) {
                    echo "<td align=\"center\">".workshop_print_submission_assessments($workshop,
                            $submission, "student")."</td>";
                }
                echo "<td align=\"center\">$grade</td>";
                echo "<td align=\"center\">".number_format($gradinggrade + $grade, 1)."</td></tr>\n";
            }
            echo "</table><br clear=\"all\" />\n";
            workshop_print_key($workshop);
        } else {
            print_heading(get_string('nowork', 'workshop'));
        }
        if ($workshop->showleaguetable) {
            workshop_print_league_table($workshop);
        }
    }


    /****************** assignment not available (for students)***********************/
    elseif ($action == 'notavailable') {
        print_heading(get_string("notavailable", "workshop"));
    }


    /****************** student's view could be in 1 of 4 stages ***********************/
    elseif ($action == 'studentsview') {
        // is a password needed?
        if ($workshop->usepassword) {
            $correctpass = false;
            if (isset($_POST['userpassword'])) {
                if ($workshop->password == md5(trim($_POST['userpassword']))) {
                    $USER->workshoploggedin[$workshop->id] = true;
                    $correctpass = true;
                }
            } elseif (isset($USER->workshoploggedin[$workshop->id])) {
                $correctpass = true;
            }

            if (!$correctpass) {
                print_simple_box_start("center");
                echo "<form name=\"password\" method=\"post\" action=\"view.php\">\n";
                echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
                echo "<table cellpadding=\"7px\">";
                if (isset($_POST['userpassword'])) {
                    echo "<tr align=\"center\" style='color:#DF041E;'><td>".get_string("wrongpassword", "workshop").
                        "</td></tr>";
                }
                echo "<tr align=\"center\"><td>".get_string("passwordprotectedworkshop", "workshop", format_string($workshop->name)).
                    "</td></tr>";
                echo "<tr align=\"center\"><td>".get_string("enterpassword", "workshop").
                    " <input type=\"password\" name=\"userpassword\" /></td></tr>";

                echo "<tr align=\"center\"><td>";
                echo "<input type=\"button\" value=\"".get_string("cancel").
                    "\" onclick=\"parent.location='../../course/view.php?id=$course->id';\">  ";
                echo "<input type=\"button\" value=\"".get_string("continue").
                    "\" onclick=\"document.password.submit();\" />";
                echo "</td></tr></table>";
                print_simple_box_end();
                exit();
            }
        }
        workshop_print_assignment_info($workshop);

        // if the student has not yet submitted show the full description
        if (!record_exists('workshop_submissions', 'workshopid', $workshop->id, 'userid', $USER->id)) {
            print_simple_box(format_text($workshop->description, $workshop->format), 'center', '70%', '', 5, 'generalbox', 'intro');
        } else {
            print_heading("<b><a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
                get_string("showdescription", 'workshop')."</a></b>");
        }

        // in Stage 1? - are there any teacher's submissions? and...
        // ...has student assessed the required number of the teacher's submissions
        if ($workshop->ntassessments and (!workshop_test_user_assessments($workshop, $USER))) {
            print_heading(get_string("pleaseassesstheseexamplesfromtheteacher", "workshop",
                        $course->teacher));
            workshop_list_teacher_submissions($workshop, $USER);
        }
        // in stage 2? - submit own first attempt
        else {
            if ($workshop->ntassessments) {
                // show assessment of the teacher's examples, there may be feedback from teacher
                print_heading(get_string("yourassessmentsofexamplesfromtheteacher", "workshop",
                            $course->teacher));
                workshop_list_teacher_submissions($workshop, $USER);
            }
            // has user submitted anything yet?
            if (!workshop_get_user_submissions($workshop, $USER)) {
                if ($timenow < $workshop->submissionend) {
                    // print upload form
                    print_heading(get_string("submitassignmentusingform", "workshop").":");
                    workshop_print_upload_form($workshop);
                } else {
                    print_heading(get_string("submissionsnolongerallowed", "workshop"));
                }
            }
            // in stage 3? - grade other student's submissions, resubmit and list all submissions
            else {
                // is self assessment used in this workshop?
                if ($workshop->includeself) {
                    // prints a table if there are any submissions which have not been self assessed yet
                    workshop_list_self_assessments($workshop, $USER);
                }
                // if peer assessments are being done then show some  to assess...
                if ($workshop->nsassessments and ($workshop->assessmentstart < $timenow and $workshop->assessmentend > $timenow)) {
                    workshop_list_student_submissions($workshop, $USER);
                }
                // ..and any they have already done (and have gone cold)...
                if (workshop_count_user_assessments($workshop, $USER, "student")) {
                    print_heading(get_string("yourassessments", "workshop"));
                    workshop_list_assessed_submissions($workshop, $USER);
                }
                // list any assessments by teachers
                if (workshop_count_teacher_assessments_by_user($workshop, $USER) and ($timenow > $workshop->releasegrades)) {
                    print_heading(get_string("assessmentsby", "workshop", $course->teachers));
                    workshop_list_teacher_assessments_by_user($workshop, $USER);
                }
                // ... and show peer assessments
                if (workshop_count_peer_assessments($workshop, $USER)) {
                    print_heading(get_string("assessmentsby", "workshop", $course->students));
                    workshop_list_peer_assessments($workshop, $USER);
                }
                // list previous submissions
                print_heading(get_string("yoursubmissions", "workshop"));
                workshop_list_user_submissions($workshop, $USER);

                // are resubmissions allowed and the workshop is in submission/assessment phase?
                if ($workshop->resubmit and ($timenow > $workshop->assessmentstart and $timenow < $workshop->submissionend)) {
                    // see if there are any cold assessments of the last submission
                    // if there are then print upload form
                    if ($submissions = workshop_get_user_submissions($workshop, $USER)) {
                        foreach ($submissions as $submission) {
                            $lastsubmission = $submission;
                            break;
                        }
                        $n = 0; // number of cold assessments (not include self assessments)
                        if ($assessments = workshop_get_assessments($lastsubmission)) {
                            foreach ($assessments as $assessment) {
                                if ($assessment->userid <> $USER->id) {
                                    $n++;
                                }
                            }
                        }
                        if ($n) {
                            echo "<hr size=\"1\" noshade=\"noshade\" />";
                            print_heading(get_string("submitrevisedassignment", "workshop").":");
                            workshop_print_upload_form($workshop);
                            echo "<hr size=\"1\" noshade=\"noshade\" />";
                        }
                    }
                }
            }
        }
    }


    /****************** submission of example by teacher only***********************/
    elseif ($action == 'submitexample') {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }

        // list previous submissions from teacher
        workshop_list_user_submissions($workshop, $USER);

        echo "<hr size=\"1\" noshade=\"noshade\" />";

        // print upload form
        print_heading(get_string("submitexampleassignment", "workshop").":");
        workshop_print_upload_form($workshop);

        print_heading("<a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id\">".get_string("cancel")."</a>");
    }


    /****************** teacher's view - display admin page  ************/
    elseif ($action == 'teachersview') {

        if (!isteacher($course->id)) {
            error("Only teachers can look at this page");
        }

        // automatically grade assessments if workshop has examples and/or peer assessments
        if ($workshop->gradingstrategy and ($workshop->ntassessments or $workshop->nsassessments)) {
            workshop_grade_assessments($workshop);
        }

        /// Check to see if groups are being used in this workshop
        /// and if so, set $currentgroup to reflect the current group
        // $changegroup = isset($_GET['_param('group',0,PARAM_INT);group']) ? $_GET['group'] : -1;  // Group change requested?
        $changegroup = $group;
        $groupmode = groupmode($course, $cm);   // Groups are being used?
        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);

        /// Print settings and things in a table across the top
        echo '<table width="100%" border="0" cellpadding="3" cellspacing="0"><tr valign="top">';

        /// Allow the teacher to change groups (for this session)
        if ($groupmode and isteacheredit($course->id)) {
            if ($groups = get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")) {
                echo '<td>';
                print_group_menu($groups, $groupmode, $currentgroup, "view.php?id=$cm->id");
                echo '</td>';
            }
        }
        /// Print admin links
        echo "<td align=\"right\">";
        echo "<a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
            get_string("administration")."</a>\n";

        echo '</td></tr>';

        echo '<tr><td>';
        workshop_print_assignment_info($workshop);
        print_heading("<a href=\"view.php?id=$cm->id&amp;action=showdescription\">".
               get_string("showdescription", 'workshop')."</a>");
        echo '</td></tr>';

        /// Print grade tables /////////////////////////////////////////////////

        // display the teacher's submissions
        if ($workshop->ntassessments) {
            $table->head = array(get_string("examplesubmissions", "workshop"),
                get_string("assessmentsby", "workshop", $course->teachers),
                get_string("assessmentsby", "workshop", $course->students));
            $table->data = array();
            if ($submissions = workshop_get_teacher_submissions($workshop)) {
                foreach ($submissions as $submission) {
                    $teacherassessments = workshop_print_submission_assessments($workshop, $submission, "teacher");
                    // If not yet assessed, show assess link
                    if ($teacherassessments == '&nbsp;') {
                        $teacherassessments = '<a href="assess.php?id='.
                            $cm->id.'&amp;sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
                    }
                    $title = workshop_print_submission_title($workshop, $submission).
                        " <a href=\"submissions.php?action=editsubmission&amp;id=$cm->id&amp;sid=$submission->id\">".
                        "<img src=\"$CFG->pixpath/t/edit.gif\" ".
                        'height="11" width="11" border="0" alt="'.get_string('edit').'" /></a>'.
                        " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
                        "<img src=\"$CFG->pixpath/t/delete.gif\" ".
                        'height="11" width="11" border="0" alt="'.get_string('delete', 'workshop').'" /></a>';
                    $table->data[] = array($title, $teacherassessments,
                        workshop_print_submission_assessments($workshop, $submission, "student"));
                }
            }
            // Put in a submission link
            $table->data[] = array("<b><a href=\"view.php?id=$cm->id&amp;action=submitexample\">".
                get_string("submitexampleassignment", "workshop")."</a></b>".
                helpbutton("submissionofexamples", get_string("submitexampleassignment", "workshop"), "workshop", true, false, '', true),
                '&nbsp;', '&nbsp;');
            print_table($table);
            workshop_print_key($workshop);
        }

        // Get all the students
        if (!$users = get_course_students($course->id, "u.lastname, u.firstname")) {
            print_heading(get_string("nostudentsyet"));
            print_footer($course);
            exit;
        }

        /// Now prepare table with student assessments and submissions
        $tablesort->data = array();
        $tablesort->sortdata = array();
        foreach ($users as $user) {
            // skip if student not in group
            if ($currentgroup) {
                if (!ismember($currentgroup, $user->id)) {
                    continue;
                }
            }
            if ($submissions = workshop_get_user_submissions($workshop, $user)) {
                foreach ($submissions as $submission) {
                    $data = array();
                    $sortdata = array();

                    $data[] = "<a name=\"userid$user->id\" href=\"{$CFG->wwwroot}/user/view.php?id=$user->id&amp;course=$course->id\">".
                        fullname($user).'</a>';
                    $sortdata['firstname'] = $user->firstname;
                    $sortdata['lastname'] = $user->lastname;

                    if ($workshop->wtype) {
                        $data[] = workshop_print_user_assessments($workshop, $user, $gradinggrade);

                        $data[] = $gradinggrade;
                        $sortdata['agrade'] = $gradinggrade;
                    }

                    $data[] = workshop_print_submission_title($workshop, $submission).
                        " <a href=\"submissions.php?action=adminamendtitle&amp;id=$cm->id&amp;sid=$submission->id\">".
                        "<img src=\"$CFG->pixpath/t/edit.gif\" ".
                        'height="11" width="11" border="0" alt="'.get_string('amendtitle', 'workshop').'" /></a>'.
                        " <a href=\"submissions.php?action=confirmdelete&amp;id=$cm->id&amp;sid=$submission->id\">".
                        "<img src=\"$CFG->pixpath/t/delete.gif\" ".
                        'height="11" width="11" border="0" alt="'.get_string('delete', 'workshop').'" /></a>';
                    $sortdata['title'] = $submission->title;

                    $data[] = userdate($submission->timecreated, get_string('datestr', 'workshop'));
                    $sortdata['date'] = $submission->timecreated;

                    if (($tmp = workshop_print_submission_assessments($workshop, $submission, "teacher")) == '&nbsp;') {
                        $data[] = '<a href="assess.php?id='.
                            $cm->id.'&amp;sid='.$submission->id.'">'.get_string('assess', 'workshop').'</a>';
                        $sortdata['tassmnt'] = -1;
                    } else {
                        $data[] = $tmp;
                        $sortdata['tassmnt'] = 1; // GWD still have to fix this
                    }

                    if ($workshop->wtype) {
                        $data[] = workshop_print_submission_assessments($workshop, $submission, "student");
                    }

                    $grade = workshop_submission_grade($workshop, $submission);
                    $data[] = $grade;
                    $sortdata['sgrade'] = $grade;

                    if ($workshop->wtype) {
                        $data[] = number_format($gradinggrade + $grade, 1);
                        $sortdata['ograde'] = $gradinggrade + $grade;
                    }

                    $tablesort->data[] = $data;
                    $tablesort->sortdata[] = $sortdata;
                }
            }
        }

        function workshop_sortfunction($a, $b) {
           global $sort, $dir;
           if ($dir == 'ASC') {
               return ($a[$sort] > $b[$sort]);
           } else {
               return ($a[$sort] < $b[$sort]);
           }
        }
        uasort($tablesort->sortdata, 'workshop_sortfunction');
        $table->data = array();
        foreach($tablesort->sortdata as $key => $row) {
            $table->data[] = $tablesort->data[$key];
        }

        if ($workshop->wtype) {
            $table->align = array ('left', 'center', 'center', 'left', 'center', 'center', 'center', 'center', 'center', 'center');
            $columns = array('firstname', 'lastname', 'agrade', 'title', 'date', 'tassmnt', 'sgrade', 'ograde');
        } else {
            $table->align = array ('left', 'left', 'center', 'center', 'center', 'center');
            $columns = array('firstname', 'lastname', 'title', 'date', 'tassmnt', 'ograde');
        }
        $table->width = "95%";

        foreach ($columns as $column) {
            $string[$column] = get_string("$column", 'workshop');
            if ($sort != $column) {
                $columnicon = '';
                $columndir = 'ASC';
            } else {
                $columndir = $dir == 'ASC' ? 'DESC':'ASC';
                if ($column == 'lastaccess') {
                    $columnicon = $dir == 'ASC' ? 'up':'down';
                } else {
                    $columnicon = $dir == 'ASC' ? 'down':'up';
                }
                $columnicon = " <img src=\"$CFG->pixpath/t/$columnicon.gif\" alt=\"$columnicon\" />";

            }
            $$column = "<a href=\"view.php?id=$id&amp;sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
        }

        if ($workshop->wtype) {
            $table->head = array ("$firstname / $lastname", get_string("assmnts", "workshop"), $agrade,
                $title, $date, $tassmnt, get_string('passmnts', 'workshop'), $sgrade, $ograde);
        } else {
            $table->head = array ("$firstname / $lastname", $title, $date, $tassmnt, $ograde);
        }

        echo '<tr><td>';
        print_table($table);
        echo '</td></tr>';
        echo '<tr><td>';
        workshop_print_key($workshop);
        echo '</td></tr>';

        // grading grade analysis
        unset($table);
        $table->head = array (get_string("count", "workshop"), get_string("mean", "workshop"),
            get_string("standarddeviation", "workshop"), get_string("maximum", "workshop"),
            get_string("minimum", "workshop"));
        $table->align = array ("center", "center", "center", "center", "center");
        $table->size = array ("*", "*", "*", "*", "*");
        $table->cellpadding = 2;
        $table->cellspacing = 0;
        if ($currentgroup) {
            $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean,
                    STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max
                    FROM {$CFG->prefix}groups_members g, {$CFG->prefix}workshop_assessments a
                    WHERE g.groupid = $currentgroup AND a.userid = g.userid AND a.timegraded > 0
                    AND a.workshopid = $workshop->id");
        } else { // no group/all participants
            $stats = get_record_sql("SELECT COUNT(*) as count, AVG(gradinggrade) AS mean,
                    STDDEV(gradinggrade) AS stddev, MIN(gradinggrade) AS min, MAX(gradinggrade) AS max
                    FROM {$CFG->prefix}workshop_assessments a
                    WHERE a.timegraded > 0 AND a.workshopid = $workshop->id");
        }
        $table->data[] = array($stats->count, number_format($stats->mean * $workshop->gradinggrade / 100, 1),
                number_format($stats->stddev * $workshop->gradinggrade /100, 1),
                number_format($stats->max * $workshop->gradinggrade / 100, 1),
                number_format($stats->min* $workshop->gradinggrade / 100, 1));
        echo '<tr><td>';
        print_heading(get_string("gradinggrade", "workshop")." ".get_string("analysis", "workshop"));
        print_table($table);
        echo '</td></tr>';

        if ($workshop->showleaguetable and time() > $workshop->assessmentend) {
            workshop_print_league_table($workshop);
            if ($workshop->anonymous) {
                echo "<p>".get_string("namesnotshowntostudents", "workshop", $course->students)."</p>\n";
            }
        }
        echo '</table>';
    }


    /****************** show description  ************/
    elseif ($action == 'showdescription') {

        workshop_print_assignment_info($workshop);
        print_simple_box(format_text($workshop->description, $workshop->format), 'center', '70%', '', 5, 'generalbox', 'intro');
        print_continue($_SERVER["HTTP_REFERER"]);
    }


    /****************** teacher's view - list all submissions  ************/
    elseif ($action == 'allsubmissions') {

        if ($submissions = get_records('workshop_submissions', 'workshopid', $workshop->id)) {
            foreach($submissions as $submission) {
                $user = get_record('user', 'id', $submission->userid);
                print_heading('"'.$submission->title.'" '.get_string('by', 'workshop').' '.fullname($user));
                workshop_print_submission($workshop, $submission);
            }
        }
    }


    /*************** no man's land **************************************/
    else {
        error("Fatal Error: Unknown Action: ".$action."\n");
    }


    print_footer($course);

?>
