/** * Performs course update * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error updating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function course_update(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::course_update_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); $data = (object) $data; // Validate course exists if (!($crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->idnumber)))) { throw new data_object_exception('ws_course_update_fail_invalid_idnumber', 'local_datahub', '', $data); } // Capability checking. require_capability('local/elisprogram:course_edit', \local_elisprogram\context\course::instance($crsid)); // Initialize version1elis importplugin for utility functions. $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis'); // Validate credits. if (isset($data->credits) && !(is_numeric($data->credits) && $data->credits >= 0)) { throw new data_object_exception('ws_course_update_fail_invalid_credits', 'local_datahub', '', $data); } // Validate completion grade. if (isset($data->completion_grade) && !(is_numeric($data->completion_grade) && $data->completion_grade >= 0 && $data->completion_grade <= 100)) { throw new data_object_exception('ws_course_update_fail_invalid_completion_grade', 'local_datahub', '', $data); } // Handle assignment to program. if (isset($data->assignment) && !empty($data->assignment)) { $programid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->assignment)); if ($programid) { $curriculumcourseid = $DB->get_field(curriculumcourse::TABLE, 'id', array('curriculumid' => $programid, 'courseid' => $crsid)); // Only assign if it is not already assigned if (!$curriculumcourseid) { $data->curriculum = array($programid); } } else { throw new data_object_exception('ws_course_update_fail_invalid_assignment', 'local_datahub', '', $data); } } $course = new course($crsid); $course->load(); $course->set_from_data($data); $course->save(); // Handle linking to Moodle course. if (isset($data->link) && !empty($data->link)) { $moodlecourseid = $DB->get_field('course', 'id', array('shortname' => $data->link)); if ($moodlecourseid) { $importplugin->associate_course_to_moodle_course($data, $course->id); } else { throw new data_object_exception('ws_course_update_fail_invalid_link', 'local_datahub', '', $data); } } // Respond. if (!empty($course->id)) { $courserec = (array) $DB->get_record(course::TABLE, array('id' => $course->id)); $courseobj = $course->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_course_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($courseobj[$fullfieldname]) && is_array($courseobj[$fullfieldname])) { $courseobj[$fullfieldname] = implode(',', $courseobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_course_update_success_code', 'local_datahub'), 'message' => get_string('ws_course_update_success_msg', 'local_datahub'), 'record' => array_merge($courserec, $courseobj)); } else { throw new data_object_exception('ws_course_update_fail', 'local_datahub'); } }
/** * Performs course creation * @throws moodle_exception If there was an error in passed parameters. * @throws data_object_exception If there was an error creating the entity. * @param array $data The incoming data parameter. * @return array An array of parameters, if successful. */ public static function course_create(array $data) { global $USER, $DB; if (static::require_elis_dependencies() !== true) { throw new moodle_exception('ws_function_requires_elis', 'local_datahub'); } // Parameter validation. $params = self::validate_parameters(self::course_create_parameters(), array('data' => $data)); // Context validation. $context = context_user::instance($USER->id); self::validate_context($context); // Capability checking. require_capability('local/elisprogram:course_create', context_system::instance()); $data = (object) $data; // Set default syllabus value if required. if (!isset($data->syllabus)) { $data->syllabus = ''; } // Validate credits. if (isset($data->credits) && !(is_numeric($data->credits) && $data->credits >= 0)) { throw new data_object_exception('ws_course_create_fail_invalid_credits', 'local_datahub', '', $data); } else { if (!isset($data->credits)) { $data->credits = 0; } } // Validate completion grade. if (isset($data->completion_grade) && !(is_numeric($data->completion_grade) && $data->completion_grade >= 0 && $data->completion_grade <= 100)) { throw new data_object_exception('ws_course_create_fail_invalid_completion_grade', 'local_datahub', '', $data); } // Handle assignment to program. if (isset($data->assignment) && !empty($data->assignment)) { $curriculumid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->assignment)); if ($curriculumid) { $data->curriculum = array($curriculumid); } else { throw new data_object_exception('ws_course_create_fail_invalid_assignment', 'local_datahub', '', $data); } } // Handle linking to Moodle course. if (isset($data->link) && !empty($data->link)) { $moodlecourseid = $DB->get_field('course', 'id', array('shortname' => $data->link)); if ($moodlecourseid) { $data->location = $moodlecourseid; $data->templateclass = 'moodlecourseurl'; } else { throw new data_object_exception('ws_course_create_fail_invalid_link', 'local_datahub', '', $data); } } $course = new course(); $course->set_from_data($data); $course->save(); // Respond. if (!empty($course->id)) { $courserec = (array) $DB->get_record(course::TABLE, array('id' => $course->id)); $courseobj = $course->to_array(); // convert multi-valued custom field arrays to comma-separated listing $fields = self::get_course_custom_fields(); foreach ($fields as $field) { // Generate name using custom field prefix. $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname; if ($field->multivalued && isset($courseobj[$fullfieldname]) && is_array($courseobj[$fullfieldname])) { $courseobj[$fullfieldname] = implode(',', $courseobj[$fullfieldname]); } } return array('messagecode' => get_string('ws_course_create_success_code', 'local_datahub'), 'message' => get_string('ws_course_create_success_msg', 'local_datahub'), 'record' => array_merge($courserec, $courseobj)); } else { throw new data_object_exception('ws_course_create_fail', 'local_datahub'); } }