-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmarks.php
128 lines (108 loc) · 4.75 KB
/
webmarks.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
<?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/>.
/**
* Script to manage the links for the Webmarks (Website Bookmarks) block
*
* This code fragment is called by moodle_needs_upgrading() and
* /admin/index.php
*
* @package block
* @subpackage webmarks
* @copyright 2012 Nottingham University
* @author Benjamin Ellis - [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
require_once('webmarks_form.php');
require_once('lib.php');
global $CFG, $DB, $PAGE;
$errormessage = '';
require_login();
$courseid = required_param('courseid',PARAM_INT);
//need this later to set the course
$thiscourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); //throws an exception???
//should really check for the following 2 existance but cycles are expensive
$blockid = required_param('blockid',PARAM_INT);
//$thisblock = $DB->get_record('block', array('id' => $blockid), '*', MUST_EXIST); //throws an exception???
$webmarkid = optional_param('webmarkid',0,PARAM_INT);
$theaction = optional_param('action', '', PARAM_ALPHA);
/* All the following is required for all types of action */
$context = get_context_instance(CONTEXT_COURSE, $courseid);
//fix up for the page display
$PAGE->set_course($thiscourse);
$PAGE->set_url('/blocks/webmarks/webmarks.php');
$PAGE->set_heading($SITE->fullname);
$PAGE->set_pagelayout('course');
$PAGE->set_title(get_string('webmarkstitle', 'block_webmarks'));
$PAGE->navbar->add(get_string('webmarkstitle', 'block_webmarks'));
//javascript stuff
//not sure what this does but it appears to instruct Moodle to load the relevant YUI classes
$jsmodule = array(
'name' => 'block_webmarks',
'fullpath' => '/blocks/webmarks/module.js',
'requires' => array('base', 'node')
);
//load and initialise the js
$PAGE->requires->js_init_call('M.block_webmarks.init',array(),false,$jsmodule);
//set up the form - Get form default/current values
$formdata = array();
$formdata['courseid'] = $courseid;
$formdata['blockid'] = $blockid;
$formdata['id'] = $webmarkid;
$formdata['wbm_title'] = optional_param('wmtitle', null, PARAM_TEXT);
$formdata['wbm_desc'] = optional_param('wmdesc', null, PARAM_TEXT);
$formdata['wbm_link'] = optional_param('wmlink', null, PARAM_URL);
if ($theaction == 'editing' && $webmarkid) {
//here we are editing - this bit is normally done by ajax if JS is switched on - otherwise it has to be done here
if ($webmark = getWebmarkByID($webmarkid)) {
$formdata['wbm_title'] = $webmark->wbm_title;
$formdata['wbm_desc'] = $webmark->wbm_desc;
$formdata['wbm_link'] = $webmark->wbm_link;
} //else we should throw an error
}elseif ($theaction == 'deleting' && $webmarkid) {
$DB->delete_records('block_webmarks',array('id'=>$webmarkid));
}elseif ($theaction == 'saving' && (optional_param('save',0,PARAM_RAW)) ) { //MAKE SURE WE HAVE SUBMITTED NOT CANCELLED
//check we have a valid URL for the link - moodle would have replaced it with '' if it was not a valid URL because we specified PARAM_URL
//NO IT DOES NOT SOMETHING NOT RIGHT HERE
if ($formdata['wbm_link']) {
//the record information is already set in $formdata
$updrecord = (object) $formdata;
if ($webmarkid) {
$DB->update_record('block_webmarks',$updrecord);
$formdata['webmarkid'] = 0; //reset recordid
}else {
$DB->insert_record('block_webmarks',$updrecord);
}
//now ensure we have a clear form for the next transaction
$formdata['wbm_title'] = '';
$formdata['wbm_desc'] = '';
$formdata['wbm_link'] = '';
}else{
$errormessage = 'Please specificy a valid URL for this weblink';
}
}
//get the existing records ready for editing
$formdata['existingrecs'] = $DB->get_records('block_webmarks',array('blockid'=>$blockid),'wbm_title');
$wmform = new webmarks_form(null,$formdata);
//display the editing page
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('wmeditingtitle', 'block_webmarks'), 3, 'main');
$wmform->display();
if (!empty($errormessage)) {
echo $errormessage;
}
echo $OUTPUT->footer();
?>