MDL-18229 gradebook: Additional fixes.
[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) {
5f3decb5
ZD
78 if (has_capability('moodle/grade:manage', $this->context)) {
79 return $this->screen->process($data);
80 }
7bd4a361
ZD
81 }
82
83 function process_action($target, $action) {
84 }
85
86 function _s($key, $a = null) {
d124a60b 87 return get_string($key, 'gradereport_singleview', $a);
7bd4a361
ZD
88 }
89
90 function __construct($courseid, $gpr, $context, $itemtype, $itemid, $groupid=null) {
91 parent::__construct($courseid, $gpr, $context);
92
93 $class = self::classname($itemtype);
94
95 $this->screen = new $class($courseid, $itemid, $groupid);
96
7bd4a361
ZD
97 // Load custom or predifined js
98 $this->screen->js();
99
d124a60b 100 $base = '/grade/report/singleview/index.php';
7bd4a361
ZD
101
102 $id_params = array('id' => $courseid);
103
104 $this->baseurl = new moodle_url($base, $id_params);
105
106 $this->pbarurl = new moodle_url($base, $id_params + array(
107 'item' => $itemtype,
108 'itemid' => $itemid
109 ));
110
111 $this->setup_groups();
112 }
113
114 function output() {
115 global $OUTPUT;
116 return $OUTPUT->box($this->screen->html());
117 }
118}
119
8ec7b088 120function gradereport_singleview_profilereport($course, $user) {
7bd4a361
ZD
121 global $CFG, $OUTPUT;
122
8ec7b088 123 if (!function_exists('gradereport_user_profilereport')) {
7bd4a361
ZD
124 require_once $CFG->dirroot . '/grade/report/user/lib.php';
125 }
126
127 $context = context_course::instance($course->id);
128
129 $can_use = (
d124a60b 130 has_capability('gradereport/singleview:view', $context) and
7bd4a361
ZD
131 has_capability('moodle/grade:viewall', $context) and
132 has_capability('moodle/grade:edit', $context)
133 );
134
135 if (!$can_use) {
8ec7b088 136 gradereport_user_profilereport($course, $user);
7bd4a361
ZD
137 } else {
138 $gpr = new grade_plugin_return(array(
139 'type' => 'report',
d124a60b 140 'plugin' => 'singleview',
7bd4a361
ZD
141 'courseid' => $course->id,
142 'userid' => $user->id
143 ));
144
8ec7b088 145 $report = new gradereport_singleview($course->id, $gpr, $context, 'user', $user->id);
7bd4a361
ZD
146
147 echo $OUTPUT->heading($report->screen->heading());
148 echo $report->output();
149 }
150}