MDL-18229 gradereport: updated index.php test to calculate navigation based on new...
[moodle.git] / grade / report / singleview / lib.php
CommitLineData
7bd4a361
ZD
1<?php
2
65c134f9
ZD
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/>.
17
18/**
19 * Base lib class for singleview functionality.
20 *
21 * @package gradereport_singleview
8ec7b088 22 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
65c134f9
ZD
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
7bd4a361
ZD
25
26require_once($CFG->dirroot . '/grade/report/lib.php');
d124a60b 27require_once($CFG->dirroot . '/grade/report/singleview/classes/lib.php');
7bd4a361 28
8ec7b088 29class gradereport_singleview extends grade_report {
7bd4a361
ZD
30
31 public static function valid_screens() {
32 $screendir = dirname(__FILE__) . '/screens';
33
34 $is_valid = function($filename) use ($screendir) {
35 if (preg_match('/^\./', $filename)){
36 return false;
37 }
38
39 $file = $screendir . '/' . $filename;
40
41 if (is_file($file)) {
42 return false;
43 }
44
45 $plugin = $file . '/lib.php';
46 return file_exists($plugin);
47 };
48
49 return array_filter(scandir($screendir), $is_valid);
50 }
51
52 public static function classname($screen) {
53 $screendir = dirname(__FILE__) . '/screens/' . $screen;
54
55 require_once $screendir . '/lib.php';
56
8ec7b088 57 return 'gradereport_singleview_' . $screen;
7bd4a361
ZD
58 }
59
60 public static function filters() {
8ec7b088 61 $classnames = array('gradereport_singleview', 'classname');
7bd4a361
ZD
62 $classes = array_map($classnames, self::valid_screens());
63
64 $screens = array_filter($classes, function($screen) {
65 return method_exists($screen, 'filter');
66 });
67
68 return function($item) use ($screens) {
69 $reduced = function($in, $screen) use ($item) {
70 return $in && $screen::filter($item);
71 };
72
73 return array_reduce($screens, $reduced, true);
74 };
75 }
76
77 function process_data($data) {
78 return $this->screen->process($data);
79 }
80
81 function process_action($target, $action) {
82 }
83
84 function _s($key, $a = null) {
d124a60b 85 return get_string($key, 'gradereport_singleview', $a);
7bd4a361
ZD
86 }
87
88 function __construct($courseid, $gpr, $context, $itemtype, $itemid, $groupid=null) {
89 parent::__construct($courseid, $gpr, $context);
90
91 $class = self::classname($itemtype);
92
93 $this->screen = new $class($courseid, $itemid, $groupid);
94
7bd4a361
ZD
95 // Load custom or predifined js
96 $this->screen->js();
97
d124a60b 98 $base = '/grade/report/singleview/index.php';
7bd4a361
ZD
99
100 $id_params = array('id' => $courseid);
101
102 $this->baseurl = new moodle_url($base, $id_params);
103
104 $this->pbarurl = new moodle_url($base, $id_params + array(
105 'item' => $itemtype,
106 'itemid' => $itemid
107 ));
108
109 $this->setup_groups();
110 }
111
112 function output() {
113 global $OUTPUT;
114 return $OUTPUT->box($this->screen->html());
115 }
116}
117
8ec7b088 118function gradereport_singleview_profilereport($course, $user) {
7bd4a361
ZD
119 global $CFG, $OUTPUT;
120
8ec7b088 121 if (!function_exists('gradereport_user_profilereport')) {
7bd4a361
ZD
122 require_once $CFG->dirroot . '/grade/report/user/lib.php';
123 }
124
125 $context = context_course::instance($course->id);
126
127 $can_use = (
d124a60b 128 has_capability('gradereport/singleview:view', $context) and
7bd4a361
ZD
129 has_capability('moodle/grade:viewall', $context) and
130 has_capability('moodle/grade:edit', $context)
131 );
132
133 if (!$can_use) {
8ec7b088 134 gradereport_user_profilereport($course, $user);
7bd4a361
ZD
135 } else {
136 $gpr = new grade_plugin_return(array(
137 'type' => 'report',
d124a60b 138 'plugin' => 'singleview',
7bd4a361
ZD
139 'courseid' => $course->id,
140 'userid' => $user->id
141 ));
142
8ec7b088 143 $report = new gradereport_singleview($course->id, $gpr, $context, 'user', $user->id);
7bd4a361
ZD
144
145 echo $OUTPUT->heading($report->screen->heading());
146 echo $report->output();
147 }
148}