-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpecialCourseEditor.php
207 lines (197 loc) · 7.5 KB
/
SpecialCourseEditor.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
<?php
if ( !defined( 'MEDIAWIKI' ) ){
die( );
}
class SpecialCourseEditor extends SpecialPage {
public function __construct( $name = 'CourseEditor', $restriction = 'move' ) {
parent::__construct( $name );
}
/**
* This function is an entrypoint (Controller, Façade ...) of the CourseEditor
* extension. It uses the query string or the $par param to generate right view
* requested by the user.
* @param String $par Optional param used to generate SpecialPage subpages
*/
public function execute($par) {
$request = $this->getRequest();
$user = $this->getUser();
//Redirect user if he is not logged
if ( ! ( $user->isAllowed( 'move' ) ) ) {
global $wgOut;
$title = Title::newFromText('Special:UserLogin');
$pageName = "Special:" . $this->mName;
$params = strstr($request->getRequestURL(), '?');
$returnTo = "returnto=" . $pageName;
if($params != ""){
$returnTo .= "&returntoquery=" . urlencode($params);
}
$wgOut->redirect($title->getFullURL($returnTo));
}
$actionType = $request->getVal('actiontype');
if($par === 'ReadyCourses'){
if (!$user->isAllowed( 'undelete' )){
throw new PermissionsError( 'undelete' );
}
$this->readyToBePublishedCourses();
return;
}
switch ($actionType){
case 'editleveltwo':
$levelTwoName = $request->getVal('pagename');
$this->editLevelTwo($levelTwoName);
return;
case 'editcourse':
$courseName = $request->getVal('pagename');
$this->editCourse($courseName);
return;
case 'readycourses':
if (!$user->isAllowed( 'undelete' )){
throw new PermissionsError( 'undelete' );
}
$this->readyToBePublishedCourses();
return;
case 'createcourse':
if($request->getVal('department')){
$department = $request->getVal('department');
$this->createNewCourseFromDepartment($department);
}else if($request->getVal('topic')){
$topic = $request->getVal('topic');
$this->createNewCourseFromTopic($topic);
}
return;
case 'managemetadata':
$courseName = $request->getVal('pagename');
$this->manageMetadata($courseName);
return;
default:
$this->renderCreditsAndInfo();
return;
}
}
/**
* Generate the view to edit a course
* @param String $courseName the name of the course (included namespace)
*/
private function editCourse($courseName){
$out = $this->getOutput();
$out->enableOOUI();
//Get levelTwo elements of the course used to generate the Drag'n'Drop
$levelsTwo = CourseEditorUtils::getLevelsTwo($courseName);
$this->setHeaders();
$out->setPageTitle(wfMessage('courseeditor-editcourse-pagetitle'));
$out->addInlineScript(" var levelsTwo = " . json_encode($levelsTwo) . ", editStack = [];");
$out->addModules( 'ext.courseEditor.course' );
$template = new CourseEditorTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('course', $courseName);
$out->addTemplate( $template );
}
/**
* Generate the view to edit a levelTwo element
* @param String $levelTwoName the name of the levelTwo (included namespace
* and the name of the course)
*/
private function editLevelTwo($levelTwoName){
$out = $this->getOutput();
$out->enableOOUI();
//Get levelThree elements of the levelTwo used to generate the Drag'n'Drop
$levelsThree = CourseEditorUtils::getLevelsThree($levelTwoName);
$this->setHeaders();
$out->setPageTitle(wfMessage('courseeditor-editlevelTwo-pagetitle'));
$out->addInlineScript(" var levelsThree = " . json_encode($levelsThree) . ", editStack = [];");
$out->addModules( 'ext.courseEditor.levelTwo' );
$template = new LevelTwoEditorTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('levelTwo', $levelTwoName);
$out->addTemplate( $template );
}
/**
* Generate the view to publish ready private courses.
* This view is for admins only.
*/
private function readyToBePublishedCourses(){
global $wgCourseEditorNamespaces;
$readyCourses = CourseEditorUtils::getReadyToBePublishedCourses();
$out = $this->getOutput();
$out->enableOOUI();
$out->setPageTitle(wfMessage('courseeditor-publish-course-pagetitle'));
$out->addJsConfigVars('wgCourseEditor', $wgCourseEditorNamespaces);
$template = new PublishCourseTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('readyCourses', $readyCourses);
$out->addTemplate( $template );
}
/**
* Generate the view to create a new course if the user comes from a
* department page.
* @param String $department the name of the department page
*/
private function createNewCourseFromDepartment($department){
$out = $this->getOutput();
$out->enableOOUI();
$out->addModules('ext.courseEditor.create');
$out->setPageTitle(wfMessage('courseeditor-createcourse-pagetitle'));
$template = new CourseCreatorTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('department', $department);
$out->addTemplate( $template );
}
/**
* Generate the view to create a new course if the user comes from a
* topic page.
* @param String $topic the name of the topic page
*/
private function createNewCourseFromTopic($topic) {
$out = $this->getOutput();
$out->enableOOUI();
$out->addModules('ext.courseEditor.create');
$out->setPageTitle(wfMessage('courseeditor-createcourse-pagetitle'));
$template = new CourseCreatorTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('topic', $topic);
$out->addTemplate( $template );
}
/**
* Generate the view to manage the metadata of a course.
* @param String $courseName the name of the course
*/
private function manageMetadata($courseName){
global $wgCourseEditorNamespaces;
$out = $this->getOutput();
$out->enableOOUI();
$out->addModules('ext.courseEditor.manageMetadata');
$out->setPageTitle(wfMessage('courseeditor-managemetata-pagetitle'));
$out->addJsConfigVars('wgCourseEditor', $wgCourseEditorNamespaces);
// Remove username from courseName (if there is)
$explodedString = explode('/', $courseName, 2);
$courseNameNoUser = (sizeof($explodedString) === 1) ? $explodedString[0] : $explodedString[1];
$username = (sizeof($explodedString) === 1) ? 'null' : $explodedString[0];
$isPrivate =(sizeof($explodedString) === 1) ? false : true;
$template = new ManageMetadataTemplate();
$template->setRef('courseEditor', $this);
$template->set('context', $this->getContext());
$template->set('course', $courseNameNoUser);
$template->set('private', $isPrivate);
$template->set('username', $username);
$template->set('userObj', $this->getUser());
$topics = CourseEditorUtils::getTopics();
$template->set('topics', $topics);
$metadataResult = CourseEditorUtils::getMetadata($courseName);
if($metadataResult !== null){
$template->set('metadataResult', $metadataResult);
}
$out->addTemplate( $template );
}
/**
* Generate the credits and the info of the extension.
*/
private function renderCreditsAndInfo() {
$out = $this->getOutput();
$out->addWikiMsg('courseeditor-credits-info');
}
}