MDL-35740 Assignment: Change the assignment index page to use a renderer.
authorDamyon Wiese <damyon@moodle.org>
Tue, 20 Nov 2012 06:56:03 +0000 (14:56 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 18 Dec 2012 00:44:53 +0000 (08:44 +0800)
This also cleans up the patch to prevent students from seeing the number of submissions for the assignment.

mod/assign/index.php
mod/assign/lang/en/assign.php
mod/assign/locallib.php
mod/assign/renderable.php
mod/assign/renderer.php

index f54f04f..3b40740 100644 (file)
@@ -24,8 +24,8 @@
 
 require_once("../../config.php");
 require_once($CFG->dirroot.'/mod/assign/locallib.php');
-
-$id = required_param('id', PARAM_INT); // Course ID
+// For this type of page this is the course id.
+$id = required_param('id', PARAM_INT);
 
 $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
 require_login($course);
@@ -36,108 +36,16 @@ add_to_log($course->id, "assign", "view all", "index.php?id=$course->id", "");
 
 // Print the header
 $strplural = get_string("modulenameplural", "assign");
-$strsectionname  = get_string('sectionname', 'format_'.$course->format);
-$strduedate = get_string("duedate", "assign");
-$strsubmission = get_string("submission", "assign");
-$strgrade = get_string("grade");
 $PAGE->navbar->add($strplural);
 $PAGE->set_title($strplural);
 $PAGE->set_heading($course->fullname);
 echo $OUTPUT->header();
 
-if (!$cms = get_coursemodules_in_course('assign', $course->id, 'cm.idnumber, m.duedate')) {
-    notice(get_string('thereareno', 'moodle', $strplural), new moodle_url('/course/view.php', array('id' => $course->id)));
-    die;
-}
-
-$usesections = course_format_uses_sections($course->format);
-if ($usesections) {
-    $sections = get_all_sections($course->id);
-}
-
-$timenow = time();
-
-// Check if we need the closing date header
-$table = new html_table();
-if ($usesections) {
-    $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
-    $table->align = array ('left', 'left', 'center', 'right', 'right');
-} else {
-    $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
-    $table->align = array ('left', 'left', 'center', 'right');
-}
-
-$currentsection = "";
-$table->data = array();
-$modinfo = get_fast_modinfo($course);
-foreach ($modinfo->instances['assign'] as $cm) {
-    if (!$cm->uservisible) {
-        continue;
-    }
-
-    $cm->timedue        = $cms[$cm->id]->duedate;
-    $cm->idnumber       = $cms[$cm->id]->idnumber;
-
-    $link = html_writer::link(new moodle_url('/mod/assign/view.php', array('id' => $cm->id)), $cm->name);
-
-    $printsection = "";
-    if ($usesections) {
-        if ($cm->sectionnum !== $currentsection) {
-            if ($cm->sectionnum) {
-                $printsection = get_section_name($course, $sections[$cm->sectionnum]);
-            }
-            if ($currentsection !== "") {
-                $table->data[] = 'hr';
-            }
-            $currentsection = $cm->sectionnum;
-        }
-    }
-
-    $submitted = "";
-    $cmod = get_coursemodule_from_instance('assign', $cm->instance, 0, false, MUST_EXIST);
-    $context = context_module::instance($cmod->id);
-    $assignment = new assign($context, $cmod, $course);
-    if (has_capability('mod/assign:grade', $context)) {
-        $params = array('assignment' => $cm->instance, 'status' => ASSIGN_SUBMISSION_STATUS_SUBMITTED);
-        $submissioncount = $DB->count_records('assign_submission', $params);
-        if ($submissioncount == 1) {
-            $submitted = html_writer::link(new moodle_url('/mod/assign/view.php', array('id' => $assignment->get_course_module()->id,
-                                'action' => 'grading')), $submissioncount . " " . get_string('submission', 'assign'));
-        } else if ($submissioncount > 1) {
-            $submitted = html_writer::link(new moodle_url('/mod/assign/view.php', array('id' => $assignment->get_course_module()->id,
-                                'action' => 'grading')), $submissioncount . " " . get_string('submissions', 'assign'));
-        } else {
-            $submitted = $submissioncount;
-        }
-    } else if(has_capability('mod/assign:submit', $context)) {
-        $submissionstatus = $assignment->get_user_submission($USER->id, false);
-        if (!empty($submissionstatus->status)) {
-            $submitted = get_string('submissionstatus_' . $submissionstatus->status, 'assign');
-        } else {
-            $submitted = get_string('submissionstatus_', 'assign');
-        }
-    } else {
-        $submitted = new html_table_cell(get_string('nopermission', 'assign'));
-        $submitted->attributes = array('class'=> 'submittedlate');
-    }
-    $grading_info = grade_get_grades($course->id, 'mod', 'assign', $cm->instance, $USER->id);
-    if (isset($grading_info->items[0]) && !$grading_info->items[0]->grades[$USER->id]->hidden ) {
-        $grade = $grading_info->items[0]->grades[$USER->id]->str_grade;
-    }
-    else {
-        $grade = '-';
-    }
-
-    $due = $cm->timedue ? userdate($cm->timedue) : '-';
+$context = context_course::instance($course->id);
 
-    if ($usesections) {
-        $row = array ($printsection, $link, $due, $submitted, $grade);
-    } else {
-        $row = array ($link, $due, $submitted, $grade);
-    }
+require_capability('mod/assign:view', $context);
 
-    $table->data[] = $row;
+$assign = new assign($context, null, $course);
 
-}
-echo html_writer::table($table);
-echo $OUTPUT->footer();
+// Get the assign to render the page.
+echo $assign->view('viewcourseindex');
index 1e169c5..ef1c7ed 100644 (file)
@@ -190,7 +190,6 @@ $string['modulename_link'] = 'mod/assignment/view';
 $string['modulenameplural'] = 'Assignments';
 $string['mysubmission'] = 'My submission: ';
 $string['newsubmissions'] = 'Assignments submitted';
-$string['nopermission'] = 'No permission';
 $string['nofiles'] = 'No files. ';
 $string['nograde'] = 'No grade. ';
 $string['nolatesubmissions'] = 'No late submissions accepted. ';
index 3701d08..6163841 100644 (file)
@@ -417,6 +417,8 @@ class assign {
             $o .= $this->view_plugin_grading_batch_operation($mform);
         } else if ($action == 'viewpluginpage') {
              $o .= $this->view_plugin_page();
+        } else if ($action == 'viewcourseindex') {
+             $o .= $this->view_course_index();
         } else {
             $o .= $this->view_submission_page();
         }
@@ -1687,7 +1689,84 @@ class assign {
     }
 
     /**
-     * View a page rendered by a plugin
+     * View a summary listing of all assignments in the current course.
+     *
+     * @return string
+     */
+    private function view_course_index() {
+        global $USER;
+
+        $o = '';
+
+        $course = $this->get_course();
+        $strplural = get_string('modulenameplural', 'assign');
+
+        if (!$cms = get_coursemodules_in_course('assign', $course->id, 'm.duedate')) {
+            $o .= $this->get_renderer()->notification(get_string('thereareno', 'moodle', $strplural));
+            $o .= $this->get_renderer()->continue_button(new moodle_url('/course/view.php', array('id' => $course->id)));
+            return $o;
+        }
+
+        $strsectionname  = get_string('sectionname', 'format_'.$course->format);
+        $usesections = course_format_uses_sections($course->format);
+        $modinfo = get_fast_modinfo($course);
+
+        if ($usesections) {
+            $sections = $modinfo->get_section_info_all();
+        }
+        $courseindexsummary = new assign_course_index_summary($usesections, $strsectionname);
+
+        $timenow = time();
+
+        $currentsection = '';
+        foreach ($modinfo->instances['assign'] as $cm) {
+            if (!$cm->uservisible) {
+                continue;
+            }
+
+            $timedue        = $cms[$cm->id]->duedate;
+
+            $sectionname = '';
+            if ($usesections && $cm->sectionnum) {
+                $sectionname = get_section_name($course, $sections[$cm->sectionnum]);
+            }
+
+            $submitted = '';
+            $context = context_module::instance($cm->id);
+
+            $assignment = new assign($context, $cm, $course);
+
+            if (has_capability('mod/assign:grade', $context)) {
+                $submitted = $assignment->count_submissions_with_status(ASSIGN_SUBMISSION_STATUS_SUBMITTED);
+
+            } else if (has_capability('mod/assign:submit', $context)) {
+                $usersubmission = $assignment->get_user_submission($USER->id, false);
+
+                if (!empty($usersubmission->status)) {
+                    $submitted = get_string('submissionstatus_' . $usersubmission->status, 'assign');
+                } else {
+                    $submitted = get_string('submissionstatus_', 'assign');
+                }
+            }
+            $grading_info = grade_get_grades($course->id, 'mod', 'assign', $cm->instance, $USER->id);
+            if (isset($grading_info->items[0]) && !$grading_info->items[0]->grades[$USER->id]->hidden ) {
+                $grade = $grading_info->items[0]->grades[$USER->id]->str_grade;
+            } else {
+                $grade = '-';
+            }
+
+            $courseindexsummary->add_assign_info($cm->id, $cm->name, $sectionname, $timedue, $submitted, $grade);
+
+        }
+
+        $o .= $this->get_renderer()->render($courseindexsummary);
+        $o .= $this->view_footer();
+
+        return $o;
+    }
+
+    /**
+     * View a page rendered by a plugin.
      *
      * Uses url parameters 'pluginaction', 'pluginsubtype', 'plugin', and 'id'
      *
index 986f1da..2875b78 100644 (file)
@@ -504,6 +504,55 @@ class assign_grading_summary implements renderable {
 
 }
 
+/**
+ * Renderable course index summary
+ * @package   mod_assign
+ * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class assign_course_index_summary implements renderable {
+    /** @var array assignments - A list of course module info and submission counts or statuses */
+    public $assignments = array();
+    /** @var boolean usesections - Does this course format support sections? */
+    public $usesections = false;
+    /** @var string courseformat - The current course format name */
+    public $courseformatname = '';
+
+    /**
+     * constructor
+     *
+     * @param $usesections boolean - True if this course format uses sections
+     * @param $courseformatname string - The id of this course format
+     */
+    public function __construct($usesections, $courseformatname) {
+        $this->usesections = $usesections;
+        $this->courseformatname = $courseformatname;
+    }
+
+    /**
+     * Add a row of data to display on the course index page
+     *
+     * @param int $cmid - The course module id for generating a link
+     * @param string $cmname - The course module name for generating a link
+     * @param string $sectionname - The name of the course section (only if $usesections is true)
+     * @param int $timedue - The due date for the assignment - may be 0 if no duedate
+     * @param string $submissioninfo - A string with either the number of submitted assignments, or the
+     *                                 status of the current users submission depending on capabilities.
+     * @param string $gradeinfo - The current users grade if they have been graded and it is not hidden.
+     */
+    public function add_assign_info($cmid, $cmname, $sectionname, $timedue, $submissioninfo, $gradeinfo) {
+        $this->assignments[] = array('cmid'=>$cmid,
+                               'cmname'=>$cmname,
+                               'sectionname'=>$sectionname,
+                               'timedue'=>$timedue,
+                               'submissioninfo'=>$submissioninfo,
+                               'gradeinfo'=>$gradeinfo);
+    }
+
+
+}
+
+
 /**
  * An assign file class that extends rendererable class and is used by the assign module.
  *
index c8b83e5..ed1a972 100644 (file)
@@ -788,6 +788,64 @@ class mod_assign_renderer extends plugin_renderer_base {
         return $o;
     }
 
+    /**
+     * Render a course index summary
+     *
+     * @param assign_course_index_summary $indexsummary
+     * @return string
+     */
+    public function render_assign_course_index_summary(assign_course_index_summary $indexsummary) {
+        $o = '';
+
+        $strplural = get_string('modulenameplural', 'assign');
+        $strsectionname  = $indexsummary->courseformatname;
+        $strduedate = get_string('duedate', 'assign');
+        $strsubmission = get_string('submission', 'assign');
+        $strgrade = get_string('grade');
+
+        $table = new html_table();
+        if ($indexsummary->usesections) {
+            $table->head  = array ($strsectionname, $strplural, $strduedate, $strsubmission, $strgrade);
+            $table->align = array ('left', 'left', 'center', 'right', 'right');
+        } else {
+            $table->head  = array ($strplural, $strduedate, $strsubmission, $strgrade);
+            $table->align = array ('left', 'left', 'center', 'right');
+        }
+        $table->data = array();
+
+        $currentsection = '';
+        foreach ($indexsummary->assignments as $info) {
+            $params = array('id' => $info['cmid']);
+            $link = html_writer::link(new moodle_url('/mod/assign/view.php', $params),
+                                      $info['cmname']);
+            $due = $info['timedue'] ? userdate($info['timedue']) : '-';
+
+            $printsection = '';
+            if ($indexsummary->usesections) {
+                if ($info['sectionname'] !== $currentsection) {
+                    if ($info['sectionname']) {
+                        $printsection = $info['sectionname'];
+                    }
+                    if ($currentsection !== '') {
+                        $table->data[] = 'hr';
+                    }
+                    $currentsection = $info['sectionname'];
+                }
+            }
+
+            if ($indexsummary->usesections) {
+                $row = array($printsection, $link, $due, $info['submissioninfo'], $info['gradeinfo']);
+            } else {
+                $row = array($link, $due, $info['submissioninfo'], $info['gradeinfo']);
+            }
+            $table->data[] = $row;
+        }
+
+        $o .= html_writer::table($table);
+
+        return $o;
+    }
+
 
 
     /**