| Total Complexity | 72 |
| Total Lines | 690 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WSCMCourse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WSCMCourse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class WSCMCourse extends WSCM |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Deletes a course. |
||
| 17 | * |
||
| 18 | * @param string API secret key |
||
| 19 | * @param string Course id field name |
||
| 20 | * @param string Course id value |
||
| 21 | */ |
||
| 22 | public function DeleteCourse( |
||
| 23 | $secret_key, |
||
| 24 | $course_id_field_name, |
||
| 25 | $course_id_value |
||
| 26 | ) { |
||
| 27 | $verifKey = $this->verifyKey($secret_key); |
||
| 28 | if ($verifKey instanceof WSError) { |
||
| 29 | $this->handleError($verifKey); |
||
| 30 | } else { |
||
| 31 | $result = $this->deleteCourseHelper( |
||
| 32 | $course_id_field_name, |
||
| 33 | $course_id_value |
||
| 34 | ); |
||
| 35 | if ($result instanceof WSError) { |
||
|
|
|||
| 36 | $this->handleError($result); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Deletes multiple courses. |
||
| 43 | * |
||
| 44 | * @param string API secret key |
||
| 45 | * @param array Array of courses with elements of the form |
||
| 46 | * array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') |
||
| 47 | * |
||
| 48 | * @return array Array with elements like |
||
| 49 | * array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 50 | * Note that if the result array contains a code different |
||
| 51 | * than 0, an error occured |
||
| 52 | */ |
||
| 53 | public function DeleteCourses($secret_key, $courses) |
||
| 54 | { |
||
| 55 | $verifKey = $this->verifyKey($secret_key); |
||
| 56 | if ($verifKey instanceof WSError) { |
||
| 57 | $this->handleError($verifKey); |
||
| 58 | } else { |
||
| 59 | $results = []; |
||
| 60 | foreach ($courses as $course) { |
||
| 61 | $result_tmp = []; |
||
| 62 | $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']); |
||
| 63 | $result_tmp['course_id_value'] = $course['course_id_value']; |
||
| 64 | if ($result_op instanceof WSCMError) { |
||
| 65 | // Return the error in the results |
||
| 66 | $result_tmp['result'] = $result_op->toArray(); |
||
| 67 | } else { |
||
| 68 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 69 | } |
||
| 70 | $results[] = $result_tmp; |
||
| 71 | } |
||
| 72 | |||
| 73 | return $results; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Creates a course. |
||
| 79 | * |
||
| 80 | * @param string API secret key |
||
| 81 | * @param string Title |
||
| 82 | * @param string Category code |
||
| 83 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 84 | * @param string Tutor name |
||
| 85 | * @param string Course admin user id field name |
||
| 86 | * @param string Course admin user id value |
||
| 87 | * @param string Course language |
||
| 88 | * @param string Course id field name |
||
| 89 | * @param string Course id value |
||
| 90 | * @param array Course extra fields |
||
| 91 | * |
||
| 92 | * @return int Course id generated |
||
| 93 | */ |
||
| 94 | public function CreateCourse( |
||
| 95 | $secret_key, |
||
| 96 | $title, |
||
| 97 | $category_code, |
||
| 98 | $wanted_code, |
||
| 99 | $tutor_name, |
||
| 100 | $course_admin_user_id_field_name, |
||
| 101 | $course_admin_user_id_value, |
||
| 102 | $language, |
||
| 103 | $course_id_field_name, |
||
| 104 | $course_id_value, |
||
| 105 | $extras |
||
| 106 | ) { |
||
| 107 | // First, verify the secret key |
||
| 108 | $verifKey = $this->verifyKey($secret_key); |
||
| 109 | if ($verifKey instanceof WSError) { |
||
| 110 | $this->handleError($verifKey); |
||
| 111 | } else { |
||
| 112 | $result = $this->createCourseHelper( |
||
| 113 | $title, |
||
| 114 | $category_code, |
||
| 115 | $wanted_code, |
||
| 116 | $tutor_name, |
||
| 117 | $course_admin_user_id_field_name, |
||
| 118 | $course_admin_user_id_value, |
||
| 119 | $language, |
||
| 120 | $course_id_field_name, |
||
| 121 | $course_id_value, |
||
| 122 | $extras |
||
| 123 | ); |
||
| 124 | if ($result instanceof WSError) { |
||
| 125 | $this->handleError($result); |
||
| 126 | } else { |
||
| 127 | return $result; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create multiple courses. |
||
| 134 | * |
||
| 135 | * @param string API secret key |
||
| 136 | * @param array Courses to be created, with elements following the structure presented in CreateCourse |
||
| 137 | * |
||
| 138 | * @return array Array with elements of the form |
||
| 139 | * array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 140 | */ |
||
| 141 | public function CreateCourses($secret_key, $courses) |
||
| 142 | { |
||
| 143 | // First, verify the secret key |
||
| 144 | $verifKey = $this->verifyKey($secret_key); |
||
| 145 | if ($verifKey instanceof WSCMError) { |
||
| 146 | $this->handleError($verifKey); |
||
| 147 | } else { |
||
| 148 | $results = []; |
||
| 149 | foreach ($courses as $course) { |
||
| 150 | $result_tmp = []; |
||
| 151 | //reinitialize variables just in case |
||
| 152 | $title = $category_code = $wanted_code = $tutor_name = $course_admin_user_id_field_name = $course_admin_user_id_value = $language = $course_id_field_name = $course_id_value = $extras = null; |
||
| 153 | extract($course); |
||
| 154 | $result = $this->createCourseHelper( |
||
| 155 | $title, |
||
| 156 | $category_code, |
||
| 157 | $wanted_code, |
||
| 158 | $tutor_name, |
||
| 159 | $course_admin_user_id_field_name, |
||
| 160 | $course_admin_user_id_value, |
||
| 161 | $language, |
||
| 162 | $course_id_field_name, |
||
| 163 | $course_id_value, |
||
| 164 | $extras |
||
| 165 | ); |
||
| 166 | if ($result instanceof WSCMError) { |
||
| 167 | $result_tmp['result'] = $result->toArray(); |
||
| 168 | $result_tmp['course_id_value'] = $course_id_value; |
||
| 169 | $result_tmp['course_id_generated'] = 0; |
||
| 170 | } else { |
||
| 171 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 172 | $result_tmp['course_id_value'] = $course_id_value; |
||
| 173 | $result_tmp['course_id_generated'] = $result; |
||
| 174 | } |
||
| 175 | $results[] = $result_tmp; |
||
| 176 | } |
||
| 177 | |||
| 178 | return $results; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Edits a course. |
||
| 184 | * |
||
| 185 | * @param string API secret key |
||
| 186 | * @param string Course id field name |
||
| 187 | * @param string Course id value |
||
| 188 | * @param string Title |
||
| 189 | * @param string Category code |
||
| 190 | * @param string Department name |
||
| 191 | * @param string Department url |
||
| 192 | * @param string Course language |
||
| 193 | * @param int Visibility |
||
| 194 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 195 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 196 | * @param string Visual code |
||
| 197 | * @param array Course extra fields |
||
| 198 | */ |
||
| 199 | public function EditCourse( |
||
| 200 | $secret_key, |
||
| 201 | $course_id_field_name, |
||
| 202 | $course_id_value, |
||
| 203 | $title, |
||
| 204 | $category_code, |
||
| 205 | $department_name, |
||
| 206 | $department_url, |
||
| 207 | $language, |
||
| 208 | $visibility, |
||
| 209 | $subscribe, |
||
| 210 | $unsubscribe, |
||
| 211 | $visual_code, |
||
| 212 | $extras |
||
| 213 | ) { |
||
| 214 | $verifKey = $this->verifyKey($secret_key); |
||
| 215 | if ($verifKey instanceof WSCMError) { |
||
| 216 | $this->handleError($verifKey); |
||
| 217 | } else { |
||
| 218 | $result = $this->editCourseHelper( |
||
| 219 | $course_id_field_name, |
||
| 220 | $course_id_value, |
||
| 221 | $title, |
||
| 222 | $category_code, |
||
| 223 | $department_name, |
||
| 224 | $department_url, |
||
| 225 | $language, |
||
| 226 | $visibility, |
||
| 227 | $subscribe, |
||
| 228 | $unsubscribe, |
||
| 229 | $visual_code, |
||
| 230 | $extras |
||
| 231 | ); |
||
| 232 | if ($result instanceof WSCMError) { |
||
| 233 | $this->handleError($result); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * List courses. |
||
| 240 | * |
||
| 241 | * @param string API secret key |
||
| 242 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 243 | * |
||
| 244 | * @return array An array with elements of the form |
||
| 245 | * ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility', |
||
| 246 | * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') |
||
| 247 | */ |
||
| 248 | public function ListCourses($secret_key, $course_id_field_name) |
||
| 249 | { |
||
| 250 | $verifKey = $this->verifyKey($secret_key); |
||
| 251 | if ($verifKey instanceof WSError) { |
||
| 252 | $this->handleError($verifKey); |
||
| 253 | } else { |
||
| 254 | $courses_result = []; |
||
| 255 | $category_names = []; |
||
| 256 | |||
| 257 | $courses = CourseManager::get_courses_list(); |
||
| 258 | foreach ($courses as $course) { |
||
| 259 | $course_tmp = []; |
||
| 260 | $course_tmp['id'] = $course['id']; |
||
| 261 | $course_tmp['code'] = $course['code']; |
||
| 262 | $course_tmp['title'] = $course['title']; |
||
| 263 | $course_tmp['language'] = $course['course_language']; |
||
| 264 | $course_tmp['visibility'] = $course['visibility']; |
||
| 265 | |||
| 266 | // Determining category name |
||
| 267 | if ($category_names[$course['category_code']]) { |
||
| 268 | $course_tmp['category_name'] = $category_names[$course['category_code']]; |
||
| 269 | } else { |
||
| 270 | $category = CourseManager::get_course_category($course['category_code']); |
||
| 271 | $category_names[$course['category_code']] = $category['name']; |
||
| 272 | $course_tmp['category_name'] = $category['name']; |
||
| 273 | } |
||
| 274 | |||
| 275 | // Determining number of students registered in course |
||
| 276 | $user_list = CourseManager::get_user_list_from_course_code($course['code']); |
||
| 277 | $course_tmp['number_students'] = count($user_list); |
||
| 278 | |||
| 279 | // Determining external course id |
||
| 280 | $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_id_field_name, $course['code']); |
||
| 281 | $courses_result[] = $course_tmp; |
||
| 282 | } |
||
| 283 | |||
| 284 | return $courses_result; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Subscribe user to a course. |
||
| 290 | * |
||
| 291 | * @param string API secret key |
||
| 292 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 293 | * @param string course id value |
||
| 294 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 295 | * @param string User id value |
||
| 296 | * @param int Status (1 = Teacher, 5 = Student) |
||
| 297 | */ |
||
| 298 | public function SubscribeUserToCourse( |
||
| 299 | $secret_key, |
||
| 300 | $course_id_field_name, |
||
| 301 | $course_id_value, |
||
| 302 | $user_id_field_name, |
||
| 303 | $user_id_value, |
||
| 304 | $status |
||
| 305 | ) { |
||
| 306 | $verifKey = $this->verifyKey($secret_key); |
||
| 307 | if ($verifKey instanceof WSError) { |
||
| 308 | $this->handleError($verifKey); |
||
| 309 | } else { |
||
| 310 | $result = $this->changeUserSubscription( |
||
| 311 | $course_id_field_name, |
||
| 312 | $course_id_value, |
||
| 313 | $user_id_field_name, |
||
| 314 | $user_id_value, |
||
| 315 | 1, |
||
| 316 | $status |
||
| 317 | ); |
||
| 318 | if ($result instanceof WSError) { |
||
| 319 | $this->handleError($result); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Unsusbscribe user from course. |
||
| 326 | * |
||
| 327 | * @param string API secret key |
||
| 328 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 329 | * @param string course id value |
||
| 330 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 331 | * @param string User id value |
||
| 332 | */ |
||
| 333 | public function UnsubscribeUserFromCourse( |
||
| 334 | $secret_key, |
||
| 335 | $course_id_field_name, |
||
| 336 | $course_id_value, |
||
| 337 | $user_id_field_name, |
||
| 338 | $user_id_value |
||
| 339 | ) { |
||
| 340 | $verifKey = $this->verifyKey($secret_key); |
||
| 341 | if ($verifKey instanceof WSError) { |
||
| 342 | $this->handleError($verifKey); |
||
| 343 | } else { |
||
| 344 | $result = $this->changeUserSubscription( |
||
| 345 | $course_id_field_name, |
||
| 346 | $course_id_value, |
||
| 347 | $user_id_field_name, |
||
| 348 | $user_id_value, |
||
| 349 | 0 |
||
| 350 | ); |
||
| 351 | if ($result instanceof WSError) { |
||
| 352 | $this->handleError($result); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the descriptions of a course, along with their id. |
||
| 359 | * |
||
| 360 | * @param string API secret key |
||
| 361 | * @param string Course id field name |
||
| 362 | * @param string Course id value |
||
| 363 | * |
||
| 364 | * @return array Returns an array with elements of the form |
||
| 365 | * array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') |
||
| 366 | */ |
||
| 367 | public function GetCourseDescriptions( |
||
| 368 | $secret_key, |
||
| 369 | $course_id_field_name, |
||
| 370 | $course_id_value |
||
| 371 | ) { |
||
| 372 | $verifKey = $this->verifyKey($secret_key); |
||
| 373 | if ($verifKey instanceof WSError) { |
||
| 374 | $this->handleError($verifKey); |
||
| 375 | } else { |
||
| 376 | $course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
||
| 377 | if ($course_id instanceof WSError) { |
||
| 378 | return $course_id; |
||
| 379 | } else { |
||
| 380 | // Course exists, get its descriptions |
||
| 381 | $descriptions = CourseDescription::get_descriptions($course_id); |
||
| 382 | $results = []; |
||
| 383 | foreach ($descriptions as $description) { |
||
| 384 | $results[] = ['course_desc_id' => $description->get_description_type(), |
||
| 385 | 'course_desc_title' => $description->get_title(), |
||
| 386 | 'course_desc_content' => $description->get_content(), ]; |
||
| 387 | } |
||
| 388 | |||
| 389 | return $results; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Edit course description. |
||
| 396 | * |
||
| 397 | * @param string API secret key |
||
| 398 | * @param string Course id field name |
||
| 399 | * @param string Course id value |
||
| 400 | * @param int Category id from course description |
||
| 401 | * @param string Description title |
||
| 402 | * @param string Course description content |
||
| 403 | */ |
||
| 404 | public function EditCourseDescription( |
||
| 405 | $secret_key, |
||
| 406 | $course_id_field_name, |
||
| 407 | $course_id_value, |
||
| 408 | $course_desc_id, |
||
| 409 | $course_desc_title, |
||
| 410 | $course_desc_content |
||
| 411 | ) { |
||
| 412 | $verifKey = $this->verifyKey($secret_key); |
||
| 413 | if ($verifKey instanceof WSError) { |
||
| 414 | $this->handleError($verifKey); |
||
| 415 | } else { |
||
| 416 | $course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
||
| 417 | if ($course_id instanceof WSError) { |
||
| 418 | return $course_id; |
||
| 419 | } else { |
||
| 420 | // Create the new course description |
||
| 421 | $cd = new CourseDescription(); |
||
| 422 | $cd->set_description_type($course_desc_id); |
||
| 423 | $cd->set_title($course_desc_title); |
||
| 424 | $cd->set_content($course_desc_content); |
||
| 425 | $cd->set_session_id(0); |
||
| 426 | |||
| 427 | // Get course info |
||
| 428 | $course_info = CourseManager::get_course_information( |
||
| 429 | CourseManager::get_course_code_from_course_id($course_id) |
||
| 430 | ); |
||
| 431 | // Check if this course description exists |
||
| 432 | $descriptions = CourseDescription::get_descriptions($course_id); |
||
| 433 | $exists = false; |
||
| 434 | foreach ($descriptions as $description) { |
||
| 435 | if ($description->get_description_type() == $course_desc_id) { |
||
| 436 | $exists = true; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | $cd->set_course_id($course_info['real_id']); |
||
| 440 | if (!$exists) { |
||
| 441 | $cd->set_progress(0); |
||
| 442 | $cd->insert(); |
||
| 443 | } else { |
||
| 444 | $cd->update(); |
||
| 445 | } |
||
| 446 | } |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | public function unreadMessage($username, $password) |
||
| 451 | { |
||
| 452 | if ($this->verifyUserPass($username, $password) == "valid") { |
||
| 453 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 454 | $user_id = UserManager::get_user_id_from_username($username); |
||
| 455 | $condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1'); |
||
| 456 | |||
| 457 | $sql_query = "SELECT COUNT(*) as number_messages |
||
| 458 | FROM $table_message |
||
| 459 | WHERE $condition_msg_status AND user_receiver_id=".$user_id; |
||
| 460 | |||
| 461 | $sql_result = Database::query($sql_query); |
||
| 462 | $result = Database::fetch_array($sql_result); |
||
| 463 | |||
| 464 | return $result['number_messages']; |
||
| 465 | } |
||
| 466 | |||
| 467 | return "0"; |
||
| 468 | } |
||
| 469 | |||
| 470 | public function get_message_data($username, $password) |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | public function nada($username, $password) |
||
| 478 | { |
||
| 479 | if ($this->verifyUserPass($username, $password) == "valid") { |
||
| 480 | return $username.$password; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Deletes a course (helper method). |
||
| 488 | * |
||
| 489 | * @param string Course id field name |
||
| 490 | * @param string Course id value |
||
| 491 | * |
||
| 492 | * @return mixed True if the course was successfully deleted, WSError otherwise |
||
| 493 | */ |
||
| 494 | protected function deleteCourseHelper( |
||
| 495 | $course_id_field_name, |
||
| 496 | $course_id_value |
||
| 497 | ) { |
||
| 498 | $course_id = $this->getCourseId( |
||
| 499 | $course_id_field_name, |
||
| 500 | $course_id_value |
||
| 501 | ); |
||
| 502 | if ($course_id instanceof WSCMError) { |
||
| 503 | return $course_id; |
||
| 504 | } else { |
||
| 505 | $course_code = CourseManager::get_course_code_from_course_id( |
||
| 506 | $course_id |
||
| 507 | ); |
||
| 508 | CourseManager::delete_course($course_code); |
||
| 509 | |||
| 510 | return true; |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Creates a course (helper method). |
||
| 516 | * |
||
| 517 | * @param string Title |
||
| 518 | * @param string Category code |
||
| 519 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 520 | * @param string Tutor name |
||
| 521 | * @param string Course admin user id field name |
||
| 522 | * @param string Course admin user id value |
||
| 523 | * @param string Course language |
||
| 524 | * @param string Course id field name |
||
| 525 | * @param string Course id value |
||
| 526 | * @param array Course extra fields |
||
| 527 | * |
||
| 528 | * @return mixed Generated id if creation was successful, WSError otherwise |
||
| 529 | */ |
||
| 530 | protected function createCourseHelper( |
||
| 531 | $title, |
||
| 532 | $category_code, |
||
| 533 | $wanted_code, |
||
| 534 | $tutor_name, |
||
| 535 | $course_admin_user_id_field_name, |
||
| 536 | $course_admin_user_id_value, |
||
| 537 | $language, |
||
| 538 | $course_id_field_name, |
||
| 539 | $course_id_value, |
||
| 540 | $extras |
||
| 541 | ) { |
||
| 542 | // Add the original course id field name and value to the extra fields if needed |
||
| 543 | $extras_associative = []; |
||
| 544 | if ($course_id_field_name != "chamilo_course_id") { |
||
| 545 | $extras_associative[$course_id_field_name] = $course_id_value; |
||
| 546 | } |
||
| 547 | foreach ($extras as $extra) { |
||
| 548 | $extras_associative[$extra['field_name']] = $extra['field_value']; |
||
| 549 | } |
||
| 550 | $course_admin_id = $this->getUserId($course_admin_user_id_field_name, $course_admin_user_id_value); |
||
| 551 | if ($course_admin_id instanceof WSError) { |
||
| 552 | return $course_admin_id; |
||
| 553 | } |
||
| 554 | if ($wanted_code == '') { |
||
| 555 | $wanted_code = CourseManager::generate_course_code($title); |
||
| 556 | } |
||
| 557 | $result = create_course( |
||
| 558 | $wanted_code, |
||
| 559 | $title, |
||
| 560 | $tutor_name, |
||
| 561 | $category_code, |
||
| 562 | $language, |
||
| 563 | $course_admin_id, |
||
| 564 | $this->_configuration['db_prefix'], |
||
| 565 | 0 |
||
| 566 | ); |
||
| 567 | if (!$result) { |
||
| 568 | return new WSError(202, 'There was an error creating the course'); |
||
| 569 | } else { |
||
| 570 | // Update extra fields |
||
| 571 | foreach ($extras_associative as $fname => $fvalue) { |
||
| 572 | CourseManager::update_course_extra_field_value($result, $fname, $fvalue); |
||
| 573 | } |
||
| 574 | // Get course id |
||
| 575 | $course_info = CourseManager::get_course_information($result); |
||
| 576 | |||
| 577 | return $course_info['real_id']; |
||
| 578 | } |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Edits a course (helper method). |
||
| 583 | * |
||
| 584 | * @param string Course id field name |
||
| 585 | * @param string Course id value |
||
| 586 | * @param string Title |
||
| 587 | * @param string Category code |
||
| 588 | * @param string Department name |
||
| 589 | * @param string Department url |
||
| 590 | * @param string Course language |
||
| 591 | * @param int Visibility |
||
| 592 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 593 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 594 | * @param string Visual code |
||
| 595 | * @param array Course extra fields |
||
| 596 | * |
||
| 597 | * @return mixed True in case of success, WSError otherwise |
||
| 598 | */ |
||
| 599 | protected function editCourseHelper( |
||
| 600 | $course_id_field_name, |
||
| 601 | $course_id_value, |
||
| 602 | $title, |
||
| 603 | $category_code, |
||
| 604 | $department_name, |
||
| 605 | $department_url, |
||
| 606 | $language, |
||
| 607 | $visibility, |
||
| 608 | $subscribe, |
||
| 609 | $unsubscribe, |
||
| 610 | $visual_code, |
||
| 611 | $extras |
||
| 612 | ) { |
||
| 613 | $course_id = $this->getCourseId($course_id_field_name, $course_id_value); |
||
| 614 | if ($course_id instanceof WSCMError) { |
||
| 615 | return $course_id; |
||
| 616 | } else { |
||
| 617 | $attributes = []; |
||
| 618 | if (!empty($title)) { |
||
| 619 | $attributes['title'] = $title; |
||
| 620 | } |
||
| 621 | if (!empty($category_code)) { |
||
| 622 | $attributes['category_code'] = $category_code; |
||
| 623 | } |
||
| 624 | if (!empty($department_name)) { |
||
| 625 | $attributes['department_name'] = $department_name; |
||
| 626 | } |
||
| 627 | if (!empty($department_url)) { |
||
| 628 | $attributes['department_url'] = $department_url; |
||
| 629 | } |
||
| 630 | if (!empty($language)) { |
||
| 631 | $attributes['course_language'] = $language; |
||
| 632 | } |
||
| 633 | if ($visibility != '') { |
||
| 634 | $attributes['visibility'] = (int) $visibility; |
||
| 635 | } |
||
| 636 | if ($subscribe != '') { |
||
| 637 | $attributes['subscribe'] = (int) $subscribe; |
||
| 638 | } |
||
| 639 | if ($unsubscribe != '') { |
||
| 640 | $attributes['unsubscribe'] = (int) $unsubscribe; |
||
| 641 | } |
||
| 642 | if (!empty($visual_code)) { |
||
| 643 | $attributes['visual_code'] = $visual_code; |
||
| 644 | } |
||
| 645 | if (!empty($attributes)) { |
||
| 646 | CourseManager::update_attributes($course_id, $attributes); |
||
| 647 | } |
||
| 648 | if (!empty($extras)) { |
||
| 649 | $course_code = CourseManager::get_course_code_from_course_id($course_id); |
||
| 650 | $extras_associative = []; |
||
| 651 | foreach ($extras as $extra) { |
||
| 652 | $extras_associative[$extra['field_name']] = $extra['field_value']; |
||
| 653 | } |
||
| 654 | foreach ($extras_associative as $fname => $fvalue) { |
||
| 655 | CourseManager::update_extra_field_value($course_code, $fname, $fvalue); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | |||
| 659 | return true; |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Subscribe or unsubscribe user to a course (helper method). |
||
| 665 | * |
||
| 666 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 667 | * @param string course id value |
||
| 668 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 669 | * @param string User id value |
||
| 670 | * @param int Set to 1 to subscribe, 0 to unsubscribe |
||
| 671 | * @param int Status (STUDENT or TEACHER) Used for subscription only |
||
| 672 | * |
||
| 673 | * @return mixed True if subscription or unsubscription was successful, false otherwise |
||
| 674 | */ |
||
| 675 | protected function changeUserSubscription( |
||
| 703 | } |
||
| 704 | } |
||
| 705 | } |
||
| 706 | } |
||
| 707 | } |
||
| 708 | } |
||
| 709 |