MDL-18229 gradebook: Links to Single view via icons on Grade Report.
[moodle.git] / grade / report / singleview / lib.php
CommitLineData
7bd4a361
ZD
1<?php
2
3///////////////////////////////////////////////////////////////////////////
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25require_once($CFG->dirroot . '/grade/report/lib.php');
d124a60b 26require_once($CFG->dirroot . '/grade/report/singleview/classes/lib.php');
7bd4a361 27
d124a60b 28class grade_report_singleview extends grade_report {
7bd4a361
ZD
29
30 public static function valid_screens() {
31 $screendir = dirname(__FILE__) . '/screens';
32
33 $is_valid = function($filename) use ($screendir) {
34 if (preg_match('/^\./', $filename)){
35 return false;
36 }
37
38 $file = $screendir . '/' . $filename;
39
40 if (is_file($file)) {
41 return false;
42 }
43
44 $plugin = $file . '/lib.php';
45 return file_exists($plugin);
46 };
47
48 return array_filter(scandir($screendir), $is_valid);
49 }
50
51 public static function classname($screen) {
52 $screendir = dirname(__FILE__) . '/screens/' . $screen;
53
54 require_once $screendir . '/lib.php';
55
d124a60b 56 return 'singleview_' . $screen;
7bd4a361
ZD
57 }
58
59 public static function filters() {
d124a60b 60 $classnames = array('grade_report_singleview', 'classname');
7bd4a361
ZD
61 $classes = array_map($classnames, self::valid_screens());
62
63 $screens = array_filter($classes, function($screen) {
64 return method_exists($screen, 'filter');
65 });
66
67 return function($item) use ($screens) {
68 $reduced = function($in, $screen) use ($item) {
69 return $in && $screen::filter($item);
70 };
71
72 return array_reduce($screens, $reduced, true);
73 };
74 }
75
76 function process_data($data) {
77 return $this->screen->process($data);
78 }
79
80 function process_action($target, $action) {
81 }
82
83 function _s($key, $a = null) {
d124a60b 84 return get_string($key, 'gradereport_singleview', $a);
7bd4a361
ZD
85 }
86
87 function __construct($courseid, $gpr, $context, $itemtype, $itemid, $groupid=null) {
88 parent::__construct($courseid, $gpr, $context);
89
90 $class = self::classname($itemtype);
91
92 $this->screen = new $class($courseid, $itemid, $groupid);
93
94 // TODO update events to new model
95 qe_events_trigger($class . '_instantiated', $this->screen);
96
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
d124a60b 120function grade_report_singleview_profilereport($course, $user) {
7bd4a361
ZD
121 global $CFG, $OUTPUT;
122
123 if (!function_exists('grade_report_user_profilereport')) {
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) {
136 grade_report_user_profilereport($course, $user);
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
d124a60b 145 $report = new grade_report_singleview($course->id, $gpr, $context, 'user', $user->id);
7bd4a361
ZD
146
147 echo $OUTPUT->heading($report->screen->heading());
148 echo $report->output();
149 }
150}
151
152/**
153 * qe_events_trigger hack for using legacy events without debug screaming at us
154 */
155function qe_events_trigger($eventname, $eventdata) {
156 if (function_exists('events_trigger_legacy')) {
157 events_trigger_legacy($eventname, $eventdata);
158 } else {
159 events_trigger($eventname, $eventdata);
160 }
161}