Beispiel #1
0
 /**
  * Create an ELIS class instance.
  * @param course &$course The course description to assign the class to.
  * @param field &$field A custom field to set when creating the class.
  * @return pmclass The created class.
  */
 public function create_class(course &$course, field &$field)
 {
     $data = new stdClass();
     $data->courseid = $course->id;
     $data->idnumber = 'CLS101';
     $data->startdate = 0;
     $data->enddate = 0;
     $data->starttime = 31603200;
     $data->endtime = 31603200;
     $data->maxstudents = 0;
     $data->moodleCourses = array('moodlecourseid' => '0');
     $data->enrol_from_waitlist = '0';
     $data->field_class_testfield = '';
     $data->starttimeminute = 61;
     $data->starttimehour = 61;
     $data->endtimeminute = 61;
     $data->endtimehour = 61;
     $fieldvar = 'field_' . $field->shortname;
     $data->{$fieldvar} = 'test field data';
     $cls = new pmclass();
     $cls->set_from_data($data);
     $cls->save();
     return $cls;
 }
Beispiel #2
0
 /**
  * Performs class 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 class_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::class_create_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     // Capability checking.
     require_capability('local/elisprogram:class_create', context_system::instance());
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     // Create the class.
     $data = (object) $data;
     $data = $importplugin->add_custom_field_prefixes($data);
     // Parse startdate and enddate.
     foreach (array('startdate', 'enddate') as $date) {
         if (isset($data->{$date})) {
             $data->{$date} = $importplugin->parse_date($data->{$date});
         }
     }
     // Check for duplicate idnumbers.
     if ($DB->record_exists(pmclass::TABLE, array('idnumber' => $data->idnumber))) {
         throw new moodle_exception('ws_class_create_fail_duplicateidnumber', 'local_datahub');
     }
     // Do course assignment.
     $crsid = $DB->get_field(course::TABLE, 'id', array('idnumber' => $data->assignment));
     if (empty($crsid)) {
         throw new moodle_exception('ws_class_create_fail_invalidcourseassignment', 'local_datahub');
     }
     $data->courseid = $crsid;
     $class = new pmclass();
     $class->set_from_data($data);
     $class->save();
     // Associate this class instance to a track, if necessary.
     $importplugin->associate_class_to_track($data, $class->id);
     // Associate this class instance to a Moodle course, if necessary.
     $importplugin->associate_class_to_moodle_course($data, $class->id);
     // Respond.
     if (!empty($class->id)) {
         $classrec = (array) $DB->get_record(pmclass::TABLE, array('id' => $class->id));
         $classobj = $class->to_array();
         // Convert multi-valued custom field arrays to comma-separated listing.
         $fields = static::get_class_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 && !empty($classobj[$fullfieldname]) && is_array($classobj[$fullfieldname])) {
                 $classobj[$fullfieldname] = implode(',', $classobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_class_create_success_code', 'local_datahub'), 'message' => get_string('ws_class_create_success_msg', 'local_datahub'), 'record' => array_merge($classrec, $classobj));
     } else {
         throw new data_object_exception('ws_class_create_fail', 'local_datahub');
     }
 }
Beispiel #3
0
 function class_update($record, $filename)
 {
     global $DB, $CFG;
     require_once $CFG->dirroot . '/local/elisprogram/lib/setup.php';
     require_once elispm::lib('data/pmclass.class.php');
     //NOTE: not checking field lengths because only idnumber can be too long, and
     //we can't set this during updates
     $message = "";
     if (isset($record->idnumber)) {
         if (!($clsid = $DB->get_field(pmclass::TABLE, 'id', array('idnumber' => $record->idnumber)))) {
             $identifier = $this->get_field_mapping('idnumber');
             $this->fslogger->log_failure("{$identifier} value of \"{$record->idnumber}\" does not refer to a valid class instance.", 0, $filename, $this->linenumber, $record, "class");
             return false;
         }
         if (isset($record->assignment)) {
             $message = "Class instance with idnumber \"{$record->idnumber}\" was not re-assigned to course description with name \"{$record->assignment}\" because moving class instances between course descriptions is not supported.";
         }
     }
     if (!$this->validate_class_data('update', $record, $filename)) {
         return false;
     }
     $record = $this->add_custom_field_prefixes($record);
     //custom field validation
     if (!$this->validate_custom_field_data('update', $record, $filename, 'class')) {
         return false;
     }
     $record->id = $clsid;
     $pmclass = new pmclass();
     $pmclass->set_from_data($record);
     $pmclass->save();
     //associate this class instance to a track, if necessary
     $this->associate_class_to_track($record, $pmclass->id);
     //associate this class instance to a Moodle course, if necessary
     $this->associate_class_to_moodle_course($record, $pmclass->id);
     //log success
     $success_message = "Class instance with idnumber \"{$record->idnumber}\" successfully updated.";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }