Fixed workshop view logging
[moodle.git] / mod / workshop / view.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Prints a particular instance of workshop
20  *
21  * You can have a rather longer description of the file as well,
22  * if you like, and it can span multiple lines.
23  *
24  * @package    mod
25  * @subpackage workshop
26  * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
27  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28  */
30 require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
31 require_once(dirname(__FILE__).'/locallib.php');
33 $id         = optional_param('id', 0, PARAM_INT); // course_module ID, or
34 $w          = optional_param('w', 0, PARAM_INT);  // workshop instance ID
35 $editmode   = optional_param('editmode', null, PARAM_BOOL);
37 if ($id) {
38     $cm         = get_coursemodule_from_id('workshop', $id, 0, false, MUST_EXIST);
39     $course     = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
40     $workshop   = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
41 } else {
42     $workshop   = $DB->get_record('workshop', array('id' => $w), '*', MUST_EXIST);
43     $course     = $DB->get_record('course', array('id' => $workshop->course), '*', MUST_EXIST);
44     $cm         = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
45 }
47 require_login($course, true, $cm);
48 require_capability('mod/workshop:view', $PAGE->context);
50 $workshop = new workshop($workshop, $cm, $course);
51 $workshop->log('view');
53 if (!is_null($editmode) && $PAGE->user_allowed_editing()) {
54     $USER->editing = $editmode;
55 }
57 $PAGE->set_url($workshop->view_url());
58 $PAGE->set_title($workshop->name);
59 $PAGE->set_heading($course->fullname);
61 $output = $PAGE->get_renderer('mod_workshop');
62 $userplan = new workshop_user_plan($workshop, $USER->id);
64 /// Output starts here
66 echo $output->header();
67 echo $output->heading_with_help(format_string($workshop->name), 'userplan', 'workshop');
68 echo $output->render($userplan);
70 switch ($workshop->phase) {
71 case workshop::PHASE_SETUP:
72     if (trim($workshop->intro)) {
73         print_collapsible_region_start('', 'workshop-viewlet-intro', get_string('introduction', 'workshop'));
74         echo $output->box(format_module_intro('workshop', $workshop, $workshop->cm->id), 'generalbox');
75         print_collapsible_region_end();
76     }
77     if ($workshop->useexamples and has_capability('mod/workshop:manageexamples', $PAGE->context)) {
78         print_collapsible_region_start('', 'workshop-viewlet-allexamples', get_string('examplesubmissions', 'workshop'));
79         echo $output->box_start('generalbox examples');
80         if ($workshop->grading_strategy_instance()->form_ready()) {
81             if (! $examples = $workshop->get_examples_for_manager()) {
82                 echo $output->container(get_string('noexamples', 'workshop'), 'noexamples');
83             }
84             foreach ($examples as $example) {
85                 $summary = $workshop->prepare_example_summary($example);
86                 $summary->editable = true;
87                 echo $output->render($summary);
88             }
89             $aurl = new moodle_url($workshop->exsubmission_url(0), array('edit' => 'on'));
90             echo $output->single_button($aurl, get_string('exampleadd', 'workshop'), 'get');
91         } else {
92             echo $output->container(get_string('noexamplesformready', 'workshop'));
93         }
94         echo $output->box_end();
95         print_collapsible_region_end();
96     }
97     break;
98 case workshop::PHASE_SUBMISSION:
99     if (trim($workshop->instructauthors)) {
100         $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
101             'mod_workshop', 'instructauthors', 0, workshop::instruction_editors_options($PAGE->context));
102         print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'));
103         echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
104         print_collapsible_region_end();
105     }
107     // does the user have to assess examples before submitting their own work?
108     $examplesmust = ($workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_SUBMISSION);
110     // is the assessment of example submissions considered finished?
111     $examplesdone = has_capability('mod/workshop:manageexamples', $workshop->context);
112     if ($workshop->assessing_examples_allowed()
113             and has_capability('mod/workshop:submit', $workshop->context)
114                     and ! has_capability('mod/workshop:manageexamples', $workshop->context)) {
115         $examples = $userplan->get_examples();
116         $total = count($examples);
117         $left = 0;
118         // make sure the current user has all examples allocated
119         foreach ($examples as $exampleid => $example) {
120             if (is_null($example->assessmentid)) {
121                 $examples[$exampleid]->assessmentid = $workshop->add_allocation($example, $USER->id, 0);
122             }
123             if (is_null($example->grade)) {
124                 $left++;
125             }
126         }
127         if ($left > 0 and $workshop->examplesmode != workshop::EXAMPLES_VOLUNTARY) {
128             $examplesdone = false;
129         } else {
130             $examplesdone = true;
131         }
132         print_collapsible_region_start('', 'workshop-viewlet-examples', get_string('exampleassessments', 'workshop'), false, $examplesdone);
133         echo $output->box_start('generalbox exampleassessments');
134         if ($total == 0) {
135             echo $output->heading(get_string('noexamples', 'workshop'), 3);
136         } else {
137             foreach ($examples as $example) {
138                 $summary = $workshop->prepare_example_summary($example);
139                 echo $output->render($summary);
140             }
141         }
142         echo $output->box_end();
143         print_collapsible_region_end();
144     }
146     if (has_capability('mod/workshop:submit', $PAGE->context) and (!$examplesmust or $examplesdone)) {
147         print_collapsible_region_start('', 'workshop-viewlet-ownsubmission', get_string('yoursubmission', 'workshop'));
148         echo $output->box_start('generalbox ownsubmission');
149         if ($submission = $workshop->get_submission_by_author($USER->id)) {
150             echo $output->render($workshop->prepare_submission_summary($submission, true));
151             if ($workshop->modifying_submission_allowed()) {
152                 $btnurl = new moodle_url($workshop->submission_url(), array('edit' => 'on'));
153                 $btntxt = get_string('editsubmission', 'workshop');
154             }
155         } else {
156             echo $output->container(get_string('noyoursubmission', 'workshop'));
157             if ($workshop->creating_submission_allowed()) {
158                 $btnurl = new moodle_url($workshop->submission_url(), array('edit' => 'on'));
159                 $btntxt = get_string('createsubmission', 'workshop');
160             }
161         }
162         if (!empty($btnurl)) {
163             echo $output->single_button($btnurl, $btntxt, 'get');
164         }
165         echo $output->box_end();
166         print_collapsible_region_end();
167     }
169     if (has_capability('mod/workshop:viewallsubmissions', $PAGE->context)) {
170         $shownames = has_capability('mod/workshop:viewauthornames', $PAGE->context);
171         print_collapsible_region_start('', 'workshop-viewlet-allsubmissions', get_string('allsubmissions', 'workshop'));
172         echo $output->box_start('generalbox allsubmissions');
173         if (! $submissions = $workshop->get_submissions('all')) {
174             echo $output->container(get_string('nosubmissions', 'workshop'), 'nosubmissions');
175         }
176         foreach ($submissions as $submission) {
177             echo $output->render($workshop->prepare_submission_summary($submission, $shownames));
178         }
179         echo $output->box_end();
180         print_collapsible_region_end();
181     }
183     break;
185 case workshop::PHASE_ASSESSMENT:
187     $ownsubmissionexists = null;
188     if (has_capability('mod/workshop:submit', $PAGE->context)) {
189         if ($ownsubmission = $workshop->get_submission_by_author($USER->id)) {
190             print_collapsible_region_start('', 'workshop-viewlet-ownsubmission', get_string('yoursubmission', 'workshop'), false, true);
191             echo $output->box_start('generalbox ownsubmission');
192             echo $output->render($workshop->prepare_submission_summary($ownsubmission, true));
193             $ownsubmissionexists = true;
194         } else {
195             print_collapsible_region_start('', 'workshop-viewlet-ownsubmission', get_string('yoursubmission', 'workshop'));
196             echo $output->box_start('generalbox ownsubmission');
197             echo $output->container(get_string('noyoursubmission', 'workshop'));
198             $ownsubmissionexists = false;
199             if ($workshop->creating_submission_allowed()) {
200                 $btnurl = new moodle_url($workshop->submission_url(), array('edit' => 'on'));
201                 $btntxt = get_string('createsubmission', 'workshop');
202             }
203         }
204         if (!empty($btnurl)) {
205             echo $output->single_button($btnurl, $btntxt, 'get');
206         }
207         echo $output->box_end();
208         print_collapsible_region_end();
209     }
211     if (has_capability('mod/workshop:viewallassessments', $PAGE->context)) {
212         $page       = optional_param('page', 0, PARAM_INT);
213         $sortby     = optional_param('sortby', 'lastname', PARAM_ALPHA);
214         $sorthow    = optional_param('sorthow', 'ASC', PARAM_ALPHA);
215         $perpage    = 10;           // todo let the user modify this
216         $groups     = '';           // todo let the user choose the group
217         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
218         $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
219         if ($data) {
220             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
221             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
223             // prepare paging bar
224             $pagingbar              = new paging_bar($data->totalcount, $page, $perpage, $PAGE->url, 'page');
226             // grading report display options
227             $reportopts                         = new stdclass();
228             $reportopts->showauthornames        = $showauthornames;
229             $reportopts->showreviewernames      = $showreviewernames;
230             $reportopts->sortby                 = $sortby;
231             $reportopts->sorthow                = $sorthow;
232             $reportopts->showsubmissiongrade    = false;
233             $reportopts->showgradinggrade       = false;
235             echo $output->render($pagingbar);
236             echo $output->render(new workshop_grading_report($data, $reportopts));
237             echo $output->render($pagingbar);
238         }
239     }
240     if (trim($workshop->instructreviewers)) {
241         $instructions = file_rewrite_pluginfile_urls($workshop->instructreviewers, 'pluginfile.php', $PAGE->context->id,
242             'mod_workshop', 'instructreviewers', 0, workshop::instruction_editors_options($PAGE->context));
243         print_collapsible_region_start('', 'workshop-viewlet-instructreviewers', get_string('instructreviewers', 'workshop'));
244         echo $output->box(format_text($instructions, $workshop->instructreviewersformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
245         print_collapsible_region_end();
246     }
248     // does the user have to assess examples before assessing other's work?
249     $examplesmust = ($workshop->useexamples and $workshop->examplesmode == workshop::EXAMPLES_BEFORE_ASSESSMENT);
251     // is the assessment of example submissions considered finished?
252     $examplesdone = has_capability('mod/workshop:manageexamples', $workshop->context);
254     // can the examples be assessed?
255     $examplesavailable = true;
257     if (!$examplesdone and $examplesmust and ($ownsubmissionexists === false)) {
258         print_collapsible_region_start('', 'workshop-viewlet-examplesfail', get_string('exampleassessments', 'workshop'));
259         echo $output->box(get_string('exampleneedsubmission', 'workshop'));
260         print_collapsible_region_end();
261         $examplesavailable = false;
262     }
264     if ($workshop->assessing_examples_allowed()
265             and has_capability('mod/workshop:submit', $workshop->context)
266                 and ! has_capability('mod/workshop:manageexamples', $workshop->context)
267                     and $examplesavailable) {
268         $examples = $userplan->get_examples();
269         $total = count($examples);
270         $left = 0;
271         // make sure the current user has all examples allocated
272         foreach ($examples as $exampleid => $example) {
273             if (is_null($example->assessmentid)) {
274                 $examples[$exampleid]->assessmentid = $workshop->add_allocation($example, $USER->id, 0);
275             }
276             if (is_null($example->grade)) {
277                 $left++;
278             }
279         }
280         if ($left > 0 and $workshop->examplesmode != workshop::EXAMPLES_VOLUNTARY) {
281             $examplesdone = false;
282         } else {
283             $examplesdone = true;
284         }
285         print_collapsible_region_start('', 'workshop-viewlet-examples', get_string('exampleassessments', 'workshop'), false, $examplesdone);
286         echo $output->box_start('generalbox exampleassessments');
287         if ($total == 0) {
288             echo $output->heading(get_string('noexamples', 'workshop'), 3);
289         } else {
290             foreach ($examples as $example) {
291                 $summary = $workshop->prepare_example_summary($example);
292                 echo $output->render($summary);
293             }
294         }
295         echo $output->box_end();
296         print_collapsible_region_end();
297     }
298     if (!$examplesmust or $examplesdone) {
299         print_collapsible_region_start('', 'workshop-viewlet-assignedassessments', get_string('assignedassessments', 'workshop'));
300         if (! $assessments = $workshop->get_assessments_by_reviewer($USER->id)) {
301             echo $output->box_start('generalbox assessment-none');
302             echo $output->heading(get_string('assignedassessmentsnone', 'workshop'), 3);
303             echo $output->box_end();
304         } else {
305             $shownames = has_capability('mod/workshop:viewauthornames', $PAGE->context);
306             foreach ($assessments as $assessment) {
307                 $submission                     = new stdClass();
308                 $submission->id                 = $assessment->submissionid;
309                 $submission->title              = $assessment->submissiontitle;
310                 $submission->timecreated        = $assessment->submissioncreated;
311                 $submission->timemodified       = $assessment->submissionmodified;
312                 $submission->authorid           = $assessment->authorid;
313                 $submission->authorfirstname    = $assessment->authorfirstname;
314                 $submission->authorlastname     = $assessment->authorlastname;
315                 $submission->authorpicture      = $assessment->authorpicture;
316                 $submission->authorimagealt     = $assessment->authorimagealt;
317                 $submission->authoremail        = $assessment->authoremail;
319                 // transform the submission object into renderable component
320                 $submission = $workshop->prepare_submission_summary($submission, $shownames);
322                 if (is_null($assessment->grade)) {
323                     $submission->status = 'notgraded';
324                     $class = ' notgraded';
325                     $buttontext = get_string('assess', 'workshop');
326                 } else {
327                     $submission->status = 'graded';
328                     $class = ' graded';
329                     $buttontext = get_string('reassess', 'workshop');
330                 }
332                 echo $output->box_start('generalbox assessment-summary' . $class);
333                 echo $output->render($submission);
334                 $aurl = $workshop->assess_url($assessment->id);
335                 echo $output->single_button($aurl, $buttontext, 'get');
336                 echo $output->box_end();
337             }
338         }
339         print_collapsible_region_end();
340     }
341     break;
342 case workshop::PHASE_EVALUATION:
343     if (has_capability('mod/workshop:viewallassessments', $PAGE->context)) {
344         $page       = optional_param('page', 0, PARAM_INT);
345         $sortby     = optional_param('sortby', 'lastname', PARAM_ALPHA);
346         $sorthow    = optional_param('sorthow', 'ASC', PARAM_ALPHA);
347         $perpage    = 10;           // todo let the user modify this
348         $groups     = '';           // todo let the user choose the group
349         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
350         $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
351         if ($data) {
352             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
353             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
355             if (has_capability('mod/workshop:overridegrades', $PAGE->context)) {
356                 // load the grading evaluator
357                 $evaluator = $workshop->grading_evaluation_instance();
358                 $form = $evaluator->get_settings_form(new moodle_url($workshop->aggregate_url(),
359                         compact('sortby', 'sorthow', 'page')));
360                 $form->display();
361             }
363             // prepare paging bar
364             $pagingbar              = new paging_bar($data->totalcount, $page, $perpage, $PAGE->url, 'page');
366             // grading report display options
367             $reportopts                         = new stdclass();
368             $reportopts->showauthornames        = $showauthornames;
369             $reportopts->showreviewernames      = $showreviewernames;
370             $reportopts->sortby                 = $sortby;
371             $reportopts->sorthow                = $sorthow;
372             $reportopts->showsubmissiongrade    = true;
373             $reportopts->showgradinggrade       = true;
375             echo $output->render($pagingbar);
376             echo $output->render(new workshop_grading_report($data, $reportopts));
377             echo $output->render($pagingbar);
378         }
379     }
380     if (has_capability('mod/workshop:overridegrades', $workshop->context)) {
381         print_collapsible_region_start('', 'workshop-viewlet-cleargrades', get_string('toolbox', 'workshop'), false, true);
382         echo $output->box_start('generalbox toolbox');
384         // Clear aggregated grades
385         $url = new moodle_url($workshop->toolbox_url('clearaggregatedgrades'));
386         $btn = new single_button($url, get_string('clearaggregatedgrades', 'workshop'), 'post');
387         $btn->add_confirm_action(get_string('clearaggregatedgradesconfirm', 'workshop'));
388         echo $output->container_start('toolboxaction');
389         echo $output->render($btn);
390         echo $output->help_icon('clearaggregatedgrades', 'workshop');
391         echo $output->container_end();
392         // Clear assessments
393         $url = new moodle_url($workshop->toolbox_url('clearassessments'));
394         $btn = new single_button($url, get_string('clearassessments', 'workshop'), 'post');
395         $btn->add_confirm_action(get_string('clearassessmentsconfirm', 'workshop'));
396         echo $output->container_start('toolboxaction');
397         echo $output->render($btn);
398         echo $output->help_icon('clearassessments', 'workshop');
399         echo html_writer::empty_tag('img', array('src' => $output->pix_url('i/risk_dataloss'),
400                                                  'title' => get_string('riskdatalossshort', 'admin'),
401                                                  'alt' => get_string('riskdatalossshort', 'admin')));
402         echo $output->container_end();
404         echo $output->box_end();
405         print_collapsible_region_end();
406     }
407     if (has_capability('mod/workshop:submit', $PAGE->context)) {
408         print_collapsible_region_start('', 'workshop-viewlet-ownsubmission', get_string('yoursubmission', 'workshop'));
409         echo $output->box_start('generalbox ownsubmission');
410         if ($submission = $workshop->get_submission_by_author($USER->id)) {
411             echo $output->render(new workshop_submission_summary($submission, true));
412         } else {
413             echo $output->container(get_string('noyoursubmission', 'workshop'));
414         }
415         echo $output->box_end();
416         print_collapsible_region_end();
417     }
418     if ($assessments = $workshop->get_assessments_by_reviewer($USER->id)) {
419         print_collapsible_region_start('', 'workshop-viewlet-assignedassessments', get_string('assignedassessments', 'workshop'));
420         $shownames = has_capability('mod/workshop:viewauthornames', $PAGE->context);
421         foreach ($assessments as $assessment) {
422             $submission                     = new stdclass();
423             $submission->id                 = $assessment->submissionid;
424             $submission->title              = $assessment->submissiontitle;
425             $submission->timecreated        = $assessment->submissioncreated;
426             $submission->timemodified       = $assessment->submissionmodified;
427             $submission->authorid           = $assessment->authorid;
428             $submission->authorfirstname    = $assessment->authorfirstname;
429             $submission->authorlastname     = $assessment->authorlastname;
430             $submission->authorpicture      = $assessment->authorpicture;
431             $submission->authorimagealt     = $assessment->authorimagealt;
432             $submission->authoremail        = $assessment->authoremail;
434             if (is_null($assessment->grade)) {
435                 $class = ' notgraded';
436                 $submission->status = 'notgraded';
437                 $buttontext = get_string('assess', 'workshop');
438             } else {
439                 $class = ' graded';
440                 $submission->status = 'graded';
441                 $buttontext = get_string('reassess', 'workshop');
442             }
443             echo $output->box_start('generalbox assessment-summary' . $class);
444             echo $output->render($workshop->prepare_submission_summary($submission, $shownames));
445             echo $output->box_end();
446         }
447         print_collapsible_region_end();
448     }
449     break;
450 case workshop::PHASE_CLOSED:
451     if (has_capability('mod/workshop:viewallassessments', $PAGE->context)) {
452         $page       = optional_param('page', 0, PARAM_INT);
453         $sortby     = optional_param('sortby', 'lastname', PARAM_ALPHA);
454         $sorthow    = optional_param('sorthow', 'ASC', PARAM_ALPHA);
455         $perpage    = 10;           // todo let the user modify this
456         $groups     = '';           // todo let the user choose the group
457         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
458         $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
459         if ($data) {
460             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
461             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
463             // prepare paging bar
464             $pagingbar = new paging_bar($data->totalcount, $page, $perpage, $PAGE->url, 'page');
466             // grading report display options
467             $reportopts                         = new stdclass();
468             $reportopts->showauthornames        = $showauthornames;
469             $reportopts->showreviewernames      = $showreviewernames;
470             $reportopts->sortby                 = $sortby;
471             $reportopts->sorthow                = $sorthow;
472             $reportopts->showsubmissiongrade    = true;
473             $reportopts->showgradinggrade       = true;
475             print_collapsible_region_start('', 'workshop-viewlet-gradereport', get_string('gradesreport', 'workshop'));
476             echo $output->render($pagingbar);
477             echo $output->render(new workshop_grading_report($data, $reportopts));
478             echo $output->render($pagingbar);
479             print_collapsible_region_end();
480         }
481     }
482     if (has_capability('mod/workshop:submit', $PAGE->context)) {
483         print_collapsible_region_start('', 'workshop-viewlet-ownsubmission', get_string('yoursubmission', 'workshop'));
484         echo $output->box_start('generalbox ownsubmission');
485         if ($submission = $workshop->get_submission_by_author($USER->id)) {
486             echo $output->render($workshop->prepare_submission_summary($submission, true));
487         } else {
488             echo $output->container(get_string('noyoursubmission', 'workshop'));
489         }
490         echo $output->box_end();
491         print_collapsible_region_end();
492     }
493     if (has_capability('mod/workshop:viewpublishedsubmissions', $workshop->context)) {
494         if ($submissions = $workshop->get_published_submissions()) {
495             print_collapsible_region_start('', 'workshop-viewlet-publicsubmissions', get_string('publishedsubmissions', 'workshop'));
496             foreach ($submissions as $submission) {
497                 echo $output->box_start('generalbox submission-summary');
498                 echo $output->render($workshop->prepare_submission_summary($submission, true));
499                 echo $output->box_end();
500             }
501             print_collapsible_region_end();
502         }
503     }
504     break;
505 default:
508 $completion = new completion_info($course);
509 $completion->set_module_viewed($cm);
511 echo $output->footer();