/** * Process incoming enrolment data into an array ready to construct a student object. * * @param int $classid The class ID we're enroling into. * @param array $rawenroldata The raw enrolment data received from javascript. * @return array An array ready to construct a student object. */ protected function process_enrolment_data($classid, $rawenroldata) { if (empty($rawenroldata) || !is_array($rawenroldata)) { return array(); } // Rawenroldata comes in as an array of objects with name and value properties, convert it into an accessible array of // name => value. $enroldata = array(); foreach ($rawenroldata as $param) { $enroldata[$param->name] = $param->value; } // Basic info. $sturecord = array('classid' => $classid); // Enrolment time AND completion time - same validation w/ predictable naming scheme, use array/loop to do both with once // chunk of code. $times = array('enrolmenttime' => 'start', 'completetime' => 'end'); foreach ($times as $stuparam => $inputprefix) { $sturecord[$stuparam] = ds_process_js_date_data($enroldata, $inputprefix); } // Completion status. $validstatus = array('notcomplete' => STUSTATUS_NOTCOMPLETE, 'passed' => STUSTATUS_PASSED, 'failed' => STUSTATUS_FAILED); $status = isset($enroldata['completestatusid']) && isset($validstatus[$enroldata['completestatusid']]) ? $validstatus[$enroldata['completestatusid']] : STUSTATUS_NOTCOMPLETE; $sturecord['completestatusid'] = $status; // Additional info. $sturecord['grade'] = !empty($enroldata['grade']) ? $enroldata['grade'] : 0; $sturecord['credits'] = !empty($enroldata['credits']) ? $enroldata['credits'] : 0; $sturecord['locked'] = !empty($enroldata['locked']) ? 1 : 0; return $sturecord; }
/** * Process incoming learning objective information. * * @param string $rawlearnobjdata The raw learning objective information sent from javascript, in JSON format. * @return array An array of processed learning objective information. Will contain multuple arrays, indexed by the * learning objective ID, each ready to construct a student_grade object. */ protected function process_learning_objectives_data($rawlearnobjdata) { $learnobjdata = array(); $rawlearnobjdata = @json_decode($rawlearnobjdata, true); if (empty($rawlearnobjdata) || !is_array($rawlearnobjdata)) { return array(); } foreach ($rawlearnobjdata as $i => $input) { if (!isset($input['name'], $input['value'])) { continue; } $nameparts = explode('_', $input['name'], 2); if (empty($nameparts) || !is_numeric($nameparts[0]) || count($nameparts) < 2) { continue; } if ($input['value'] == 'on') { $input['value'] = 1; } $learnobjdata[$nameparts[0]][$nameparts[1]] = $input['value']; } foreach ($learnobjdata as $learnobjid => &$data) { if (!isset($data['locked']) || $data['locked'] !== 1) { $data['locked'] = 0; } if (!isset($data['grade']) || !is_numeric($data['grade'])) { $data['grade'] = 0; } $data['timegraded'] = ds_process_js_date_data($data); } return $learnobjdata; }