-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
120 lines (113 loc) · 4.11 KB
/
view.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
<?php
require_once('../../config.php');
require_once('nsreas_form.php');
global $USER, $DB, $OUTPUT, $PAGE, $COURSE;
$courseid = required_param('courseid', PARAM_INT);
$blockid = required_param('blockid', PARAM_INT);
$id = optional_param('id', 0, PARAM_INT);
$component = required_param('component', PARAM_RAW);
$contextid = required_param('context_id',PARAM_INT);
if (!$course = $DB->get_record('course', array('id' => $courseid)))
{
print_error('invalidcourse', 'block_nsreas', $courseid);
}
require_login($course);
$PAGE->set_url('/blocks/nsreas/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('edithtml', 'block_nsreas'));
$PAGE->set_title('Nuevo Rea');
$PAGE->set_context(context::instance_by_id($contextid));
$itemid = new DateTime();
if($id!=0)
{
$ay=$DB->get_record('block_nsreas', array('id' => $id));
$iditem=$ay->item_id;
}
else
{
$iditem=$itemid->getTimestamp();
}
$simplehtml = new nsreas_form(null, array('itemid'=>$iditem,'contextid'=>$contextid));
$toform['blockid'] = $blockid;
$toform['courseid'] = $courseid;
$toform['component'] = $component;
$toform['id'] = $id;
$toform['status']=1;
$toform['context_id']=$contextid;
$simplehtml->set_data($toform);
$url = new moodle_url('/course/view.php', array('id' => $courseid));
if($simplehtml->is_cancelled()) {
redirect($url);
}
else if ($simplehtml->get_data())
{
$fromform=$simplehtml->get_data();
$name = $simplehtml->get_new_filename('attachment');
$simplehtml->save_stored_file('attachment',$contextid,'block_nsreas','draft',$iditem,'/',$name,true,$USER->id);
$fromform->file_name=$name;
$fromform->item_id=$iditem;
$fromform->context_id=$contextid;
if($fromform->linkurl!=null)
{
if (strpos($fromform->linkurl, 'http')===false){
//print_object(strpos($fromform->linkurl, 'http'));
$fromform->linkurl = 'http://'.$fromform->linkurl;
}
}
//------------------
if ($fromform->id != 0)
{
$fs = get_file_storage();
$oldentry = $DB->get_record('block_nsreas',array('id'=>$fromform->id));
// Prepare file record object
//print_object($oldentry);
$fileinfo = array(
'component' => 'block_nsreas',
'filearea' => 'draft', // usually = table name
'itemid' => $oldentry->item_id, // usually = ID of row in table
'contextid' => $oldentry->context_id, // ID of context
'filepath' => '/', // any path beginning and ending in /
'filename' => $oldentry->file_name); // any filename
// Get file
if($fileinfo['itemid']!=$fromform->item_id){
$file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],
$fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);
if($file){
$hash = $file->get_contenthash();
$file->delete();
$filestable = 'files';
$DB->delete_records($filestable, array('contenthash'=>$hash));
}
}
if (!$DB->update_record('block_nsreas', $fromform)) {
print_error('updateerror', 'block_nsreas');
}else{
}
// if (!$DB->update_record('block_nsreas', $fromform)) {
// print_error('updateerror', 'block_nsreas');
// }
}else
{
$fromform->click_count=0;
if (!$DB->insert_record('block_nsreas', $fromform)) {
print_error('inserterror', 'block_nsreas');
}
}
redirect($url);
}
else
{
$site = get_site();
echo $OUTPUT->header();
if ($id)
{
$simplehtmlpage = $DB->get_record('block_nsreas', array('id' => $id));
$simplehtml->set_data($simplehtmlpage);
$simplehtml->display();
}
else
{
$simplehtml->display();
}
}
echo $OUTPUT->footer();