MDL-22507 workshop grading report rendering
authorDavid Mudrak <david.mudrak@gmail.com>
Wed, 20 Oct 2010 13:13:21 +0000 (13:13 +0000)
committerDavid Mudrak <david.mudrak@gmail.com>
Wed, 20 Oct 2010 13:13:21 +0000 (13:13 +0000)
mod/workshop/locallib.php
mod/workshop/renderer.php
mod/workshop/view.php

index bdcf9c8..8e7a907 100644 (file)
@@ -1272,7 +1272,7 @@ class workshop {
      * @param string $sorthow ASC|DESC
      * @return stdclass data for the renderer
      */
-    public function prepare_grading_report($userid, $groups, $page, $perpage, $sortby, $sorthow) {
+    public function prepare_grading_report_data($userid, $groups, $page, $perpage, $sortby, $sorthow) {
         global $DB;
 
         $canviewall     = has_capability('mod/workshop:viewallassessments', $this->context, $userid);
@@ -2705,3 +2705,40 @@ class workshop_allocation_init_result implements renderable {
         return $this->continue;
     }
 }
+
+/**
+ * Renderable component containing all the data needed to display the grading report
+ */
+class workshop_grading_report implements renderable {
+
+    /** @var stdClass returned by {@see workshop::prepare_grading_report_data()} */
+    protected $data;
+    /** @var stdClass rendering options */
+    protected $options;
+
+    /**
+     * Grades in $data must be already rounded to the set number of decimals or must be null
+     * (in which later case, the [mod_workshop,nullgrade] string shall be displayed)
+     *
+     * @param stdClass $data prepared by {@link workshop::prepare_grading_report_data()}
+     * @param stdClass $options display options (showauthornames, showreviewernames, sortby, sorthow, showsubmissiongrade, showgradinggrade)
+     */
+    public function __construct(stdClass $data, stdClass $options) {
+        $this->data     = $data;
+        $this->options  = $options;
+    }
+
+    /**
+     * @return stdClass grading report data
+     */
+    public function get_data() {
+        return $this->data;
+    }
+
+    /**
+     * @return stdClass rendering options
+     */
+    public function get_options() {
+        return $this->options;
+    }
+}
index 12b723b..9a282e8 100644 (file)
@@ -16,7 +16,7 @@
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 /**
- * All workshop module renderers are defined here
+ * Workshop module renderering methods are defined here
  *
  * @package    mod
  * @subpackage workshop
@@ -35,7 +35,7 @@ defined('MOODLE_INTERNAL') || die();
 class mod_workshop_renderer extends plugin_renderer_base {
 
     ////////////////////////////////////////////////////////////////////////////
-    // Methods to render workshop renderable components
+    // External API - methods to render workshop renderable components
     ////////////////////////////////////////////////////////////////////////////
 
     /**
@@ -342,130 +342,18 @@ class mod_workshop_renderer extends plugin_renderer_base {
         return $o;
     }
 
-    ////////////////////////////////////////////////////////////////////////////
-    // Rendering helper methods
-    ////////////////////////////////////////////////////////////////////////////
-
-    /**
-     * Renders a list of files attached to the submission
-     *
-     * If format==html, then format a html string. If format==text, then format a text-only string.
-     * Otherwise, returns html for non-images and html to display the image inline.
-     *
-     * @param int $submissionid submission identifier
-     * @param string format the format of the returned string - html|text
-     * @return string formatted text to be echoed
-     */
-    protected function helper_submission_attachments($submissionid, $format = 'html') {
-        global $CFG;
-        require_once($CFG->libdir.'/filelib.php');
-
-        $fs     = get_file_storage();
-        $ctx    = $this->page->context;
-        $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
-
-        $outputimgs     = '';   // images to be displayed inline
-        $outputfiles    = '';   // list of attachment files
-
-        foreach ($files as $file) {
-            if ($file->is_directory()) {
-                continue;
-            }
-
-            $filepath   = $file->get_filepath();
-            $filename   = $file->get_filename();
-            $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
-                                '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
-            $type       = $file->get_mimetype();
-            $type       = mimeinfo_from_type('type', $type);
-            $image      = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
-
-            $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
-            $linktxt    = "$filename [$fileurl]";
-
-            if ($format == 'html') {
-                if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
-                    $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
-                    $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
-                    $outputimgs .= $this->output->container($preview);
-
-                } else {
-                    $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
-                }
-
-            } else if ($format == 'text') {
-                $outputfiles .= $linktxt . PHP_EOL;
-            }
-        }
-
-        if ($format == 'html') {
-            if ($outputimgs) {
-                $outputimgs = $this->output->container($outputimgs, 'images');
-            }
-
-            if ($outputfiles) {
-                $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
-            }
-
-            return $this->output->container($outputimgs . $outputfiles, 'attachments');
-
-        } else {
-            return $outputfiles;
-        }
-    }
-
-    /**
-     * Renders the tasks for the single phase in the user plan
-     *
-     * @param stdClass $tasks
-     * @return string html code
-     */
-    protected function helper_user_plan_tasks(array $tasks) {
-        $out = '';
-        foreach ($tasks as $taskcode => $task) {
-            $classes = '';
-            $icon = null;
-            if ($task->completed === true) {
-                $classes .= ' completed';
-            } elseif ($task->completed === false) {
-                $classes .= ' fail';
-            } elseif ($task->completed === 'info') {
-                $classes .= ' info';
-            }
-            if (is_null($task->link)) {
-                $title = $task->title;
-            } else {
-                $title = html_writer::link($task->link, $task->title);
-            }
-            $title = $this->output->container($title, 'title');
-            $details = $this->output->container($task->details, 'details');
-            $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
-        }
-        if ($out) {
-            $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
-        }
-        return $out;
-    }
-
-
-    ////////////////////////////////////////////////////////////////////////////
-    // TODO Obsolete
-    //
-
     /**
      * Renders the workshop grading report
      *
-     * Grades must be already rounded to the set number of decimals or must be null (in which later case,
-     * the [[nullgrade]] string shall be displayed).
-     *
-     * @param stdClass $data prepared by {@link workshop::prepare_grading_report()}
-     * @param stdClass $options display options object with properties ->showauthornames ->showreviewernames ->sortby ->sorthow
-     *          ->showsubmissiongrade ->showgradinggrade
+     * @param workshop_grading_report $gradingreport
      * @return string html code
      */
-    public function grading_report(stdclass $data, stdclass $options) {
-        $grades             = $data->grades;
-        $userinfo           = $data->userinfo;
+    protected function render_workshop_grading_report(workshop_grading_report $gradingreport) {
+
+        $data       = $gradingreport->get_data();
+        $options    = $gradingreport->get_options();
+        $grades     = $data->grades;
+        $userinfo   = $data->userinfo;
 
         if (empty($grades)) {
             return '';
@@ -474,8 +362,8 @@ class mod_workshop_renderer extends plugin_renderer_base {
         $table = new html_table();
         $table->attributes['class'] = 'grading-report';
 
-        $sortbyfirstname = $this->sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
-        $sortbylastname = $this->sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
+        $sortbyfirstname = $this->helper_sortable_heading(get_string('firstname'), 'firstname', $options->sortby, $options->sorthow);
+        $sortbylastname = $this->helper_sortable_heading(get_string('lastname'), 'lastname', $options->sortby, $options->sorthow);
         if (self::fullname_format() == 'lf') {
             $sortbyname = $sortbylastname . ' / ' . $sortbyfirstname;
         } else {
@@ -484,16 +372,16 @@ class mod_workshop_renderer extends plugin_renderer_base {
 
         $table->head = array();
         $table->head[] = $sortbyname;
-        $table->head[] = $this->sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
+        $table->head[] = $this->helper_sortable_heading(get_string('submission', 'workshop'), 'submissiontitle',
                 $options->sortby, $options->sorthow);
-        $table->head[] = $this->sortable_heading(get_string('receivedgrades', 'workshop'));
+        $table->head[] = $this->helper_sortable_heading(get_string('receivedgrades', 'workshop'));
         if ($options->showsubmissiongrade) {
-            $table->head[] = $this->sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
+            $table->head[] = $this->helper_sortable_heading(get_string('submissiongradeof', 'workshop', $data->maxgrade),
                     'submissiongrade', $options->sortby, $options->sorthow);
         }
-        $table->head[] = $this->sortable_heading(get_string('givengrades', 'workshop'));
+        $table->head[] = $this->helper_sortable_heading(get_string('givengrades', 'workshop'));
         if ($options->showgradinggrade) {
-            $table->head[] = $this->sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
+            $table->head[] = $this->helper_sortable_heading(get_string('gradinggradeof', 'workshop', $data->maxgradinggrade),
                     'gradinggrade', $options->sortby, $options->sorthow);
         }
 
@@ -533,7 +421,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                 // column #1 - participant - spans over all rows
                 if ($tr == 0) {
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_participant($participant, $userinfo);
+                    $cell->text = $this->helper_grading_report_participant($participant, $userinfo);
                     $cell->rowspan = $numoftrs;
                     $cell->attributes['class'] = 'participant';
                     $row->cells[] = $cell;
@@ -541,7 +429,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                 // column #2 - submission - spans over all rows
                 if ($tr == 0) {
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_submission($participant);
+                    $cell->text = $this->helper_grading_report_submission($participant);
                     $cell->rowspan = $numoftrs;
                     $cell->attributes['class'] = 'submission';
                     $row->cells[] = $cell;
@@ -551,7 +439,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                     $idx = intval($tr / $spanreceived);
                     $assessment = self::array_nth($participant->reviewedby, $idx);
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
+                    $cell->text = $this->helper_grading_report_assessment($assessment, $options->showreviewernames, $userinfo,
                             get_string('gradereceivedfrom', 'workshop'));
                     $cell->rowspan = $spanreceived;
                     $cell->attributes['class'] = 'receivedgrade';
@@ -565,7 +453,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                 // column #4 - total grade for submission
                 if ($options->showsubmissiongrade and $tr == 0) {
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
+                    $cell->text = $this->helper_grading_report_grade($participant->submissiongrade, $participant->submissiongradeover);
                     $cell->rowspan = $numoftrs;
                     $cell->attributes['class'] = 'submissiongrade';
                     $row->cells[] = $cell;
@@ -575,7 +463,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                     $idx = intval($tr / $spangiven);
                     $assessment = self::array_nth($participant->reviewerof, $idx);
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_assessment($assessment, $options->showauthornames, $userinfo,
+                    $cell->text = $this->helper_grading_report_assessment($assessment, $options->showauthornames, $userinfo,
                             get_string('gradegivento', 'workshop'));
                     $cell->rowspan = $spangiven;
                     $cell->attributes['class'] = 'givengrade';
@@ -589,7 +477,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
                 // column #6 - total grade for assessment
                 if ($options->showgradinggrade and $tr == 0) {
                     $cell = new html_table_cell();
-                    $cell->text = $this->grading_report_grade($participant->gradinggrade);
+                    $cell->text = $this->helper_grading_report_grade($participant->gradinggrade);
                     $cell->rowspan = $numoftrs;
                     $cell->attributes['class'] = 'gradinggrade';
                     $row->cells[] = $cell;
@@ -602,6 +490,112 @@ class mod_workshop_renderer extends plugin_renderer_base {
         return html_writer::table($table);
     }
 
+
+    ////////////////////////////////////////////////////////////////////////////
+    // Internal rendering helper methods
+    ////////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Renders a list of files attached to the submission
+     *
+     * If format==html, then format a html string. If format==text, then format a text-only string.
+     * Otherwise, returns html for non-images and html to display the image inline.
+     *
+     * @param int $submissionid submission identifier
+     * @param string format the format of the returned string - html|text
+     * @return string formatted text to be echoed
+     */
+    protected function helper_submission_attachments($submissionid, $format = 'html') {
+        global $CFG;
+        require_once($CFG->libdir.'/filelib.php');
+
+        $fs     = get_file_storage();
+        $ctx    = $this->page->context;
+        $files  = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid);
+
+        $outputimgs     = '';   // images to be displayed inline
+        $outputfiles    = '';   // list of attachment files
+
+        foreach ($files as $file) {
+            if ($file->is_directory()) {
+                continue;
+            }
+
+            $filepath   = $file->get_filepath();
+            $filename   = $file->get_filename();
+            $fileurl    = file_encode_url($CFG->wwwroot . '/pluginfile.php',
+                                '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true);
+            $type       = $file->get_mimetype();
+            $type       = mimeinfo_from_type('type', $type);
+            $image      = html_writer::empty_tag('img', array('src'=>$this->output->pix_url(file_mimetype_icon($type)), 'alt'=>$type, 'class'=>'icon'));
+
+            $linkhtml   = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename);
+            $linktxt    = "$filename [$fileurl]";
+
+            if ($format == 'html') {
+                if (in_array($type, array('image/gif', 'image/jpeg', 'image/png'))) {
+                    $preview     = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview'));
+                    $preview     = html_writer::tag('a', $preview, array('href' => $fileurl));
+                    $outputimgs .= $this->output->container($preview);
+
+                } else {
+                    $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type));
+                }
+
+            } else if ($format == 'text') {
+                $outputfiles .= $linktxt . PHP_EOL;
+            }
+        }
+
+        if ($format == 'html') {
+            if ($outputimgs) {
+                $outputimgs = $this->output->container($outputimgs, 'images');
+            }
+
+            if ($outputfiles) {
+                $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files'));
+            }
+
+            return $this->output->container($outputimgs . $outputfiles, 'attachments');
+
+        } else {
+            return $outputfiles;
+        }
+    }
+
+    /**
+     * Renders the tasks for the single phase in the user plan
+     *
+     * @param stdClass $tasks
+     * @return string html code
+     */
+    protected function helper_user_plan_tasks(array $tasks) {
+        $out = '';
+        foreach ($tasks as $taskcode => $task) {
+            $classes = '';
+            $icon = null;
+            if ($task->completed === true) {
+                $classes .= ' completed';
+            } elseif ($task->completed === false) {
+                $classes .= ' fail';
+            } elseif ($task->completed === 'info') {
+                $classes .= ' info';
+            }
+            if (is_null($task->link)) {
+                $title = $task->title;
+            } else {
+                $title = html_writer::link($task->link, $task->title);
+            }
+            $title = $this->output->container($title, 'title');
+            $details = $this->output->container($task->details, 'details');
+            $out .= html_writer::tag('li', $title . $details, array('class' => $classes));
+        }
+        if ($out) {
+            $out = html_writer::tag('ul', $out, array('class' => 'tasks'));
+        }
+        return $out;
+    }
+
     /**
      * Renders a text with icons to sort by the given column
      *
@@ -614,7 +608,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
      *
      * @return string
      */
-    protected function sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
+    protected function helper_sortable_heading($text, $sortid=null, $sortby=null, $sorthow=null) {
         global $PAGE;
 
         $out = html_writer::tag('span', $text, array('class'=>'text'));
@@ -639,7 +633,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
      * @param array $userinfo
      * @return string
      */
-    protected function grading_report_participant(stdclass $participant, array $userinfo) {
+    protected function helper_grading_report_participant(stdclass $participant, array $userinfo) {
         $userid = $participant->userid;
         $out  = $this->output->user_picture($userinfo[$userid], array('courseid' => $this->page->course->id, 'size' => 35));
         $out .= html_writer::tag('span', fullname($userinfo[$userid]));
@@ -651,7 +645,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
      * @param stdClass $participant
      * @return string
      */
-    protected function grading_report_submission(stdclass $participant) {
+    protected function helper_grading_report_submission(stdclass $participant) {
         global $CFG;
 
         if (is_null($participant->submissionid)) {
@@ -672,7 +666,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
      * @param string $separator between the grade and the reviewer/author
      * @return string
      */
-    protected function grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
+    protected function helper_grading_report_assessment($assessment, $shownames, array $userinfo, $separator) {
         global $CFG;
 
         if (is_null($assessment)) {
@@ -716,7 +710,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
     /**
      * Formats the aggreagated grades
      */
-    protected function grading_report_grade($grade, $over=null) {
+    protected function helper_grading_report_grade($grade, $over=null) {
         $a = new stdclass();
         $a->grade = is_null($grade) ? get_string('nullgrade', 'workshop') : $grade;
         if (is_null($over)) {
@@ -729,7 +723,7 @@ class mod_workshop_renderer extends plugin_renderer_base {
     }
 
     ////////////////////////////////////////////////////////////////////////////
-    // Helper methods                                                         //
+    // Static helpers
     ////////////////////////////////////////////////////////////////////////////
 
     /**
@@ -764,5 +758,4 @@ class mod_workshop_renderer extends plugin_renderer_base {
             return 'fl';
         }
     }
-
 }
index 9db9bd4..97aee6c 100644 (file)
@@ -215,7 +215,7 @@ case workshop::PHASE_ASSESSMENT:
         $perpage    = 10;           // todo let the user modify this
         $groups     = '';           // todo let the user choose the group
         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
-        $data = $workshop->prepare_grading_report($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
+        $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
         if ($data) {
             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
@@ -233,7 +233,7 @@ case workshop::PHASE_ASSESSMENT:
             $reportopts->showgradinggrade       = false;
 
             echo $output->render($pagingbar);
-            echo $output->grading_report($data, $reportopts);
+            echo $output->render(new workshop_grading_report($data, $reportopts));
             echo $output->render($pagingbar);
         }
     }
@@ -347,7 +347,7 @@ case workshop::PHASE_EVALUATION:
         $perpage    = 10;           // todo let the user modify this
         $groups     = '';           // todo let the user choose the group
         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
-        $data = $workshop->prepare_grading_report($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
+        $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
         if ($data) {
             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
@@ -373,7 +373,7 @@ case workshop::PHASE_EVALUATION:
             $reportopts->showgradinggrade       = true;
 
             echo $output->render($pagingbar);
-            echo $output->grading_report($data, $reportopts);
+            echo $output->render(new workshop_grading_report($data, $reportopts));
             echo $output->render($pagingbar);
         }
     }
@@ -455,7 +455,7 @@ case workshop::PHASE_CLOSED:
         $perpage    = 10;           // todo let the user modify this
         $groups     = '';           // todo let the user choose the group
         $PAGE->set_url($PAGE->url, compact('sortby', 'sorthow', 'page')); // TODO: this is suspicious
-        $data = $workshop->prepare_grading_report($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
+        $data = $workshop->prepare_grading_report_data($USER->id, $groups, $page, $perpage, $sortby, $sorthow);
         if ($data) {
             $showauthornames    = has_capability('mod/workshop:viewauthornames', $workshop->context);
             $showreviewernames  = has_capability('mod/workshop:viewreviewernames', $workshop->context);
@@ -474,7 +474,7 @@ case workshop::PHASE_CLOSED:
 
             print_collapsible_region_start('', 'workshop-viewlet-gradereport', get_string('gradesreport', 'workshop'));
             echo $output->render($pagingbar);
-            echo $output->grading_report($data, $reportopts);
+            echo $output->render(new workshop_grading_report($data, $reportopts));
             echo $output->render($pagingbar);
             print_collapsible_region_end();
         }