|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 class profile_define_base { 00004 00009 function define_form(&$form) { 00010 $form->addElement('header', '_commonsettings', get_string('profilecommonsettings', 'admin')); 00011 $this->define_form_common($form); 00012 00013 $form->addElement('header', '_specificsettings', get_string('profilespecificsettings', 'admin')); 00014 $this->define_form_specific($form); 00015 } 00016 00022 function define_form_common(&$form) { 00023 00024 $strrequired = get_string('required'); 00025 00026 $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"'); 00027 $form->addRule('shortname', $strrequired, 'required', null, 'client'); 00028 $form->setType('shortname', PARAM_ALPHANUM); 00029 00030 $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"'); 00031 $form->addRule('name', $strrequired, 'required', null, 'client'); 00032 $form->setType('name', PARAM_MULTILANG); 00033 00034 $form->addElement('editor', 'description', get_string('profiledescription', 'admin'), null, null); 00035 00036 $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin')); 00037 00038 $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin')); 00039 00040 $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin')); 00041 00042 $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin')); 00043 00044 $choices = array(); 00045 $choices[PROFILE_VISIBLE_NONE] = get_string('profilevisiblenone', 'admin'); 00046 $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin'); 00047 $choices[PROFILE_VISIBLE_ALL] = get_string('profilevisibleall', 'admin'); 00048 $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices); 00049 $form->addHelpButton('visible', 'profilevisible', 'admin'); 00050 $form->setDefault('visible', PROFILE_VISIBLE_ALL); 00051 00052 $choices = profile_list_categories(); 00053 $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices); 00054 } 00055 00061 function define_form_specific(&$form) { 00063 } 00064 00072 function define_validate($data, $files) { 00073 00074 $data = (object)$data; 00075 $err = array(); 00076 00077 $err += $this->define_validate_common($data, $files); 00078 $err += $this->define_validate_specific($data, $files); 00079 00080 return $err; 00081 } 00082 00090 function define_validate_common($data, $files) { 00091 global $USER, $DB; 00092 00093 $err = array(); 00094 00096 if (empty($data->shortname)) { 00097 $err['shortname'] = get_string('required'); 00098 00099 } else { 00101 $field = $DB->get_record('user_info_field', array('shortname'=>$data->shortname)); 00103 if ($field and $field->id <> $data->id) { 00104 $err['shortname'] = get_string('profileshortnamenotunique', 'admin'); 00105 } 00106 00107 //NOTE: since 2.0 the shortname may collide with existing fields in $USER because we load these fields into $USER->profile array instead 00108 } 00109 00111 return $err; 00112 } 00113 00120 function define_validate_specific($data, $files) { 00122 return array(); 00123 } 00124 00129 function define_after_data(&$mform) { 00131 } 00132 00138 function define_save($data) { 00139 global $DB; 00140 00141 $data = $this->define_save_preprocess($data); 00142 00143 $old = false; 00144 if (!empty($data->id)) { 00145 $old = $DB->get_record('user_info_field', array('id'=>(int)$data->id)); 00146 } 00147 00149 if (!$old or $old->categoryid != $data->categoryid) { 00150 $data->sortorder = $DB->count_records('user_info_field', array('categoryid'=>$data->categoryid)) + 1; 00151 } 00152 00153 00154 if (empty($data->id)) { 00155 unset($data->id); 00156 $data->id = $DB->insert_record('user_info_field', $data); 00157 } else { 00158 $DB->update_record('user_info_field', $data); 00159 } 00160 } 00161 00169 function define_save_preprocess($data) { 00171 return $data; 00172 } 00173 00182 function define_editors() { 00183 return array(); 00184 } 00185 } 00186 00187 00188 00193 function profile_reorder_fields() { 00194 global $DB; 00195 00196 if ($categories = $DB->get_records('user_info_category')) { 00197 foreach ($categories as $category) { 00198 $i = 1; 00199 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) { 00200 foreach ($fields as $field) { 00201 $f = new stdClass(); 00202 $f->id = $field->id; 00203 $f->sortorder = $i++; 00204 $DB->update_record('user_info_field', $f); 00205 } 00206 } 00207 } 00208 } 00209 } 00210 00215 function profile_reorder_categories() { 00216 global $DB; 00217 00218 $i = 1; 00219 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) { 00220 foreach ($categories as $cat) { 00221 $c = new stdClass(); 00222 $c->id = $cat->id; 00223 $c->sortorder = $i++; 00224 $DB->update_record('user_info_category', $c); 00225 } 00226 } 00227 } 00228 00234 function profile_delete_category($id) { 00235 global $DB; 00236 00238 if (!$category = $DB->get_record('user_info_category', array('id'=>$id))) { 00239 print_error('invalidcategoryid'); 00240 } 00241 00242 if (!$categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) { 00243 print_error('nocate', 'debug'); 00244 } 00245 00246 unset($categories[$category->id]); 00247 00248 if (!count($categories)) { 00249 return; //we can not delete the last category 00250 } 00251 00253 if ($DB->count_records('user_info_field', array('categoryid'=>$category->id))) { 00254 if (array_key_exists($category->sortorder-1, $categories)) { 00255 $newcategory = $categories[$category->sortorder-1]; 00256 } else if (array_key_exists($category->sortorder+1, $categories)) { 00257 $newcategory = $categories[$category->sortorder+1]; 00258 } else { 00259 $newcategory = reset($categories); // get first category if sortorder broken 00260 } 00261 00262 $sortorder = $DB->count_records('user_info_field', array('categoryid'=>$newcategory->id)) + 1; 00263 00264 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) { 00265 foreach ($fields as $field) { 00266 $f = new stdClass(); 00267 $f->id = $field->id; 00268 $f->sortorder = $sortorder++; 00269 $f->categoryid = $newcategory->id; 00270 $DB->update_record('user_info_field', $f); 00271 //echo "<pre>";var_dump($f);echo"</pre>"; 00272 } 00273 } 00274 } 00275 00277 $DB->delete_records('user_info_category', array('id'=>$category->id)); 00278 profile_reorder_categories(); 00279 return true; 00280 } 00281 00282 00283 function profile_delete_field($id) { 00284 global $DB; 00285 00287 if (!$DB->delete_records('user_info_data', array('fieldid'=>$id))) { 00288 print_error('cannotdeletecustomfield'); 00289 } 00290 00292 $DB->delete_records('user_info_field', array('id'=>$id)); 00293 00295 profile_reorder_fields(); 00296 } 00297 00304 function profile_move_field($id, $move) { 00305 global $DB; 00306 00308 if (!$field = $DB->get_record('user_info_field', array('id'=>$id), 'id, sortorder, categoryid')) { 00309 return false; 00310 } 00312 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid)); 00313 00315 if ( ($move == 'up') and ($field->sortorder > 1)) { 00316 $neworder = $field->sortorder - 1; 00317 } elseif ( ($move == 'down') and ($field->sortorder < $fieldcount)) { 00318 $neworder = $field->sortorder + 1; 00319 } else { 00320 return false; 00321 } 00322 00324 if ($swapfield = $DB->get_record('user_info_field', array('categoryid'=>$field->categoryid, 'sortorder'=>$neworder), 'id, sortorder')) { 00325 00327 $swapfield->sortorder = $field->sortorder; 00328 $field->sortorder = $neworder; 00329 00331 $DB->update_record('user_info_field', $field); 00332 $DB->update_record('user_info_field', $swapfield); 00333 } 00334 00335 profile_reorder_fields(); 00336 } 00337 00344 function profile_move_category($id, $move) { 00345 global $DB; 00347 if (!($category = $DB->get_record('user_info_category', array('id'=>$id), 'id, sortorder'))) { 00348 return false; 00349 } 00350 00352 $categorycount = $DB->count_records('user_info_category'); 00353 00355 if ( ($move == 'up') and ($category->sortorder > 1)) { 00356 $neworder = $category->sortorder - 1; 00357 } elseif ( ($move == 'down') and ($category->sortorder < $categorycount)) { 00358 $neworder = $category->sortorder + 1; 00359 } else { 00360 return false; 00361 } 00362 00364 if ($swapcategory = $DB->get_record('user_info_category', array('sortorder'=>$neworder),'id, sortorder')) { 00365 00367 $swapcategory->sortorder = $category->sortorder; 00368 $category->sortorder = $neworder; 00369 00371 $DB->update_record('user_info_category', $category) and $DB->update_record('user_info_category', $swapcategory); 00372 return true; 00373 } 00374 00375 return false; 00376 } 00377 00382 function profile_list_datatypes() { 00383 global $CFG; 00384 00385 $datatypes = array(); 00386 00387 $plugins = get_plugin_list('profilefield'); 00388 foreach ($plugins as $type=>$unused) { 00389 $datatypes[$type] = get_string('pluginname', 'profilefield_'.$type); 00390 } 00391 asort($datatypes); 00392 00393 return $datatypes; 00394 } 00395 00400 function profile_list_categories() { 00401 global $DB; 00402 if (!$categories = $DB->get_records_menu('user_info_category', NULL, 'sortorder ASC', 'id, name')) { 00403 $categories = array(); 00404 } 00405 return $categories; 00406 } 00407 00408 00410 function profile_edit_category($id, $redirect) { 00411 global $CFG, $DB, $OUTPUT; 00412 00413 require_once('index_category_form.php'); 00414 $categoryform = new category_form(); 00415 00416 if ($category = $DB->get_record('user_info_category', array('id'=>$id))) { 00417 $categoryform->set_data($category); 00418 } 00419 00420 if ($categoryform->is_cancelled()) { 00421 redirect($redirect); 00422 } else { 00423 if ($data = $categoryform->get_data()) { 00424 if (empty($data->id)) { 00425 unset($data->id); 00426 $data->sortorder = $DB->count_records('user_info_category') + 1; 00427 $DB->insert_record('user_info_category', $data, false); 00428 } else { 00429 $DB->update_record('user_info_category', $data); 00430 } 00431 profile_reorder_categories(); 00432 redirect($redirect); 00433 00434 } 00435 00436 if (empty($id)) { 00437 $strheading = get_string('profilecreatenewcategory', 'admin'); 00438 } else { 00439 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name)); 00440 } 00441 00443 echo $OUTPUT->header(); 00444 echo $OUTPUT->heading($strheading); 00445 $categoryform->display(); 00446 echo $OUTPUT->footer(); 00447 die; 00448 } 00449 00450 } 00451 00452 function profile_edit_field($id, $datatype, $redirect) { 00453 global $CFG, $DB, $OUTPUT, $PAGE; 00454 00455 if (!$field = $DB->get_record('user_info_field', array('id'=>$id))) { 00456 $field = new stdClass(); 00457 $field->datatype = $datatype; 00458 $field->description = ''; 00459 $field->descriptionformat = FORMAT_HTML; 00460 $field->defaultdata = ''; 00461 $field->defaultdataformat = FORMAT_HTML; 00462 } 00463 00464 00465 // Clean and prepare description for the editor 00466 $field->description = clean_text($field->description, $field->descriptionformat); 00467 $field->description = array('text'=>$field->description, 'format'=>$field->descriptionformat, 'itemid'=>0); 00468 00469 require_once('index_field_form.php'); 00470 $fieldform = new field_form(null, $field->datatype); 00471 00472 // Convert the data format for 00473 if (is_array($fieldform->editors())) { 00474 foreach ($fieldform->editors() as $editor) { 00475 if (isset($field->$editor)) { 00476 $field->$editor = clean_text($field->$editor, $field->{$editor.'format'}); 00477 $field->$editor = array('text'=>$field->$editor, 'format'=>$field->{$editor.'format'}, 'itemid'=>0); 00478 } 00479 } 00480 } 00481 00482 $fieldform->set_data($field); 00483 00484 if ($fieldform->is_cancelled()) { 00485 redirect($redirect); 00486 00487 } else { 00488 if ($data = $fieldform->get_data()) { 00489 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php'); 00490 $newfield = 'profile_define_'.$datatype; 00491 $formfield = new $newfield(); 00492 00493 // Collect the description and format back into the proper data structure from the editor 00494 // Note: This field will ALWAYS be an editor 00495 $data->descriptionformat = $data->description['format']; 00496 $data->description = $data->description['text']; 00497 00498 // Check whether the default data is an editor, this is (currently) only the 00499 // textarea field type 00500 if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) { 00501 // Collect the default data and format back into the proper data structure from the editor 00502 $data->defaultdataformat = $data->defaultdata['format']; 00503 $data->defaultdata = $data->defaultdata['text']; 00504 } 00505 00506 // Convert the data format for 00507 if (is_array($fieldform->editors())) { 00508 foreach ($fieldform->editors() as $editor) { 00509 if (isset($field->$editor)) { 00510 $field->{$editor.'format'} = $field->{$editor}['format']; 00511 $field->$editor = $field->{$editor}['text']; 00512 } 00513 } 00514 } 00515 00516 $formfield->define_save($data); 00517 profile_reorder_fields(); 00518 profile_reorder_categories(); 00519 redirect($redirect); 00520 } 00521 00522 $datatypes = profile_list_datatypes(); 00523 00524 if (empty($id)) { 00525 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]); 00526 } else { 00527 $strheading = get_string('profileeditfield', 'admin', $field->name); 00528 } 00529 00531 $PAGE->navbar->add($strheading); 00532 echo $OUTPUT->header(); 00533 echo $OUTPUT->heading($strheading); 00534 $fieldform->display(); 00535 echo $OUTPUT->footer(); 00536 die; 00537 } 00538 } 00539 00540