@webship.co/workspace-learnpress 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +312 -2
- package/ai/agents/learnpress-site-builder.md +112 -0
- package/ai/agents/learnpress-tester.md +163 -0
- package/ai/commands/learnpress-test.md +97 -0
- package/ai/prompts/learnpress-build-and-test.md +25 -0
- package/ai/prompts/learnpress-quiz-round.md +32 -0
- package/ai/skills/learnpress-scenarios/SKILL.md +211 -0
- package/ai/skills/learnpress-scenarios/examples/enrolling-and-lessons.feature +34 -0
- package/ai/skills/learnpress-scenarios/examples/the-course-editor.feature +29 -0
- package/ai/skills/learnpress-scenarios/examples/the-quiz.feature +43 -0
- package/commands/cmd-learnpress-project.sh +37 -0
- package/commands/cmd-tools-learnpress-courses.sh +26 -0
- package/dist/src/index.d.ts +37 -0
- package/dist/src/index.js +60 -0
- package/dist/src/index.js.map +1 -0
- package/features/learnpress-runtimes.feature +54 -0
- package/features/learnpress.feature +44 -0
- package/package.json +102 -4
- package/scripts/args/arg-learnpress-courses.sh +22 -0
- package/scripts/args/arg-learnpress.sh +40 -0
- package/scripts/functions/fun-learnpress.sh +415 -0
- package/scripts/learning-site.php +442 -0
- package/testing/courses.feature +38 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* The courses of the test site: the accounts' places and the test content.
|
|
4
|
+
*
|
|
5
|
+
* Run inside the site with WP-CLI, once the plugin is active:
|
|
6
|
+
*
|
|
7
|
+
* wp eval-file learning-site.php the courses, their lessons and quiz, and the student's enrolment
|
|
8
|
+
* (what is there already is kept)
|
|
9
|
+
* wp eval-file learning-site.php content the same
|
|
10
|
+
* wp eval-file learning-site.php reset put the courses back as the build left them: what a stopped run left
|
|
11
|
+
* behind goes (the students' orders, progress, quiz attempts and their
|
|
12
|
+
* history, "Webship test …" courses), the student's enrolment comes back
|
|
13
|
+
* wp eval-file learning-site.php status say what the courses hold
|
|
14
|
+
* wp eval-file learning-site.php counts the counts a test run must leave as it found them (CI compares them)
|
|
15
|
+
* wp eval-file learning-site.php orphans remove what the plugin's tables keep of deleted accounts and courses
|
|
16
|
+
*
|
|
17
|
+
* Everything is made with the plugin's own models (CourseService, CoursePostModel::add_section,
|
|
18
|
+
* CourseSectionModel::add_items, LessonPostModel, QuizPostModel, QuestionPostModel, UserCourseModel), so the counts and
|
|
19
|
+
* the curriculum are the ones the plugin keeps. No account is created here: the build creates them.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
use LearnPress\Models\CoursePostModel;
|
|
23
|
+
use LearnPress\Models\LessonPostModel;
|
|
24
|
+
use LearnPress\Models\QuizPostModel;
|
|
25
|
+
use LearnPress\Models\Question\QuestionPostModel;
|
|
26
|
+
use LearnPress\Models\UserItems\UserCourseModel;
|
|
27
|
+
use LearnPress\Models\UserItems\UserItemModel;
|
|
28
|
+
use LearnPress\Services\CourseService;
|
|
29
|
+
|
|
30
|
+
$webship_mode = isset( $args[0] ) ? $args[0] : 'all';
|
|
31
|
+
|
|
32
|
+
function webship_user_id( $login ) {
|
|
33
|
+
$user = get_user_by( 'login', $login );
|
|
34
|
+
return $user ? (int) $user->ID : 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The courses of the test content: each with its sections, and each section with its lessons or quiz. */
|
|
38
|
+
function webship_courses() {
|
|
39
|
+
return array(
|
|
40
|
+
'webship-testing-basics' => array(
|
|
41
|
+
'title' => 'Webship Testing Basics',
|
|
42
|
+
'content' => 'Learn how a feature, its scenarios and their steps describe what a site does.',
|
|
43
|
+
'sections' => array(
|
|
44
|
+
'Getting started' => array(
|
|
45
|
+
array( 'lesson', 'What a scenario is', 'A scenario is one example of what the site does, written as steps a person can read.' ),
|
|
46
|
+
array( 'lesson', 'Steps and features', 'A feature groups the scenarios of one part of the site. Each step is a sentence a definition matches.' ),
|
|
47
|
+
),
|
|
48
|
+
'Check what you learned' => array(
|
|
49
|
+
array( 'quiz', 'Basics quiz', 'Three questions on features, scenarios and steps.' ),
|
|
50
|
+
),
|
|
51
|
+
),
|
|
52
|
+
),
|
|
53
|
+
'webship-advanced-testing' => array(
|
|
54
|
+
'title' => 'Webship Advanced Testing',
|
|
55
|
+
'content' => 'Tags, hooks and the reports a run writes.',
|
|
56
|
+
'sections' => array(
|
|
57
|
+
'Running a suite' => array(
|
|
58
|
+
array( 'lesson', 'Tags and hooks', 'A tag picks the scenarios a run takes; a hook runs before or after each of them.' ),
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The questions of the quiz: type, title, answers (title => correct). */
|
|
66
|
+
function webship_questions() {
|
|
67
|
+
return array(
|
|
68
|
+
array( 'true_or_false', 'A feature holds scenarios.', array( 'True' => true, 'False' => false ) ),
|
|
69
|
+
array( 'single_choice', 'Which keyword sets the scene of a scenario?', array( 'Given' => true, 'When' => false, 'Then' => false ) ),
|
|
70
|
+
array( 'multi_choice', 'Which words start a step?', array( 'Given' => true, 'When' => true, 'Then' => true, 'Before' => false ) ),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function webship_find_post( $title, $type ) {
|
|
75
|
+
$posts = get_posts( array( 'post_type' => $type, 'title' => $title, 'post_status' => 'any', 'numberposts' => 1, 'fields' => 'ids' ) );
|
|
76
|
+
return $posts ? (int) $posts[0] : 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function webship_add_question( $quiz_id, $type, $title, $answers ) {
|
|
80
|
+
global $wpdb;
|
|
81
|
+
$question_id = wp_insert_post( array(
|
|
82
|
+
'post_title' => $title,
|
|
83
|
+
'post_type' => LP_QUESTION_CPT,
|
|
84
|
+
'post_status' => 'publish',
|
|
85
|
+
'post_author' => webship_user_id( 'instructor' ),
|
|
86
|
+
) );
|
|
87
|
+
update_post_meta( $question_id, '_lp_type', $type );
|
|
88
|
+
$wpdb->insert( $wpdb->learnpress_quiz_questions, array( 'quiz_id' => $quiz_id, 'question_id' => $question_id ), array( '%d', '%d' ) );
|
|
89
|
+
$rows = array();
|
|
90
|
+
$order = 0;
|
|
91
|
+
foreach ( $answers as $answer => $correct ) {
|
|
92
|
+
$order++;
|
|
93
|
+
$row = array(
|
|
94
|
+
'title' => $answer,
|
|
95
|
+
'value' => sanitize_title( $answer ) . '-' . $question_id,
|
|
96
|
+
'is_true' => $correct ? 'yes' : 'no',
|
|
97
|
+
);
|
|
98
|
+
$wpdb->insert( $wpdb->learnpress_question_answers, array_merge( array( 'question_id' => $question_id, 'order' => $order ), $row ), array( '%d', '%d', '%s', '%s', '%s' ) );
|
|
99
|
+
$rows[] = $row;
|
|
100
|
+
}
|
|
101
|
+
$model = QuestionPostModel::find( $question_id, true );
|
|
102
|
+
if ( $model ) {
|
|
103
|
+
$model->save_answers_config( $rows );
|
|
104
|
+
}
|
|
105
|
+
return $question_id;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function webship_set_courses() {
|
|
109
|
+
$instructor = webship_user_id( 'instructor' );
|
|
110
|
+
wp_set_current_user( $instructor );
|
|
111
|
+
foreach ( webship_courses() as $slug => $course ) {
|
|
112
|
+
if ( webship_find_post( $course['title'], LP_COURSE_CPT ) ) {
|
|
113
|
+
echo " course {$course['title']}: there already\n";
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
$model = CourseService::instance()->create_info_main( array(
|
|
117
|
+
'post_title' => $course['title'],
|
|
118
|
+
'post_name' => $slug,
|
|
119
|
+
'post_type' => LP_COURSE_CPT,
|
|
120
|
+
'post_status' => 'publish',
|
|
121
|
+
'post_content' => $course['content'],
|
|
122
|
+
'post_author' => $instructor,
|
|
123
|
+
'meta_input' => array(
|
|
124
|
+
CoursePostModel::META_KEY_DURATION => '2 week',
|
|
125
|
+
CoursePostModel::META_KEY_LEVEL => 'all',
|
|
126
|
+
),
|
|
127
|
+
) );
|
|
128
|
+
foreach ( $course['sections'] as $section_name => $items ) {
|
|
129
|
+
$section = $model->add_section( array( 'section_name' => $section_name ) );
|
|
130
|
+
foreach ( $items as $item ) {
|
|
131
|
+
list( $kind, $title, $content ) = $item;
|
|
132
|
+
if ( 'lesson' === $kind ) {
|
|
133
|
+
$post = new LessonPostModel();
|
|
134
|
+
$post->post_type = LP_LESSON_CPT;
|
|
135
|
+
} else {
|
|
136
|
+
$post = new QuizPostModel();
|
|
137
|
+
$post->post_type = LP_QUIZ_CPT;
|
|
138
|
+
}
|
|
139
|
+
$post->post_title = $title;
|
|
140
|
+
$post->post_status = 'publish';
|
|
141
|
+
$post->post_author = $instructor;
|
|
142
|
+
$post->post_content = $content;
|
|
143
|
+
$post->save();
|
|
144
|
+
$section->add_items( array( 'items' => array( array( 'id' => $post->get_id(), 'type' => $post->post_type ) ) ) );
|
|
145
|
+
if ( 'quiz' === $kind ) {
|
|
146
|
+
update_post_meta( $post->get_id(), QuizPostModel::META_KEY_PASSING_GRADE, 60 );
|
|
147
|
+
update_post_meta( $post->get_id(), QuizPostModel::META_KEY_RETAKE_COUNT, 2 );
|
|
148
|
+
update_post_meta( $post->get_id(), QuizPostModel::META_KEY_REVIEW, 'yes' );
|
|
149
|
+
update_post_meta( $post->get_id(), QuizPostModel::META_KEY_SHOW_CORRECT_REVIEW, 'yes' );
|
|
150
|
+
foreach ( webship_questions() as $question ) {
|
|
151
|
+
webship_add_question( $post->get_id(), $question[0], $question[1], $question[2] );
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
echo " course {$course['title']}\n";
|
|
157
|
+
}
|
|
158
|
+
wp_set_current_user( 0 );
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function webship_set_enrolment() {
|
|
162
|
+
$student = webship_user_id( 'student' );
|
|
163
|
+
$course_id = webship_find_post( 'Webship Testing Basics', LP_COURSE_CPT );
|
|
164
|
+
if ( ! $student || ! $course_id ) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
$existing = UserCourseModel::find( $student, $course_id, false );
|
|
168
|
+
if ( $existing instanceof UserCourseModel ) {
|
|
169
|
+
echo " student: enrolled already\n";
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
$enrolment = new UserCourseModel();
|
|
173
|
+
$enrolment->user_id = $student;
|
|
174
|
+
$enrolment->item_id = $course_id;
|
|
175
|
+
$enrolment->item_type = LP_COURSE_CPT;
|
|
176
|
+
$enrolment->ref_type = '';
|
|
177
|
+
$enrolment->status = UserItemModel::STATUS_ENROLLED;
|
|
178
|
+
$enrolment->graduation = UserItemModel::GRADUATION_IN_PROGRESS;
|
|
179
|
+
$enrolment->start_time = gmdate( 'Y-m-d H:i:s' );
|
|
180
|
+
$enrolment->save();
|
|
181
|
+
echo " student: enrolled in Webship Testing Basics\n";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function webship_status() {
|
|
185
|
+
foreach ( webship_courses() as $slug => $course ) {
|
|
186
|
+
$course_id = webship_find_post( $course['title'], LP_COURSE_CPT );
|
|
187
|
+
echo "{$course['title']}: " . ( $course_id ? get_permalink( $course_id ) : 'not on the site' ) . "\n";
|
|
188
|
+
}
|
|
189
|
+
global $wpdb;
|
|
190
|
+
foreach ( array( 'instructor', 'student', 'student2' ) as $login ) {
|
|
191
|
+
$user_id = webship_user_id( $login );
|
|
192
|
+
$items = $user_id ? $wpdb->get_results( $wpdb->prepare( "SELECT item_type, status, graduation FROM {$wpdb->learnpress_user_items} WHERE user_id = %d ORDER BY user_item_id", $user_id ) ) : array();
|
|
193
|
+
echo "{$login}: " . ( $user_id ? ( $items ? implode( ', ', array_map( function ( $i ) { return "{$i->item_type} {$i->status}" . ( $i->graduation ? " ({$i->graduation})" : '' ); }, $items ) ) : 'nothing' ) : 'not on the site' ) . "\n";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The accounts of the suite that learn: their courses, progress and orders are what a run changes. */
|
|
198
|
+
function webship_learners() {
|
|
199
|
+
return array( 'student', 'student2' );
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The orders of an account (any status, trash included). */
|
|
203
|
+
function webship_orders_of( $user_id ) {
|
|
204
|
+
global $wpdb;
|
|
205
|
+
return array_map( 'intval', $wpdb->get_col( $wpdb->prepare(
|
|
206
|
+
"SELECT p.ID FROM {$wpdb->posts} p JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_user_id' WHERE p.post_type = %s AND m.meta_value = %d",
|
|
207
|
+
LP_ORDER_CPT,
|
|
208
|
+
$user_id
|
|
209
|
+
) ) );
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function webship_delete_order( $order_id ) {
|
|
213
|
+
global $wpdb;
|
|
214
|
+
$items = $wpdb->get_col( $wpdb->prepare( "SELECT order_item_id FROM {$wpdb->learnpress_order_items} WHERE order_id = %d", $order_id ) );
|
|
215
|
+
foreach ( $items as $item ) {
|
|
216
|
+
$wpdb->delete( $wpdb->learnpress_order_itemmeta, array( 'learnpress_order_item_id' => $item ), array( '%d' ) );
|
|
217
|
+
}
|
|
218
|
+
$wpdb->delete( $wpdb->learnpress_order_items, array( 'order_id' => $order_id ), array( '%d' ) );
|
|
219
|
+
wp_delete_post( $order_id, true );
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** A course, its sections, and the lessons, quizzes and questions only it uses, deleted. */
|
|
223
|
+
function webship_delete_course( $course_id ) {
|
|
224
|
+
global $wpdb;
|
|
225
|
+
$sections = $wpdb->get_col( $wpdb->prepare( "SELECT section_id FROM {$wpdb->learnpress_sections} WHERE section_course_id = %d", $course_id ) );
|
|
226
|
+
foreach ( $sections as $section_id ) {
|
|
227
|
+
$items = $wpdb->get_col( $wpdb->prepare( "SELECT item_id FROM {$wpdb->learnpress_section_items} WHERE section_id = %d", $section_id ) );
|
|
228
|
+
foreach ( $items as $item_id ) {
|
|
229
|
+
webship_delete_item( (int) $item_id );
|
|
230
|
+
}
|
|
231
|
+
$wpdb->delete( $wpdb->learnpress_section_items, array( 'section_id' => $section_id ), array( '%d' ) );
|
|
232
|
+
$wpdb->delete( $wpdb->learnpress_sections, array( 'section_id' => $section_id ), array( '%d' ) );
|
|
233
|
+
}
|
|
234
|
+
$wpdb->delete( $wpdb->learnpress_user_items, array( 'item_id' => $course_id ), array( '%d' ) );
|
|
235
|
+
$wpdb->delete( $wpdb->learnpress_user_items, array( 'ref_id' => $course_id ), array( '%d' ) );
|
|
236
|
+
wp_delete_post( $course_id, true );
|
|
237
|
+
$courses = $wpdb->prefix . 'learnpress_courses';
|
|
238
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $courses ) ) === $courses ) {
|
|
239
|
+
$wpdb->delete( $courses, array( 'ID' => $course_id ), array( '%d' ) );
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A lesson or a quiz (with its questions and their answers), deleted. */
|
|
244
|
+
function webship_delete_item( $item_id ) {
|
|
245
|
+
global $wpdb;
|
|
246
|
+
if ( LP_QUIZ_CPT === get_post_type( $item_id ) ) {
|
|
247
|
+
$questions = $wpdb->get_col( $wpdb->prepare( "SELECT question_id FROM {$wpdb->learnpress_quiz_questions} WHERE quiz_id = %d", $item_id ) );
|
|
248
|
+
foreach ( $questions as $question_id ) {
|
|
249
|
+
webship_delete_question( (int) $question_id );
|
|
250
|
+
}
|
|
251
|
+
$wpdb->delete( $wpdb->learnpress_quiz_questions, array( 'quiz_id' => $item_id ), array( '%d' ) );
|
|
252
|
+
}
|
|
253
|
+
$wpdb->delete( $wpdb->learnpress_section_items, array( 'item_id' => $item_id ), array( '%d' ) );
|
|
254
|
+
wp_delete_post( $item_id, true );
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function webship_delete_question( $question_id ) {
|
|
258
|
+
global $wpdb;
|
|
259
|
+
$wpdb->delete( $wpdb->learnpress_question_answers, array( 'question_id' => $question_id ), array( '%d' ) );
|
|
260
|
+
$config = $wpdb->prefix . 'learnpress_question_answer_config';
|
|
261
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $config ) ) === $config ) {
|
|
262
|
+
$wpdb->delete( $config, array( 'question_id' => $question_id ), array( '%d' ) );
|
|
263
|
+
}
|
|
264
|
+
$wpdb->delete( $wpdb->learnpress_quiz_questions, array( 'question_id' => $question_id ), array( '%d' ) );
|
|
265
|
+
wp_delete_post( $question_id, true );
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* What a stopped run left behind: the learners' orders and their enrolments and progress (the student's enrolment of
|
|
270
|
+
* the build comes back fresh), the courses, lessons, quizzes and questions of the suite ("Webship test …"), and the
|
|
271
|
+
* status of the build's courses.
|
|
272
|
+
*/
|
|
273
|
+
function webship_reset() {
|
|
274
|
+
global $wpdb;
|
|
275
|
+
foreach ( webship_learners() as $login ) {
|
|
276
|
+
$user_id = webship_user_id( $login );
|
|
277
|
+
if ( ! $user_id ) {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
foreach ( webship_orders_of( $user_id ) as $order_id ) {
|
|
281
|
+
webship_delete_order( $order_id );
|
|
282
|
+
echo " {$login}: order {$order_id} removed\n";
|
|
283
|
+
}
|
|
284
|
+
$courses = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->learnpress_user_items} WHERE user_id = %d AND item_type = %s", $user_id, LP_COURSE_CPT ) );
|
|
285
|
+
foreach ( $courses as $course_id ) {
|
|
286
|
+
LP_User_Items_DB::getInstance()->delete_user_items_old( $user_id, (int) $course_id );
|
|
287
|
+
}
|
|
288
|
+
$wpdb->delete( $wpdb->learnpress_user_items, array( 'user_id' => $user_id ), array( '%d' ) );
|
|
289
|
+
// The plugin's history of the account's enrolments and attempts: a row a user item, kept by its own tools when
|
|
290
|
+
// the user item goes.
|
|
291
|
+
$wpdb->delete( $wpdb->learnpress_user_item_results, array( 'user_id' => $user_id ), array( '%d' ) );
|
|
292
|
+
}
|
|
293
|
+
foreach ( array( LP_COURSE_CPT, LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT ) as $type ) {
|
|
294
|
+
// "Webship test " exactly, case and space included: LIKE alone ignores case, and "Webship Testing Basics" is the
|
|
295
|
+
// build's own course.
|
|
296
|
+
$posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_title LIKE BINARY %s", $type, 'Webship test %' ) );
|
|
297
|
+
foreach ( $posts as $post_id ) {
|
|
298
|
+
if ( ! get_post( $post_id ) ) {
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if ( LP_COURSE_CPT === $type ) {
|
|
302
|
+
webship_delete_course( (int) $post_id );
|
|
303
|
+
} elseif ( LP_QUESTION_CPT === $type ) {
|
|
304
|
+
webship_delete_question( (int) $post_id );
|
|
305
|
+
} else {
|
|
306
|
+
webship_delete_item( (int) $post_id );
|
|
307
|
+
}
|
|
308
|
+
echo " {$type} {$post_id} removed\n";
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
foreach ( webship_courses() as $slug => $course ) {
|
|
312
|
+
$course_id = webship_find_post( $course['title'], LP_COURSE_CPT );
|
|
313
|
+
if ( $course_id && 'publish' !== get_post_status( $course_id ) ) {
|
|
314
|
+
wp_update_post( array( 'ID' => $course_id, 'post_status' => 'publish' ) );
|
|
315
|
+
echo " course {$course['title']}: published again\n";
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
webship_set_enrolment();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* What a run must leave as it found it, one count a line, the same on every run of the same site: the courses by
|
|
323
|
+
* status, the lessons, quizzes, questions, sections and their items, the orders by status, the plugin's history rows
|
|
324
|
+
* (one an enrolment or an attempt), and each account's enrolments, progress rows and quiz attempts by status, its
|
|
325
|
+
* orders and its history rows. CI compares it before and after the suite.
|
|
326
|
+
*/
|
|
327
|
+
function webship_counts() {
|
|
328
|
+
global $wpdb;
|
|
329
|
+
$lines = array();
|
|
330
|
+
foreach ( array( LP_COURSE_CPT, LP_LESSON_CPT, LP_QUIZ_CPT, LP_QUESTION_CPT, LP_ORDER_CPT ) as $type ) {
|
|
331
|
+
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT post_status, COUNT(*) AS n FROM {$wpdb->posts} WHERE post_type = %s GROUP BY post_status ORDER BY post_status", $type ) );
|
|
332
|
+
$lines[] = "{$type}: " . ( $rows ? implode( ', ', array_map( function ( $r ) { return "{$r->post_status} {$r->n}"; }, $rows ) ) : 'none' );
|
|
333
|
+
}
|
|
334
|
+
$lines[] = 'sections: ' . (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->learnpress_sections}" );
|
|
335
|
+
$lines[] = 'section items: ' . (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->learnpress_section_items}" );
|
|
336
|
+
$lines[] = 'quiz questions: ' . (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->learnpress_quiz_questions}" );
|
|
337
|
+
$lines[] = 'history rows: ' . (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->learnpress_user_item_results}" );
|
|
338
|
+
foreach ( array( 'administrator', 'instructor', 'student', 'student2' ) as $login ) {
|
|
339
|
+
$user_id = webship_user_id( $login );
|
|
340
|
+
if ( ! $user_id ) {
|
|
341
|
+
$lines[] = "{$login}: not on the site";
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT item_type, status, graduation, COUNT(*) AS n FROM {$wpdb->learnpress_user_items} WHERE user_id = %d GROUP BY item_type, status, graduation ORDER BY item_type, status, graduation", $user_id ) );
|
|
345
|
+
$lines[] = "{$login} items: " . ( $rows ? implode( ', ', array_map( function ( $r ) { return trim( "{$r->item_type} {$r->status} {$r->graduation}" ) . " {$r->n}"; }, $rows ) ) : 'none' );
|
|
346
|
+
$lines[] = "{$login} orders: " . count( webship_orders_of( $user_id ) );
|
|
347
|
+
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT item_type, COUNT(*) AS n FROM {$wpdb->learnpress_user_item_results} WHERE user_id = %d GROUP BY item_type ORDER BY item_type", $user_id ) );
|
|
348
|
+
$lines[] = "{$login} history: " . ( $rows ? implode( ', ', array_map( function ( $r ) { return "{$r->item_type} {$r->n}"; }, $rows ) ) : 'none' );
|
|
349
|
+
}
|
|
350
|
+
foreach ( webship_courses() as $slug => $course ) {
|
|
351
|
+
$course_id = webship_find_post( $course['title'], LP_COURSE_CPT );
|
|
352
|
+
$lines[] = "{$course['title']}: " . ( $course_id ? get_post_status( $course_id ) . ', ' . (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->learnpress_user_items} WHERE item_id = %d AND item_type = %s", $course_id, LP_COURSE_CPT ) ) . ' learners' : 'not on the site' );
|
|
353
|
+
}
|
|
354
|
+
echo implode( "\n", $lines ) . "\n";
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* What the plugin keeps of what WordPress deleted from outside the plugin's screens. An account deleted with WP-CLI
|
|
359
|
+
* takes its courses along, and the plugin keeps the account's enrolments, progress and history, its orders, the
|
|
360
|
+
* sections of its courses and their items, and its lessons, quizzes and questions (types WordPress does not delete
|
|
361
|
+
* with an author). The Workspace's cancel of the test users runs this once the accounts are gone.
|
|
362
|
+
*/
|
|
363
|
+
function webship_orphans() {
|
|
364
|
+
global $wpdb;
|
|
365
|
+
$gone = array();
|
|
366
|
+
$orders = $wpdb->get_col( $wpdb->prepare(
|
|
367
|
+
"SELECT p.ID FROM {$wpdb->posts} p JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = '_user_id' LEFT JOIN {$wpdb->users} u ON u.ID = m.meta_value WHERE p.post_type = %s AND m.meta_value > 0 AND u.ID IS NULL",
|
|
368
|
+
LP_ORDER_CPT
|
|
369
|
+
) );
|
|
370
|
+
foreach ( $orders as $order_id ) {
|
|
371
|
+
webship_delete_order( (int) $order_id );
|
|
372
|
+
}
|
|
373
|
+
$gone['orders'] = count( $orders );
|
|
374
|
+
$items = $wpdb->learnpress_user_items;
|
|
375
|
+
$meta = $wpdb->learnpress_user_itemmeta;
|
|
376
|
+
$result = $wpdb->learnpress_user_item_results;
|
|
377
|
+
$gone['enrolments and progress'] = (int) $wpdb->query( "DELETE ui FROM {$items} ui LEFT JOIN {$wpdb->users} u ON u.ID = ui.user_id WHERE ui.user_id > 0 AND u.ID IS NULL" )
|
|
378
|
+
+ (int) $wpdb->query( "DELETE ui FROM {$items} ui LEFT JOIN {$wpdb->posts} p ON p.ID = ui.item_id WHERE p.ID IS NULL" );
|
|
379
|
+
$wpdb->query( "DELETE m FROM {$meta} m LEFT JOIN {$items} ui ON ui.user_item_id = m.learnpress_user_item_id WHERE ui.user_item_id IS NULL" );
|
|
380
|
+
$gone['history rows'] = (int) $wpdb->query( "DELETE r FROM {$result} r LEFT JOIN {$wpdb->users} u ON u.ID = r.user_id WHERE r.user_id > 0 AND u.ID IS NULL" );
|
|
381
|
+
$sections = $wpdb->learnpress_sections;
|
|
382
|
+
$gone['sections'] = (int) $wpdb->query( "DELETE s FROM {$sections} s LEFT JOIN {$wpdb->posts} p ON p.ID = s.section_course_id WHERE p.ID IS NULL" );
|
|
383
|
+
$gone['section items'] = (int) $wpdb->query( "DELETE si FROM {$wpdb->learnpress_section_items} si LEFT JOIN {$sections} s ON s.section_id = si.section_id LEFT JOIN {$wpdb->posts} p ON p.ID = si.item_id WHERE s.section_id IS NULL OR p.ID IS NULL" );
|
|
384
|
+
// WordPress deletes an account's courses with it, but not its lessons, quizzes and questions (their types have no
|
|
385
|
+
// author support): those of an account that is gone go when no course uses them any more.
|
|
386
|
+
$items = $wpdb->get_col( $wpdb->prepare(
|
|
387
|
+
"SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->users} u ON u.ID = p.post_author LEFT JOIN {$wpdb->learnpress_section_items} si ON si.item_id = p.ID WHERE p.post_type IN (%s, %s) AND p.post_author > 0 AND u.ID IS NULL AND si.item_id IS NULL",
|
|
388
|
+
LP_LESSON_CPT,
|
|
389
|
+
LP_QUIZ_CPT
|
|
390
|
+
) );
|
|
391
|
+
foreach ( $items as $item_id ) {
|
|
392
|
+
webship_delete_item( (int) $item_id );
|
|
393
|
+
}
|
|
394
|
+
$gone['lessons and quizzes'] = count( $items );
|
|
395
|
+
$questions = $wpdb->get_col( $wpdb->prepare(
|
|
396
|
+
"SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->users} u ON u.ID = p.post_author LEFT JOIN {$wpdb->learnpress_quiz_questions} qq ON qq.question_id = p.ID WHERE p.post_type = %s AND p.post_author > 0 AND u.ID IS NULL AND qq.question_id IS NULL",
|
|
397
|
+
LP_QUESTION_CPT
|
|
398
|
+
) );
|
|
399
|
+
foreach ( $questions as $question_id ) {
|
|
400
|
+
webship_delete_question( (int) $question_id );
|
|
401
|
+
}
|
|
402
|
+
$gone['questions'] = count( $questions );
|
|
403
|
+
$gone['quiz questions'] = (int) $wpdb->query( "DELETE qq FROM {$wpdb->learnpress_quiz_questions} qq LEFT JOIN {$wpdb->posts} q ON q.ID = qq.quiz_id LEFT JOIN {$wpdb->posts} p ON p.ID = qq.question_id WHERE q.ID IS NULL OR p.ID IS NULL" );
|
|
404
|
+
$answers = $wpdb->learnpress_question_answers;
|
|
405
|
+
$wpdb->query( "DELETE am FROM {$wpdb->learnpress_question_answermeta} am LEFT JOIN {$answers} a ON a.question_answer_id = am.learnpress_question_answer_id LEFT JOIN {$wpdb->posts} p ON p.ID = a.question_id WHERE a.question_answer_id IS NULL OR p.ID IS NULL" );
|
|
406
|
+
$gone['answers'] = (int) $wpdb->query( "DELETE a FROM {$answers} a LEFT JOIN {$wpdb->posts} p ON p.ID = a.question_id WHERE p.ID IS NULL" );
|
|
407
|
+
$config = $wpdb->prefix . 'learnpress_question_answer_config';
|
|
408
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $config ) ) === $config ) {
|
|
409
|
+
$wpdb->query( "DELETE c FROM {$config} c LEFT JOIN {$wpdb->posts} p ON p.ID = c.question_id WHERE p.ID IS NULL" );
|
|
410
|
+
}
|
|
411
|
+
$courses = $wpdb->prefix . 'learnpress_courses';
|
|
412
|
+
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $courses ) ) === $courses ) {
|
|
413
|
+
$wpdb->query( "DELETE c FROM {$courses} c LEFT JOIN {$wpdb->posts} p ON p.ID = c.ID WHERE p.ID IS NULL" );
|
|
414
|
+
}
|
|
415
|
+
$left = array_filter( $gone );
|
|
416
|
+
echo $left ? ' removed what the deleted accounts and courses left: ' . implode( ', ', array_map( function ( $what, $n ) { return "{$n} {$what}"; }, array_keys( $left ), $left ) ) . "\n" : " nothing left by deleted accounts or courses\n";
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
wp_set_current_user( 1 );
|
|
420
|
+
|
|
421
|
+
switch ( $webship_mode ) {
|
|
422
|
+
case 'all':
|
|
423
|
+
case 'content':
|
|
424
|
+
webship_set_courses();
|
|
425
|
+
webship_set_enrolment();
|
|
426
|
+
break;
|
|
427
|
+
case 'reset':
|
|
428
|
+
webship_reset();
|
|
429
|
+
break;
|
|
430
|
+
case 'status':
|
|
431
|
+
webship_status();
|
|
432
|
+
break;
|
|
433
|
+
case 'counts':
|
|
434
|
+
webship_counts();
|
|
435
|
+
break;
|
|
436
|
+
case 'orphans':
|
|
437
|
+
webship_orphans();
|
|
438
|
+
break;
|
|
439
|
+
default:
|
|
440
|
+
fwrite( STDERR, "Unknown mode {$webship_mode}: content, reset, status, counts or orphans.\n" );
|
|
441
|
+
exit( 1 );
|
|
442
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Feature: The courses answer, and their accounts are in them
|
|
2
|
+
As the owner of the site
|
|
3
|
+
I want the course list, a course and each default account to work
|
|
4
|
+
So that a build that came out broken is caught before anything else is tested
|
|
5
|
+
|
|
6
|
+
# Placed by the workspace's testing setup as a starting point: it is yours to change.
|
|
7
|
+
# The accounts are the default ones (cmd-tools-learnpress-courses.sh --add-users); a scenario logs in by a handle of
|
|
8
|
+
# the users registry in cucumber.js, so no password is written here. It passes on a site built with --install
|
|
9
|
+
# --add-users --sample-course.
|
|
10
|
+
|
|
11
|
+
@critical
|
|
12
|
+
Scenario: A visitor finds a course and reads its curriculum
|
|
13
|
+
Given I am an anonymous user
|
|
14
|
+
When I go to the course list
|
|
15
|
+
Then the course list should show "Webship Testing Basics"
|
|
16
|
+
When I go to the course "Webship Testing Basics"
|
|
17
|
+
Then the curriculum should show the section "Getting started" with "What a scenario is", "Steps and features"
|
|
18
|
+
And the course page should offer "Start Now"
|
|
19
|
+
|
|
20
|
+
Scenario: The enrolled student is offered to continue, and its profile counts the course
|
|
21
|
+
Given I am a logged in user with the "student" user
|
|
22
|
+
When I go to the course "Webship Testing Basics"
|
|
23
|
+
Then the course page should offer "Continue"
|
|
24
|
+
When I go to my courses
|
|
25
|
+
Then my profile should count "Enrolled Course" as "1"
|
|
26
|
+
And my profile should list "Webship Testing Basics"
|
|
27
|
+
|
|
28
|
+
Scenario: A student who is not enrolled is offered to start
|
|
29
|
+
Given I am a logged in user with the "student2" user
|
|
30
|
+
When I go to the course "Webship Testing Basics"
|
|
31
|
+
Then the course page should offer "Start Now"
|
|
32
|
+
|
|
33
|
+
Scenario: The administrator reads the courses and their settings
|
|
34
|
+
Given I am a logged in user with the "admin" user
|
|
35
|
+
When I go to the "Courses" admin screen
|
|
36
|
+
Then the administration should list the course "Webship Testing Basics" with "2 Sections 2 Lessons 1 Quiz"
|
|
37
|
+
When I go to the "Course settings" admin screen
|
|
38
|
+
Then the "Review courses" option should be on
|