mirrored from git://git.moodle.org/moodle.git
/
ajaxlib.php
191 lines (165 loc) · 8.27 KB
/
ajaxlib.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?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/>.
/**
* Library functions to facilitate the use of ajax JavaScript in Moodle.
*
* @package moodlecore
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* You need to call this function if you wish to use the set_user_preference
* method in javascript_static.php, to white-list the preference you want to update
* from JavaScript, and to specify the type of cleaning you expect to be done on
* values.
*
* @param string $name the name of the user_perference we should allow to be
* updated by remote calls.
* @param integer $paramtype one of the PARAM_{TYPE} constants, user to clean
* submitted values before set_user_preference is called.
* @return void
*/
function user_preference_allow_ajax_update($name, $paramtype) {
global $USER, $PAGE;
// Make sure that the required JavaScript libraries are loaded.
$PAGE->requires->yui2_lib('connection');
// Record in the session that this user_preference is allowed to updated remotely.
$USER->ajax_updatable_user_prefs[$name] = $paramtype;
}
/**
* Returns whether ajax is enabled/allowed or not.
* @param array $browsers optional list of alowed browsers, empty means use default list
* @return bool
*/
function ajaxenabled(array $browsers = null) {
global $CFG, $USER;
if (!empty($browsers)) {
$valid = false;
foreach ($browsers as $brand => $version) {
if (check_browser_version($brand, $version)) {
$valid = true;
}
}
if (!$valid) {
return false;
}
}
$ie = check_browser_version('MSIE', 6.0);
$ff = check_browser_version('Gecko', 20051106);
$op = check_browser_version('Opera', 9.0);
$sa = check_browser_version('Safari', 412);
$ch = check_browser_version('Chrome', 6);
if (!$ie && !$ff && !$op && !$sa && !$ch) {
/** @see http://en.wikipedia.org/wiki/User_agent */
// Gecko build 20051107 is what is in Firefox 1.5.
// We still have issues with AJAX in other browsers.
return false;
}
if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
return true;
} else {
return false;
}
}
// ==============================================================================
// TODO: replace this with something more up-to-date with our coding standards
/**
* Used to create view of document to be passed to JavaScript on pageload.
* We use this class to pass data from PHP to JavaScript.
*/
class jsportal {
var $currentblocksection = null;
var $blocks = array();
/**
* Takes id of block and adds it
*/
function block_add($id, $hidden=false){
$hidden_binary = 0;
if ($hidden) {
$hidden_binary = 1;
}
$this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
}
/**
* Prints the JavaScript code needed to set up AJAX for the course.
*/
function print_javascript($courseid, $return=false) {
global $CFG, $USER, $OUTPUT, $COURSE, $DB;
$blocksoutput = $output = '';
for ($i=0; $i<count($this->blocks); $i++) {
$blocksoutput .= "['".$this->blocks[$i][0]."',
'".$this->blocks[$i][1]."',
'".$this->blocks[$i][2]."']";
if ($i != (count($this->blocks) - 1)) {
$blocksoutput .= ',';
}
}
$output .= "<script type=\"text/javascript\">\n";
$output .= " main.portal.id = ".$courseid.";\n";
$output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
$output .= " main.portal.strings['courseformat']='".$COURSE->format."';\n";
$output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
$output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n";
$output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n";
$output .= " main.portal.numsections = ".$COURSE->numsections.";\n";
$output .= " main.portal.lastsection = ".$DB->get_field_sql("SELECT MAX(section) FROM {course_sections} WHERE course = ?", array($courseid)).";\n"; // needed for orphaned activities in unavailable sections
$output .= " main.portal.strings['hide']='".get_string('hide')."';\n";
$output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n";
$output .= " main.portal.strings['show']='".get_string('show')."';\n";
$output .= " main.portal.strings['delete']='".get_string('delete')."';\n";
$output .= " main.portal.strings['move']='".get_string('move')."';\n";
$output .= " main.portal.strings['movesection']='".get_string('movesection', '', '_var_')."';\n";
$output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
$output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
$output .= " main.portal.strings['update']='".get_string('update')."';\n";
$output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
$output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
$output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
$output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
$output .= " main.portal.strings['deletecheck']='".get_string('deletecheckfull','','_var_')."';\n";
$output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
$output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
$output .= " main.portal.strings['sesskey']='".sesskey()."';\n";
foreach (array_keys(get_plugin_list('mod')) as $modtype) {
$output .= " main.portal.strings['modtype_".$modtype."']='".get_string('pluginname', 'mod_'.$modtype)."';\n";
}
$output .= " main.portal.icons['spacerimg']='".$OUTPUT->pix_url('spacer')."';\n";
$output .= " main.portal.icons['marker']='".$OUTPUT->pix_url('i/marker')."';\n";
$output .= " main.portal.icons['ihide']='".$OUTPUT->pix_url('i/hide')."';\n";
$output .= " main.portal.icons['move_2d']='".$OUTPUT->pix_url('i/move_2d')."';\n";
$output .= " main.portal.icons['show']='".$OUTPUT->pix_url('t/show')."';\n";
$output .= " main.portal.icons['hide']='".$OUTPUT->pix_url('t/hide')."';\n";
$output .= " main.portal.icons['delete']='".$OUTPUT->pix_url('t/delete')."';\n";
$output .= " main.portal.icons['groupn']='".$OUTPUT->pix_url('t/groupn')."';\n";
$output .= " main.portal.icons['groups']='".$OUTPUT->pix_url('t/groups')."';\n";
$output .= " main.portal.icons['groupv']='".$OUTPUT->pix_url('t/groupv')."';\n";
if (right_to_left()) {
$output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/right')."';\n";
$output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/left')."';\n";
} else {
$output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/left')."';\n";
$output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/right')."';\n";
}
$output .= " onloadobj.load();\n";
$output .= " main.process_blocks();\n";
$output .= "</script>";
if ($return) {
return $output;
} else {
echo $output;
}
}
}