-
Notifications
You must be signed in to change notification settings - Fork 2
/
addlocaltocourse.php
214 lines (183 loc) · 7.46 KB
/
addlocaltocourse.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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/>.
/**
*
* @package mod_sharedresource
* @author Valery Fremaux <[email protected]>
* @copyright Valery Fremaux (activeprolearn.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
/*
* TODO : clarify doc.
* this action screen allows adding a sharedresource from an external browse or search result
* directly in the current course and the resource results being already known as a local proxy, or
* it is a locally stored resource.
* This possibility will only be available when
* external resource repositories are queried from a course starting context.
* Adding local resource should always provide identifier.
*/
use mod_sharedresource\entry_factory;
use mod_sharedresource\entry;
use mod_sharedresource\entry_extended;
require('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/mod/sharedresource/lib.php');
require_once($CFG->dirroot.'/mod/sharedresource/locallib.php');
require_once($CFG->dirroot.'/mod/sharedresource/forms/admin_convert_form.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/mod/scorm/lib.php');
$courseid = optional_param('id', '', PARAM_INT);
$section = optional_param('section', '', PARAM_INT);
$identifier = required_param('identifier', PARAM_TEXT);
$mode = optional_param('mode', 'shared', PARAM_ALPHA);
$course = $DB->get_record('course', ['id' => "$courseid"]);
if (empty($course)) {
throw new moodle_exception('coursemisconf');
}
if (!$section) {
// When we add directly from library without course action.
$section = sharedresource_get_course_section_to_add($course);
}
// Security.
require_login($course);
$context = context_course::instance($course->id);
if (!has_any_capability(['repository/sharedresources:use', 'repository/sharedresources:create'], $context)) {
throw new moodle_exception('noaccessform', 'sharedresource');
}
$config = get_config('sharedresource');
$strtitle = get_string('addlocal', 'sharedresource');
$params = ['id' => $courseid, 'identifier' => $identifier, 'mode' => $mode];
$url = new moodle_url('/mod/sharedresource/addlocaltocourse.php', $params);
$PAGE->set_url($url);
$PAGE->set_pagelayout('standard');
$PAGE->set_context($context);
$PAGE->set_title($strtitle);
$PAGE->set_heading($SITE->fullname);
$PAGE->navbar->add($strtitle, 'addlocaltocourse.php', 'misc');
$PAGE->set_focuscontrol('');
$PAGE->set_cacheable(false);
$PAGE->set_button('');
$class = \mod_sharedresource\entry_factory::get_entry_class();
if ($class == '\mod_sharedresource\entry_extended') {
$shrentry = entry_extended::read($identifier);
} else {
$shrentry = entry::read($identifier);
}
/*
* The sharedresource has been recognized as a deployable backup.
* Take the physical file and deploy it with the activity publisher utility.
*/
if ($mode == 'deploy') {
require_capability('moodle/course:manageactivities', $context);
if (file_exists($CFG->dirroot.'/blocks/activity_publisher/lib/activity_publisher.class.php')) {
sharedresource_deploy_activity($shrentry, $course, $section);
// TODO : Terminate procedure and return to course silently.
redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
}
// No one should be here....
}
// Scorm package case.
if ($mode == 'scorm') {
/*
* Sharedresource has been recognized as a scorm package, we deploy it as a scorm
* activity. Scorm type will depend on global configuration of
* the content integration section of sharedresource.
*/
list($cm, $instance, $modname) = sharedresource_deploy_scorm($shrentry, $course, $section);
// TODO : Terminate procedure and return to course silently.
redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
}
/*
* The sharedresource has been recognized as being a LTI descriptor
*/
if (($mode == 'ltiinstall') || ($mode == 'lticonfirm')) {
$instance = sharedresource_deploy_lti($shrentry, $courseid, $section);
$modulename = 'lti';
} else {
// Elsewhere add a sharedresource instance.
// Make a shared resource on the sharedresource_entry.
$instance = new \mod_sharedresource\base(0, $shrentry->identifier);
$instance->options = 0;
$instance->popup = 0;
$instance->type = 'file';
$instance->identifier = $shrentry->identifier;
$instance->name = $shrentry->title;
$instance->course = $courseid;
$instance->intro = $shrentry->description;
$instance->introformat = 0;
$instance->alltext = '';
$instance->timemodified = time();
if (!$instance->id = $instance->add_instance($instance)) {
throw new moodle_exception('erroraddinstance', 'sharedresource');
}
$modulename = 'sharedresource';
}
if (empty($cm)) {
$cm = sharedresource_build_cm($courseid, $section, $modulename, $shrentry, $instance);
}
// Reset the course modinfo cache.
$course->modinfo = null;
$DB->update_record('course', $course);
if (!$sectionid = course_add_cm_to_section($course, $cm->id, $section)) {
throw new moodle_exception('errorsectionaddition', 'sharedresource');
}
if (!$DB->set_field('course_modules', 'section', $sectionid, ['id' => $cm->id])) {
throw new moodle_exception('errorcmsectionbinding', 'sharedresource');
}
// If we are in page format, add page_item to section bound page.
if ($course->format == 'page') {
require_once($CFG->dirroot.'/course/format/page/classes/page.class.php');
require_once($CFG->dirroot.'/course/format/page/lib.php');
$coursepage = \format_page\course_page::get_current_page($course->id);
$coursepage->add_cm_to_page($cm->id);
}
$report = '';
// Finally if localization was asked, transform the sharedresource in real resource.
if ($modulename == 'sharedresource') {
if ($mode == 'local') {
// We make a standard resource from the sharedresource.
$instance->id = sharedresource_convertfrom($instance, $report);
$modulename = 'resource';
} else {
$modulename = 'sharedresource';
}
}
// Fire event.
$modcontext = context_module::instance($cm->id);
$eventdata = new StdClass();
$eventdata->modulename = $modulename;
$eventdata->courseid = $courseid;
$eventdata->sectionid = $sectionid;
$eventdata->modname = $eventdata->modulename;
$eventdata->id = $eventdata->coursemodule = $cm->id;
$eventdata->instance = $instance->id;
$eventdata->name = $instance->name;
$event = \core\event\course_module_created::create_from_cm($eventdata, $modcontext);
$event->trigger();
// Finish.
if ($CFG->debug == DEBUG_DEVELOPER) {
echo $OUTPUT->header();
echo '<pre>';
echo $report;
echo '</pre>';
echo $OUTPUT->continue_button(new moodle_url('/course/view.php', ['id' => $course->id]));
echo $OUTPUT->footer();
die;
} else {
// TODO : Terminate procedure and return to course silently.
redirect(new moodle_url('/course/view.php', ['id' => $course->id]));
die;
}