forked from marxjohnson/moodle-local_progressreview
/
course_selector.php
150 lines (138 loc) · 6.16 KB
/
course_selector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the course selector fields used for adding reviews to courses
*
* By overriding and massaging user_selector_base, the class is repurposed to allow selecting
* of courses instead of users
*
* @package local_progressreview
* @copyright 2011 Taunton's College, UK
* @author Mark Johnson <mark.johnson@tauntons.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once($CFG->dirroot.'/local/progressreview/lib.php');
require_once($CFG->dirroot.'/user/selector/lib.php');
/**
* Bastardises the user_selector to allow selection of courses to add reviews to
*/
class progressreview_potential_course_selector extends user_selector_base {
/**
* Add the file name to the $options array to make AJAX searching work
* @return array
*/
protected function get_options() {
$options = parent::get_options();
$options['file'] = 'local/progressreview/course_selector.php';
return $options;
}
/**
* Returns the courses matching the search term
*
* @param string $search
* @return array 2D array of optgroup => options for select list
*/
public function find_users($search = '') {
global $DB;
$options = array();
$categories = $DB->get_records('course_categories');
foreach ($categories as $category) {
$where = 'category = ?';
$params = array($category->id);
if (!empty($search)) {
$shortnamelike = $DB->sql_like('shortname', '?');
$fullnamelike = $DB->sql_like('fullname', '?');
$where .= 'AND ('.$shortnamelike.' OR '.$fullnamelike.') ';
$params = array_merge($params, array('%'.$search.'%', '%'.$search.'%'));
}
if (!empty($this->exclude)) {
list($not_in_sql, $not_in_params) = $DB->get_in_or_equal($this->exclude, SQL_PARAMS_QM, '', false);
$where .= 'AND id '.$not_in_sql.' ';
$params = array_merge($params, $not_in_params);
}
$fields = 'id, shortname AS lastname, "" AS firstname, fullname AS email';
$courses = $DB->get_records_select('course', $where, $params, 'shortname', $fields);
$options[$category->name] = $courses;
}
return $options;
}
}
/**
* Bastardises the user_selector to allow selection of courses which have had reviews generated
*/
class progressreview_distributed_course_selector extends progressreview_potential_course_selector {
/**
* The ID of the session we're looking for reviews in
*
* @var int
*/
private $sessionid;
/**
* The type of review that we're looking for
*
* @var int
*/
private $reviewtype;
/**
* Constructor, does initialisation then defines session ID and review type
*
* @param string $name
* @param int $sessionid
* @param string $reviewtype
* @param array $extraoptions
*/
public function __construct($name, $sessionid, $reviewtype = PROGRESSREVIEW_SUBJECT, $extraoptions = array()) {
parent::__construct($name, $extraoptions);
$this->sessionid = $sessionid;
$this->reviewtype = $reviewtype;
}
/**
* Returns the courses matching the search term with reviews of the defined type in the defined session
*
* @param string $search
* @return array 2D array of optgroup => options for select list
*/
public function find_users($search = '') {
global $DB;
$options = array(get_string('courseswithreviews', 'local_progressreview') => array());
$reviewcourses = $DB->get_records_sql('SELECT DISTINCT courseid FROM {progressreview} WHERE sessionid = ? AND reviewtype = ?', array($this->sessionid, $this->reviewtype));
foreach($reviewcourses as $reviewcourse) {
$select = 'SELECT c.id, c.shortname AS lastname, "" AS firstname, c.fullname AS email, cc.name AS category ';
$from = 'FROM {course} c JOIN {course_categories} cc on c.category = cc.id ';
$where = 'WHERE c.id = ?';
$params = array($reviewcourse->courseid);
if (!empty($search)) {
$shortnamelike = $DB->sql_like('shortname', '?');
$fullnamelike = $DB->sql_like('fullname', '?');
$where .= 'AND ('.$shortnamelike.' OR '.$fullnamelike.') ';
$params = array_merge($params, array('%'.$search.'%', '%'.$search.'%'));
}
if (!empty($this->exclude)) {
list($not_in_sql, $not_in_params) = $DB->get_in_or_equal($this->exclude, SQL_PARAMS_QM, '', false);
$where .= 'AND id '.$not_in_sql.' ';
$params = array_merge($params, $not_in_params);
}
if ($course = $DB->get_record_sql($select.$from.$where, $params)) {
if (!isset($options[$course->category])) {
$options[$course->category] = array();
}
$options[$course->category][$course->id] = $course;
}
}
return $options;
}
}